def test_hijack_loggers_sentry(mocker):
    events = set()

    def before_send(event, hint):
        events.add(event['logger'])
        return

    sentry_sdk.init(
        "https://[email protected]/0",
        before_send=before_send)
    logger = logging.getLogger('testlog')
    child = logging.getLogger('testlog.child')
    notchild = logging.getLogger('testloggggger')
    with utils.hijack_loggers(['testlog']):
        logging.getLogger('root').error('will show up')
        logger.error('AHHH')
        child.error('Im a baby gotta love me')
        notchild.error('pfft')
    assert 'root' in events
    assert 'testlog' not in events
    assert 'testlog.child' not in events
    assert 'testloggggger' in events

    events = set()
    logging.getLogger('root').error('will show up')
    logger.error('AHHH')
    child.error('c')
    assert 'root' in events
    assert 'testlog' in events
    assert 'testlog.child' in events
    assert logger.propagate
    assert child.propagate
def test_hijack_loggers(mocker):
    old_handler = mocker.MagicMock()
    new_handler = mocker.MagicMock()
    mocker.patch('solarforecastarbiter.utils.ListHandler',
                 return_value=new_handler)
    logger = logging.getLogger('testhijack')
    logger.addHandler(old_handler)
    assert logger.handlers[0] == old_handler
    with utils.hijack_loggers(['testhijack']):
        assert logger.handlers[0] == new_handler
    assert logger.handlers[0] == old_handler
예제 #3
0
def create_raw_report_from_data(report, data):
    """
    Create a raw report using data and report metadata.

    Parameters
    ----------
    report : :py:class:`solarforecastarbiter.datamodel.Report`
        Metadata describing report
    data : dict
        Keys are all Forecast and Observation (or Aggregate)
        objects in the report, values are the corresponding data.

    Returns
    -------
    raw_report : :py:class:`solarforecastarbiterdatamodel.RawReport`

    Todo
    ----
    * add reference forecast
    """
    generated_at = pd.Timestamp.now(tz='UTC')
    report_params = report.report_parameters
    timezone = infer_timezone(report_params)
    versions = get_versions()
    with hijack_loggers([
        'solarforecastarbiter.metrics',
        'solarforecastarbiter.reports.figures.plotly_figures'],
                        ) as handler:
        # Validate, fill forecast, and resample
        processed_fxobs = preprocessing.process_forecast_observations(
            report_params.object_pairs,
            report_params.filters,
            report_params.forecast_fill_method,
            report_params.start, report_params.end,
            data, timezone,
            costs=report_params.costs)

        # Calculate metrics
        metrics_list = calculator.calculate_metrics(
            processed_fxobs,
            list(report_params.categories),
            list(report_params.metrics))
        summary_stats = calculator.calculate_all_summary_statistics(
            processed_fxobs, list(report_params.categories))

        report_plots = plotly_figures.raw_report_plots(report, metrics_list)
        messages = handler.export_records()
    raw_report = datamodel.RawReport(
        generated_at=generated_at, timezone=timezone, versions=versions,
        plots=report_plots, metrics=tuple(metrics_list + summary_stats),
        processed_forecasts_observations=tuple(processed_fxobs),
        messages=messages)
    return raw_report
예제 #4
0
def test_hijack_loggers_sentry(mocker):
    events = set()

    def before_send(event, hint):
        events.add(event['logger'])
        return

    sentry_sdk.init("https://[email protected]/0",
                    before_send=before_send)
    logger = logging.getLogger('testlog')
    with utils.hijack_loggers(['testlog']):
        logging.getLogger('root').error('will show up')
        logger.error('AHHH')
    assert 'root' in events
    assert 'testlog' not in events

    events = set()
    logging.getLogger('root').error('will show up')
    logger.error('AHHH')
    assert 'root' in events
    assert 'testlog' in events