def test_calculate_data_for_prediction(cfg_setup, utcdate, timezone, params):

    period_info = prediction._PREDICTION_PERIODS[params["period"]]
    with on_time(utcdate, timezone):
        now = int(time.time())
        assert callable(period_info.groupby)
        timegroup = period_info.groupby(now)[0]

        time_windows = prediction._time_slices(
            now, int(params["horizon"] * 86400), period_info, timegroup
        )

    hostname, service_description, dsname = HostName("test-prediction"), "CPU load", "load15"
    rrd_datacolumn = cmk.utils.prediction.rrd_datacolum(
        hostname, service_description, dsname, "MAX"
    )
    data_for_pred = prediction._calculate_data_for_prediction(time_windows, rrd_datacolumn)

    expected_reference = _load_expected_result(
        "%s/tests/integration/cmk/base/test-files/%s/%s" % (repo_path(), timezone, timegroup)
    )

    assert isinstance(expected_reference, dict)
    assert sorted(asdict(data_for_pred)) == sorted(expected_reference)
    for key in expected_reference:
        if key == "points":
            for cal, ref in zip(data_for_pred.points, expected_reference["points"]):
                assert cal == pytest.approx(ref, rel=1e-12, abs=1e-12)
        else:
            assert getattr(data_for_pred, key) == expected_reference[key]
def test_calculate_data_for_prediction(cfg_setup, utcdate, timezone, params):

    period_info = prediction._PREDICTION_PERIODS[params['period']]
    with on_time(utcdate, timezone):
        now = int(time.time())
        assert callable(period_info.groupby)
        timegroup = period_info.groupby(now)[0]

        time_windows = prediction._time_slices(now,
                                               int(params["horizon"] * 86400),
                                               period_info, timegroup)

    hostname, service_description, dsname = 'test-prediction', "CPU load", 'load15'
    rrd_datacolumn = cmk.utils.prediction.rrd_datacolum(
        hostname, service_description, dsname, "MAX")
    data_for_pred = prediction._calculate_data_for_prediction(
        time_windows, rrd_datacolumn)

    expected_reference = _load_expected_result(
        "%s/tests/integration/cmk/base/test-files/%s/%s" %
        (repo_path(), timezone, timegroup))

    assert isinstance(expected_reference, dict)
    assert sorted(data_for_pred) == sorted(expected_reference)
    for key in data_for_pred:
        if key == "points":
            for cal, ref in zip(data_for_pred['points'],
                                expected_reference['points']):
                assert cal == pytest.approx(ref, rel=1e-12, abs=1e-12)
        else:
            # TypedDict key must be a string literal
            assert data_for_pred[key] == expected_reference[
                key]  # type: ignore[misc]
def test_calculate_data_for_prediction(cfg_setup, utcdate, timezone, params):

    period_info = prediction._PREDICTION_PERIODS[params['period']]
    with on_time(utcdate, timezone):
        now = int(time.time())
        groupby = period_info["groupby"]
        assert callable(groupby)
        timegroup = groupby(now)[0]

        time_windows = prediction._time_slices(now,
                                               int(params["horizon"] * 86400),
                                               period_info, timegroup)

    hostname, service_description, dsname = 'test-prediction', "CPU load", 'load15'
    rrd_datacolumn = cmk.utils.prediction.rrd_datacolum(
        hostname, service_description, dsname, "MAX")
    data_for_pred = prediction._calculate_data_for_prediction(
        time_windows, rrd_datacolumn)

    path = "%s/tests/integration/cmk/base/test-files/%s/%s" % (
        repo_path(), timezone, timegroup)
    reference = cmk.utils.prediction.retrieve_data_for_prediction(
        path, timegroup)
    data_points = data_for_pred.pop('points')
    assert reference is not None
    ref_points = reference.pop('points')
    for cal, ref in zip(data_points, ref_points):
        assert cal == pytest.approx(ref, rel=1e-12, abs=1e-12)
    assert data_for_pred == reference
def test_retieve_grouped_data_from_rrd(cfg_setup, utcdate, timezone, params, reference):
    "This mostly verifies the up-sampling"

    period_info = prediction._PREDICTION_PERIODS[params["period"]]
    with on_time(utcdate, timezone):
        now = int(time.time())
        assert callable(period_info.groupby)
        timegroup = period_info.groupby(now)[0]
        time_windows = prediction._time_slices(
            now, int(params["horizon"] * 86400), period_info, timegroup
        )

    hostname, service_description, dsname = HostName("test-prediction"), "CPU load", "load15"
    rrd_datacolumn = cmk.utils.prediction.rrd_datacolum(
        hostname, service_description, dsname, "MAX"
    )
    result = prediction._retrieve_grouped_data_from_rrd(rrd_datacolumn, time_windows)

    assert result == reference