def post(self): """Provide statistics about terminal. """ status = ErrorCode.SUCCESS try: data = DotDict(json_decode(self.request.body)) logging.info("[UWEB] statistic request: %s, uid: %s, tid: %s", data, self.current_user.uid, self.current_user.tid) except Exception as e: status = ErrorCode.ILLEGAL_DATA_FORMAT self.write_ret(status) return try: page_size = UWEB.LIMIT.PAGE_SIZE_STATISTICS page_number = int(data.pagenum) page_count = int(data.pagecnt) tid = data.tid res = [] graphics = [] counts = [] label = u'' sql_cmd = ("SELECT longitude, latitude FROM T_LOCATION" " WHERE tid = %s" " AND (timestamp BETWEEN %s AND %s)" " AND type = 0" " ORDER BY timestamp asc") last_cmd = ("SELECT timestamp FROM T_LOCATION" " WHERE tid = %s" " AND (timestamp BETWEEN %s AND %s)" " AND type = 0" " ORDER BY timestamp desc limit 1") next_cmd = ("SELECT timestamp FROM T_LOCATION" " WHERE tid = %s" " AND (timestamp BETWEEN %s AND %s)" " AND type = 0" " ORDER BY timestamp asc limit 1") dis_sum = Decimal() current_time = int(time.time()) statistics_type = data.statistics_type if statistics_type == UWEB.STATISTICS_TYPE.YEAR: label = data.year + u'年' start_time, end_time = start_end_of_year(year=data.year) for month in range(1,12+1): start_time_, end_time_ = start_end_of_month(year=data.year, month=str(month)) if start_time_ > current_time: break re = {} re['name'] = str(month) distance = Decimal() points = self.db.query(sql_cmd, tid, start_time_, end_time_) for i in range(len(points)-1): if points[i].longitude and points[i].latitude and \ points[i+1].longitude and points[i+1].latitude: dis = get_distance(points[i].longitude, points[i].latitude, points[i+1].longitude, points[i+1].latitude) distance += Decimal(str(dis)) # meter --> km distance = '%0.1f' % (distance/1000,) if float(distance) == 0: distance = 0 graphics.append(float(distance)) dis_sum += Decimal(distance) re['mileage'] = distance res.append(re) counts = [float(dis_sum),] elif statistics_type == UWEB.STATISTICS_TYPE.MONTH: label = data.year + u'年' + data.month + u'月' start_time, end_time = start_end_of_month(year=data.year, month=data.month) days = days_of_month(year=data.year, month=data.month) distance = Decimal() points_ = self.db.query(sql_cmd, tid, start_time, end_time) for i in range(len(points_)-1): if points_[i].longitude and points_[i].latitude and \ points_[i+1].longitude and points_[i+1].latitude: dis = get_distance(points_[i].longitude, points_[i].latitude, points_[i+1].longitude, points_[i+1].latitude) dis=Decimal(str(dis)) distance += dis distance = '%0.1f' % (distance/1000,) dis_sum = distance for day in range(1,days+1): start_time_, end_time_ = start_end_of_day(year=data.year, month=data.month, day=str(day)) if start_time_ > current_time: break last_point = self.db.get(last_cmd, tid, start_time_-60*60*24, start_time_,) next_point = self.db.get(next_cmd, tid, end_time_, end_time_+60*60*24) start_time_ = last_point['timestamp'] if last_point else start_time_ end_time_ = next_point['timestamp'] if next_point else end_time_ re = {} re['name'] = str(day) distance = Decimal() points = self.db.query(sql_cmd, tid, start_time_, end_time_) for i in range(len(points)-1): if points[i].longitude and points[i].latitude and \ points[i+1].longitude and points[i+1].latitude: dis = get_distance(points[i].longitude, points[i].latitude, points[i+1].longitude, points[i+1].latitude) distance += Decimal(str(dis)) # meter --> km distance = '%0.1f' % (distance/1000,) if float(distance) == 0: distance = 0 graphics.append(float(distance)) re['mileage'] = distance res.append(re) counts = [float(dis_sum),] else: logging.error("[UWEB] Error statistics type: %s", statistics_type) if page_count == -1: items_count = len(res) d, m = divmod(items_count, page_size) page_count = (d + 1) if m else d # store resutl in redis m = hashlib.md5() m.update(self.request.body) hash_ = m.hexdigest() mem_key = self.KEY_TEMPLATE % (self.current_user.uid, hash_) self.redis.setvalue(mem_key, (res, counts, label, statistics_type), time=UWEB.STATISTIC_INTERVAL) res= res[page_number*page_size:(page_number+1)*page_size] self.write_ret(status, dict_=dict(res=res, counts=counts, graphics=graphics, pagecnt=page_count, hash_=hash_)) except Exception as e: logging.exception("[UWEB] uid:%s, tid:%s statistic failed. Exception: %s", self.current_user.uid, self.current_user.tid, e.args) status = ErrorCode.SERVER_BUSY self.write_ret(status)
def post(self): """Provie some statistics for one terminals. """ status = ErrorCode.SUCCESS try: data = DotDict(json_decode(self.request.body)) logging.info("[UWEB] event single statistic request: %s, uid: %s, tid: %s", data, self.current_user.uid, self.current_user.tid) except Exception as e: status = ErrorCode.ILLEGAL_DATA_FORMAT self.write_ret(status) return try: page_size = UWEB.LIMIT.PAGE_SIZE_STATISTICS page_number = int(data.pagenum) page_count = int(data.pagecnt) tid = data.tid res = [] graphics = [] counts_dct= {} counts = [] label = u'' sql_cmd = ("SELECT COUNT(*) as count FROM V_EVENT" " WHERE tid = %s" " AND (timestamp BETWEEN %s AND %s) AND category != 5 ") sql_cmd_category = sql_cmd + " AND category =%s" current_time = int(time.time()) CATEGORY_DCT = DotDict(powerlow=EVENTER.CATEGORY.POWERLOW, illegashake=EVENTER.CATEGORY.ILLEGALSHAKE, illegalmove=EVENTER.CATEGORY.ILLEGALMOVE, #sos=EVENTER.CATEGORY.EMERGENCY, heartbeat_lost=EVENTER.CATEGORY.HEARTBEAT_LOST) CATEGORY_KEY = ['illegalmove','illegashake', 'heartbeat_lost', 'powerlow' ] for key in CATEGORY_KEY: counts_dct[key] = 0 statistics_type = data.statistics_type if statistics_type == UWEB.STATISTICS_TYPE.YEAR: label = data.year + u'年' start_time, end_time = start_end_of_year(year=data.year) for month in range(1,12+1): start_time_, end_time_ = start_end_of_month(year=data.year, month=str(month)) if start_time_ > current_time: break re = {} re['name'] = str(month) events = {} for key in CATEGORY_KEY: res_item = {} item = self.db.get(sql_cmd_category, tid, start_time_, end_time_, CATEGORY_DCT[key]) res_item[key] = item.count events.update(res_item) #event = self.db.get(sql_cmd, tid, start_time_, end_time_) #graphics.append(event.count) re['events'] = events res.append(re) #for category in CATEGORY_DCT.itervalues(): # event_count = self.db.get(sql_cmd_category, tid, start_time, end_time, category) # counts.append(event_count.count) for r in res: graphics.append(sum(r['events'].itervalues())) event = r['events'] for key in CATEGORY_KEY: counts_dct[key] += event[key] for key in CATEGORY_KEY: counts.append(counts_dct[key]) elif statistics_type == UWEB.STATISTICS_TYPE.MONTH: label = data.year + u'年' + data.month + u'月' start_time, end_time = start_end_of_month(year=data.year, month=data.month) days = days_of_month(year=data.year, month=data.month) for day in range(1,days+1): start_time_, end_time_ = start_end_of_day(year=data.year, month=data.month, day=str(day)) if start_time_ > current_time: break re = {} re['name'] = str(day) events = {} for key in CATEGORY_KEY: res_item = {} item = self.db.get(sql_cmd_category, tid, start_time_, end_time_, CATEGORY_DCT[key]) res_item[key] = item.count events.update(res_item) re['events'] = events res.append(re) for r in res: graphics.append(sum(r['events'].itervalues())) event = r['events'] for key in CATEGORY_KEY: counts_dct[key] += event[key] for key in CATEGORY_KEY: counts.append(counts_dct[key]) else: logging.error("[UWEB] Error statistics type: %s", statistics_type) if page_count == -1: items_count = len(res) d, m = divmod(items_count, page_size) page_count = (d + 1) if m else d # store resutl in redis m = hashlib.md5() m.update(self.request.body) hash_ = m.hexdigest() mem_key = self.KEY_TEMPLATE % (self.current_user.uid, hash_) self.redis.setvalue(mem_key, (res, counts, label), time=UWEB.STATISTIC_INTERVAL) res= res[page_number*page_size:(page_number+1)*page_size] self.write_ret(status, dict_=dict(res=res, counts=counts, graphics=graphics, pagecnt=page_count, hash_=hash_)) except Exception as e: logging.exception("[UWEB] event statistic, uid:%s, tid:%s failed. Exception: %s", self.current_user.uid, self.current_user.tid, e.args) status = ErrorCode.SERVER_BUSY self.write_ret(status)
def post(self): """Provide statistics about terminal. """ status = ErrorCode.SUCCESS try: data = DotDict(json_decode(self.request.body)) logging.info("[UWEB] Single statistic request: %s, uid: %s, tid: %s", data, self.current_user.uid, self.current_user.tid) except Exception as e: status = ErrorCode.ILLEGAL_DATA_FORMAT self.write_ret(status) return try: page_size = UWEB.LIMIT.PAGE_SIZE_STATISTICS page_number = int(data.pagenum) page_count = int(data.pagecnt) tid = data.tid res = [] graphics = [] counts = [] label = u'' dis_sum = Decimal() current_time = int(time.time()) statistics_type = data.statistics_type if statistics_type == UWEB.STATISTICS_TYPE.YEAR: label = data.year + u'年' start_time, end_time = start_end_of_year(year=data.year) for month in range(1, 12 + 1): start_time_, end_time_ = start_end_of_month( year=data.year, month=str(month)) if start_time_ > current_time: break re = {} re['name'] = str(month) distance = Decimal() mileage_log = self.db.get("SELECT SUM(distance) AS distance " " FROM T_MILEAGE_LOG" " WHERE tid = %s" " AND (timestamp BETWEEN %s AND %s)", tid, start_time_, end_time_) if mileage_log and mileage_log['distance']: distance = '%0.1f' % ( Decimal(mileage_log['distance']) / 1000,) else: distance = 0 graphics.append(float(distance)) dis_sum += Decimal(distance) re['mileage'] = float(distance) res.append(re) counts = [float(dis_sum), ] elif statistics_type == UWEB.STATISTICS_TYPE.MONTH: label = data.year + u'年' + data.month + u'月' start_time, end_time = start_end_of_month( year=data.year, month=data.month) days = days_of_month(year=data.year, month=data.month) distance = Decimal() for day in range(1, days + 1): start_time_, end_time_ = start_end_of_day( year=data.year, month=data.month, day=str(day)) if start_time_ > current_time: break re = {} re['name'] = str(day) distance = Decimal() # NOTE:TODO get mileage mileage_log = self.db.get("SELECT distance FROM T_MILEAGE_LOG" " WHERE tid = %s" " AND timestamp = %s", tid, end_time_) distance = mileage_log['distance'] if mileage_log else 0 # meter --> km distance = '%0.1f' % (Decimal(distance) / 1000,) graphics.append(float(distance)) dis_sum += Decimal(distance) re['mileage'] = float(distance) res.append(re) counts = [float(dis_sum), ] else: logging.error( "[UWEB] Error statistics type: %s", statistics_type) if page_count == -1: items_count = len(res) d, m = divmod(items_count, page_size) page_count = (d + 1) if m else d # store resutl in redis m = hashlib.md5() m.update(self.request.body) hash_ = m.hexdigest() mem_key = self.KEY_TEMPLATE % (self.current_user.uid, hash_) self.redis.setvalue( mem_key, (res, counts, label, statistics_type), time=UWEB.STATISTIC_INTERVAL) res = res[page_number * page_size:(page_number + 1) * page_size] self.write_ret(status, dict_=dict(res=res, counts=counts, graphics=graphics, pagecnt=page_count, hash_=hash_)) except Exception as e: logging.exception("[UWEB] Statistic single failed. uid:%s, tid:%s, Exception: %s", self.current_user.uid, self.current_user.tid, e.args) status = ErrorCode.SERVER_BUSY self.write_ret(status)