-
Notifications
You must be signed in to change notification settings - Fork 0
/
application.py
370 lines (250 loc) · 9.19 KB
/
application.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
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
from flask import Flask, render_template, request, redirect, url_for, session
import boto
import os
from boto import rds
import MySQLdb
import datetime
import string
import memcache
import time
import random
import csv
import hashlib
application = Flask(__name__)
app = application
@app.route('/')
def hello_word():
return """
<form id ="upload_file" enctype="multipart/form-data" method="post" action="/upload_file">
<input id="fileupload" name="myfile" type="file" />
<input type ="submit" value="submit" name ="submit"></form>
<p> upload to S3</p>
<form id ="upload_db" enctype="multipart/form-data" method="post" action="/upload_db">
<input id="dbupload" name="mydb" type="file" />
<input type ="submit" value="submit" name ="submit"></form>
<p> upload to RDS</p>
"""
@app.route('/upload_db', methods=['POST'])
def upload_db():
file = request.files['mydb']
filecontent=file.read()
filename=file.filename
fo = open(filename, "w")
fo.write(filecontent);
fo.close()
coloumn =[]
with open(filename, 'rb') as f:
data = list(csv.reader(f))
coloumn_first = data[0]
for word in coloumn_first:
if(word == "COUNT" or word == "Count" or word == "count"):
word = "my_count"
if(word == "Dec"):
word ="decem"
coloumn_second = word.replace(" ", "_").replace("?", "").replace("Count","my_count")
coloumn.append(coloumn_second.replace("-", "_"))
print coloumn
str =""
name = "my_table"
table = filename[:-4].replace("-", "_")
print filename
print table
for c in coloumn:
str =str+c+" "+"varchar(255)"+","
print str
str1 = str[:-1]
print str1
sql = "LOAD DATA LOCAL INFILE '"+filename+"' INTO TABLE "+table+" FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' LINES TERMINATED BY '\r\n' IGNORE 1 LINES SET my_number=CEIL(RAND() *6) "
print filename
print table
db = MySQLdb.connect(host="yourhostname.com",
user="user",
passwd="passwd",
db="yourdb")
#print "create table "+table+"(Country varchar(255),station varchar(255),vmo_number int, unit varchar(255), jan float, feb float, mar float, apr float, may, float, jun float, jul float, aug float, sep float, oct float, nov float, dec float,ID int NOT NULL AUTO_INCREMENT,"+str1+",PRIMARY KEY (ID))"
cur = db.cursor()
start_time = time.time()
cur.execute("create table UNPrecip(Country varchar(255),station varchar(255),vmo_number int, unit varchar(255), jan float, feb float, mar float, apr float, may float, jun float, jul float, aug float, sep float, oct float, nov float, decem float,ID int NOT NULL AUTO_INCREMENT,my_number int, PRIMARY KEY (ID))")
db.commit()
cur.execute(sql)
end_time = time.time() - start_time
db.commit()
del_time = time.time()
cur.execute("delete from UNPrecip where (JAN > 10000 or FEB > 10000 or MAR > 10000)")
db.commit()
del_end_time = time.time() - del_time
cur.execute("select COUNT(*) from UNPrecip")
for r in cur.fetchall():
num_count = r
print r
db.close()
os.remove(filename)
# return "this is your table name "+table+" with these coloumns "+str1+"""
return render_template('dbtables.html',table = table, end_time = end_time,del_end_time=del_end_time)
@app.route('/query_normal', methods=['POST'])
def query_normal():
connect_time = datetime.datetime.now()
db = MySQLdb.connect(host="yourhostname.com",
user="user",
passwd="passwd",
db="yourdb")
conn_done = datetime.datetime.now() - connect_time
tab_name = request.form.getlist('norm_table')
#print tab_name[0]
user_table = request.form.getlist('user_table_norm')
u_query = user_table[0]
# table_name = request.form['table']
#cols = request.form['str1']
# print "select MAX(MAR), MAX(JUN) from "+table_name
# cur = db.cursor()
# start_time = time.time()
# cur.execute("select MAX(MAR), MAX(JUN), MIN(MAR), MIN(JUN) from "+table_name+" WHERE ((JUN) > 0) and(MAR) > 0")
# end_time = time.time() - start_time
# db.commit()
# for row in cur.fetchall():
# print row
start_time = datetime.datetime.now()
for i in range(1,10000):
#ran_num = random.randint(1,6)
cur = db.cursor()
#print "select * from "+table_name+" where (ID BETWEEN 1 and 200) and my_number=FLOOR(RAND() *5);"
cur.execute(u_query)
rows = cur.fetchall()
vals = rows
end_time = datetime.datetime.now() - start_time
db.close()
return "write query"+" "+str(end_time)+"connection time"+str(conn_done)
@app.route('/min_max_query', methods=['POST'])
def min_max_query():
qu = request.form['do_min max']
qu1 = request.form['min_max']
return """ <form id ="user_input" enctype="multipart/form-data" method="post" action="/user_input">
<input type ="text" name ="user_text"/>
<input type ="submit" value ="submit query" name = "submit_query"></form>
"""
@app.route('/quest_7', methods=['POST'])
def quest_7():
text_1 = request.form['u_i']
tx = text_1
text_2 = request.form['u_j']
print tx
print text_2
return "success"
@app.route('/user_input', methods=['POST'])
def user_input():
db = MySQLdb.connect(host="yourhostname",
user="user",
passwd="yourdb",
db="passwd")
query_string = request.form['user_text']
val = query_string.split()
print val[1]
if 'max' in query_string:
cur = db.cursor()
start_time1 = time.time()
cur.execute("select COUNT(MAR), COUNT(JUN) from UNPrecip where (MAR <= "+val[1]+") and (JUN <= "+val[1]+") ")
end_time1 = time.time() - start_time1
db.commit()
for row in cur.fetchall():
print row
return 'time taken is'+str(end_time1)
if 'min' in query_string:
cur = db.cursor()
start_time2 = time.time()
cur.execute("select COUNT(MAR), COUNT(JUN) from UNPrecip where Country ='ALEX and JUN > 10")
end_time2 = time.time() - start_time2
db.commit()
for row in cur.fetchall():
print row
return 'time taken is'+str(end_time2)
return 'invalid query'
@app.route('/memecache', methods=['POST'])
def memecache():
db = MySQLdb.connect(host="yourhostname.com",
user="user",
passwd="passwd",
db="yourdb")
mc = memcache.Client(['yourhostname'], debug = 0)
#mc.flush_all()
#print str_query
start_time = datetime.datetime.now()
for i in range(1,200):
#ran_num = random.randint(1,6)
value = mc.get("rak")
print value
if value is None:
print "fethcing from db"
#print u_query
cur = db.cursor()
cur.execute("select * from UNPrecip where Country ='ALEX' and JUN > 10")
rows = cur.fetchall()
b =mc.set("rak",rows[0])
print b
# print "set to cache"
# for row in value:
# print "%s , %s" %(row[0],row[1])
else:
print "cache hit"
end_time = datetime.datetime.now() - start_time
db.commit()
db.close()
# print db_val
return "done"+str(end_time)
@app.route('/c_content', methods=['POST'])
def c_content():
mc = memcache.Client(['yourhostname.com'], debug = 0)
string_query = request.form['view_cache_contents']
ss = hashlib.md5(string_query).hexdigest()
print string_query
value = mc.get("rak")
if value is not None:
print value
else:
print "not in cache"
return "get cache cntent"
@app.route('/flush_cache', methods=['POST'])
def flush_cache():
mc = memcache.Client(['yourhostname.com'], debug = 0)
mc.flush_all()
return "flushed cache "
@app.route('/query_tuples', methods=['POST'])
def query_tuples():
db = MySQLdb.connect(host="yourhostname.com",
user="user",
passwd="passwd",
db="yourdb")
table_name = request.form['table_query']
#cols = request.form['str1']
for i in range(1,100):
cur = db.cursor()
#print "select * from "+table_name+" where ID="+str(i)+";"
start_time = time.time()
cur.execute("select * from "+table_name+" where my_number=FLOOR(RAND() *5);")
end_time = time.time() - start_time
db.commit()
#for row in cur.fetchall():
#print row
db.commit()
db.close()
print end_time
return "write query"+table_name+" "
@app.route('/upload_file', methods=['POST'])
def upload_file():
file = request.files['myfile']
filecontent=file.read()
filename=file.filename
fo = open(filename, "w")
fo.write(filecontent);
s3 = boto.connect_s3("yourkey","your key")
bucket = s3.create_bucket('bucketname')
start_time =time.time()
key = bucket.new_key('allfiles/'+filename)
key.set_contents_from_filename(filename)
end_time = time.time() - start_time
key.set_acl('public-read')
fo.close()
os.remove(filename)
return 'your file ' + ' is uploaded and it took '+str(end_time)
if __name__ == '__main__':
app.run(debug = True)
#app.run(host='0.0.0.0', port=port)