-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.py
More file actions
196 lines (162 loc) · 6.74 KB
/
Copy pathmodel.py
File metadata and controls
196 lines (162 loc) · 6.74 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
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
import threading, math, dateutil.parser
from numpy import genfromtxt
from datetime import datetime, timezone, timedelta
class DataModel():
# each row has 4 columns(timestamp, id, type, status)
dataHead = list()
data = list()
dayList = list()
selector = {}
pre_popular = {}
recentData = {}
date_format = '%Y-%m-%d'
def __init__(self, readRef):
self.processing(readRef)
def setInterval(self, func, seconds):
def funcWrapper():
self.setInterval(func, seconds)
t = threading.Timer(seconds, funcWrapper)
t.start()
return t
def readCsv(self):
temp = genfromtxt('report.csv', delimiter=',', dtype=str)
self.dataHead = temp[0:0]
self.data = temp[1:]
def processingFn(self):
self.readCsv()
self.preReporting()
# Each hour
def processing(self, seconds):
self.processingFn()
return self.setInterval(self.processingFn, seconds)
def calcOccurence(self, targetDayStr, deviceId):
if targetDayStr not in self.pre_popular:
self.pre_popular[targetDayStr] = {}
self.dayList.append(targetDayStr)
if deviceId in self.pre_popular[targetDayStr]:
self.pre_popular[targetDayStr][deviceId] += 1
else:
self.pre_popular[targetDayStr][deviceId] = 1
def addSelector(self, deviceType, conn):
if deviceType not in self.selector['type']:
self.selector['type'].append(deviceType)
if conn not in self.selector['connection']:
self.selector['connection'].append(conn)
def dayDiff(self, datetime1, datetime2):
diff = datetime1 - datetime2
return diff.days
def trimTimeStr(self, payload, dayDiff=0):
targetTime = dateutil.parser.parse(payload) - timedelta(days=dayDiff)
targetDayStr = targetTime.strftime(self.date_format)
return targetDayStr
def strDayDiff(self, dayStr1, dayStr2):
day1 = dateutil.parser.parse(dayStr1)
day2 = dateutil.parser.parse(self.trimTimeStr(dayStr2))
return self.dayDiff(day1, day2)
def eachStatusReport(self, payload):
lastIdx = payload['lastIdx']
timePoint = payload['time']
reqType = payload['type']
reqConn = payload['connection']
recentData = {}
for i in range(lastIdx, 0, -1):
row = self.data[i]
if self.strDayDiff(timePoint, row[0]) <= 30:
dayStr = self.trimTimeStr(row[0])
if row[2] == reqType and row[3] == reqConn:
if dayStr not in recentData:
recentData[dayStr] = {}
recentData[dayStr][row[2]] = 1
elif row[2] not in recentData[dayStr]:
recentData[dayStr][row[2]] = 1
else:
recentData[dayStr][row[2]] += 1
return recentData
def statusReporting(self):
# Assume, last day is previous than now
# last 30 days is from the date of last data
lastIdx = len(self.data) - 1
time = self.trimTimeStr(self.data[lastIdx][0])
recentData = {}
for devType in self.selector['type']:
recentData[devType] = {}
for conn in self.selector['connection']:
payload = {
'time': time,
'lastIdx': lastIdx,
'type': devType,
'connection': conn
}
recentData[devType][conn] = self.eachStatusReport(payload)
self.recentData = recentData
def preReporting(self):
self.selector = {
'type': list(),
'connection': list()
}
self.pre_popular = {}
for row in self.data:
targetDayStr = self.trimTimeStr(row[0])
self.calcOccurence(targetDayStr, row[1])
self.addSelector(row[2], row[3])
self.statusReporting()
def pickTopTen(self, targetDic):
popular = {
'num': list(),
'device': list(),
}
for key, value in targetDic.items():
tempOne = { 'device': False, 'num': False}
tempTwo = {}
if len(popular['num']) < 10:
popular['num'].append(value)
popular['device'].append(key)
else:
for idx, num in enumerate(popular['num']):
# ignore same popularity
if value > num and tempOne['device'] == False:
tempOne['device'] = popular['device'][idx]
tempOne['num'] = popular['num'][idx]
popular['device'][idx] = key
popular['num'][idx] = value
if tempOne['num'] > num:
tempTwo['device'] = popular['device'][idx]
tempTwo['num'] = popular['num'][idx]
popular['device'][idx] = tempOne['device']
popular['num'][idx] = tempOne['num']
tempOne = tempTwo
return popular
def calcPercentage(self, curr, prev):
changePercentage = 'none'
if isinstance(curr, int) and isinstance(prev, int):
changePercentage = math.floor(((curr - prev) / prev) * 100) - 100
return changePercentage
def findAnWeekAgo(self, time, deviceId):
prev = 'none'
if time in self.pre_popular and deviceId in self.pre_popular[time]:
prev = self.pre_popular[time][deviceId]
return prev
def calcChangePercentage(self, payload):
targetDayStr = self.trimTimeStr(payload['time'])
print('targetDayStr:', targetDayStr)
anWeekAgo = self.trimTimeStr(payload['time'], 7)
deviceId = payload['device']
curr = self.pre_popular[targetDayStr][deviceId]
prev = self.findAnWeekAgo(anWeekAgo, deviceId)
return self.calcPercentage(curr, prev)
def getDayInfo(self):
return self.dayList
def getPopularDevices(self, pickedTime):
requestTopTen = self.pickTopTen(self.pre_popular[pickedTime])
requestTopTen['change'] = list()
for device in requestTopTen['device']:
requestTopTen['change'].append(self.calcChangePercentage({ 'time': pickedTime, 'device': device }))
return requestTopTen
def getSelectInfo(self):
return self.selector
def getRecentInfo(self, payload):
# Assume, last day is previous than now
# last 30 days is from the date of last data
reqType = payload['type']
reqConn = payload['connection']
return self.recentData[reqType][reqConn]