Пример #1
0
    def put(self):
        """Modify smsoptions for the given owner_mobile.
        """
        status = ErrorCode.SUCCESS
        try:
            data = DotDict(json_decode(self.request.body))
            owner_mobile = data['owner_mobile']
            logging.info("[UWEB] smsoption request: %s, uid: %s", 
                         data, self.current_user.uid)
        except Exception as e:
            status = ErrorCode.ILLEGAL_DATA_FORMAT
            logging.exception("[UWEB] Invalid data format. Exception: %s",
                              e.args)
            self.write_ret(status)
            return 

        try:
            del data['owner_mobile']
            fields = DotDict(login="******",
                             powerlow="powerlow = %s",
                             powerdown="powerdown = %s",
                             illegalshake="illegalshake = %s",
                             illegalmove="illegalmove = %s",
                             sos="sos = %s",
                             heartbeat_lost="heartbeat_lost = %s",
                             charge="charge = %s",
                             region_enter="region_enter = %s",
                             region_out="region_out = %s",
                             stop="stop = %s",
                             speed_limit="speed_limit = %s")
            for key, value in data.iteritems():
                data[key] = fields[key] % data[key] 
            set_clause = ','.join([v for v in data.itervalues() if v is not None])
            if set_clause:
                self.db.execute("UPDATE T_SMS_OPTION SET " + set_clause +
                                "  WHERE uid = %s",
                                owner_mobile)
            self.write_ret(status)
        except Exception as e:
            logging.exception("[UWEB] uid:%s tid:%s update SMS Options failed.  Exception: %s", 
                              self.current_user.uid,self.current_user.tid, e.args)
            status = ErrorCode.SERVER_BUSY
            self.write_ret(status)
Пример #2
0
    def post(self):
        """Provide some statistics about terminals.
        """
        status = ErrorCode.SUCCESS
        try:
            data = DotDict(json_decode(self.request.body))
            logging.info("[UWEB] event 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:
            res = []
            page_size = UWEB.LIMIT.PAGE_SIZE
            page_number = int(data.pagenum)
            page_count = int(data.pagecnt)
            start_time = data.start_time
            end_time = data.end_time
            tids = str_to_list(data.tids)
            # the interval between start_time and end_time is one week

            if self.current_user.cid != UWEB.DUMMY_CID: # no checks for enterprise
                pass
            elif (int(end_time) - int(start_time)) > UWEB.QUERY_INTERVAL:
                self.write_ret(ErrorCode.QUERY_INTERVAL_EXCESS)
                return

            CATEGORY_DCT = DotDict(illegalmove=EVENTER.CATEGORY.ILLEGALMOVE,
                                   illegashake=EVENTER.CATEGORY.ILLEGALSHAKE,
                                   #sos=EVENTER.CATEGORY.EMERGENCY,
                                   heartbeat_lost=EVENTER.CATEGORY.HEARTBEAT_LOST, 
                                   powerlow=EVENTER.CATEGORY.POWERLOW)
            if page_count == -1:
                count = len(tids)
                d, m = divmod(count, page_size)
                page_count = (d + 1) if m else d


            for tid in tids: 
                res_item = {} 
                res_item['alias'] = QueryHelper.get_alias_by_tid(tid, self.redis, self.db)
                for key, category in CATEGORY_DCT.iteritems():
                     item = self.db.get("SELECT COUNT(*) as count FROM V_EVENT"
                                        "  WHERE tid = %s"
                                        "    AND (timestamp BETWEEN %s AND %s)"
                                        "    AND category = %s",
                                        tid, start_time, end_time, category)
                     res_item[key] = item.count
                res.append(res_item)

            # 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, time=UWEB.STATISTIC_INTERVAL)

            res = res[(page_number * page_size):((page_number+1) * page_size)]

            self.write_ret(status, 
                           dict_=DotDict(res=res,
                                         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)