views.py
6.6 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
190
191
192
import flask
import flask.ext.login as flask_login
from flask_login import current_user,login_required,login_user
from flask import render_template, redirect, request, url_for, flash
from flask.ext.bootstrap import Bootstrap
from app import app,login_manager
from .models import User
from .forms import LoginForm, TrainForm, ExportDataForm, SpiderForm, JumpForm,JumpSpiderExportForm
from werkzeug.utils import secure_filename
import os
import subprocess
import time
users = {'root': {'pw': 'abc123456789'}}
@login_manager.user_loader
def user_loader(username):
if username not in users:
return
user = User()
user.id = username
return user
@app.route('/', methods=['GET','POST'])
@app.route('/login', methods=['GET','POST'])
def login():
form = LoginForm()
if form.validate_on_submit():
username = form.username.data
passwd = form.password.data
print username
if username in users and users[username]['pw'] == passwd:
user = User()
user.id = username
login_user(user)
return redirect(request.args.get('next') or url_for('main'))
flash('Invalid username or password.')
return render_template('login.html', form=form)
@app.route('/main', methods=['GET', 'POST'])
@login_required
def main():
form_1 = TrainForm()
form_2 = JumpSpiderExportForm()
package_dir = os.path.dirname(os.path.abspath(__file__))
shell_path = os.path.join(package_dir, '../../run.sh')
if form_1.validate_on_submit():
#print 'hello'
temp_list = []
type_name = form_1.type_name.data.encode('utf-8')
temp_list.append(type_name)
table_name = form_1.table_name.data.encode('utf-8')
temp_list.append(table_name)
img_dir = form_1.img_source.data.encode('utf-8')
temp_list.append(img_dir)
model_name = form_1.model_name.data.encode('utf-8')
temp_list.append(model_name)
label_name = form_1.label_source.data.encode('utf-8')
temp_list.append(label_name)
feat_type = form_1.feat_type.data.encode('utf-8')
temp_list.append(feat_type)
cmd_result = ','.join(temp_list)
print cmd_result
if type_name == 'train':
if label_name == 'null':
start = time.time()
retcode = subprocess.call(shell_path + ' ' + cmd_result, shell = True)
end = time.time()
time_consume = str(end - start)
if retcode == 0:
flash('train without label file, ' + 'time consume: ' + time_consume + 's')
else:
flash('wrong job')
else:
start = time.time()
retcode = subprocess.call(shell_path + ' ' + cmd_result, shell = True)
end = time.time()
time_consume = str(end - start)
if retcode == 0:
flash('train with label file, '+ 'time consume: ' + time_consume + 's')
else:
flash('wrong job')
if type_name == 'analysis':
if img_dir == 'null':
start = time.time()
retcode = subprocess.call(shell_path + ' ' + cmd_result, shell = True)
end = time.time()
time_consume = str(end - start)
if retcode == 0:
flash('analysis data from hbase table, ' + 'time consume: ' + time_consume + 's')
else:
flash('wrong job')
else:
start = time.time()
retcode = subprocess.call(shell_path + ' ' + cmd_result, shell = True)
end = time.time()
time_consume = str(end - start)
if retcode == 0:
flash('analysis data from local directory, ' + 'time consume: ' + time_consume + 's')
else:
flash('wrong job')
if form_2.validate_on_submit():
type_name = form_2.type_name.data.encode('utf-8')
if type_name == 'spider':
return redirect(url_for('spider'))
if type_name =='export':
return redirect(url_for('export'))
return render_template('main.html', form_1=form_1, form_2=form_2,name = current_user.id)
@app.route('/export', methods=['GET', 'POST'])
@login_required
def export():
form_1 = ExportDataForm()
form_2 = JumpForm()
package_dir = os.path.dirname(os.path.abspath(__file__))
shell_path = os.path.join(package_dir, '../../export.sh')
if form_1.validate_on_submit():
temp_list = []
type_name = form_1.type_name.data.encode('utf-8')
temp_list.append(type_name)
table_name = form_1.table_name.data.encode('utf-8')
temp_list.append(table_name)
dst_dir = form_1.dst_dir.data.encode('utf-8')
temp_list.append(dst_dir)
cmd_result = ','.join(temp_list)
print cmd_result
start = time.time()
retcode = subprocess.call(shell_path + ' ' + cmd_result, shell = True)
end = time.time()
time_consume = str(end - start)
if retcode == 0:
flash('export data from hbase to local directory, ' + 'time consume: ' + time_consume + 's')
else:
flash('wrong job')
if form_2.validate_on_submit():
return redirect(url_for('main'))
return render_template('export.html', form_1=form_1, form_2=form_2)
@app.route('/spider', methods=['GET', 'POST'])
@login_required
def spider():
form_1 = SpiderForm()
form_2 = JumpForm()
package_dir = os.path.dirname(os.path.abspath(__file__))
shell_path = os.path.join(package_dir, '../../spider1.sh')
if form_1.validate_on_submit():
temp_list = []
spider_source = form_1.spider_source.data.encode('utf-8')
temp_list.append(spider_source)
print spider_source
table_name = form_1.table_name.data.encode('utf-8')
temp_list.append(table_name)
cmd_result = ' '.join(temp_list)
start = time.time()
retcode = subprocess.call(shell_path + ' ' + cmd_result, shell = True)
end = time.time()
time_consume = str(end - start)
if retcode == 0:
flash('scrape data from web to hbase, ' + 'time consume: ' + time_consume + 's')
else:
flash('wrong job')
if form_2.validate_on_submit():
return redirect(url_for('main'))
return render_template('spider.html', form_1=form_1, form_2 = form_2)
@app.errorhandler(404)
def page_not_found(e):
return render_template('404.html'), 404
@login_manager.unauthorized_handler
def unauthorized_handler():
return 'Unauthorized'