-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmk_json_query.py
212 lines (199 loc) · 4.17 KB
/
mk_json_query.py
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#python mk_json_quert.py & excute it in backgroud
# excute ES query and export it to apache directory for data visulization
import datetime
from elasticsearch import Elasticsearch
from elasticsearch import helpers
import os.path
import time
import json
#JSON export for further data visulization
FILE_PATH_JSON_HITINFO = "/var/www/cache_info1.json"
FILE_PATH_JSON_TOP_REQUEST_URL = "/var/www/toprequestURL.json"
FILE_PATH_JSON_TOP_MISS_URL = "/var/www/topchachemissURL.json"
FILE_PATH_JSON_SYSINFO = "/var/www/sysinfo.json"
#ES information
ES_HOST = {
"host" : "10.0.0.158",
"port" : 9200
}
INDEX_ATS = 'ats'
INDEX_ATS_SYS = 'ats_sysinfo'
POLL_INTERVAL = 5 #5 seconds
mBody_hitInfo = {
"query": {
"filtered": {
"query": {
"match_all": {}
},
"filter": {
"range": {
"accessTime": {
"gte": "now-1d/d"
}
}
}
}
},
"size": 0,
"aggregations": {
"accessTime_1h": {
"date_histogram": {
"field": "accessTime",
"interval": "1h",
"order": {
"_key": "desc"
},
"min_doc_count": 0
},
"aggs": {
"hit_ratio": {
"avg": {
"field": "cacheCode"
}
},
"size_access_info": {
"terms": {
"field": "cacheCode",
"size": 2
},
"aggs": {
"sum_cache_size": {
"sum": {
"field": "contentLength"
}
}
}
}
}
},
"accessTime_5m": {
"date_histogram": {
"field": "accessTime",
"interval": "5m",
"order": {
"_key": "desc"
},
"min_doc_count": 0
},
"aggs": {
"hit_ratio": {
"avg": {
"field": "cacheCode"
}
},
"size_access_info": {
"terms": {
"field": "cacheCode",
"size": 2
},
"aggs": {
"sum_cache_size": {
"sum": {
"field": "contentLength"
}
}
}
}
}
}
}
}
mBody_topRequestURLInfo = {
"size": 0,
"aggs": {
"top_request_URL": {
"terms": {
"field": "requestURL",
"size": 10,
"order": {
"_count": "desc"
}
},
"aggs": {
"spentTime_stats": {
"stats": {
"field": "spentTime"
}
},
"cache_ratio": {
"avg": {
"field": "cacheCode"
}
},
"contentSize": {
"avg": {
"field": "contentLength"
}
}
}
}
}
}
mBody_topCacheMissURLInfo = {
"query": {
"filtered": {
"query": {
"match_all": {}
},
"filter": {
"term": {
"cacheCode": 0
}
}
}
},
"size": 0,
"aggs": {
"top_request_URL": {
"terms": {
"field": "requestURL",
"size": 10,
"order": {
"_count": "desc"
}
},
"aggs": {
"spentTime_stats": {
"stats": {
"field": "spentTime"
}
},
"contentSize": {
"avg": {
"field": "contentLength"
}
}
}
}
}
}
mBody_sysInfo ={
"query": {
"match_all": {}
},
"size": 1,
"sort": [
{
"timeStamp": {
"order": "desc"
}
}
]
}
# create ES client, create index
es = Elasticsearch(hosts = [ES_HOST])
def exportInfo(index_for_query,mBody,filePath):
#print (mBody)
res = es.search(index = index_for_query, size=1, body = mBody)
#print(res)
f = open(filePath,'w') #clear the contents
f.write(json.dumps(res))
f.close()
#Main function
if __name__ == '__main__':
while (True):
exportInfo(INDEX_ATS,mBody_hitInfo,FILE_PATH_JSON_HITINFO)
exportInfo(INDEX_ATS,mBody_topRequestURLInfo,FILE_PATH_JSON_TOP_REQUEST_URL)
exportInfo(INDEX_ATS,mBody_topCacheMissURLInfo,FILE_PATH_JSON_TOP_MISS_URL)
exportInfo(INDEX_ATS_SYS,mBody_sysInfo,FILE_PATH_JSON_SYSINFO)
time.sleep(POLL_INTERVAL)