-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquery.py
More file actions
189 lines (144 loc) · 5.28 KB
/
Copy pathquery.py
File metadata and controls
189 lines (144 loc) · 5.28 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
import sys
import redis
import time
import datetime
import getopt
def main():
try:
opts, args = getopt.getopt(sys.argv[1:],'i', ['WipeMe=', 'ApprovalDate=', 'IssueDate=', 'Title=', 'Description='])
wipeMe = ""
approvalDate = ""
issueDate = ""
title = ""
description = ""
for o, a in opts:
if o == '--ApprovalDate':
approvalDate = a
if o == '--IssueDate':
issueDate = a
if o == '--Title':
title = a
if o == '--Description':
description = a
if o == '--WipeMe':
wipeMe = True
except getopt.GetoptError:
print 'query.py --ApprovalDate=*-19700101 --title="computer writing" --description="art tool"'
sys.exit(2)
#Connect to redis server
rServer = redis.StrictRedis(host='localhost', port=6379, db=0)#NOTE: Using DB 0
finalResult = None
if len(title) > 0:
finalResult = queryByTitle(rServer, title)
if len(approvalDate) > 0:
if finalResult == None:
finalResult = queryByApprovalDate(rServer, approvalDate)
elif len(finalResult) > 0:
approvalDateMatches = queryByApprovalDate(rServer, approvalDate)
if approvalDateMatches is not None:
finalResult = finalResult & approvalDateMatches
if len(issueDate) > 0:
if finalResult == None:
finalResult = finalResult & queryByIssueDate(rServer, issueDate)
elif len(finalResult) > 0:
issueDateMatches = queryByIssueDate(rServer,issueDate)
if issueDateMatches is not None:
finalResult = finalResult & issueDateMatches
if len(description) > 0:
if finalResult == None:
finalResult = queryByDescription(rServer, description)
elif len(finalResult) > 0:
descriptionMatches = queryByDescription(rServer, description)
if descriptionMatches is not None:
finalResult = finalResult & descriptionMatches
if wipeMe == True:
wipeRedisDB(rServer)
else:
printQueryResults(rServer, finalResult)
def printQueryResults(rServer, results):
sys.stderr.write("Results: " + str(len(results)) + '\n')
for result in results:
print "-------------"
print "Key:" , result
print "Title:", str(rServer.hmget(result, ["Title"]))[2:-2]
descriptionWordArray = str(rServer.hmget(result, ["Description"])).split(' ')
descriptionText = ""
for word in descriptionWordArray[:100]:
descriptionText = descriptionText + word + ' '
if len(descriptionWordArray) > 100:
descriptionText = descriptionText + "..."
print "Description:" , descriptionText[2:-3]
print "Approved:" , str(convertTimestampToDate(rServer.hget(result, 'ApprovalDate')))
print "Issued:" , str(convertTimestampToDate(rServer.hget(result, 'IssueDate')))
print "-------------"
#Confirmed, functions as expected
def queryByApprovalDate(rServer, query):
if ( query.split('-')[0] == '*' ):
minVal = 0
else:
minVal = convertDateToTimestamp(query.split('-')[0])
if ( query.split('-')[1] == '*' ):
maxVal = time.time()
else:
maxVal = convertDateToTimestamp(query.split('-')[1])
matches = rServer.zrangebyscore('ApprovalDate', minVal, maxVal)
return set(matches)
for match in matches:
print convertTimestampToDate(rServer.hget(match, 'ApprovalDate')) + "," + rServer.hget(match, 'Title')
#Confirmed, functions as expected
def queryByIssueDate(rServer, query):
if ( query.split('-')[0] == '*' ):
minVal = 0
else:
minVal = convertDateToTimestamp(query.split('-')[0])
if ( query.split('-')[1] == '*' ):
maxVal = time.time()
else:
maxVal = convertDateToTimestamp(query.split('-')[1])
matches = rServer.zrangebyscore('IssueDate', minVal, maxVal)
return set(matches)
#Confirmed, functions as expected
def queryByTitle(rServer, query):
keys = set()
query = query.replace('"', '').split(" ")
for title in rServer.sscan_iter('Title'):
numberOfQueryWordMatches = 0
#Case insensitive title string matching. Niave implementation
for q in query:
for t in title.split(" "):
if q.lower() == t.lower():
numberOfQueryWordMatches = numberOfQueryWordMatches + 1
if numberOfQueryWordMatches == len(query):
keys.add(title.split(':')[1])
return keys
#Confirmed, functions as expected
def queryByDescription(rServer, query):
keys = set()
query = query.replace('"', '').split(" ")
for description in rServer.sscan_iter('Description'):
numberOfQueryWordMatches = 0
#Case insensitive description string matching. Niave implementation
for q in query:
for t in description.split(" "):
if q.lower() == t.lower():
numberOfQueryWordMatches = numberOfQueryWordMatches + 1
if numberOfQueryWordMatches == len(query):
keys.add(description.split(':')[1])
return keys
#Confirmed, functions as expected
def wipeRedisDB(rServer):
print "Deleting all data in database"
rServer.flushall()
#Confirmed, functions as expected
def convertDateToTimestamp(date):
year = date[:4].strip()
month = date[4:6].strip()
day = date[6:8].strip()
timeString = day + "/" + month + "/" + year
total = time.mktime(datetime.datetime.strptime(timeString, "%d/%m/%Y").timetuple())
return total
def convertTimestampToDate(timestamp):
if type(timestamp) is float or type(timestamp) is str:
return datetime.datetime.fromtimestamp(float(timestamp)).strftime('%Y-%m-%d %H:%M:%S')
if __name__ == '__main__':
main()