-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathload.py
More file actions
161 lines (121 loc) · 5.16 KB
/
load.py
File metadata and controls
161 lines (121 loc) · 5.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#! /usr/bin/env python3
import pandas as pd
import numpy as np
import glob
from datetime import datetime
from dateutil.parser import parse
from elasticsearch import Elasticsearch
def _load_vmstat(monitoring_data):
monitoring_data["timestamp"] = pd.to_datetime(monitoring_data["timestamp"]+ 3600, unit='s')
monitoring_data = monitoring_data.rename(columns={"r":"processes","b":"waiting","swdp":"virtual mem","free":"free","buff":"buffers","si":"mem_on_disk","so":"mem_to_disk","bi":"blockIn","bo":"blockOut","in":"interrupts","cs":"switches","us":"cpu_user","sy":"cpu_system","id":"cpu_idle","wa":"blocked"})
return monitoring_data
def load_vmstat(load_from_cache=False,store_cache_file=False,cache_file=None):
monitoring_data = None
if load_from_cache and cache_file is not None:
monitoring_data = pd.read_csv(cache_file)
else:
for file in glob.glob("vmstats/*"):
df = pd.read_csv(file, skiprows = 0,error_bad_lines=False)
if monitoring_data is None:
monitoring_data = df
else:
monitoring_data = pd.concat([monitoring_data, df], sort=True)
#clean up data
monitoring_data = _load_vmstat(monitoring_data)
if store_cache_file:
monitoring_data.to_csv(cache_file)
return monitoring_data
def load_elastic(load_from_cache=False,store_cache_file=False,cache_file=None,es=None,experiment_dates=[]):
monitoring_data = None
if load_from_cache and cache_file is not None:
monitoring_data = pd.read_csv(cache_file)
else:
monitoring_data = collect_monitoring_data(es,"*",experiment_dates)
if store_cache_file:
if monitoring_data is not None:
monitoring_data.to_csv(cache_file)
return monitoring_data
def load_rmstats():
monitoring_data = None
for file in glob.glob("rmstats/*.csv"):
df = pd.read_csv(file, skiprows = 0,error_bad_lines=False)
if monitoring_data is None:
monitoring_data = df
else:
monitoring_data = pd.concat([monitoring_data, df], sort=True)
return monitoring_data
def load_experiment(load_from_cache=False,store_cache_file=False,data_cache_file=None):
data = None
if load_from_cache and data_cache_file is not None:
data = pd.read_csv(data_cache_file)
else:
data = __load()
if store_cache_file:
data.to_csv(data_cache_file)
return data
def __load():
all = None
for file in glob.glob("data/*"):
names = file[5:-4].split("-")
experiment=names[0]
method=names[1]
datetime.strptime
timestamp=file[5+len(experiment)+1+len(method)+1:-4]
date=timestamp[:timestamp.index("T")]
date=datetime.strptime(date, '%Y-%m-%d')
timestamp=parse(timestamp)
df = pd.read_csv(file, skiprows = 0,error_bad_lines=False)
df['experiment']=experiment
df['method']=method
df['startTime']=pd.Timestamp(year=timestamp.year,month=timestamp.month, day=timestamp.day, hour=timestamp.hour, minute=timestamp.minute)
df['runDate']=pd.Timestamp(year=date.year,month=date.month, day=date.day)
if (all is None):
all = df
else:
all = pd.concat([all, df], sort=True)
return all
def __collect(es,index,query):
data = []
page = es.search(
index = index,
scroll = '2m',
size = 1000,
body = query)
if '_scroll_id' in page:
sid = page['_scroll_id']
scroll_size = page['hits']['total']
data = data + page['hits']['hits']
# Start scrolling
while (scroll_size > 0):
page = es.scroll(scroll_id = sid, scroll = '2m')
# Update the scroll ID
sid = page['_scroll_id']
# Get the number of results that we returned in the last scroll
scroll_size = len(page['hits']['hits'])
data = data + page['hits']['hits']
return data
else:
return data
def collect_monitoring_data(es,vdcname,dates=[]):
all = None
for runDate in dates:
esAll = []
index = "{}-{}".format(vdcname,runDate.date().strftime("%Y-%m-%d"))
print("loading data from index",index)
esAll = __collect(es,index,{"query": {"match_all": {}}})
if len(esAll) <= 0:
continue
esAll = list(map(lambda x:x["_source"],esAll))
responses = filter(lambda x:'response.code' in x,esAll)
requests = filter(lambda x:'response.code' not in x,esAll)
responses = pd.DataFrame(responses)
responses = responses[['request.id','response.code','response.length']]
requests = pd.DataFrame(requests)
requests = requests[['request.id','@timestamp','request.operationID','request.requestTime','request.client','request.method','request.path']]
df = pd.merge(requests, responses, on='request.id')
df['runDate'] = runDate
if (all is None):
all = df
else:
all = pd.concat([all, df], sort=True)
return all