def test_compute_report_compute_fail(mocker, get_report_mocked, mock_data,
                                     assert_post_called):
    mocker.patch(
        'solarforecastarbiter.reports.main.create_raw_report_from_data',
        side_effect=TypeError)
    with pytest.raises(TypeError):
        main.compute_report('nope', 'repid')
def test_compute_report(mocker, report_objects, mock_data):
    report = report_objects[0]
    mocker.patch(
        'solarforecastarbiter.io.api.APISession.get_report',
        return_value=report)
    mocker.patch(
        'solarforecastarbiter.io.api.APISession.post_raw_report'
    )
    raw = main.compute_report('nope', report.report_id)
    assert isinstance(raw, datamodel.RawReport)
    assert len(raw.plots.figures) > 0
Beispiel #3
0
def report(verbose, user, password, base_url, report_file, output_file, format,
           serialization_roundtrip, orca_server_url):
    """
    Make a report. See API documentation's POST reports section for
    REPORT_FILE requirements.
    """
    set_log_level(verbose)
    token = cli_access_token(user, password)
    with open(report_file) as f:
        metadata = json.load(f)
    session = APISession(token, base_url=base_url)
    report = session.process_report_dict(metadata)
    if orca_server_url is not None:
        import plotly.io as pio
        pio.orca.config.server_url = orca_server_url
    if serialization_roundtrip:
        with mock_raw_report_endpoints(base_url):
            session.create_report(report)
            reports.compute_report(token, 'no_id', base_url)
            full_report = session.get_report('no_id')
    else:
        data = reports.get_data_for_report(session, report)
        raw_report = reports.create_raw_report_from_data(report, data)
        full_report = report.replace(raw_report=raw_report, status='complete')
    # assumed dashboard url based on api url
    dash_url = base_url.replace('api', 'dashboard')
    if ((format == 'detect' and output_file.endswith('.html'))
            or format == 'html'):
        html_report = template.render_html(full_report,
                                           dash_url,
                                           with_timeseries=True,
                                           body_only=False)
        with open(output_file, 'w') as f:
            f.write(html_report)
    elif ((format == 'detect' and output_file.endswith('.pdf'))
          or format == 'pdf'):
        pdf_report = template.render_pdf(full_report, dash_url)
        with open(output_file, 'wb') as f:
            f.write(pdf_report)
    else:
        raise ValueError("Unable to detect format")
Beispiel #4
0
def execute_job(name, job_type, user_id, **kwargs):
    """
    Function that should be given to RQ to execute the
    proper code depending on job type

    Parameters
    ----------
    job_type : str
        The type of job to determine which code path to run
    user_id : str
        The ID of the user that this job should execute as
    **kwargs
        Additional keyword arguments passed to the execution code

    Raises
    ------
    ValueError
        If the job type is unsupported
    """
    logger.info('Running job %s of type %s', name, job_type)
    token = exchange_token(user_id)
    base_url = kwargs.get('base_url', None)
    if job_type == 'daily_observation_validation':
        start = utcnow() + pd.Timedelta(kwargs['start_td'])
        end = utcnow() + pd.Timedelta(kwargs['end_td'])
        return fetch_and_validate_all_observations(token,
                                                   start,
                                                   end,
                                                   base_url=base_url)
    elif job_type == 'reference_nwp':
        issue_buffer = pd.Timedelta(kwargs['issue_time_buffer'])
        run_time = utcnow()
        nwp.set_base_path(kwargs.get('nwp_directory', '/data'))
        return make_latest_nwp_forecasts(token, run_time, issue_buffer,
                                         base_url)
    elif job_type == 'periodic_report':
        return compute_report(token, kwargs['report_id'], base_url)
    elif job_type == 'reference_persistence':
        max_run_time = utcnow()
        return make_latest_persistence_forecasts(token,
                                                 max_run_time,
                                                 base_url=base_url)
    elif job_type == 'reference_probabilistic_persistence':
        max_run_time = utcnow()
        return make_latest_probabilistic_persistence_forecasts(
            token, max_run_time, base_url=base_url)
    else:
        raise ValueError(f'Job type {job_type} is not supported')
def test_compute_report_request_mock(
        mocker, report_objects, mock_data, requests_mock):
    report = report_objects[0]
    mocker.patch(
        'solarforecastarbiter.io.api.APISession.get_report',
        return_value=report)
    requests_mock.register_uri(
        'POST', re.compile('.*/reports/.*/values'),
        text=lambda *x: str(uuid.uuid1()))
    rep_post = requests_mock.register_uri(
        'POST', re.compile('.*/reports/.*/raw')
    )
    status_up = requests_mock.register_uri(
        'POST', re.compile('.*/reports/.*/status')
    )
    raw = main.compute_report('nope', report.report_id)
    assert isinstance(raw, datamodel.RawReport)
    assert len(raw.plots.figures) > 0
    assert rep_post.called
    assert 'complete' in status_up.last_request.path
def test_compute_report_get_report_fail(mocker, assert_post_called):
    mocker.patch('solarforecastarbiter.io.api.APISession.get_report',
                 side_effect=TypeError)
    with pytest.raises(TypeError):
        main.compute_report('nope', 'repid')