diff options
author | Gustavo Duarte <gduarte@activitycentral.com> | 2012-12-24 14:57:54 (GMT) |
---|---|---|
committer | Gustavo Duarte <gduarte@activitycentral.com> | 2012-12-24 14:57:54 (GMT) |
commit | cd26f50fa4d1fdefd7ce0a7bee3f293f2db270ae (patch) | |
tree | 7ee154dcd3a9d4dcc57331231e53b7d0e75b9ec2 | |
parent | 56395972aa593bc221c1689c703815781185ddcb (diff) |
add INSTALL file
-rw-r--r-- | INSTALL | 12 | ||||
-rw-r--r-- | src/db.py | 43 | ||||
-rw-r--r-- | src/db.pyc | bin | 6047 -> 6793 bytes | |||
-rw-r--r-- | src/rrd_files.py | 57 | ||||
-rw-r--r-- | src/rrd_files.pyc | bin | 3684 -> 4993 bytes |
5 files changed, 89 insertions, 23 deletions
@@ -0,0 +1,12 @@ +Install Instructions +==================== + +1) Run: python setup.py install + +2) Configure the following entry on crontab: +For example: +To run every day at 3 AM hs + +0 3 * * * root path_to_consolidation --db_user=root --db_pass=my_pass db_name=statistics /home/ceibal/sugar-stats/rrd + + @@ -14,7 +14,7 @@ class DB_Stats: " `start_date` TIMESTAMP NOT NULL," " `data_type` CHAR (30) NOT NULL," " `data` INTEGER NOT NULL," - " PRIMARY KEY (`start_date`,`resource_name`, `data_type`)" + " PRIMARY KEY (`user_hash`,`start_date`,`resource_name`, `data_type`)" " )") TABLES['Resources'] = ( @@ -98,7 +98,8 @@ class DB_Stats: def store_activity_uptime(self, rrd): self.store_resource(rrd.get_name()) - + self.store_user(rrd) + cursor = self.cnx.cursor() insert = ("INSERT INTO Usages " "(user_hash, " @@ -109,16 +110,16 @@ class DB_Stats: "VALUES (%s, %s, %s, %s ,%s) ") for d in rrd.get_uptime_by_interval(): - info = ('none', rrd.get_name() , datetime.fromtimestamp(float(d[0])), 'uptime', d[1]) + info = (rrd.get_user_hash(), rrd.get_name() , datetime.fromtimestamp(float(d[0])), 'uptime', d[1]) try: cursor.execute(insert, info) if self.update_last_record(rrd.get_date_last_record()) == 0: self.cnx.commit() except mysql.connector.Error as err: - print("Fail INSERT: {}".format(err)) - + print("Fail {}: {}".format(cursor.statement, err)) cursor.close() + def store_resource(self, resource_name): cursor = self.cnx.cursor() @@ -135,10 +136,32 @@ class DB_Stats: cursor.execute(insert, info) self.cnx.commit() except mysql.connector.Error as err: - print("Fail SELCT: {}".format(err)) + print("Fail {}: {}".format(cursor.statement, err)) cursor.close() + def store_user (self, rrd): + cursor = self.cnx.cursor() + op = ("SELECT hash FROM Users WHERE hash = %s") + params = (rrd.get_user_hash(), ) + try: + cursor.execute(op, params) + result = cursor.fetchone() + if result != None: + print("User {} already in db".format(rrd.user_hash)) + else: + """FIXME change hardcoded values """ + insert = ("INSERT INTO Users (hash, uuid, machine_sn, age, school, sw_version) VALUES (%s, %s, %s, %s, %s, %s)") + params = (rrd.get_user_hash(), rrd.get_uuid(), "unk_machine_sn", 0, "unk_escuela", "1.0.0") + cursor.execute(insert, params) + self.cnx.commit() + except mysql.connector.Error as err: + print("Fail {}: {}".format(cursor.statement, err)) + + cursor.close() + + + def update_last_record (self, ts): cursor = self.cnx.cursor() res = 0 @@ -149,17 +172,13 @@ class DB_Stats: result = cursor.fetchone() if result != None: - print("will update ..") op = ("UPDATE Runs SET last_ts = %s") cursor.execute(op, params) self.cnx.commit() - print("update ok") else: - print("will insert ..") op = ("INSERT INTO Runs VALUES(%s)") cursor.execute(op, params) self.cnx.commit() - print("insert ok") except mysql.connector.Error as err: print("Fail {}: {}".format(cursor.statement, err)) @@ -178,8 +197,8 @@ class DB_Stats: print ("last record: {}".format(result[0])) return result[0] else: - print ("result is None") + print ("Last date record is None") return 0 except mysql.connector.Error as err: - print("Fail SELELCT: {}".format(err)) + print("Fail {}: {}".format(cursor.statement, err)) cursor.close() Binary files differdiff --git a/src/rrd_files.py b/src/rrd_files.py index 3fbf154..a437e0d 100644 --- a/src/rrd_files.py +++ b/src/rrd_files.py @@ -1,6 +1,6 @@ import rrdtool import os - +import sys class RRD: @@ -20,26 +20,44 @@ class RRD: if date_end == None: - self.date_end = str(rrdtool.last(str(os.path.join(path,name)))) + self.date_end = str(rrdtool.last(str(os.path.join(path, name)))) else: self.date_end = str(date_end) - print "******************************************" + + self.user_hash = os.path.split(path)[1] + + self.user_path = os.path.join ( + self.get_first_part_path(path, 3), + "users", + "user", + self.user_hash[:2], + self.user_hash + ) + + self.uuid = self.get_uuid_from_file(self.user_path) + + + print "*******************************************" + print " RRD " print "start: " + self.date_start print "end: " + self.date_end print "PATH: " + path print "RRD NAME: " + name - print "******************************************" - - self.rrd = rrdtool.fetch (str(os.path.join(path,name)), 'AVERAGE', '-r 60', '-s '+ self.date_start, '-e '+self.date_end) + print "\n" + try: + self.rrd = rrdtool.fetch (str(os.path.join(path,name)), 'AVERAGE', '-r 60', '-s '+ self.date_start, '-e '+self.date_end) + except: + raise + print " DS " for item in self.DS.keys(): idx = self.get_ds_index (item) if idx != -1: self.DS[item] = idx - print item + ": " + str(self.DS[item]) + print "DS "+ item + ": " + str(self.DS[item]) else: - print item + " not found in header" - + print "DS "+ item + " not found in header" + print "***********************************************" def get_ds_index(self, ds): i=0 @@ -53,7 +71,7 @@ class RRD: ds_name = "uptime" res=list() - print "------------------- Calcule "+ ds_name +"---------------------" + print "-------Calcule "+ ds_name +"-------" i=0 found = False while i < len(self.rrd[self.data_item]): @@ -67,7 +85,8 @@ class RRD: else: if found: print start + "->" + end + ": " + uptime - res.append((start, uptime)) + if float(uptime) > 0: + res.append((start, uptime)) found = False i=i+1 return res @@ -96,3 +115,19 @@ class RRD: def set_user_hash(self, u_hash): self.user_hash = u_hash + def get_first_part_path (self, path, idx): + l=list() + l.append(path) + for i in range (idx): + l.append(os.path.split(l[i])[0]) + return l[idx] + + def get_uuid_from_file(self,path): + return open (os.path.join(path, "machine_uuid")).next() + + + def get_user_hash(self): + return self.user_hash + + def get_uuid (self): + return self.uuid diff --git a/src/rrd_files.pyc b/src/rrd_files.pyc Binary files differindex 316839b..7f26699 100644 --- a/src/rrd_files.pyc +++ b/src/rrd_files.pyc |