def test_get_latest_job_id_for_name_and_params(_get_all_job_ids, conn, gridfs): serializer = MongoResultSerializer() serializer.get_latest_job_id_for_name_and_params("report_name", None) _get_all_job_ids.assert_called_once_with("report_name", None, as_of=None, limit=1)
def test__get_all_job_ids(conn, gridfs): serializer = MongoResultSerializer() serializer._get_all_job_ids("report_name", None, limit=1) serializer.library.aggregate.assert_called_once_with( [ {"$match": {"status": {"$ne": JobStatus.DELETED.value}, "report_name": "report_name"}}, {"$sort": {"update_time": -1}}, {"$limit": 1}, {"$project": {"report_name": 1, "job_id": 1}}, ] )
def _get_job_results( job_id: str, report_name: str, serializer: MongoResultSerializer, retrying: Optional[bool] = False, ignore_cache: Optional[bool] = False, ) -> constants.NotebookResultBase: report_name = convert_report_name_url_to_path(report_name) current_result = get_report_cache(report_name, job_id) if current_result and not ignore_cache: logger.info("Fetched result from cache.") notebook_result = current_result else: notebook_result = serializer.get_check_result(job_id) set_report_cache(report_name, job_id, notebook_result) if not notebook_result: err_info = "Job results not found for report name={} / job id={}. " "Did you use an invalid job ID?".format( report_name, job_id) return constants.NotebookResultError(job_id, error_info=err_info, report_name=report_name, job_start_time=dt.now()) if isinstance(notebook_result, str): if not retrying: return _get_job_results(job_id, report_name, serializer, retrying=True) raise NotebookRunException( "An unexpected string was found as a result. Please run your request again." ) return notebook_result
def test__get_all_job_ids(conn, gridfs): serializer = MongoResultSerializer() serializer._get_all_job_ids("report_name", None, limit=1) serializer.library.find.assert_called_once_with( { "status": { "$ne": JobStatus.DELETED.value }, "report_name": "report_name" }, { "_id": 0, "job_id": 1, "report_name": 1 }, )
def test_mongo_filter_status(): mongo_filter = MongoResultSerializer._mongo_filter("report", status=JobStatus.DONE) assert mongo_filter == { "report_name": "report", "status": JobStatus.DONE.value }
def get_all_result_keys(serializer: MongoResultSerializer, limit: int = 0, force_reload: bool = False) -> List[Tuple[str, str]]: all_keys = get_cache(("all_result_keys", limit)) if not all_keys or force_reload: all_keys = serializer.get_all_result_keys(limit=limit) set_cache(("all_result_keys", limit), all_keys, timeout=1) return all_keys
def get_latest_successful_job_results_all_params( report_name: str, serializer: MongoResultSerializer, retrying: Optional[bool] = False, ignore_cache: Optional[bool] = False, ) -> Iterator[constants.NotebookResultComplete]: for job_id in serializer.get_latest_successful_job_ids_for_name_all_params( report_name): yield _get_job_results(job_id, report_name, serializer, retrying, ignore_cache)
def test_mongo_filter_overrides(): mongo_filter = MongoResultSerializer._mongo_filter("report", overrides={ "b": 1, "a": 2 }) assert mongo_filter == { "report_name": "report", "overrides.a": 2, "overrides.b": 1 }
def get_all_available_results_json( serializer: MongoResultSerializer, limit: int) -> List[constants.NotebookResultBase]: json_output = [] for result in serializer.get_all_results(limit=limit, load_payload=False): output = result.saveable_output() output["result_url"] = url_for("serve_results_bp.task_results", job_id=output["job_id"], report_name=output["report_name"]) output["ipynb_url"] = url_for("serve_results_bp.download_ipynb_result", job_id=output["job_id"], report_name=output["report_name"]) output["pdf_url"] = url_for("serve_results_bp.download_pdf_result", job_id=output["job_id"], report_name=output["report_name"]) output["rerun_url"] = url_for("run_report_bp.rerun_report", job_id=output["job_id"], report_name=output["report_name"]) json_output.append(output) return json_output
def test_mongo_filter(): mongo_filter = MongoResultSerializer._mongo_filter("report") assert mongo_filter == {"report_name": "report"}