-
Notifications
You must be signed in to change notification settings - Fork 0
/
dbmodels.py
87 lines (73 loc) · 2.59 KB
/
dbmodels.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
import pymongo
import datetime
import random
def main():
#populateDB()
return
def getMongoData():
#helper function to keep all of the MongoDB info together
client = pymongo.MongoClient()
db = client['hackDB']
data = db['testData']
return data
def getTableData(interval):
dataDict = dataSlice(interval)
return dataDict
def populateDB():
#Populate a MongoDB with test data- removes all previous data first
data = getMongoData()
data.remove()
#Data Start Date and empty list to be populated
dateStamp = datetime.datetime(2013,10,1)
recordList = []
#Create a list of numRcords records to be added to the test DB
numRecords = 500
for x in range(0,numRecords):
randMins = random.randrange(0,60)
randValue = random.randrange(0,100)
dateStamp = dateStamp + datetime.timedelta(minutes = randMins)
recordList.append({"TimeStamp" : dateStamp, "Value" : randValue})
#Add to Test DB
data.insert(recordList)
return
def dataSlice(interval):
#get data from DB
data = getMongoData()
myCursor = data.find().sort('TimeStamp')
#chose timedelta
if interval == "Min" :
timeBucket = datetime.timedelta(minutes = 1)
elif interval == "Hour" :
timeBucket = datetime.timedelta(hours = 1)
elif interval == "Day" :
timeBucket = datetime.timedelta(days = 1)
elif interval == "Week" :
timeBucket = datetime.timedelta(weeks = 1)
elif interval == "Month" :
timeBucket = datetime.timedelta(weeks = 4)
elif interval == "Year" :
timeBucket = datetime.timedelta(weeks = 52)
#get list of bucket dictionaries populate with begin and end timestamps
lastItem = myCursor[myCursor.count()-1]
firstItem = myCursor[0]
timeDiff = lastItem['TimeStamp']-firstItem['TimeStamp'] #timedelta
tempBucket = firstItem['TimeStamp']
bucketList = []
while tempBucket < lastItem['TimeStamp']:
tempDict = {}
tempDict['BeginTime'] = tempBucket
tempBucket = tempBucket + timeBucket
tempDict['EndTime'] = tempBucket
tempCursor = data.find({"TimeStamp": {"$gte": tempDict['BeginTime'],"$lt": tempDict['EndTime'] }})
value = 0
count = 0
for record in tempCursor:
value = value + record['Value']
count = count + 1
tempDict['Count'] = count
tempDict['SumValue'] = value
tempDict['ColumnLabel'] = interval + ' starting ' + str(tempDict['BeginTime'])
bucketList.append(tempDict)
return bucketList
if __name__ == '__main__' :
main()