def stat(bot, update): chat_id = update.message.chat_id user_id = update.message.from_user.id chat_type = update.message.chat.type chat_title = update.message.chat.title last_call = cache.get('last_{}'.format(chat_id)) # First request if not last_call: cache.set('last_{}'.format(chat_id), int(time.time()) - 5) last_call = int(time.time()) - 5 # If last request was over 5 seconds ago if (int(time.time()) - last_call) >= 5: if chat_type == 'group' or chat_type == 'supergroup': # Get stats for group info = Stats.get_chat(chat_id) # Get msg text for /stat msg = Stats.stat_format(chat_id, info['msg_count'], info['current_users'], info['top_users'], chat_title) bot.sendMessage(chat_id, msg, parse_mode=ParseMode.MARKDOWN) # Update last call cache.set('last_{}'.format(chat_id), int(time.time())) logger.info('Group {} requested stats'.format(chat_id))
def me(bot, update): msg_id = update.message.message_id chat_id = update.message.chat_id user_id = update.message.from_user.id username = update.message.from_user.username fullname = " ".join([ update.message.from_user.first_name, update.message.from_user.last_name ]) chat_type = update.message.chat.type if chat_type == 'private': info = Stats.get_user(user_id) token = User.generate_token(user_id) msg = Stats.me_private_format(user_id, info['groups'], info['msg_count'], token) cache.set('user_token_{}'.format(user_id), token, 600) bot.sendMessage(chat_id, msg, parse_mode=ParseMode.MARKDOWN) if chat_type == 'group' or chat_type == 'supergroup': info = Stats.get_user(user_id, chat_id) msg = Stats.me_format(user_id, fullname, username, info['group_msg_count'], info['percent'], info['msg_count']) bot.sendMessage(chat_id, msg, reply_to_message_id=msg_id, parse_mode=ParseMode.MARKDOWN) logger.info('User {} requested stats'.format(user_id))
def add(self, cid, users_count, msg_count, last_time): chat_stat = self.get(cid) today = datetime.today().day if chat_stat: last_day = datetime.fromtimestamp(timestamp=chat_stat.last_time).day c = ChatStat(cid=cid, msg_count=int(chat_stat.msg_count) + msg_count, users_count=int(chat_stat.users_count) + users_count, last_time=last_time, chat_hash=self.generate_hash(cid)) if (timedelta(today).days - timedelta(last_day).days) != 0: c = ChatStat(cid=cid, msg_count=int(chat_stat.msg_count) + msg_count, users_count=0, last_time=last_time, chat_hash=self.generate_hash(cid)) db.add(c) else: self.update(cid, {'msg_count': int(chat_stat.msg_count) + msg_count, 'users_count': int(chat_stat.users_count) + users_count, 'last_time': last_time}) else: c = ChatStat(cid=cid, msg_count=msg_count, users_count=users_count, last_time=last_time, chat_hash=self.generate_hash(cid)) db.add(c) cache.set('cstat_{}'.format(cid), c) db.commit()
def get_current_order(user: User, session_id): orders_key = __current_orders_key(user, session_id) orders = cache.get(orders_key) if orders is not None and len(orders) > 0: return orders else: cache.set(orders_key, {}) return {}
def add_to_order(user: User, session_id, items): orders_key = __current_orders_key(user, session_id) orders = get_current_order(user, session_id) for item in items: orders = __add_item_to_orders(orders, item) cache.set(orders_key, orders) return orders
def get_current_session(user: User): session_key = __session_key(user) session_id = cache.get(session_key) if is_not_blank(session_id): return Session(session_id, False) else: new_session_id = uuid.uuid4().hex cache.set(session_key, new_session_id) return Session(new_session_id, True)
def get(uid): cached = cache.get('user_{}'.format(uid)) if cached: return cached else: q = db.query(User) \ .filter(User.uid == uid) \ .limit(1) \ .all() if q: cache.set('user_{}'.format(uid), q[0]) cache.delete('web_user_{}'.format(uid)) return q[0] else: return False
def get(cid): cached = cache.get('cstat_{}'.format(cid)) if cached: return cached else: q = db.query(ChatStat) \ .filter(ChatStat.cid == cid) \ .order_by(ChatStat.id.desc()) \ .limit(1) \ .all() if q: cache.set('cstat_{}'.format(cid), q[0]) return q[0] else: return False
def get(uid, cid): cached = cache.get('ustat_{}_{}'.format(uid, cid)) if cached: return cached else: q = db.query(UserStat) \ .filter(UserStat.cid == cid, UserStat.uid == uid) \ .limit(1) \ .all() if q: cache.set('ustat_{}_{}'.format(uid, cid), q[0]) return q[0] else: return False
def get_diskusage(self) -> list: """ 获取磁盘使用情况, 一小时后重新获取 :return: """ # 先从缓存中获取 diskinfo = cache.get('diskinfo') # 获取到了就直接返回 if diskinfo: return diskinfo # 缓存过期后重新获取 disklist = [] disks = psutil.disk_partitions() # 遍历系统磁盘 for disk in disks: # 获取磁盘使用情况 # 磁盘名称 name = disk[0] # 获取磁盘使用信息 try: disk_usage = psutil.disk_usage(name) except: continue # 获取磁盘总额 total = disk_usage.total / (1024 * 1024 * 1024) # 获取磁盘已用空间 used = disk_usage.used / (1024 * 1024 * 1024) # 获取磁盘可用空间 free = disk_usage.free / (1024 * 1024 * 1024) # 获取磁盘使用百分比 percent = disk_usage.percent # 添加到列表 if "Windows" in self.__system_info["os"]: name = name[:-2] else: name = name.replace('/', '') disklist.append({ "name": name, "total": total, "used": used, "free": free, "percent": percent }) # 设置缓存设置过期时间为1小时 cache.set('diskinfo', disklist, timeout=60 * 60 * 60) return disklist
def add(self, cid, title, public_link=''): chat = self.get(cid) if chat: if chat.title != title: self.update(cid, {'title': title}) if chat.public_link != public_link: self.update(cid, {'public_link': public_link}) else: db.add(Chat(cid=cid, title=title, public_link=public_link)) cache.set('chat_{}'.format(cid), Chat(cid=cid, title=title)) cache.delete('web_chat_{}'.format(cid)) db.commit()
def add(self, uid, cid, msg_count=1): user_stat = self.get(uid, cid) if user_stat: self.update(uid, cid, {'msg_count': int(user_stat.msg_count) + msg_count, 'last_activity': int(time.time())}) cache.set('ustat_{}_{}'.format(uid, cid), UserStat(uid=uid, cid=cid, msg_count=int(user_stat.msg_count) + msg_count, last_activity=int(time.time()))) else: c = UserStat(uid=uid, cid=cid, msg_count=msg_count, last_activity=int(time.time())) db.add(c) cache.set('ustat_{}_{}'.format(uid, cid), c) db.commit()
def add(self, uid, username, fullname): user = self.get(uid) update = {} if user: if user.username != username: update['username'] = username if user.fullname != fullname: update['fullname'] = fullname if update: self.update(uid, update) else: db.add(User(uid=uid, username=username, fullname=fullname)) db.commit() cache.set('user_'.format(uid), User(uid=uid, username=username, fullname=fullname)) cache.delete('web_user_{}'.format(uid))