Skip to content

Commit

Permalink
Add new function to history plugin and fix recorder behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
he0119 committed Oct 14, 2018
1 parent a24efa0 commit 4494d8c
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 14 deletions.
20 changes: 12 additions & 8 deletions src/coolqbot/recorder.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@


class Recorder(object):
def __init__(self):
def __init__(self, path=None):
self.last_message_on = datetime.utcnow()
self.msg_send_time = []
self.repeat_list = {}
self.msg_number_list = {}
if path:
self.load_data(path)

def message_number(self, x):
'''返回x分钟内的消息条数,并清除之前的消息记录'''
Expand All @@ -21,8 +23,13 @@ def message_number(self, x):
for i in range(len(times)):
if times[i] > now - timedelta(minutes=x):
self.msg_send_time = self.msg_send_time[i:]
bot.logger.debug(self.msg_send_time)
bot.logger.debug(len(self.msg_send_time))
return len(self.msg_send_time)

# 如果没有满足条件的消息,则清除记录
self.msg_send_time = []
bot.logger.debug(self.msg_send_time)
bot.logger.debug(len(self.msg_send_time))
return len(self.msg_send_time)

Expand Down Expand Up @@ -59,7 +66,7 @@ def save_pkl(self, path):

def load_pkl(self, path):
try:
with path.open('rb') as f:
with path.open(mode='rb') as f:
data = pickle.load(f)
bot.logger.debug('记录加载成功')
return data
Expand All @@ -78,22 +85,19 @@ def clear_data(self):
self.repeat_list = {}
self.msg_number_list = {}

recorder = Recorder()

recorder.load_data(RECORDER_FILE_PATH)

# TODO:增加每月1日清除上约数据功能
# 增加查询历史记录功能(/history)
recorder = Recorder(RECORDER_FILE_PATH)


@scheduler.scheduled_job('cron', day='last', hour=23, minute=59, second=59)
async def clear_data():
'''每个月最后一分钟保存记录于历史记录文件夹,并重置记录'''
# 保存数据到历史文件夹
date = datetime.now().strftime('%Y-%m')
recorder.save_pkl(HISTORY_DIR_PATH / f'{date}.pkl')
# 清除现有数据
recorder.clear_data()
bot.logger.debug('记录清除完成')
bot.logger.info('记录清除完成')


@scheduler.scheduled_job('interval', minutes=1)
Expand Down
37 changes: 31 additions & 6 deletions src/plugins/history.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,53 @@

@bot.on_message('group', 'private')
async def history(context):
match = re.match(r'^\/history', context['message'])
match = re.match(r'^\/history ?(\d+)\-?(\d+)?|^\/history', context['message'])
if match:
date = datetime.now() - relativedelta(months=1)
str_data = ''

year = match.group(1)
month = match.group(2)
if year:
year = int(year)
if month:
month = int(month)

if not year:
str_data = '欢迎查询历史记录\n如需查询上月数据请输入/history 1\n如需查询指定月份请输入/history year-month(如2018-01)'
return {'reply': str_data, 'at_sender': False}

if year and month:
if year < 1 or year > 9999:
str_data = '请输入1到9999的年份,超过了我就不能查惹'
return {'reply': str_data, 'at_sender': False}
if month > 12:
str_data = '请输入正确的月份,众所周知,一年只有12个月'
return {'reply': str_data, 'at_sender': False}
date = datetime(year=year, month=month, day=1)
else:
date = datetime.now() - relativedelta(months=1)

history_file = HISTORY_DIR_PATH / f'{date.strftime("%Y-%m")}.pkl'
bot.logger.debug(history_file)

history_data = Recorder()
history_data.load_data(history_file)
if not history_file.exists():
str_data = f'{date.year}{date.month}月数据不存在,请换试试吧0.0'
return {'reply': str_data, 'at_sender': False}

# 如无其他情况,开始获取历史记录,并输出排行榜
history_data = Recorder(history_file)
display_number = 10000
minimal_msg_number = 0
display_total_number = True

repeat_list = history_data.repeat_list
msg_number_list = history_data.msg_number_list

str_data = ''
repeat_rate_ranking = await get_repeat_rate_ranking(repeat_list, msg_number_list, display_number, minimal_msg_number, display_total_number)
repeat_number_ranking = await get_repeat_number_ranking(repeat_list, display_number)

if repeat_rate_ranking and repeat_rate_ranking:
str_data = date.strftime("%Y-%m") + '数据\n'
str_data = f'{date.year}{date.month}月数据\n'
str_data += repeat_rate_ranking + '\n\n' + repeat_number_ranking

if not str_data:
Expand Down

0 comments on commit 4494d8c

Please sign in to comment.