def __init__(self, msgh, config): CalcManager.__init__(self, msgh) self.report_fd = None self.collectors = {} self.config = config self.stat_user = StatUser(config['start'], config['end']) self.analysis_time = StatTime(config['start_analysis'], config['end_analysis']) self.rpt_print_intvl = 40 self.commit_intvl = 30 self.eh.register_timer(self.commit_intvl * 1000, TMTAGS.DB_COMMIT, True) self.db = Mydb() self.db.connect('repository') self.dbsession = self.db.open('all_users')
class AccessUserCalcMgr(CalcManager): def __init__(self, msgh, config): CalcManager.__init__(self, msgh) self.report_fd = None self.collectors = {} self.config = config self.stat_user = StatUser(config['start'], config['end']) self.analysis_time = StatTime(config['start_analysis'], config['end_analysis']) self.rpt_print_intvl = 40 self.commit_intvl = 30 self.eh.register_timer(self.commit_intvl * 1000, TMTAGS.DB_COMMIT, True) self.db = Mydb() self.db.connect('repository') self.dbsession = self.db.open('all_users') def set_config(self, config): self.config = config def _process_msg(self, msg): debug("AccessUserCalcMgr::_process_msg() enter") msgtype = msg.get_msgtype() #print "--------------process msg", datetime.datetime.now() if msgtype == MsgType.MsgAccessUserReport: debug("AccessUserCalcMgr::_process_msg() got " "report message %s" % msg.get_body()) ptr_msg = MsgAccessUserReport() ptr_msg.cast(msg) ptr_msg.parse() #print ptr_msg.get_users_info() for user, ts in ptr_msg.get_users_info(): entry = self.dbsession.select({'user': user}) if entry is None: self.dbsession.insert({'user': user, 'timestamp': ts}) else: #print "entry----", entry exist_ts = entry[1][1] if ts < exist_ts: #print "ts < exists", ts, exist_ts self.dbsession.update({'user': user, 'ts': ts}) else: super(AccessUserCalcMgr, self)._process_msg(msg) def _process_timer(self, msg): tag = msg.get_tag() if tag == TMTAGS.DB_COMMIT: self.dbsession.commit() else: super(AccessUserCalcMgr, self)._process_timer(msg) #super(AccessUserCalcMgr, self)._process_timer(msg) def _print_report(self): entries = self.dbsession.select("*") for key, entry in entries: userid = entry[0] ts = entry[1] self.analysis_time.stat_count(int(ts)) infos = self.analysis_time.show_info() days_map = sorted(infos.items(), key=lambda info: info[0], reverse=False) total = 0 ret = [] for day, count in days_map: total += count ret.append([day, total]) print ret def _final(self): echo("going to generate final report ...") self._print_report() self.dbsession.close() echo("report generation completed") super(AccessUserCalcMgr, self)._final()
class AccessUserCalcMgr(CalcManager): def __init__(self, msgh, config): CalcManager.__init__(self, msgh) self.report_fd = None self.collectors = {} self.config = config self.stat_user = StatUser(config['start'], config['end']) self.analysis_time = StatTime(config['start_analysis'], config['end_analysis']) self.rpt_print_intvl = 40 self.commit_intvl = 30 self.eh.register_timer(self.commit_intvl * 1000, TMTAGS.DB_COMMIT, True) self.db = Mydb() self.db.connect('repository') self.dbsession = self.db.open('all_users') def set_config(self, config): self.config = config def _process_msg(self, msg): debug("AccessUserCalcMgr::_process_msg() enter") msgtype = msg.get_msgtype() #print "--------------process msg", datetime.datetime.now() if msgtype == MsgType.MsgAccessUserReport: debug("AccessUserCalcMgr::_process_msg() got " "report message %s"%msg.get_body()) ptr_msg = MsgAccessUserReport() ptr_msg.cast(msg) ptr_msg.parse() #print ptr_msg.get_users_info() for user, ts in ptr_msg.get_users_info(): entry = self.dbsession.select({'user': user}) if entry is None: self.dbsession.insert({'user': user, 'timestamp': ts}) else: #print "entry----", entry exist_ts = entry[1][1] if ts < exist_ts: #print "ts < exists", ts, exist_ts self.dbsession.update({'user':user, 'ts': ts}) else: super(AccessUserCalcMgr, self)._process_msg(msg) def _process_timer(self, msg): tag = msg.get_tag() if tag == TMTAGS.DB_COMMIT: self.dbsession.commit() else: super(AccessUserCalcMgr, self)._process_timer(msg) #super(AccessUserCalcMgr, self)._process_timer(msg) def _print_report(self): entries = self.dbsession.select("*") for key, entry in entries: userid = entry[0] ts = entry[1] self.analysis_time.stat_count(int(ts)) infos = self.analysis_time.show_info() days_map = sorted(infos.items(), key=lambda info:info[0], reverse=False) total = 0 ret = [] for day, count in days_map: total += count ret.append([day, total]) print ret def _final(self): echo("going to generate final report ...") self._print_report() self.dbsession.close() echo("report generation completed") super(AccessUserCalcMgr, self)._final()