-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
532 lines (420 loc) · 20.1 KB
/
app.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
import sys
from datetime import timedelta
from icecream import ic
import json
import traceback
from couchbase.auth import PasswordAuthenticator
from couchbase.cluster import Cluster
from couchbase.options import (ClusterOptions, ClusterTimeoutOptions, QueryOptions)
from couchbase.exceptions import CouchbaseException
from couchbase.exceptions import DocumentNotFoundException
from flask import Flask,render_template,request
class work():
debug = False
cb = None
sgLogName = "sg_debug.log"
cbHost = "127.0.0.1"
cbUser = "Administrator"
cbPass = "fujiofujio"
cbBucketName = "sg-log-reader"
cluster = None
cbScopeName = "_default"
cbCollectionName = "_default"
cbColl = None
logData = None
logfile = None
logFileName = "app.log"
logFilePath = "/logs/"
wsIdList = {}
wasBlipLines = False
oldWsDic = {}
sgDtLineOffset = 0
sgLogTag = "default"
def __init__(self, file):
self.readConfigFile(file)
self.debugIceCream()
self.makeCB()
def __del__(self):
sys.stdout = sys.__stdout__
def readConfigFile(self, configFile):
a = open(configFile, "rb" )
b = json.loads(a.read())
self.sgLogName = b["file-to-parse"]
self.cbHost = b["cb-cluster-host"]
self.cbBucketName = b["cb-bucket-name"]
self.cbUser = b["cb-bucket-user"]
self.cbPass = b["cb-bucket-user-password"]
self.debug = b["debug"]
self.sgLogTag = b["log-name"]
self.logFilePath = b["debug-log-path"]
a.close()
def makeCB(self):
try:
auth = PasswordAuthenticator(self.cbUser, self.cbPass)
self.cluster = Cluster('couchbase://'+self.cbHost, ClusterOptions(auth))
self.cluster.wait_until_ready(timedelta(seconds=5))
self.cb = self.cluster.bucket(self.cbBucketName)
#self.cbColl = self.cb.scope(self.cbScopeName).collection(self.cbCollectionName)
self.cbColl = self.cb.default_collection()
except:
ic("Error: Could not connect to CB Cluster: " , self.cbHost ," as: ",self.cbUser )
exit()
def debugIceCream(self):
if self.debug is True:
ic.enable()
#self.logfile = open(self.logFilePath+self.logFileName, 'w')
sys.stdout = self.logfile
else:
ic.disable()
def cbWsIdGet(self, wsId):
ic(wsId)
try:
return self.cbColl.get(wsId['wsId']).value
except DocumentNotFoundException:
ic("wsId doc not found for: ",wsId)
return {}
except CouchbaseException:
ic(traceback.format_exc())
return {}
def cbSgDbNameListEpoch(self, rangeData):
ic(rangeData)
if 'startDt' not in rangeData or not rangeData["startDt"] or 'endDt' not in rangeData or not rangeData["endDt"]:
return []
q = 'SELECT count(u.`sgDb`) as `sgDbCount` , u.`sgDb` FROM `'+self.cbBucketName+'`.`'+self.cbScopeName +'`.`'+ self.cbCollectionName+'` as u WHERE u.`docType` = "byWsId"'
q = q + ' AND u.`dtFullEpoch` BETWEEN $startDtEpoch AND $endDtEpoch '
q = q + ' AND u.`sgDb` IS NOT MISSING '
q = q + ' AND u.`user` IS NOT MISSING '
q = q + ' AND u.`orphane` = False '
q = q + ' GROUP BY u.`sgDb` '
q = q + ' ORDER BY u.`sgDb` '
data = []
try:
result = self.cluster.query(q, QueryOptions(named_parameters=rangeData))
for row in result.rows():
data.append(row)
return data
except CouchbaseException:
ic(traceback.format_exc())
return []
def cbDtRangeSearchEpoch(self, rangeData):
ic(rangeData)
if rangeData["viewBy"] and rangeData["viewBy"] == "sec":
dtSplit = "1"
elif rangeData["viewBy"] and rangeData["viewBy"] == "min":
dtSplit = "100"
if 'sgDb' not in rangeData or not rangeData["sgDb"]:
return []
q = 'SELECT '
if rangeData["viewBy"] and rangeData["viewBy"] in ["sec","min"]:
q = q + ' floor((u.dtFullEpoch/'+dtSplit+'))*'+dtSplit+'*1000 as `dt`, COUNT(floor((u.dtFullEpoch/'+dtSplit+'))*'+dtSplit+'*1000) as `dtCount` , SUM(u.`dtDiffSec`) as `dtDiffSec`, SUM(u.`cRow`) as `cRow` , SUM(u.`qRow`) as `qRow`, SUM(u.`tRow`) as `tRow`, SUM(u.`conflicts`) as `conflicts`, SUM(u.`errors`) as `errors` , SUM(u.`sentCount`) as `sentCount`, SUM(u.`pushAttCount`) as `pushAttCount`, SUM(u.`pushCount`) as `pushCount`, SUM(u.`pullAttCount`) as `pullAttCount` , SUM(u.`warnings`) as `warnings` '
else:
if rangeData["pie"] and rangeData["pie"] == True:
q = q + ' u.`tRow`, u.`sentCount` , u.`since` ,u.`cRow` , u.`qRow` '
else:
q = q + ' u.`dtFullEpoch`*1000 as `dt`, MILLIS_TO_STR(u.`dtFullEpoch` * 1000, "HH:mm:ss") as `dtClock` , 1 as `dtCount`,u.`user`,meta(u).id as cbKey, u.`dtDiffSec`, u.`cRow`,u.`qRow`,u.`tRow`,u.`conflicts`,u.`errors` , u.`sentCount`, u.`blipC`,u.`since`, u.`pushAttCount`, u.`pushCount`,u.`pullAttCount` ,`warnings` '
q = q + ' FROM `'+self.cbBucketName+'`.`'+self.cbScopeName +'`.`'+ self.cbCollectionName+'` as u WHERE u.`docType` = "byWsId"'
q = q + ' AND u.`dtFullEpoch` BETWEEN $startDtEpoch AND $endDtEpoch '
q = q + ' AND u.`sgDb` = $sgDb '
q = q + ' AND u.`user` IS NOT MISSING '
q = q + ' AND u.`orphane` = False '
if rangeData["noPushes"] and rangeData["noPushes"] == "no":
q = q + ' AND u.`pushCount` = 0 '
if rangeData["noPushes"] and rangeData["noPushes"] == "only":
q = q + ' AND u.`pushCount` > 0 '
if rangeData["syncResult"] and rangeData["syncResult"] == "full":
q = q + ' AND u.`sentCount` = u.`tRow` '
if rangeData["syncResult"] and rangeData["syncResult"] == "nonFull":
q = q + ' AND u.`sentCount` != u.`tRow` AND u.`sentCount` > 0 '
if rangeData["syncResult"] and rangeData["syncResult"] == "no":
q = q + ' AND u.`sentCount` = 0 '
if rangeData["errors"] and rangeData["errors"] == True:
q = q + ' AND u.`errors` > 0 '
if rangeData["conflicts"] and rangeData["conflicts"] == True:
q = q + ' AND u.`conflicts` > 0 '
if rangeData["sinceZero"] and rangeData["sinceZero"] == True:
q = q + ' AND u.`since` = ["0"] '
if rangeData["filterByChannels"] and rangeData["filterByChannels"] == True:
q = q + ' AND ARRAY_LENGTH(u.`filterBy`) > 0 '
if rangeData["attachments"] and rangeData["attachments"] == True:
q = q + ' AND u.`pushAttCount` > 0 '
if rangeData["attachmentsPull"] and rangeData["attachmentsPull"] == True:
q = q + ' AND u.`pullAttCount` > 0 '
if rangeData["syncTime"]:
q = q + ' AND u.`dtDiffSec` >= '+rangeData["syncTime"]+' '
if rangeData["changeCount"] :
q = q + ' AND u.`tRow` >= '+rangeData["changeCount"]+' '
if 'user' not in rangeData or not rangeData["user"]:
q = q + ' AND u.`user` IS NOT MISSING '
else:
q = q + ' AND u.`user` = $user '
if rangeData["viewBy"] and rangeData["viewBy"] in ["sec","min"]:
q = q + ' GROUP BY floor((u.dtFullEpoch/'+dtSplit+'))*'+dtSplit+' '
q = q + ' ORDER BY floor((u.dtFullEpoch/'+dtSplit+'))*'+dtSplit+' ASC '
else:
q = q + ' ORDER BY u.`dtFullEpoch` ASC '
data = []
ic(q)
try:
result = self.cluster.query(q, QueryOptions(named_parameters=rangeData))
for row in result.rows():
data.append(row)
return data
except CouchbaseException:
ic(traceback.format_exc())
return []
def cbStatsRange(self, rangeData, rType):
ic(rangeData)
if 'sgDb' not in rangeData or not rangeData["sgDb"]:
return []
q = 'SELECT COUNT(t.tRange) as tCount, tRange FROM ('
if rType == "cblTime":
q = q + ' SELECT CASE WHEN u.dtDiffSec <=1 then "01. 1 Sec or less" '
q = q + ' WHEN u.dtDiffSec between 2 and 10 then "02. 2 to 10 Sec" '
q = q + ' WHEN u.dtDiffSec between 11 and 30 then "03. 11 to 30 Sec" '
q = q + ' WHEN u.dtDiffSec between 31 and 60 then "04. 31 to 60 Sec" '
q = q + ' WHEN u.dtDiffSec between 61 and 120 then "05. 1 to 2 minutes" '
q = q + ' WHEN u.dtDiffSec between 121 and 300 then "06. 2 to 5 minutes" '
q = q + ' WHEN u.dtDiffSec between 301 and 900 then "07. 5 to 15 minutes" '
q = q + ' WHEN u.dtDiffSec between 901 and 1800 then "08. 15 to 30 minutes" '
q = q + ' WHEN u.dtDiffSec between 1801 and 3600 then "09. 30 to 60 minutes" '
q = q + ' WHEN u.dtDiffSec > 3601 then "10. 60 minutes or more"'
q = q + ' else "11. n/a" '
if rType == "changesSent":
q = q + ' SELECT CASE WHEN u.tRow = 0 then "01. Zero" '
q = q + ' WHEN u.tRow between 1 and 10 then "02. 1 to 10" '
q = q + ' WHEN u.tRow between 11 and 100 then "03. 11 to 100" '
q = q + ' WHEN u.tRow between 101 and 1000 then "04. 101 to 1K" '
q = q + ' WHEN u.tRow between 1001 and 10000 then "05. 1K to 10K" '
q = q + ' WHEN u.tRow between 10001 and 100000 then "06. 10K to 100K" '
q = q + ' WHEN u.tRow between 100001 and 1000000 then "07. 100K to 1M" '
q = q + ' WHEN u.tRow between 1000001 and 10000000 then "08. 1M to 10M" '
q = q + ' WHEN u.tRow > 10000000 then "09. 10M+" '
q = q + ' else "10. n/a" '
q = q + ' END as tRange '
q = q + ' FROM `'+self.cbBucketName+'`.`'+self.cbScopeName +'`.`'+ self.cbCollectionName+'` as u WHERE u.`docType` = "byWsId" '
q = q + ' AND u.`dtFullEpoch` BETWEEN $startDtEpoch AND $endDtEpoch '
q = q + ' AND u.`sgDb` = $sgDb '
q = q + ' AND u.`orphane` = False '
if rangeData["noPushes"] and rangeData["noPushes"] == "no":
q = q + ' AND u.`pushCount` = 0 '
if rangeData["noPushes"] and rangeData["noPushes"] == "only":
q = q + ' AND u.`pushCount` > 0 '
if rangeData["syncResult"] and rangeData["syncResult"] == "full":
q = q + ' AND u.`sentCount` = u.`tRow` '
if rangeData["syncResult"] and rangeData["syncResult"] == "nonFull":
q = q + ' AND u.`sentCount` != u.`tRow` AND u.`sentCount` > 0 '
if rangeData["syncResult"] and rangeData["syncResult"] == "no":
q = q + ' AND u.`sentCount` = 0 '
if rangeData["errors"] and rangeData["errors"] == True:
q = q + ' AND u.`errors` > 0 '
if rangeData["conflicts"] and rangeData["conflicts"] == True:
q = q + ' AND u.`conflicts` > 0 '
if rangeData["sinceZero"] and rangeData["sinceZero"] == True:
q = q + ' AND u.`since` = ["0"] '
if rangeData["filterByChannels"] and rangeData["filterByChannels"] == True:
q = q + ' AND ARRAY_LENGTH(u.`filterBy`) > 0 '
if rangeData["attachments"] and rangeData["attachments"] == True:
q = q + ' AND u.`pushAttCount` > 0 '
if rangeData["attachmentsPull"] and rangeData["attachmentsPull"] == True:
q = q + ' AND u.`pullAttCount` > 0 '
if rangeData["syncTime"]:
q = q + ' AND u.`dtDiffSec` >= TONUMBER('+rangeData["syncTime"]+') '
if rangeData["changeCount"] :
q = q + ' AND u.`tRow` >= TONUMBER('+rangeData["changeCount"]+') '
if 'user' not in rangeData or not rangeData["user"]:
q = q + ' AND u.`user` IS NOT MISSING '
else:
q = q + ' AND u.`user` = $user '
q = q + ' ) as t GROUP BY t.tRange ORDER by t.tRange '
data = []
ic(q)
try:
result = self.cluster.query(q, QueryOptions(named_parameters=rangeData))
for row in result.rows():
data.append(row)
return data
except CouchbaseException:
ic(traceback.format_exc())
return []
def cbUserSearch(self, userName):
try:
return self.cbColl.get(wsId).value
except DocumentNotFoundException:
ic("wsId doc not found for: ", wsId)
return {}
except CouchbaseException:
ic(traceback.format_exc())
return {}
def cbUserListEpoch(self, rangeData):
ic(rangeData)
if 'startDtEpoch' not in rangeData or not rangeData["startDtEpoch"] or 'endDtEpoch' not in rangeData or not rangeData["endDtEpoch"] or 'sgDb' not in rangeData or not rangeData["sgDb"]:
return []
q = 'SELECT count(u.`user`) as `sgUserCount` , u.`user` FROM `'+self.cbBucketName+'`.`'+self.cbScopeName +'`.`'+ self.cbCollectionName+'` as u WHERE u.`docType` = "byWsId"'
q = q + ' AND u.`dtFullEpoch` BETWEEN $startDtEpoch AND $endDtEpoch '
q = q + ' AND u.`sgDb` = $sgDb '
q = q + ' AND u.`user` IS NOT MISSING '
q = q + ' AND u.`orphane` = False '
if rangeData["noPushes"] and rangeData["noPushes"] == "no":
q = q + ' AND u.`pushCount` = 0 '
if rangeData["noPushes"] and rangeData["noPushes"] == "only":
q = q + ' AND u.`pushCount` > 0 '
if rangeData["syncResult"] and rangeData["syncResult"] == "full":
q = q + ' AND u.`sentCount` = u.`tRow` '
if rangeData["syncResult"] and rangeData["syncResult"] == "nonFull":
q = q + ' AND u.`sentCount` != u.`tRow` AND u.`sentCount` > 0 '
if rangeData["syncResult"] and rangeData["syncResult"] == "no":
q = q + ' AND u.`sentCount` = 0 '
if rangeData["errors"] and rangeData["errors"] == True:
q = q + ' AND u.`errors` > 0 '
if rangeData["conflicts"] and rangeData["conflicts"] == True:
q = q + ' AND u.`conflicts` > 0 '
if rangeData["sinceZero"] and rangeData["sinceZero"] == True:
q = q + ' AND u.`since` = ["0"] '
if rangeData["filterByChannels"] and rangeData["filterByChannels"] == True:
q = q + ' AND ARRAY_LENGTH(u.`filterBy`) > 0 '
if rangeData["attachments"] and rangeData["attachments"] == True:
q = q + ' AND u.`pushAttCount` > 0 '
if rangeData["attachmentsPull"] and rangeData["attachmentsPull"] == True:
q = q + ' AND u.`pullAttCount` > 0 '
if rangeData["syncTime"]:
q = q + ' AND u.`dtDiffSec` >= TONUMBER('+rangeData["syncTime"]+') '
if rangeData["changeCount"] :
q = q + ' AND u.`tRow` >= TONUMBER('+rangeData["changeCount"]+') '
q = q + ' GROUP BY u.`user` '
if 'sortBy' not in rangeData or not rangeData["sortBy"] or rangeData["sortBy"] == "name":
q = q + ' ORDER BY u.`user` '
elif rangeData["sortBy"] == "count":
q = q + ' ORDER BY `sgUserCount` DESC '
ic(q)
data = []
try:
result = self.cluster.query(q, QueryOptions(named_parameters=rangeData))
for row in result.rows():
data.append(row)
ic(data)
return data
except CouchbaseException:
ic(traceback.format_exc())
return []
def cbSgDbList(self, rangeDate):
return {}
def cbLastWsId(self):
return {}
def sgErrorsEpoch(self, rangeData):
if rangeData["viewBy"] and rangeData["viewBy"] == "sec":
dtSplit = "1"
elif rangeData["viewBy"] and rangeData["viewBy"] == "min":
dtSplit = "100"
q = 'SELECT '
if rangeData["viewBy"] and rangeData["viewBy"] in ["sec","min"]:
q = q + ' floor((e.`dtFullEpoch`/'+dtSplit+'))*'+dtSplit+'*1000 as `dt`, COUNT(floor((e.`dtFullEpoch`/'+dtSplit+'*1000))*'+dtSplit+') as `dtCount` , SUM(e.`query`) as `query` , SUM(e.`dcp`) as `dcp` , SUM(e.`import`) as `import` , SUM(e.`sgDb`) as `sgDb` , SUM(e.`ws`) as `ws`, SUM(e.`gen`) as `gen` '
else:
q = q + ' e.`dtFullEpoch`*1000 as `dt`, e.`query` , e.`dcp` , e.`import` , e.`ws` , e.`gen`, e.`sgDb` '
q = q + ' FROM `'+self.cbBucketName+'`.`'+self.cbScopeName +'`.`'+ self.cbCollectionName+'` as e WHERE e.`docType` = "sgErrors" '
q = q + ' AND e.`dtFullEpoch` BETWEEN $startDtEpoch AND $endDtEpoch '
if rangeData["viewBy"] and rangeData["viewBy"] in ["sec","min"]:
q = q + ' GROUP BY floor((e.`dtFullEpoch`/'+dtSplit+'))*'+dtSplit+' '
q = q + ' ORDER BY floor((e.`dtFullEpoch`/'+dtSplit+'))*'+dtSplit+' ASC '
else:
q = q + ' ORDER BY e.`dtFullEpoch` ASC '
ic(q)
data = []
try:
result = self.cluster.query(q, QueryOptions(named_parameters=rangeData))
for row in result.rows():
data.append(row)
ic(data)
return data
except CouchbaseException:
ic(traceback.format_exc())
return []
def lastWsEpoch(self):
q = 'SELECT MILLIS_TO_STR(u.`dtFullEpoch`* 1000 , "1111-11-11T11:11:11") as `dt` , MILLIS_TO_STR((u.`dtFullEpoch` - 3600) * 1000 , "1111-11-11T11:11:11") as `dtHrAgo` FROM `'+self.cbBucketName+'`.`'+self.cbScopeName +'`.`'+ self.cbCollectionName+'` as u WHERE u.`docType` = "byWsId"'
q = q + ' AND u.`dtFullEpoch` IS NOT MISSING '
q = q + ' AND u.`sgDb` IS NOT MISSING '
q = q + ' AND u.`user` IS NOT MISSING '
q = q + ' AND u.`dtDiffSec` IS NOT MISSING '
q = q + ' AND u.`orphane` = False '
q = q + ' ORDER BY u.`dtFullEpoch` DESC '
q = q + ' LIMIT 1'
ic(q)
data = []
try:
result = self.cluster.query(q)
for row in result.rows():
data.append(row)
return data
except CouchbaseException:
ic(traceback.format_exc())
return []
app = Flask(__name__)
@app.route('/')
def hello():
return render_template('index.html')
@app.route('/dateRangeEpoch', methods=['POST'])
def dateRangeEpoch():
if request.method == 'POST':
ic(request.json)
a = cb.cbDtRangeSearchEpoch(request.json)
return a
else:
return []
@app.route('/dtDiffSecStatsEpoch', methods=['POST'])
def dateDiffSecEpoch():
if request.method == 'POST':
ic(request.json)
a = cb.cbStatsRange(request.json,"cblTime")
return a
else:
return []
@app.route('/changeRangeSentStats', methods=['POST'])
def changeRange():
if request.method == 'POST':
ic(request.json)
a = cb.cbStatsRange(request.json,"changesSent")
return a
else:
return []
@app.route('/wsId',methods=['POST'])
def getWsId():
if request.method == 'POST':
ic(request.json)
a = cb.cbWsIdGet(request.json)
return a
else:
return []
@app.route('/sgDbListEpoch', methods=['POST'])
def sgDblistEpoch():
if request.method == 'POST':
ic(request.json)
a = cb.cbSgDbNameListEpoch(request.json)
return a
else:
return []
@app.route('/sgUserListEpoch',methods=['POST'])
def sgUserlistEpoch():
if request.method == 'POST':
ic(request.json)
a = cb.cbUserListEpoch(request.json)
return a
else:
return []
@app.route('/dateRangeSgErrorsEpoch',methods=['POST'])
def sgErrorsEpoch():
if request.method == 'POST':
ic(request.json)
a = cb.sgErrorsEpoch(request.json)
return a
else:
return []
@app.route('/lastWsIdEpoch')
def lastWsIdEpoch():
return cb.lastWsEpoch()
if __name__ == "__main__":
if len(sys.argv) > 1:
configFile = sys.argv[1]
else:
print("Error: No config.json file given")
exit()
cb = work(configFile)
app.run(debug = False, host="0.0.0.0", port=8080)