[时间维度]日志数据提取事件关键词,解析对应时间点计数,matplotlib绘制统计图,python

x33g5p2x  于2022-07-26 转载在 Python  
字(3.8k)|赞(0)|评价(0)|浏览(306)

**[时间维度]**日志数据提取事件关键词,解析对应时间点计数,matplotlib绘制统计图,python

https://zhangphil.blog.csdn.net/article/details/125914326

https://zhangphil.blog.csdn.net/article/details/125914326

日志或数据文件中,每一行数据均为相对结构化的数据,每一行数据均记录描述了一次事件。该事件的数据中包含特定关键词和时间。

实现一个简单的功能:

(1)数据文件中,对每一行数据模糊查找是否包含了所要寻找的关键词,如果该行以一定的概率(>80%可能性)存在目标关键词,那么该行即为认定为目标高价值数据事件行,然后再从该行中提取事件时间点。

(2)因为数据文件中的行数据时海量的,也意味着高价值事件很多,同样,这条事件对应的时间点也很多。需要把事件发生时间划入到不同的时间段(时间区间),比如(11,12)代表11点到12点,(1,2)代表凌晨1点到2点。每一个时间段内,只要有高价值事件(包含关键词的数据哈)发生的时间落入进来,就为这个时间段计数+1。

(3)这样每个时间段(时间区间)都会有计数统计。比如(9,10),计数5,表示发生再9点到10点(不包含10点)的事件为5件。

(4)最终绘制水平方向的柱状图把统计数据展示。

有些行没有时间或者数据样本残缺不全,需要过滤筛选。

from datetime import datetime
from pprint import pp

import pandas as pd
import matplotlib
import matplotlib.pyplot as plt

from fuzzywuzzy import fuzz
import re

FILE_PATH = r'源数据的存放路径'
KEY = r'要匹配查找的关键词'  # 关键词1,关键词2
threshold = 80

SECTION = 'section'
SUM = 'sum'

def drawchart(df):
    myfont = matplotlib.font_manager.FontProperties(fname='C:\Windows\Fonts\msyh.ttc')
    plt.rcParams['axes.unicode_minus'] = False  # 用来正常显示负号
    plt.rc('font', family='YaHei', weight='bold')

    order = []
    name = []
    mem = []
    for d, i in zip(df.values, df.index):
        order.append(i)
        name.append(d[0])
        mem.append(int(d[1]))

    FONT_SIZE = 12

    fig, ax = plt.subplots(figsize=(15, 13))

    b = ax.barh(y=range(len(name)), width=mem, align='center', color='red')

    # 为横向水平的柱图右侧添加数据标签。
    i = 0
    for rect in b:
        w = rect.get_width()
        ax.text(x=w, y=rect.get_y() + rect.get_height() / 2, s='%d' % (int(w)),
                horizontalalignment='left', verticalalignment='center',
                fontproperties=myfont, fontsize=FONT_SIZE - 2, color='green')
        ax.text(x=w / 2, y=rect.get_y() + rect.get_height() / 2, s=str(order[i]),
                horizontalalignment='center', verticalalignment='center',
                fontproperties=myfont, fontsize=FONT_SIZE - 3, color='white')
        i = i + 1

    ax.set_yticks(range(len(name)))
    ax.set_yticklabels(name, fontsize=FONT_SIZE - 1, fontproperties=myfont)

    ax.invert_yaxis()

    ax.set_xlabel('数据', fontsize=FONT_SIZE + 2, fontproperties=myfont)
    ax.set_title('不同时间段的数据点总量排名', fontsize=FONT_SIZE + 5, fontproperties=myfont)

    # 不要横坐标上的label标签。
    plt.xticks(())

    # 清除四周的边框线
    ax.get_yaxis().set_visible(True)
    for spine in ["left", "top", "right", "bottom"]:
        ax.spines[spine].set_visible(False)

    plt.subplots_adjust(left=0.15)  # 调整左侧边距

    # ax.margins(y=0.01) #缩放 zoom in

    ax.set_aspect('auto')

    plt.show()

def read_file():
    file = open(FILE_PATH, 'r', encoding='UTF-8')

    all_case_time = []

    case_count = 1
    cnt = 1
    for line in file:
        pr = fuzz.partial_ratio(line, KEY)

        if pr >= threshold:
            print('-----')
            print(f'第{case_count}件')
            case_count = case_count + 1

            try:
                # 正则匹配 xxxx年xx月xx日xx时xx分
                mat = re.search(r'\d{4}\年\d{1,2}\月\d{1,2}\日\d{1,2}\时\d{1,2}\分', line)
                t_str = mat.group().replace('\n', '')  # 去掉正则匹配到但是多余的 \n 换行符

                try:
                    object_time = datetime.strptime(t_str, "%Y年%m月%d日%H时%M分")
                    all_case_time.append(object_time.time())  # 把时间从日期中提取出来,放到数组中
                    print(f'时间{object_time.time().strftime("%H:%M")}')
                except:
                    print('解析日期失败')
                    pass
            except:
                t_str = '-解析异常-'
                pass

            s = '第{number}行,相似度{ratio},时间{case_time}\n{content}'
            ss = s.format(number=cnt, ratio=pr, case_time=t_str, content=line)
            pp(ss)

        # 快速调试
        # if case_count > 100:
        #    break

        cnt = cnt + 1

    file.close()

    return all_case_time

def data_frame():
    ts = read_file()

    times = []
    for i in range(24):
        times.append({SECTION: (i, i + 1), SUM: 0})

    for t in ts:
        for tx in times:
            if tx[SECTION][0] <= t.hour < tx[SECTION][1]:
                tx[SUM] = tx[SUM] + 1
                # pprint(f'{t.strftime("%H:%M")} @ {tx[SECTION]}')
                break

    return times

if __name__ == '__main__':
    times = data_frame()

    # 数据组装成pandas数据帧。
    pd_data = []
    for t in times:
        l = [t[SECTION], t[SUM]]
        pd_data.append(l)

    col = ['时间段', '时间点次数']
    df = pd.DataFrame(data=pd_data, columns=col)
    df = df.sort_values(by=col[1], axis=0, ascending=False)  # 降序

    # 重置索引
    df = df.reset_index(drop=True)
    df.index = df.index + 1

    # 前20名
    pp(df.head(20))
    # pp(df.values)

    drawchart(df)

部分输出:

时间段  时间点次数
1   (20, 21)    116
2   (21, 22)    115
3   (19, 20)     86
4   (22, 23)     80
5   (23, 24)     56
6   (13, 14)     56
7   (15, 16)     48
8   (14, 15)     45
9     (0, 1)     41
10  (18, 19)     36
11  (16, 17)     27
12  (17, 18)     26
13  (12, 13)     23
14    (1, 2)     16
15  (11, 12)     15
16    (2, 3)     12
17  (10, 11)     11
18   (9, 10)      9
19    (8, 9)      9
20    (4, 5)      5

变换不同的关键词,生成的统计图:

相关文章