-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueryFunctions.py
More file actions
179 lines (157 loc) · 6.96 KB
/
Copy pathQueryFunctions.py
File metadata and controls
179 lines (157 loc) · 6.96 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
import heapq
import math
from collections import defaultdict
__author__ = 'nishantmehta.n'
import threading2
from threading2 import *
from time import time
from time import sleep
#all the query functions will be written here
class queries():
def query1(self,arg):
print("new query request")
for i in arg.stockHash['goog']:
print(i)
def query2(self,coName,dataStructure,n,result,query1Cache):
# coName : name of the company
# obj : obj thread passed
# n : TOP n stocks variable
result.data = ''
# try to acquire the query cache lock
query1Cache.cv.acquire()
# print "I got access to cache"
try:
# check the query cache if result already exists
CurrTime = time()
if (math.fabs(CurrTime - query1Cache.time) < 10):
result.data += query1Cache.qresult.data
# print "I took my result from cache"
else:
#print "Top n max and min"+'\n'
#a min heap queue to hold max 10 values
maxHeap = []
# a min heap queue to hold min 10 values
minHeap = []
# Min Max algorithm
#--acquire the lock in shared mode so that
#--concurrent threads can acquire it simultaneously
dataStructure.lock.acquire(shared = True )
try:
# print "inside lock..l"
stockList = dataStructure.stockHash[coName]
# print "length of ds is " + str(len(stockList)) + "\n"
j = 0
for i in stockList:
temp =float(i.price)
tempn=-temp
if j < n:
heapq.heappush(maxHeap,temp)
heapq.heappush(minHeap,tempn)
j+= 1
else:
if maxHeap[0] < temp:
heapq.heapreplace(maxHeap,temp)
if minHeap[0] < tempn:
heapq.heapreplace(minHeap,tempn)
finally:
sleep(8)
dataStructure.lock.release()
# End of read , return the result back to calling function
#print the max 10 values
result.data += 'Max Values' + '<br>'
for j in maxHeap:
# print str(j) + '\n'
result.data += str(j) + '<br>'
#print the min 10 values
result.data += 'Min Values' + '<br>'
# print "length " + str(len(minHeap))
for k in minHeap:
result.data += str(-k) + '<br>'
#load the latest result in the cache and notify all
query1Cache.qresult.data = "@@Cached ::"
query1Cache.qresult.data += result.data
query1Cache.time = time()
#query1Cache.cv.notifyAll()
finally:
query1Cache.cv.release()
#@do_profile(follow=[])
def query3(self, dataStructure,result,query2Cache):
result.data=""
# try to acquire the query cache lock
query2Cache.cv.acquire()
# print "I got access to cache"
try:
# check the query cache if result already exists
CurrTime = time()
if (math.fabs(CurrTime - query2Cache.time) < 10):
result.data += query2Cache.qresult.data
# print "I took my result from cache"
else:
dataStructure.lock.acquire(shared = True )
try:
for company in dataStructure.stockHash.iterkeys():
#print company
arrayOfDiff=[]
sizeOfStruct=len(dataStructure.stockHash[company])
#print sizeOfStruct
for i in range (0,sizeOfStruct-1):
arrayOfDiff.append(float(dataStructure.stockHash[company][i+1].price)- float(dataStructure.stockHash[company][i].price))
maxDiff=arrayOfDiff[0]
#print arrayOfDiff
for j in range (1,sizeOfStruct-1):
if (arrayOfDiff[j-1]>0):
arrayOfDiff[j]+=arrayOfDiff[j-1]
if(maxDiff<arrayOfDiff[j]):
maxDiff=arrayOfDiff[j]
result.data += "max profit for " + company + " could have been " + str(maxDiff) + "<br>"
finally:
sleep(8)
dataStructure.lock.release( )
#load the latest result in the cache and notify all
query2Cache.qresult.data = "@@Cached ::"
query2Cache.qresult.data += result.data
query2Cache.time = time()
#query1Cache.cv.notifyAll()
finally:
query2Cache.cv.release()
def query4(self,dataStructure,result,query3Cache):
result.data=""
min=1000**2
# try to acquire the query cache lock
query3Cache.cv.acquire()
# print "I got access to cache"
try:
# check the query cache if result already exists
CurrTime = time()
if (math.fabs(CurrTime - query3Cache.time) < 10):
result.data += query3Cache.qresult.data
# print "I took my result from cache"
else:
#perform the analysis
dataStructure.lock.acquire(shared = True )
try:
for company in dataStructure.stockHash.iterkeys():
sizeOfStruct=len(dataStructure.stockHash[company])
data=[]
s=0
ss=0
for i in range (0,sizeOfStruct-1):
s = s + float(dataStructure.stockHash[company][i].price)
ss= ss + float(dataStructure.stockHash[company][i].price)**2
#data.append(dataStructure.stockHash[company][i].price)
# mean=s/sizeOfStruct
svar=ss-((s**2)/sizeOfStruct)
var= svar /(sizeOfStruct-1)
if(min<var):
min=str(var)+" Company Name: "+ company
result.data = "Most stable value" + min
finally:
sleep(8)
dataStructure.lock.release( )
#load the latest result in the cache and notify all
query3Cache.qresult.data = "@@Cached ::"
query3Cache.qresult.data += result.data
query3Cache.time = time()
#query1Cache.cv.notifyAll()
finally:
query3Cache.cv.release()