# -*- coding: utf-8 -*- # @Author: wangkun # @Time: 2023/7/18 import datetime # 读取日志文件并将每行日志存储到一个列表中 # with open('../logs/benshanzhufu-recommend-2023-07-17.log', 'r') as file: # with open('../logs/douyin-author-2023-07-17.log', 'r') as file: with open('../logs/douyin-recommend-2023-07-19.log', 'r') as file: # with open('../logs/kuaishou-author-2023-07-17.log', 'r') as file: # with open('../logs/kuaishou-recommend-2023-07-17.log', 'r') as file: # with open('../logs/xigua-author-2023-07-17.log', 'r') as file: # with open('../logs/xigua-search-2023-07-17.log', 'r') as file: # with open('../logs/xiaoniangao-author-2023-07-17.log', 'r') as file: # with open('../logs/xiaoniangao-play-2023-07-17.log', 'r') as file: # with open('../logs/xiaoniangao-hour-2023-07-17.log', 'r') as file: # with open('../logs/suisuiniannianyingfuqi-recommend-2023-07-17.log', 'r') as file: # with open('../logs/gongzhonghao-author1-2023-07-18.log', 'r') as file: # with open('../logs/gongzhonghao-author2-2023-07-18.log', 'r') as file: # with open('../logs/gongzhonghao-author3-2023-07-18.log', 'r') as file: # with open('../logs/gongzhonghao-author4-2023-07-18.log', 'r') as file: # with open('../logs/gongzhonghao-author5-2023-07-18.log', 'r') as file: # with open('../logs/gongzhonghao-author6-2023-07-18.log', 'r') as file: log_lines = file.readlines() # 存储间隔时间的列表 intervals = [] # 遍历日志列表,计算相邻两条日志的时间间隔 for i in range(1, len(log_lines)): if "2023-" not in log_lines[i-1] or "2023-" not in log_lines[i]: continue # 解析时间戳 timestamp1 = datetime.datetime.strptime(log_lines[i - 1].split(".")[0], '%Y-%m-%d %H:%M:%S') timestamp2 = datetime.datetime.strptime(log_lines[i].split(".")[0], '%Y-%m-%d %H:%M:%S') # 计算时间间隔 interval = timestamp2 - timestamp1 # 将时间间隔添加到间隔时间列表中 intervals.append(interval) # 对间隔时间列表进行倒序排序 intervals.sort(reverse=True) # 取前10条间隔时间 top_10_intervals = intervals[:10] # 取前10条间隔时间的秒数 top_10_intervals_seconds = [int(interval.total_seconds()) for interval in top_10_intervals] # 打印结果 print(top_10_intervals_seconds) # benshanzhufu [1, 1, 1, 1, 1, 1, 1, 1, 1, 1] # douyin_author [30, 28, 25, 20, 18, 18, 17, 17, 17, 16] # douyin_recommend [62, 50, 38, 34, 33, 31, 31, 31, 31, 31] # kuaishou_author [31, 27, 23, 21, 21, 21, 19, 19, 17, 15] # kuaishou_recommend [27, 23, 23, 23, 23, 22, 22, 22, 21, 21] # xigua_author [1, 1, 1, 1, 1, 1, 1, 1, 1, 1] # xigua_search [18, 14, 14, 13, 13, 12, 11, 11, 11, 10] # xiaoniangao_author [4, 4, 4, 4, 4, 4, 4, 4, 4, 4] # xiaoniangao_play [1, 1, 1, 1, 1, 1, 1, 1, 1, 1] # xiaoniangao_hour [61, 3, 2, 2, 2, 2, 2, 2, 1, 1] # suisuiniannian [1, 1, 1, 1, 1, 1, 1, 1, 1, 1] # gzh1 [26, 26, 26, 25, 25, 25, 25, 24, 24, 24] # gzh2 [28, 26, 25, 25, 24, 24, 24, 24, 24, 24] # gzh3 [27, 26, 26, 25, 25, 25, 24, 24, 24, 24] # gzh4 [26, 26, 25, 24, 24, 24, 24, 24, 24, 24] # gzh5 [29, 26, 25, 25, 24, 24, 24, 24, 24, 24] # gzh6 [26, 25, 25, 25, 25, 24, 24, 24, 24, 24]