2022-02-06 03:36:45 +08:00
|
|
|
import logging
|
2022-04-14 03:01:13 +08:00
|
|
|
from pathlib import Path
|
2022-02-06 03:36:45 +08:00
|
|
|
|
2022-11-06 20:09:44 +08:00
|
|
|
import pandas as pd
|
2022-01-19 08:40:02 +08:00
|
|
|
from felicity.apps.analysis.models.analysis import Sample
|
2022-11-06 20:09:44 +08:00
|
|
|
from felicity.apps.analytics import SampleAnalyticsInit, conf, models
|
2022-01-19 08:40:02 +08:00
|
|
|
from felicity.apps.job import conf as job_conf
|
2022-11-06 20:09:44 +08:00
|
|
|
from felicity.apps.job import models as job_models
|
|
|
|
from felicity.apps.notification.utils import ReportNotifier
|
2022-03-24 14:59:39 +08:00
|
|
|
|
2022-02-06 03:36:45 +08:00
|
|
|
logging.basicConfig(level=logging.INFO)
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2022-03-24 14:59:39 +08:00
|
|
|
report_notifier = ReportNotifier()
|
|
|
|
|
2022-01-19 08:40:02 +08:00
|
|
|
|
|
|
|
async def generate_report(job_uid: str):
|
|
|
|
job: job_models.Job = await job_models.Job.get(uid=job_uid)
|
2022-02-06 03:36:45 +08:00
|
|
|
report: models.ReportMeta = await models.ReportMeta.get(uid=job.job_id)
|
2022-01-19 08:40:02 +08:00
|
|
|
if report.status != conf.report_states.PENDING:
|
|
|
|
await job.change_status(new_status=job_conf.states.FAILED)
|
2022-03-24 14:59:39 +08:00
|
|
|
await report_notifier.notify(
|
2022-11-06 20:09:44 +08:00
|
|
|
f"Failed to generate {report.report_type} report", report.created_by
|
2022-03-24 14:59:39 +08:00
|
|
|
)
|
2022-01-19 08:40:02 +08:00
|
|
|
return
|
|
|
|
|
2022-11-20 23:32:53 +08:00
|
|
|
await job.change_status(new_status=job_conf.states.RUNNING)
|
2022-03-24 14:59:39 +08:00
|
|
|
analytics = SampleAnalyticsInit(Sample)
|
2022-02-06 03:36:45 +08:00
|
|
|
columns, lines = await analytics.get_line_listing(
|
|
|
|
period_start=report.period_start,
|
|
|
|
period_end=report.period_end,
|
2022-11-06 20:09:44 +08:00
|
|
|
sample_states=report.sample_states.split(", "),
|
2022-02-06 03:36:45 +08:00
|
|
|
date_column=report.date_column,
|
|
|
|
analysis_uids=[an.uid for an in report.analyses],
|
|
|
|
)
|
2022-01-19 08:40:02 +08:00
|
|
|
|
|
|
|
data_list = [line for line in lines]
|
|
|
|
|
|
|
|
df = pd.DataFrame(data_list, columns=columns)
|
|
|
|
|
|
|
|
try:
|
|
|
|
file_name = report.temp + ".csv"
|
2022-03-24 14:59:39 +08:00
|
|
|
except Exception as e: # noqa
|
2022-01-19 08:40:02 +08:00
|
|
|
await job.change_status(new_status=job_conf.states.FAILED)
|
|
|
|
await report.set_final(location=None, status=conf.report_states.FAILED)
|
2022-03-24 14:59:39 +08:00
|
|
|
await report_notifier.notify(
|
|
|
|
f"Error encountered: Failed to save generated {report.report_type} report: {e}",
|
2022-11-06 20:09:44 +08:00
|
|
|
report.created_by,
|
2022-03-24 14:59:39 +08:00
|
|
|
)
|
2022-01-19 08:40:02 +08:00
|
|
|
return
|
|
|
|
|
|
|
|
df.to_csv(file_name, index=False)
|
|
|
|
|
|
|
|
file_path = Path(file_name)
|
|
|
|
if not file_path.is_file():
|
|
|
|
await job.change_status(new_status=job_conf.states.FAILED)
|
|
|
|
await report.set_final(status=conf.report_states.FAILED, location=None)
|
2022-03-24 14:59:39 +08:00
|
|
|
await report_notifier.notify(
|
|
|
|
f"File access error: Failed to access generated {report.report_type} report",
|
2022-11-06 20:09:44 +08:00
|
|
|
report.created_by,
|
2022-03-24 14:59:39 +08:00
|
|
|
)
|
2022-01-19 08:40:02 +08:00
|
|
|
return False
|
|
|
|
|
|
|
|
await job.change_status(new_status=job_conf.states.FINISHED)
|
|
|
|
await report.set_final(location=file_name, status=conf.report_states.READY)
|
2022-03-24 14:59:39 +08:00
|
|
|
await report_notifier.notify(
|
|
|
|
f"Your {report.report_type} report was successfully generated",
|
2022-11-06 20:09:44 +08:00
|
|
|
report.created_by,
|
2022-03-24 14:59:39 +08:00
|
|
|
)
|
2022-01-19 08:40:02 +08:00
|
|
|
return True
|