下面是一个纯 python 解决方案,可以绘制数据。
输出 ( entries
) 是一个字典,其中键是时间戳,值是包含有趣信息的字典。log_lines
保存日志消息的集合并用作输入。
import json
import pprint
log_lines = [
'2021-09-09 00:30:46,742 [Timer-0] INFO - { "env": "test_environment", "capacity": 10, "available_ec2": 10, "failed_ec2": 0, "running_tasks": 2, "queued_tasks": 0, "finished_tasks": 0, "failed_tasks": 0, "cancelled_tasks": 3,"queue_wait_minutes" : { "max": 0, "mean": -318990, "max_started": 0, "mean_started": -29715 },"processing_time": {"max": 0, "mean": 0} }',
'2021-09-09 00:31:46,742 [Timer-0] INFO - { "env": "test_environment", "capacity": 10, "available_ec2": 10, "failed_ec2": 0, "running_tasks": 5, "queued_tasks": 0, "finished_tasks": 0, "failed_tasks": 0, "cancelled_tasks": 3,"queue_wait_minutes" : { "max": 0, "mean": -318990, "max_started": 0, "mean_started": -29715 },"processing_time": {"max": 0, "mean": 0} }'
]
entries = dict()
for line in log_lines:
date = line[:line.find('[') - 1]
data = json.loads(line[line.find('{'):])
sub_set = {k: data.get(k,0) for k in
["running_tasks", "queued_tasks", "finished_tasks", "failed_tasks", "cancelled_tasks"]}
entries[date] = sub_set
pprint.pprint(entries)
输出
{'2021-09-09 00:30:46,742': {'cancelled_tasks': 3,
'failed_tasks': 0,
'finished_tasks': 0,
'queued_tasks': 0,
'running_tasks': 2},
'2021-09-09 00:31:46,742': {'cancelled_tasks': 3,
'failed_tasks': 0,
'finished_tasks': 0,
'queued_tasks': 0,
'running_tasks': 5}}