This repository has been archived by the owner on May 17, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
app_dev.py
180 lines (152 loc) · 5.22 KB
/
app_dev.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
import os
import json
from flask import Flask, Response, request, render_template
from model.dataModel_v2 import DataHandler
# todo: deprecate config, it is only used for DB host address and port number
from config import CONFIG
# ----------------------------------------------------------------------------
# Global variables
# ----------------------------------------------------------------------------
# web application
app = Flask(__name__)
# data handler including
# - managing MongoDB
# - sync files in folders user selected with MongoDB
# - monitoring folders, updating some events, and broadcasting the results
# Data = DataHandler(
# rootDir=CONFIG['DATA_DIR'],
# fsMapFn=CONFIG['FSMAP'],
# db_host=CONFIG['DB']['HOST'],
# db_port=CONFIG['DB']['PORT'],
# xml_config=CONFIG['XML']
# )
Data = DataHandler(
config=CONFIG,
project_dir='./projects'
)
# ----------------------------------------------------------------------------
# Project managing route
# ----------------------------------------------------------------------------
@app.route('/api/project', methods=['POST'])
def post_project():
data = request.get_json()
action = data['action']
project = data['project']
if action not in ['get', 'check', 'update']:
return json.dumps({})
elif action == 'get':
return Data.get_projects()
elif action == 'check' and project is not None:
return Data.check_project(project)
return json.dumps({})
@app.route('/api/project/validate', methods=['POST'])
def post_project_validate():
data = request.get_json()
path = data['path']
if os.path.exists(path):
return json.dumps('')
else:
return json.dumps('invalid path')
# ----------------------------------------------------------------------------
# Syncing route (update db from filesystem)
# ----------------------------------------------------------------------------
def parse_progress(project:dict):
"""Return progress in percentage"""
def _parse(_str):
_nums_str = _str.split('/')
_processed = int(_nums_str[0])
_total = int(_nums_str[1])
return _processed, _total
p_xml, t_xml = _parse(project['xml'])
p_jpg, t_jpg = _parse(project['jpg'])
p_tif, t_tif = _parse(project['tiff'])
total = t_xml + t_jpg + t_tif
processed = p_xml + p_jpg + p_tif
return int(processed/total*100) if total else 0.
@app.route('/api/sync/request', methods=['POST'])
def sync_request():
"""
Request updating DB based on a given project.
"""
project = request.get_json()
status, project = Data.run_syncer(project)
progress = parse_progress(project)
project['status'] = status
project['progress'] = progress
return json.dumps(project)
@app.route('/api/sync/progress', methods=['POST'])
def sync_progress():
"""
Queried by clients to get sync. information, if any.
Returns:
Returns sync. progress on multi-threaded operations
"""
results = Data.get_projects_in_sync()
projects = []
for res in results:
finished, project = res
status = 'FINISHED' if finished else 'RUNNING'
progress = parse_progress(project)
project['status'] = status
project['progress'] = progress
projects.append(project)
return json.dumps(projects)
# ----------------------------------------------------------------------------
# Data
# ----------------------------------------------------------------------------
@app.route('/api/data/samplelist', methods=['POST'])
def get_db_samplelist():
project = request.get_json()
samplelist = Data.get_samplelist(project)
return json.dumps(samplelist)
@app.route('/api/data/sample', methods=['POST'])
def get_sample():
data = request.get_json()
sampleNames = data['sampleNames']
project = data['project']
data = Data.get_samples(sampleNames, project)
return json.dumps({
'sampleList': sampleNames,
'sampleData': data
})
@app.route('/api/data/tiff', methods=['POST'])
def get_tiff():
data = request.get_json()
id = data['id']
db = data['db']
col = data['col']
return json.dumps(Data.get_tiff(id, db, col))
# ----------------------------------------------------------------------------
# main
# ----------------------------------------------------------------------------
@app.route('/')
def start():
"""
Render index page
"""
return render_template('index.html')
def finalize():
"""
Finalize web server before exiting the program
"""
pass
def main(host, port):
try:
app.run(host=host, port=port, threaded=True)
except KeyboardInterrupt:
pass
finally:
finalize()
if __name__ == '__main__':
import argparse
argparser = argparse.ArgumentParser(description="MultiSciView")
argparser.add_argument("-s", "--serverhost",
type=str,
default='0.0.0.0',
help="Web server host address")
argparser.add_argument("-p", "--serverport",
type=int,
default=8001,
help="Web server port number")
args = argparser.parse_args()
main(host=args.serverhost, port=args.serverport)