forked from pkuzcxy/DSAB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
application.py
525 lines (471 loc) · 18.1 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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
from flask import Flask,render_template,request,jsonify,redirect,url_for
from werkzeug.utils import secure_filename
from subprocess import run,Popen
import os
import json
app = Flask(__name__) #创建一个wsgi应用
#to store sketches choosen by users
sketchList = []
writeList = []
task="frequency"
metrics = ""
numOfXinterval =0
numOfLines=0
fileList = []
@app.route('/') #添加路由:根
def index():
return render_template('homepage.html')
@app.route('/dataset') #add route dataset
def dataset():
return render_template('dataset.html')
@app.route('/existing',methods=['POST','GET'])
def datasetDisplay():
filepath = 'existingDataset.json'
with open(filepath, 'r') as f:
json_info = json.load(f)
return jsonify(json_info)
@app.route('/uploadNewSketch',methods=['POST','GET'])
def upload():
if request.method == 'POST':
f = request.files['file']
basepath = os.path.dirname(__file__) # 当前文件所在路径
upload_path = os.path.join(basepath, 'static/uploads',secure_filename(f.filename)) #注意:没有的文件夹一定要先创建,不然会提示没有该路径
f.save(upload_path)
return redirect(url_for('upload'))
return render_template('uploadNewSketch.html')
@app.route('/choosenSketch',methods=['POST','GET'])
def choosenSketch():
'''
user has finished choosing sketches to be tested
'''
clearConfig('sketch')
global sketchList
sketchList=request.form.getlist('sketch')
tasks = ['freq', 'topk']
for sketch in sketchList:
filepath = './config/' + sketch +'.txt'
with open(filepath,'w') as f: #clear old content
f.write('sketch='+ sketch +'\n')
writeConfig('sketch='+ sketch +'\n')
for task in tasks: #add supported task
for sketch in request.form.getlist(task):
filepath = './config/' + sketch +'.txt'
with open(filepath,'a+') as f:
f.write('task=' + task + '\n')
return render_template('choosenSketch.html')
@app.route('/choosenDataset',methods=['POST','GET'])
def choosenDataset():
'''
user has finished choosing dataset to be tested
'''
clearConfig('dataset')
if request.method == 'POST':
datasetList = request.form.getlist('dataset')
for i in datasetList:
writeConfig('dataset='+i)
dataset_path = './dataset_temp'
if not os.path.exists(dataset_path):
os.makedirs(dataset_path)
os.makedirs(dataset_path + '/zipf')
for d in datasetList:
dataset_file = dataset_path + '/' + d
if not os.path.exists(dataset_file):
cloud_download(dataset_file, d)
return redirect('/')
@app.route('/distribution', methods=['POST', 'GET'])
def distributionDisplay():
if request.method == 'POST':
filepath = 'existingDistribution.json'
with open(filepath, 'r') as f:
json_info = json.load(f)
return jsonify(json_info)
else:
return render_template('distribution.html')
@app.route('/choosenDistribution',methods=['POST','GET'])
def choosenDistribution():
'''
user has finished choosing distribution to be generated
'''
global distributionList
distributionList=request.form.getlist('distribution')
return render_template('choosenDistribution.html')
@app.route('/distributionParaShow',methods=['POST','GET'])
def distributionParaShow():
'''
user has finished choosing distributions to be generated
now they need to set parameters
'''
if request.method == 'POST':
filepath = 'existingDistribution.json'
with open(filepath, 'r') as f:
json_info = json.load(f)
upload_json_info = {}
upload_json_info['distributionArray'] = []
for i in range(len(json_info['distributionArray'])):
if json_info['distributionArray'][i]['name'] in distributionList:
upload_json_info['distributionArray'].append(json_info['distributionArray'][i])
return jsonify(upload_json_info)
@app.route('/setDistributionPara',methods=['POST','GET'])
def setDistributionPara():
'''
distribution parameters settings are returned
datasets are generated
'''
import json
global distributionList
if request.method == 'POST':
filepath = 'existingDistribution.json'
with open(filepath, 'r') as f:
json_info = json.load(f)
for i in range(len(distributionList)):
for j in range(len(json_info['distributionArray'])):
distributionName = json_info['distributionArray'][j]['name']
if distributionList[i] == distributionName:
writeConfig('dataset='+ distributionName)
break
para = request.form.get("parameter")
total = request.form.get("total")
dist = request.form.get("distinct")
writeConfig('_' + para)
writeConfig('_' + total)
writeConfig('_' + dist)
#生成对应的数据集
minFreq, maxFreq = generate_dataset(distributionName, float(para), int(total), int(dist))
with open('existingDataset.json', 'r') as f:
json_info = json.load(f)
json_info['datasetArray'].append({"name": distributionName, "totalNum": total,
"distinctNum": dist, "minFrequency": str(minFreq),
"maxFrequency": str(maxFreq), "bytePerItem": '4'})
with open('existingDataset.json', 'w') as f:
json.dump(json_info, f)
return redirect('/')
@app.route('/sketchParaShow',methods=['POST','GET'])
def sketchParaShow():
'''
user has finished choosing sketches to be tested
now they need to set parameters
'''
if request.method == 'POST':
filepath = 'existingSketch.json'
with open(filepath, 'r') as f:
json_info = json.load(f)
upload_json_info = {}
upload_json_info['sketchArray'] = []
for i in range(len(json_info['sketchArray'])):
if json_info['sketchArray'][i]['name'] in sketchList:
upload_json_info['sketchArray'].append(json_info['sketchArray'][i])
return jsonify(upload_json_info)
@app.route('/setSketchPara',methods=['POST','GET'])
def setSketchPara():
'''
sketch parameters settings are returned
:return:
'''
paraDict = {}
global sketchList
global writeList
global fileList
if request.method == 'POST':
filepath = 'existingSketch.json'
with open(filepath, 'r') as f:
json_info = json.load(f)
for i in range(len(sketchList)):
sketchName = json_info['sketchArray'][i]['name']
paraList =[]
for p in json_info['sketchArray'][i]['parameterList']:
low = sketchName+'_'+p+'_lowerbound'
high = sketchName+'_'+p+'_upperbound'
interval = sketchName+'_'+p+'_interval'
tmpList = []
lowValue = float(request.form.get(low))
highValue = float(request.form.get(high))
intervalValue = float(request.form.get(interval))
while(lowValue<=highValue):
tmpList.append(p+'='+str(lowValue))
lowValue += intervalValue
paraList.append(tmpList)
writeList = []
writeSketchConfig(paraList,0,'')
with open('./config/'+sketchName+'.txt','a+') as f:
for i in writeList:
f.write(i+'\n')
filename ="accurate_"+sketchName
tmp = i.split()
for j in tmp:
filename += "+"+j
filename += '.txt'
fileList.append(filename)
#os.system('./AAA.out')
Popen('./AAA.out > ./result/process.txt',shell =True)
return redirect('/wait')
@app.route('/wait',methods=['POST','GET'])
def wait():
global writeList
if request.method == 'POST':
filepath = './result/process.txt'
with open(filepath,'r') as f:
lines = f.readlines()
json_info ={}
json_info['process'] = int(float(len(lines))/len(writeList) * 100)
return jsonify(json_info)
else:
return render_template('wait.html')
@app.route('/sketch',methods=['POST','GET'])
def sketchDisplay():
if request.method == 'POST':
filepath = 'existingSketch.json'
with open(filepath, 'r') as f:
json_info = json.load(f)
return jsonify(json_info)
else:
return render_template('sketch.html')
@app.route('/graph') #add route dataset
def drawingGraph():
return render_template('graph.html')
@app.route('/graphSetting',methods=['POST','GET']) #add route dataset
def graphSetting():
if request.method == 'POST':
json_result = {}
filepath = 'existingMetrics.json'
with open(filepath, 'r') as f:
json_info = json.load(f)
json_result['metrics'] = json_info[task]
print(json_result['metrics'])
return jsonify(json_result)
@app.route('/selectPoint',methods=['POST','GET'])
def selectPoint():
global metrics,numOfXinterval,numOfLines
if request.method == 'POST':
metrics = request.form.get('metrics')
numOfXinterval = int(request.form.get('xscale'))
numOfLines = int(request.form.get('lines'))
return render_template('choosenPoint.html')
@app.route('/choosingDrawPoint',methods=['POST','GET'])
def offerDrawPoint():
global numOfXinterval,numOfLines,fileList
if request.method == 'POST':
json_info ={}
json_info['numOfXinterval'] =numOfXinterval
json_info['numOfLines'] = numOfLines
json_info['fileList'] = fileList
return jsonify(json_info)
@app.route('/draw',methods=['POST','GET'])
def draw():
'''
{
"type": "freq",
"xs": [1, 2, 3],
"lines": [ ["frequency_asketch_test1.dat_hashnum4_bitPerCounter16_counterPerArray65344.txt", "frequency_asketch_test2.dat_hashnum4_bitPerCounter16_counterPerArray65344.txt", "frequency_asketch_test3.dat_hashnum4_bitPerCounter16_counterPerArray65344.txt"],
["frequency_cmmsketch_test1.dat_hashnum4_bitPerCounter16_counterPerArray65536.txt", "frequency_cmmsketch_test2.dat_hashnum4_bitPerCounter16_counterPerArray65536.txt", "frequency_cmmsketch_test3.dat_hashnum4_bitPerCounter16_counterPerArray65536.txt"]
],
"output": "test.pdf"
}
:return:
'''
'''
:return:
'''
global numOfXinterval,numOfLines
if request.method == 'POST':
json_info = {}
json_info['type'] = 'freq'
json_info['xs'] =[ i+1 for i in range(numOfXinterval)]
tmpList = []
json_info['lines'] = [] #init lines
j = 0
for i in range(numOfXinterval*numOfLines):
tmpList.append('./result/'+request.form.get(str(i)))
j += 1
if j == numOfXinterval:
json_info['lines'].append(tmpList)
tmpList = []
j = 0
json_info['output'] = 'zcxy.pdf'
print(json_info)
json_point_path = './result/json_freq.txt'
with open(json_point_path,'w') as f:
json.dump(json_info,f)
p = Popen('python3 ./result/res_simple_analyzer.py ./result/json_freq.txt',shell=True)
p.wait() #wait for drawing to finish
return redirect('/graphShow')
@app.route('/graphShow',methods=['POST','GET'])
def graphShow():
return render_template('graphShow.html')
def writeSketchConfig(paraList,idx,paraStr):
global writeList
if(idx==len(paraList)-1):
for i in paraList[idx]:
writeList.append(paraStr+i)
else:
for i in paraList[idx]:
writeSketchConfig(paraList,idx+1,paraStr+i+' ')
def writeConfig(s):
configFilePath = './config/config.txt'
with open(configFilePath,'a+') as f:
f.write(s+'\n')
def clearConfig(s): #filter out configs starting with s
configFilePath = './config/config.txt'
with open(configFilePath, 'r') as f:
lines = [line.strip() for line in f if not line.startswith(s)]
lines = [line + '\n' for line in lines if len(line) > 1] #remove empty lines
with open(configFilePath, 'w') as f:
f.writelines(lines)
def cloud_download(local_path, cloud_path):
from qcloud_cos import CosConfig
from qcloud_cos import CosS3Client
import sys
import logging
#设置用户配置
logging.basicConfig(level=logging.INFO, stream=sys.stdout)
secret_id = 'AKIDTzTsGYhBOYceduNNXbfeXL3No494wOwR'
secret_key = 'Xv8qNl5JSeqw2660rdEiCYk4LSOEwT5d'
region = 'ap-beijing'
token = ''
config = CosConfig(Secret_id=secret_id, Secret_key=secret_key, Region=region, Token=token)
#获取客户端对象
client = CosS3Client(config)
#文件下载
response = client.get_object(
Bucket = 'test01-1257045059',
Key = cloud_path,
)
response['Body'].get_stream_to_file(local_path)
def generate_dataset(distriName, para, tot, dis):
import sys
import random
from scipy.stats import powerlaw
import numpy as np
import math
bytePerStr = 4
filepath = "./dataset_temp/" + distriName + '_'+str(para) + '_'+str(tot) + '_'+str(dis) + ".dat"
filenum = 1000
filesize = tot // filenum
if (os.path.exists("./dataset_temp") == False):
os.mkdir("./dataset_temp")
os.mkdir("./dataset_temp/temp")
def powerlaw(N, s):
res = []
base = 0.0
for n in range(1, N + 1):
t = 1 / (n ** s)
base += t
res.append(t)
return [r / base for r in res]
def weibull(N, p, k):
res = []
for n in range(0, N, 1):
power1 = n ** k
p1 = (1 - p) ** power1
power2 = (n + 1) ** k
p2 = (1 - p) ** power2
res.append(p1 - p2)
return res
def gen_random_strings(len, byts):
strs = set()
res = []
for i in range(len):
s = os.urandom(byts)
while s in strs:
s = os.urandom(byts)
res.append(s)
strs.add(s)
return res
def gen(freqs, byts):
strs = gen_random_strings(len(freqs), byts)
chs = [i for i in range(len(freqs))]
for fileno in range(0, filenum - 1):
temp_filepath = "./dataset_temp/temp/" + str(fileno) + ".dat"
with open(temp_filepath, "ab") as f:
for j in range(0, filesize):
p = random.randint(0, len(chs) - 1)
pos = chs[p]
f.write(strs[pos])
if(freqs[pos] > 0):
freqs[pos] -= 1
if freqs[pos] == 0:
del chs[p]
f.close()
last_filesize = 0
fileno = filenum - 1
temp_filepath = "./dataset_temp/temp/" + str(fileno) + ".dat"
with open(temp_filepath, "ab") as f:
while len(chs) != 0:
p = random.randint(0, len(chs) - 1)
pos = chs[p]
f.write(strs[pos])
last_filesize += 1
if(freqs[pos] > 0):
freqs[pos] -= 1
if freqs[pos] == 0:
del chs[p]
def read_str(fp, sl, bytesNum):
st = fp.read(bytesNum)
while st:
sl.append(st)
st = fp.read(bytesNum)
def read_shuffle_write(sl, fp1, fp2, bytesNum):
with open(fp1, "rb") as f1:
read_str(f1, sl, bytesNum)
f1.close()
with open(fp2, "rb") as f2:
read_str(f2, sl, bytesNum)
f2.close()
random.shuffle(sl)
with open(fp1, "wb") as f1:
for j in range(filesize):
f1.write(sl[j])
f1.close()
with open(fp2, "wb") as f2:
for j in range(filesize, len(sl)):
f2.write(sl[j])
f2.close()
def front_tail_shuffle(bytesNum):
frontfile = [i for i in range(100, 200)]
tailfile = [i for i in range(900, 1000)]
random.shuffle(frontfile)
random.shuffle(tailfile)
for i in range(0, 100):
str_list = []
frontfilepath = "./dataset_temp/temp/" + str(frontfile[i]) + ".dat"
tailfilepath = "./dataset_temp/temp/" + str(tailfile[i]) + ".dat"
read_shuffle_write(str_list, frontfilepath, tailfilepath, bytesNum)
def whole_shuffle(bytesNum):
file1 = random.randint(0, filenum - 1)
file2 = random.randint(0, filenum - 1)
while file2 == file1:
file2 = random.randint(0, filenum - 1)
str_list = []
filepath1 = "./dataset_temp/temp/" + str(file1) + ".dat"
filepath2 = "./dataset_temp/temp/" + str(file2) + ".dat"
read_shuffle_write(str_list, filepath1, filepath2, bytesNum)
if distriName == 'zipf' or distriName == 'powerlaw':
props = powerlaw(dis, para)
elif distriName == 'weibull':
pa = 0.1
props = weibull(dis, pa, para)
freq = [math.ceil(prop * tot) for prop in props]
minFreq = min(freq)
maxFreq = max(freq)
gen(freq, bytePerStr)
front_tail_shuffle(bytePerStr)
for i in range(100):
whole_shuffle(bytePerStr)
file_list = [i for i in range(filenum)]
random.shuffle(file_list)
with open(filepath, "wb") as f:
for i in range(filenum):
readfilepath = "./dataset_temp/temp/" + str(file_list[i]) + ".dat"
with open(readfilepath, "rb") as rf:
st = rf.read(bytePerStr)
while st:
f.write(st)
st = rf.read(bytePerStr)
rf.close()
f.close()
for fileno in range(0, filenum):
temp_filepath = "./dataset_temp/temp/" + str(fileno) + ".dat"
os.remove(temp_filepath)
os.rmdir("./dataset_temp/temp")
return(minFreq, maxFreq)
if __name__ == '__main__':
app.run(host= '0.0.0.0',port =8086, debug=True) #启动app的调试模式