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
|
"""File parsing module"""
import os
from functools import reduce
from redis import Redis
from rq import get_current_job
from flask import (
request,
url_for,
redirect,
Blueprint,
render_template,
current_app as app)
from . import jobs
from quality_control.errors import ParseError
from quality_control.parsing import (
FileType,
parse_file,
strain_names,
parse_errors,
parse_strains)
parsebp = Blueprint("parse", __name__)
def queued_parse(filepath, filetype, strainsfile, redis_url):
job = get_current_job()
with Redis.from_url(redis_url) as rconn:
jobs.update_meta(rconn, job, status = "in-progress", progress = 0)
job.save_meta()
try:
parsed = parse_file(
filepath, filetype, strain_names(parse_strains(strainsfile)))
for line, curr_size in parsed:
jobs.update_meta(
rconn, job, progress = (curr_size/job.meta["filesize"]) * 100,
message = f"Parsed {curr_size} bytes")
print(f"Progress: {curr_size} bytes: {(curr_size/job.meta['filesize']) * 100}%")
os.remove(filepath)
jobs.update_meta(
rconn, job, progress = 10, status = "success",
message = "no errors found")
except ParseError as pe:
pe_dict = pe.args[0]
jobs.update_meta(
rconn, job, status = "parse-error", results = {
"filename": os.path.basename(filepath), "filetype": filetype,
"position": pe_dict["position"],
"line_number": pe_dict["line_number"]
})
@parsebp.route("/parse", methods=["GET"])
def parse():
"""Trigger file parsing"""
# TODO: Maybe implement external process to parse the files
errors = False
filename = request.args.get("filename")
filetype = request.args.get("filetype")
if filename is None:
flash("No file provided", "alert-error")
errors = True
if filetype is None:
flash("No filetype provided", "alert-error")
errors = True
filepath = os.path.join(app.config["UPLOAD_FOLDER"], filename)
if not os.path.exists(filepath):
flash("Selected file does not exist (any longer)", "alert-danger")
errors = True
if errors:
return redirect(url_for("entry.index"))
filetype = (
FileType.AVERAGE if filetype == "average" else FileType.STANDARD_ERROR)
job = jobs.enqueue_job(
"qc_app.parse.queued_parse", filepath, filetype,
os.path.join(os.getcwd(), "etc/strains.csv"), app.config["REDIS_URL"],
additional_jobs_meta = {
"filename": filename, "filesize": os.stat(filepath).st_size})
return redirect(url_for("parse.parse_status", job_id=job.get_id()))
@parsebp.route("/status/<job_id>", methods=["GET"])
def parse_status(job_id):
job = jobs.job(job_id)
if job:
job_id = job.get_id()
progress = job.meta["progress"]
status = job.meta["status"]
filename = job.meta.get("filename", "uploaded file")
if status == "success":
return redirect(url_for("parse.results", job_id=job_id))
if status == "parse-error":
return redirect(url_for("parse.fail", job_id=job_id))
return render_template(
"job_progress.html",
job_id = job_id,
job_status = status,
progress = progress,
message = job.meta.get("message", ""),
job_name = f"Parsing '{filename}'")
return render_template("no_such_job.html", job_id=job_id)
@parsebp.route("/results/<job_id>", methods=["GET"])
def results(job_id):
"""Show results of parsing..."""
job = jobs.job(job_id)
if job:
filename = job.meta["filename"]
errors = job.meta.get("errors", [])
return render_template(
"parse_results.html",
errors=errors,
job_name = f"Parsing '{filename}'",
starting_line_number=job.meta.get("starting_line_number", 0))
return render_template("no_such_job.html", job_id=job_id)
def queued_collect_errors(filepath, filetype, strainsfile, redis_url, seek_pos=0):
job = get_current_job()
errors = []
count = 0
with Redis.from_url(redis_url) as rconn:
for error in parse_errors(
filepath, filetype, strain_names(parse_strains(strainsfile)),
seek_pos):
count = count + 1
jobs.update_meta(
rconn, job, message = f"Collected {count} errors", progress = (
(error["position"] / job.meta["filesize"]) * 100))
errors.append(error)
jobs.update_meta(
rconn, job, errors = errors, progress = 100, status = "success")
@parsebp.route("/fail/<job_id>", methods=["GET"])
def fail(job_id):
"""Handle parsing failure"""
old_job = jobs.job(job_id)
if old_job:
old_job.refresh()
job_id = old_job.get_id()
progress = old_job.meta.get("progress", 0)
status = old_job.meta["status"]
results = old_job.meta["results"]
filename = old_job.meta.get("filename", "uploaded file")
new_job = jobs.enqueue_job(
"qc_app.parse.queued_collect_errors",
os.path.join(
app.config["UPLOAD_FOLDER"], old_job.meta["filename"]),
results["filetype"], os.path.join(os.getcwd(), "etc/strains.csv"),
app.config["REDIS_URL"], results["position"],
additional_jobs_meta = {
"status": "Collecting Errors",
"filename": old_job.meta["filename"],
"filesize": old_job.meta["filesize"],
"starting_line_number": results["line_number"]})
return redirect(url_for("parse.parse_status", job_id=new_job.get_id()))
return render_template("no_such_job.html", job_id=job_id)
|