Web · Wiki · Activities · Blog · Lists · Chat · Meeting · Bugs · Git · Translate · Archive · People · Donate

Commit 28c4581305d5545aadf23132d9d957143a1a71f2

  • avatar
  • ceibal <ceibal @cei…al.ceibal.uy>
  • Wed Feb 13 09:34:05 EST 2013
Fixes:
	+ Allow update uptime valuo into db, although an activity has an entry already with the same date start.
	+ Change last_record update value from last record of las rrd to current time of server

Improves:
	+ Enhacement of sql report, add all option.
  
4545
4646logging.getLogger('sugar_stats_consolidation.db').addHandler(ch)
4747
48def activity_most_used(re, start, end):
49 activity = re.most_activity_used(start, end);
50 log.debug("**************************************************************************************************************")
51 log.debug(" Most Activity used ")
52 log.debug('NAME: %s', activity[0])
53 log.debug('INTERVAL: %s-> %s', start, end)
54 log.debug('TIME: %.2f hrs / %.2f mins / %.2f secs', (activity[1]/60/60), (activity[1]/60), activity[1])
55 log.debug("***************************************************************************************************************")
4856
57def frequency_usage(re, start, end):
58 ts_end = long (datetime.strptime(end, "%Y-%m-%d").strftime("%s"))
59 ts_start = long (datetime.strptime(start, "%Y-%m-%d").strftime("%s"))
60
61 fres = re.frequency_usage(start, end);
62 f_x_day = (fres / ((ts_end - ts_start)/(60*60*24)))/60
63 f_total = (fres / 60)
64 log.debug("**************************************************************************************************************")
65 log.debug(" Fequency Usage ")
66 log.debug('INTERVAL: %s-> %s', start, end)
67 log.debug('PER DAY: %.2f min / day', (f_x_day))
68 log.debug('TOTAL: %.2f min', (f_total))
69 log.debug("**************************************************************************************************************")
70
71
72
4973def main():
5074 re = DB_Stats (args.db_name, args.db_user, args.db_pass)
5175 re.connect()
76
5277 if args.query == 'activity_most_used':
53 activity = re.most_activity_used(args.start_date, args.end_date);
54 log.debug("**************************************************************************************************************")
55 log.debug(" Most Activity used ")
56 log.debug('NAME: %s', activity[0])
57 log.debug('PERIOD: %s-> %s', args.start_date, args.end_date)
58 log.debug('TIME: %.2f hrs / %.2f mins / %.2f secs', (activity[1]/60/60), (activity[1]/60), activity[1])
59 log.debug("***************************************************************************************************************")
60 if args.query == 'frequency_usage':
61 ts_end = long (datetime.strptime(args.end_date, "%Y-%m-%d").strftime("%s"))
62 ts_start = long (datetime.strptime(args.start_date, "%Y-%m-%d").strftime("%s"))
63
64 fres = re.frequency_usage(args.start_date, args.end_date);
65 f_x_day = (fres / ((ts_end - ts_start)/(60*60*24)))/60
66 f_total = (fres / 60)
67 log.debug("**************************************************************************************************************")
68 log.debug(" Fequency Usage ")
69 log.debug('PERIOD: %s-> %s', args.start_date, args.end_date)
70 log.debug('USAGE PER DAY: %.2f min / day', (f_x_day))
71 log.debug('USAGE TOTAL: %.2f min', (f_total))
72 log.debug("**************************************************************************************************************")
78 activity_most_used(re, args.start_date, args.end_date);
79 elif args.query == 'frequency_usage':
80 frequency_usage(re, args.start_date, args.end_date)
81 elif args.query == 'all':
82 activity_most_used(re, args.start_date, args.end_date);
83 frequency_usage(re, args.start_date, args.end_date)
84 else:
85 log.error("Query option unkwon: %s", args.query)
86
87 re.close()
7388
7489if __name__ == '__main__':
7590 main()
  
11#!/bin/bash
22#QUERY=activity_most_used
3QUERY=frequency_usage
3#QUERY=frequency_usage
4QUERY=all
45START_DATE=2013-02-10
56END_DATE=2013-02-14
67DB_NAME=statistics
  
4242 else:
4343 log.warning('RRD file not found: %s', os.path.join(self.base_path, id_hash, user_hash))
4444 else:
45 log.warning('Hash user direcotory not found: %s', os.path.join(self.base_path, id_hash))
45 log.warning('Hash user direcotory not found: %s', os.path.join(self.base_path, id_hash))
46 self.db.update_last_record();
47 log.info("End RRDs processing")
4648 else:
4749 log.error('Hash ids not found on: %s', self.base_path)
4850 except Exception as e:
  
5454 try:
5555 con = Consolidation(args.rrd_path, db)
5656 con.process_rrds()
57 db.close()
58
59 log.info('**************************************')
60 log.info(' Finish consolidation ')
61 log.info('**************************************')
5762 except Exception as e:
5863 log.warning('Processing rrd file: %s', str(e))
5964
  
