Skip to content

Commit

Permalink
New function 'top'
Browse files Browse the repository at this point in the history
Thanks to this new function, it is now possible to view a list of the top ten (or any specified length) members.
  • Loading branch information
Hyd3L committed May 22, 2017
1 parent 69d914c commit 31e4f1a
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 32 deletions.
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# weeelab
[![License](http://img.shields.io/:license-GPL3.0-blue.svg)](http://www.gnu.org/licenses/gpl-3.0.html)
![Version](https://img.shields.io/badge/version-1.3.6-blue.svg)
![Version](https://img.shields.io/badge/version-1.4-yellow.svg)

Python script for garbaging paper sign sheet.

Expand All @@ -13,11 +13,12 @@ Available options:
`login` : Sign in to the lab.
`logout` : Sign out from the lab.
`show` : Obtain status informations.
`stats` : Compute stats for a user or for all users.
`top` : Show a list of top members.
`stat` : Compute stats for a user or for all users.

Available infos:
`log` : Show log file.
`ops` : View a list of operators in lab now.
`inlab` : View a list of operators in lab now.

## NOTES
- The file `log.dat` is filled by adding new lines on top.
Expand All @@ -27,8 +28,8 @@ This is important because makes searching for last login easier and it's even mo
- The `login` and `logout` functions now work with serial numbers and
with nicknames as well.


## ToDo
- [x] Implement a function to compute stats for a user and for all users.
- [ ] Make the stats function less stupid.
- [ ] Implement a function `presence` to print a list of users ordered by `most active first`.
- [x] Implement a function `top` to print a list of users ordered by `most active first`.
- [ ] Improve some algorithms (Long Term Project)

87 changes: 61 additions & 26 deletions weeelab
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import os
import sys
from datetime import datetime

VERSION = "1.3.6"
VERSION = "1.4"
EXECNAME = "weeelab"
HOSTNAME = "schifomacchina"
HOSTUSER = "weeeopen"
Expand Down Expand Up @@ -53,10 +53,13 @@ def help(forWhat):
print " inlab : View a list of students in lab now."
sys.exit()

elif forWhat == "stats":
print "usage: "+EXECNAME+" stats <username>"
print HOSTNAME+": If you wish to view stats for everybody,"
print " use \""+EXECNAME+" stats everybody\""
elif forWhat == "stat":
print "usage: "+EXECNAME+" stat <username>"
print HOSTNAME+": If you want to view stats for everybody,"
print " use \""+EXECNAME+" stat all\""

elif forWhat == "top":
print "usage: "+EXECNAME+" top <list_length>"

else:
print "WEEELAB v"+VERSION+" - Log management module for garbaging paper sign sheet."
Expand All @@ -69,7 +72,8 @@ def help(forWhat):
print " login <username> : Sign access to the lab."
print " logout <username> : Sign quit from the lab."
print " show <option> : Retrieve informations."
print " stats <username> : Compute stats for a user or for all users."
print " stat <username> : Compute stats for a user or for all users."
print " top <list_length> : Show a list of top <list_length> members."
sys.exit()


Expand Down Expand Up @@ -285,6 +289,7 @@ def show(option):
print "error: option "+option+" is not defined."


# Returns total work time in minutes
def totWorkTime(username):
checkLogFile()
timeSpent = 0
Expand All @@ -293,39 +298,60 @@ def totWorkTime(username):
if (username in line) and not ("INLAB" in line):
timeSpent += ((int(line[39:41])*60) + int(line[42:44]))
logFile.close()
return str(timeSpent / 60)+"h " + str(timeSpent % 60)+"m"
return timeSpent


# Convert minutes in a formatted string
def timeConv(minutes):
return str(minutes / 60)+"h " + str(minutes % 60)+"m"

def stats(username):

def stat(username):
print HOSTNAME+": Computing stats...\n"
currMonth = datetime.now().strftime(" [%B %Y]")

# Compute stats for all users (Extremely stupid algorithm, but works fine)
# TODO: Improve this algorithm, maybe by parsing the log file once.
# Open log file and users file, load all usernames in a tuple
# [ str(username), int(workTime) ]. Close users file and read log file
# line per line summing the workTime of each user found in log,
# and continue reading the log file till its end, saving all to the tuple.
# At the end, close the log file and print stats reading data from the tuple.
if username == "everybody":
checkLogFile()
if username == "all":
checkUsersFile()
usersFile = open(USERS_PATH, "r")
for line in usersFile:
currUser = line.split(" ")[0]
print "[+] Name: "+nameExtr(currUser)
print "[+] WorkTime: "+totWorkTime(currUser) + currMonth+"\n"
print "[+] WorkTime: "+timeConv(totWorkTime(currUser)) + currMonth+"\n"
usersFile.close()

elif username == "presence":
# Print users list ordered by most active first.
# TODO: To be implemented.
pass

else:
username = checkMember(username)
print "[+] Name: "+nameExtr(username)
print "[+] WorkTime: "+totWorkTime(username) + currMonth
print "[+] WorkTime: "+timeConv(totWorkTime(username)) + currMonth


# Print users list ordered by most active first.
def top(length):
checkUsersFile()
usersList = [(int, str)]
usersFile = open(USERS_PATH, "r")

# Loading usernames from file
for line in usersFile:
usersList.append((0, line.split(" ")[0].replace("\n","")))
usersFile.close()
usersList.remove((int, str))

# Computing total work time for each member
for i in xrange(0,len(usersList)):
user = usersList[i]
user = (totWorkTime(user[1]), ) + user[1:]
usersList[i] = user
usersList.sort(reverse=True)

print HOSTNAME+": Hall of Fame\n"
count = 1
for i in xrange(0, length):
if len(usersList) <= i:
break
print "["+str(count).zfill(2)+"] "+nameExtr(usersList[i][1])
count += 1


def main(args):
Expand Down Expand Up @@ -365,12 +391,21 @@ def main(args):
else:
show(args[2])

elif command == "stats":
elif command == "stat":
if len(args) != 3:
print "usage: "+EXECNAME+" stats <username>"
print "usage: "+EXECNAME+" stat <username>"
sys.exit()
else:
stats(args[2])
stat(args[2])

elif command == "top":
if len(args) == 3:
if args[2].isdigit is False:
print "usage: "+EXECNAME+" top <list_length>"
else:
top(int(args[2]))
else:
top(10)

else:
print "error: Invalid command detected -> " + args[1]
Expand Down

0 comments on commit 31e4f1a

Please sign in to comment.