Skip to main content

seasonal.go

seasonal.go - Overview

  1. Overview

This file defines the base seasonal anomaly detection provider and related functionalities. It includes options for configuring the provider with cache, key generator, feature lookup, and data reader. It also implements methods for querying historical data, calculating predicted values, determining anomaly scores, and generating upper and lower bounds for anomaly detection.

  1. Detailed Documentation

    Variable: movingAvgWindowSize

    • Purpose: Defines the window size for calculating the moving average.
    • Type: int
    • Value: 7

    Interface: BaseProvider

    • Purpose: Defines an interface for provider types, including a method to retrieve the BaseSeasonalProvider.
    • Methods:
      • GetBaseSeasonalProvider() *BaseSeasonalProvider
        • Purpose: Retrieves the BaseSeasonalProvider instance.
        • Returns: A pointer to a BaseSeasonalProvider.

    Type: GenericProviderOption[T BaseProvider]

    • Purpose: Defines a generic function type for configuring provider options.
    • Type Parameter:
      • T: A type that implements the BaseProvider interface.

    Function: WithCache[T BaseProvider](cache cache.Cache) GenericProviderOption[T]

    • Purpose: Returns a GenericProviderOption that sets the cache for the provider.
    • Parameters:
      • cache cache.Cache: The cache to be set.
    • Returns: A GenericProviderOption that configures the cache.

    Function: WithKeyGenerator[T BaseProvider](keyGenerator cache.KeyGenerator) GenericProviderOption[T]

    • Purpose: Returns a GenericProviderOption that sets the key generator for the provider.
    • Parameters:
      • keyGenerator cache.KeyGenerator: The key generator to be set.
    • Returns: A GenericProviderOption that configures the key generator.

    Function: WithFeatureLookup[T BaseProvider](ff interfaces.FeatureLookup) GenericProviderOption[T]

    • Purpose: Returns a GenericProviderOption that sets the feature lookup for the provider.
    • Parameters:
      • ff interfaces.FeatureLookup: The feature lookup to be set.
    • Returns: A GenericProviderOption that configures the feature lookup.

    Function: WithReader[T BaseProvider](reader interfaces.Reader) GenericProviderOption[T]

    • Purpose: Returns a GenericProviderOption that sets the reader for the provider.
    • Parameters:
      • reader interfaces.Reader: The reader to be set.
    • Returns: A GenericProviderOption that configures the reader.

    Type: BaseSeasonalProvider

    • Purpose: Represents the base seasonal anomaly detection provider.
    • Fields:
      • querierV2 interfaces.Querier: An interface for querying data.
      • reader interfaces.Reader: An interface for reading data.
      • fluxInterval time.Duration: The time duration for the flux interval.
      • cache cache.Cache: A cache for storing query results.
      • keyGenerator cache.KeyGenerator: A key generator for generating cache keys.
      • ff interfaces.FeatureLookup: An interface for feature lookup.

    Method: (*BaseSeasonalProvider) getQueryParams(req *GetAnomaliesRequest) *anomalyQueryParams

    • Purpose: Prepares the anomaly query parameters based on the request.
    • Parameters:
      • req *GetAnomaliesRequest: The request containing the parameters.
    • Returns: A pointer to anomalyQueryParams.

    Method: (*BaseSeasonalProvider) getResults(ctx context.Context, params *anomalyQueryParams) (*anomalyQueryResults, error)

    • Purpose: Executes the queries to retrieve data for anomaly detection.
    • Parameters:
      • ctx context.Context: The context for the query.
      • params *anomalyQueryParams: The parameters for the anomaly query.
    • Returns:
      • *anomalyQueryResults: A pointer to anomalyQueryResults containing the results.
      • error: An error, if any.

    Method: (*BaseSeasonalProvider) getMatchingSeries(queryResult *v3.Result, series *v3.Series) *v3.Series

    • Purpose: Finds a matching series within a query result based on labels.
    • Parameters:
      • queryResult *v3.Result: The query result to search within.
      • series *v3.Series: The series to find a match for.
    • Returns:
      • *v3.Series: A pointer to the matching series, or nil if no match is found.

    Method: (*BaseSeasonalProvider) getAvg(series *v3.Series) float64

    • Purpose: Calculates the average value of a series.
    • Parameters:
      • series *v3.Series: The series to calculate the average from.
    • Returns:
      • float64: The average value of the series.

    Method: (*BaseSeasonalProvider) getStdDev(series *v3.Series) float64

    • Purpose: Calculates the standard deviation of a series.
    • Parameters:
      • series *v3.Series: The series to calculate the standard deviation from.
    • Returns:
      • float64: The standard deviation of the series.

    Method: (*BaseSeasonalProvider) getMovingAvg(series *v3.Series, movingAvgWindowSize int, startIdx int) float64

    • Purpose: Calculates the moving average of a series.
    • Parameters:
      • series *v3.Series: The series to calculate the moving average from.
      • movingAvgWindowSize int: The window size for the moving average.
      • startIdx int: The starting index for the calculation.
    • Returns:
      • float64: The moving average of the series.

    Method: (*BaseSeasonalProvider) getMean(floats ...float64) float64

    • Purpose: Calculates the mean of a slice of float64 values.
    • Parameters:
      • floats ...float64: A variadic slice of float64 values.
    • Returns:
      • float64: The mean of the float64 values.

    Method: (*BaseSeasonalProvider) getPredictedSeries(series *v3.Series, prevSeries *v3.Series, currentSeasonSeries *v3.Series, pastSeasonSeries *v3.Series, past2SeasonSeries *v3.Series, past3SeasonSeries *v3.Series) *v3.Series

    • Purpose: Predicts the series values based on historical data.
    • Parameters:
      • series *v3.Series: The current series.
      • prevSeries *v3.Series: The series from the previous period.
      • currentSeasonSeries *v3.Series: The series from the current season.
      • pastSeasonSeries *v3.Series: The series from the past season.
      • past2SeasonSeries *v3.Series: The series from the past 2 seasons.
      • past3SeasonSeries *v3.Series: The series from the past 3 seasons.
    • Returns:
      • *v3.Series: A new series containing the predicted values.

    Method: (*BaseSeasonalProvider) getBounds(series *v3.Series, predictedSeries *v3.Series, zScoreThreshold float64) (*v3.Series, *v3.Series)

    • Purpose: Calculates the upper and lower bounds for anomaly detection based on the predicted series and z-score threshold.
    • Parameters:
      • series *v3.Series: The original series.
      • predictedSeries *v3.Series: The predicted series.
      • zScoreThreshold float64: The z-score threshold.
    • Returns:
      • *v3.Series: The upper bound series.
      • *v3.Series: The lower bound series.

    Method: (*BaseSeasonalProvider) getExpectedValue(series *v3.Series, prevSeries *v3.Series, currentSeasonSeries *v3.Series, pastSeasonSeries *v3.Series, past2SeasonSeries *v3.Series, past3SeasonSeries *v3.Series, idx int) float64

    • Purpose: Calculates the expected value for a given index based on historical data.
    • Parameters:
      • series *v3.Series: The current series (unused).
      • prevSeries *v3.Series: The series from the previous period.
      • currentSeasonSeries *v3.Series: The series from the current season.
      • pastSeasonSeries *v3.Series: The series from the past season.
      • past2SeasonSeries *v3.Series: The series from the past 2 seasons.
      • past3SeasonSeries *v3.Series: The series from the past 3 seasons.
      • idx int: The index for which to calculate the expected value.
    • Returns:
      • float64: The expected value.

    Method: (*BaseSeasonalProvider) getScore(series *v3.Series, prevSeries *v3.Series, weekSeries *v3.Series, weekPrevSeries *v3.Series, past2SeasonSeries *v3.Series, past3SeasonSeries *v3.Series, value float64, idx int) float64

    • Purpose: Calculates the anomaly score for a given value and index based on historical data.
    • Parameters:
      • series *v3.Series: The current series.
      • prevSeries *v3.Series: The series from the previous period.
      • weekSeries *v3.Series: The series from the current season.
      • weekPrevSeries *v3.Series: The series from the past season.
      • past2SeasonSeries *v3.Series: The series from the past 2 seasons.
      • past3SeasonSeries *v3.Series: The series from the past 3 seasons.
      • value float64: The current value.
      • idx int: The index of the value.
    • Returns:
      • float64: The anomaly score.

    Method: (*BaseSeasonalProvider) getAnomalyScores(series *v3.Series, prevSeries *v3.Series, currentSeasonSeries *v3.Series, pastSeasonSeries *v3.Series, past2SeasonSeries *v3.Series, past3SeasonSeries *v3.Series) *v3.Series

    • Purpose: Calculates the anomaly scores for the entire series based on historical data.
    • Parameters:
      • series *v3.Series: The current series.
      • prevSeries *v3.Series: The series from the previous period.
      • currentSeasonSeries *v3.Series: The series from the current season.
      • pastSeasonSeries *v3.Series: The series from the past season.
      • past2SeasonSeries *v3.Series: The series from the past 2 seasons.
      • past3SeasonSeries *v3.Series: The series from the past 3 seasons.
    • Returns:
      • *v3.Series: A new series containing the anomaly scores.

    Method: (*BaseSeasonalProvider) getAnomalies(ctx context.Context, req *GetAnomaliesRequest) (*GetAnomaliesResponse, error)

    • Purpose: Orchestrates the anomaly detection process, retrieving data, calculating predicted values, and determining anomaly scores.
    • Parameters:
      • ctx context.Context: The context for the operation.
      • req *GetAnomaliesRequest: The request containing the parameters.
    • Returns:
      • *GetAnomaliesResponse: The response containing the anomaly detection results.
      • error: An error, if any.
  2. Code Examples

// Example of creating a BaseSeasonalProvider with cache
const cache = new InMemoryCache();
const provider = new BaseSeasonalProvider(/* ... */);
WithCache(cache)(provider);
  1. Clarity and Accuracy

The documentation is based on the provided code and aims to be as accurate and clear as possible.

Include in Getting Started: NO