106106 self.store_user(rrd)
107107
108108 cursor = self.cnx.cursor()
109 select = ("SELECT * FROM Usages WHERE "
110 "user_hash = %s AND "
111 "resource_name = %s AND "
112 "start_date = %s AND "
113 "data_type = %s")
114 update = ("UPDATE Usages SET data = %s WHERE "
115 "user_hash = %s AND "
116 "resource_name = %s AND "
117 "start_date = %s AND "
118 "data_type = %s")
109119 insert = ("INSERT INTO Usages "
110120 "(user_hash, "
111121 "resource_name, "
125125 "VALUES (%s, %s, %s, %s ,%s) ")
126126
127127 for d in rrd.get_uptime_by_interval():
128 info = (rrd.get_user_hash(), rrd.get_name() , datetime.fromtimestamp(float(d[0])), 'uptime', d[1])
128 info_sel = (rrd.get_user_hash(), rrd.get_name() , datetime.fromtimestamp(float(d[0])), 'uptime')
129129 try:
130 cursor.execute(insert, info)
131 if self.update_last_record(rrd.get_date_last_record()) == 0:
132 self.cnx.commit()
130 """Verify if this activity has an entry already at the same start_date"""
131 cursor.execute (select, info_sel)
132 result = cursor.fetchone()
133
134 if result != None:
135 log.info('Update uptime \'%s\' entry for resource \'%s\' ', d[1], rrd.get_name())
136 info_up = (d[1], rrd.get_user_hash(), rrd.get_name() , datetime.fromtimestamp(float(d[0])), 'uptime')
137 cursor.execute(update, info_up)
138 else:
139 log.info('New uptime \'%s\' entry for resource \'%s\'', d[1], rrd.get_name())
140 info_ins = (rrd.get_user_hash(), rrd.get_name() , datetime.fromtimestamp(float(d[0])), 'uptime', d[1])
141 cursor.execute(insert, info_ins)
133142
143 self.cnx.commit()
144
134145 except mysql.connector.Error as err:
135146 log.error('MySQL on store_activiy_uptime: %s %s', cursor.statement, err)
136147 cursor.close()
155155 cursor.execute(op, params)
156156 result = cursor.fetchone()
157157 if result != None:
158 log.info('Resource %s already present in DB', resource_name)
158 log.debug('Resource %s already present in DB', resource_name)
159159 else:
160160 insert = ("INSERT INTO Resources (name) VALUES (%s)")
161161 info = (resource_name, )
174174 cursor.execute(op, params)
175175 result = cursor.fetchone()
176176 if result != None:
177 log.info('User %s already in DB', rrd.user_hash)
177 log.debug('User %s already in DB', rrd.user_hash)
178178 else:
179179 insert = ("INSERT INTO Users (hash, uuid, machine_sn, age, school, sw_version) VALUES (%s, %s, %s, %s, %s, %s)")
180180 params = (rrd.get_user_hash(), rrd.get_uuid(), rrd.get_sn(), rrd.get_age(), rrd.get_school(), "1.0.0")
187187
188188
189189
190 def update_last_record (self, ts):
190 def update_last_record (self):
191191 cursor = self.cnx.cursor()
192192 res = 0
193193 op = ("SELECT * FROM Runs")
194 params = (datetime.fromtimestamp(float(ts)),)
195194 try:
196195 cursor.execute(op)
197196 result = cursor.fetchone()
198197
199198 if result != None:
200 op = ("UPDATE Runs SET last_ts = %s")
201 cursor.execute(op, params)
199 op = ("UPDATE Runs SET last_ts = CURRENT_TIMESTAMP")
200 cursor.execute(op)
202201 self.cnx.commit()
203202 else:
204 op = ("INSERT INTO Runs VALUES(%s)")
205 cursor.execute(op, params)
203 op = ("INSERT INTO Runs VALUES(CURRENT_TIMESTAMP)")
204 cursor.execute(op)
206205 self.cnx.commit()
207
206 log.info("Save last record");
208207 except mysql.connector.Error as err:
209208 log.error('MySQL on update_last_record: %s %s', cursor.statement, err)
210209 res = -1
  
11#!/usr/bin/env python
22
33import argparse
4from rrd_files import *
4from sugar_stats_consolidation.rrd_files import *
55
66parser = argparse.ArgumentParser()
77parser.add_argument('--rrd_path',required=False)
1212print "============================== TEST RRD analyze content ========================================"
1313
1414if args.rrd_path == None:
15 def_path = "/home/olpc/.sugar/default/stats"
15 def_path = "/var/lib/sugar-stats/rrd/ed/ed4f8bd4c24d4f10b7bd6c59add7032b0fbf5dbd"
1616else:
1717 def_path = args.rrd_path
1818