-
Notifications
You must be signed in to change notification settings - Fork 0
/
Metric.py
167 lines (127 loc) · 6.87 KB
/
Metric.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
from pymongo import MongoClient
import FilterExpression
from FilterExpression import FilterExpression
from FilterExpression import OperatorType
class Metric:
def __init__(self, host, port):
self.client = MongoClient(host, port)
self.db = self.client.indexes
def getMetricFactors(self, querytype, filenames, filterIdx, filterTmp):
collectionIdx = self.db.meterdata
collectionTmp = self.db.histogram
colNames = filenames.split(",")
fnames = []
result = 0
metricFactors = []
for i in range(0, len(colNames)):
print colNames[i]
colName = colNames[i].split("/")
fnames.append(colName[len(colName)-1])
if len(filterTmp) == 0:
if len(filterIdx) == 1:
right = filterIdx[0].getRight()
if isNumber(right):
if right.isdigit():
right = int(right)
else:
right = float(right)
left = filterIdx[0].getLeft()
for value in fnames:
resultIdx = collectionIdx.find({"fname": str(value), left: {filterIdx[0].getOperator(): right}}).count()
nRowsOfFile = collectionIdx.find({"fname": value}).count()
result += resultIdx
metricFactors.append(str(querytype) + ";" +str(value) + ";" + str(resultIdx) + ";" + str(nRowsOfFile))
metricFactor = [s + ";"+str(result) for s in metricFactors]
else:
if len(filterIdx) == 2:
right1 = filterIdx[0].getRight()
left1 = filterIdx[0].getLeft()
right2 = filterIdx[1].getRight()
left2 = filterIdx[1].getLeft()
if isNumber(right1):
if right1.isdigit():
right1 = int(right1)
else:
right1 = float(right1)
if isNumber(right2):
if right2.isdigit():
right2 = int(right2)
else:
right2 = float(right2)
for value in fnames:
if left1 == left2:
resultIdx = collectionIdx.find({"fname": value, left1: {filterIdx[0].getOperator(): right1,
filterIdx[1].getOperator(): right2}}).count()
else:
resultIdx = collectionIdx.find({"fname": value, left1: {filterIdx[0].getOperator(): right1},
left2: {filterIdx[1].getOperator(): right2}}).count()
nRowsOfFile = collectionIdx.find({"fname": value}).count()
result += resultIdx
metricFactors.append(str(querytype) + ";" + str(value) + ";" + str(resultIdx) + ";" + str(nRowsOfFile))
print result
metricFactor = [s + ";"+str(result) for s in metricFactors]
else:
if len(filterIdx) == 1:
right = filterIdx[0].getRight()
if isNumber(right):
if right.isdigit():
right = int(right)
else:
right = float(right)
left = filterIdx[0].getLeft()
startDate = filterTmp[0].getRight()
endDate = filterTmp[1].getRight()
for value in fnames:
resultIdx = collectionIdx.find({"fname": str(value), left: {filterIdx[0].getOperator(): right}}).count()
resSDate = collectionTmp.find({"hist.date": startDate, "name": value}, {"hist": {
"$elemMatch": {"date": {filterTmp[0].getOperator(): startDate}}}})
resEDate = collectionTmp.find({"hist.date": endDate, "name": value}, {"hist": {
"$elemMatch": {"date": {filterTmp[1].getOperator(): endDate}}}})
resultTmp = abs((resSDate[0]['hist'])[0]['cumulation'] - (resEDate[0]['hist'])[0]['cumulation'])
nRowsOfFile = collectionIdx.find({"fname": value}).count()
nRows = resultIdx if resultIdx < resultTmp else resultTmp
result += nRows
metricFactors.append(str(querytype) + ";" +str(value) + ";" + str(nRows) + ";" + str(nRowsOfFile))
metricFactor = [s + ";"+str(result) for s in metricFactors]
else:
if len(filterIdx) == 2:
right1 = filterIdx[0].getRight()
left1 = filterIdx[0].getLeft()
right2 = filterIdx[1].getRight()
left2 = filterIdx[1].getLeft()
if isNumber(right1):
if right1.isdigit():
right1 = int(right1)
else:
right1 = float(right1)
if isNumber(right2):
if right2.isdigit():
right2 = int(right2)
else:
right2 = float(right2)
startDate = filterTmp[0].getRight()
endDate = filterTmp[1].getRight()
for value in fnames:
if left1 == left2:
resultIdx = collectionIdx.find({"fname": value, left1: {filterIdx[0].getOperator(): right1,
filterIdx[1].getOperator(): right2}}).count()
else:
resultIdx = collectionIdx.find({"fname": value, left1: {filterIdx[0].getOperator(): right1},
left2: {filterIdx[1].getOperator(): right2}}).count()
resSDate = collectionTmp.find({"hist.date": startDate, "name": value}, {"hist": {
"$elemMatch": {"date": {filterTmp[0].getOperator(): startDate}}}})
resEDate = collectionTmp.find({"hist.date": endDate, "name": value}, {"hist": {
"$elemMatch": {"date": {filterTmp[1].getOperator(): endDate}}}})
resultTmp = abs((resSDate[0]['hist'])[0]['cumulation'] - (resEDate[0]['hist'])[0]['cumulation'])
nRowsOfFile = collectionIdx.find({"fname": value}).count()
nRows = resultIdx if resultIdx < resultTmp else resultTmp
result += nRows
metricFactors.append(str(querytype) + ";" +str(value) + ";" + str(nRows) + ";" + str(nRowsOfFile))
metricFactor = [s + ";"+str(result) for s in metricFactors]
return metricFactor
def isNumber(value):
try:
float(value)
return True
except ValueError:
return False