mlapi

package
v0.6.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 4, 2023 License: Apache-2.0 Imports: 11 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client is a Grafana API client.

func New

func New(baseURL string, cfg Config) (*Client, error)

New creates a new Grafana client.

func (*Client) DeleteHoliday added in v0.3.0

func (c *Client) DeleteHoliday(ctx context.Context, id string) error

DeleteHoliday deletes an existing holiday.

func (*Client) DeleteJob

func (c *Client) DeleteJob(ctx context.Context, id string) error

DeleteJob deletes a machine learning job.

func (*Client) DeleteOutlierDetector added in v0.4.0

func (c *Client) DeleteOutlierDetector(ctx context.Context, id string) error

DeleteOutlierDetector deletes an outlier detector.

func (*Client) ForecastJob added in v0.6.0

func (c *Client) ForecastJob(ctx context.Context, spec ForecastRequest) (backend.QueryDataResponse, error)

ForecastJob returns a forecast for a job definition and time range.

This is a convenience API to avoid having to create a job, wait for it to train, and then query the forecast. It is designed for exploratory usage rather than to be called regularly; if you want to regularly query a forecast, create a job and query it using the `grafanacloud-ml-metrics` datasource. Jobs specified in the ForecastRequest must have a single series, or this will return an error. This function may be slow the first time it is called, but the result will be cached for 24 hours after that.

func (*Client) Holiday added in v0.3.0

func (c *Client) Holiday(ctx context.Context, id string) (Holiday, error)

Holiday fetches an existing holiday.

func (*Client) Job

func (c *Client) Job(ctx context.Context, id string) (Job, error)

Job fetches an existing machine learning job.

func (*Client) Jobs added in v0.6.0

func (c *Client) Jobs(ctx context.Context) ([]Job, error)

Jobs fetches all existing machine learning jobs.

func (*Client) LinkHolidaysToJob added in v0.3.0

func (c *Client) LinkHolidaysToJob(ctx context.Context, jobID string, holidayIDs []string) (Job, error)

LinkHolidaysToJob links a job to a set of holidays. Only the ID and Holidays fields of the Job struct are used.

func (*Client) NewHoliday added in v0.3.0

func (c *Client) NewHoliday(ctx context.Context, holiday Holiday) (Holiday, error)

NewHoliday creates a new holiday.

func (*Client) NewJob

func (c *Client) NewJob(ctx context.Context, job Job) (Job, error)

NewJob creates a machine learning job and schedules a training.

func (*Client) NewOutlierDetector added in v0.4.0

func (c *Client) NewOutlierDetector(ctx context.Context, outlier OutlierDetector) (OutlierDetector, error)

NewOutlierDetector creates an outlier detector.

func (*Client) OutlierDetector added in v0.4.0

func (c *Client) OutlierDetector(ctx context.Context, id string) (OutlierDetector, error)

OutlierDetector fetches an existing outlier detector.

func (*Client) UpdateHoliday added in v0.3.0

func (c *Client) UpdateHoliday(ctx context.Context, holiday Holiday) (Holiday, error)

UpdateHoliday updates an existing holiday.

func (*Client) UpdateJob

func (c *Client) UpdateJob(ctx context.Context, job Job) (Job, error)

UpdateJob updates a machine learning job. A new training will be scheduled as part of updating.

func (*Client) UpdateOutlierDetector added in v0.4.0

func (c *Client) UpdateOutlierDetector(ctx context.Context, outlier OutlierDetector) (OutlierDetector, error)

UpdateOutlierDetector updates an outlier detector.

type Config

type Config struct {
	// BearerToken is an optional API key.
	BearerToken string
	// BasicAuth is optional basic auth credentials.
	BasicAuth *url.Userinfo
	// Client provides an optional HTTP client, otherwise a default will be used.
	Client *http.Client
	// NumRetries contains the number of attempted retries
	NumRetries int
}

Config contains client configuration.

type CustomPeriod added in v0.3.0

type CustomPeriod struct {
	// Name is the name of this period.
	Name string `json:"name"`
	// StartTime is the (inclusive) start of this period.
	StartTime time.Time `json:"startTime"`
	// EndTime is the (exclusive) end of this period.
	EndTime time.Time `json:"endTime"`
}

CustomPeriod is a single period to be included in a holiday.

type CustomPeriods added in v0.3.0

type CustomPeriods []CustomPeriod

CustomPeriods is a slice of CustomPeriods representing all occurrences of a holiday.

type ForecastParams added in v0.6.0

type ForecastParams struct {
	// Start is the start time of the forecast.
	Start time.Time `json:"start"`
	// End is the end time of the forecast.
	End time.Time `json:"end"`
	// Interval is the interval of the forecast.
	Interval uint `json:"interval"`
}

ForecastParams are parameters used in an ephemeral job prediction. Note this doesn't include `series` because users will not know the hash of the training's series in advance. Instead we enforce that ephemeral forecasts can only include a single series.

type ForecastRequest added in v0.6.0

type ForecastRequest struct {
	// Job is the specification of a job to run the forecast for.
	Job Job `json:"job"`
	// ForecastParams specify the start, end, and interval of the forecast.
	ForecastParams ForecastParams `json:"forecastParams"`
}

ForecastRequest is a request to run an ephemeral forecast.

type Holiday added in v0.3.0

type Holiday struct {
	ID string `json:"id,omitempty"`
	// Name is a human readable name for the holiday.
	Name string `json:"name"`
	// Description is a human readable description for the holiday.
	Description string `json:"description"`

	// ICalURL is the URL to an iCal file containing all occurrences
	// of the holiday.
	ICalURL *string `json:"iCalUrl,omitempty"`
	// ICalTimeZone is the timezone to use for 'All Day' events on the iCal in ICalURL,
	// if present. This is required because All Day events don't come with time zone information by default,
	// and there is no well-adhered-to standard for timezones of entire iCal files.
	ICalTimeZone *string `json:"iCalTimeZone,omitempty"`

	// CustomPeriods are holiday periods that are specified explicitly.
	CustomPeriods CustomPeriods `json:"customPeriods,omitempty"`

	// Jobs is a slice of IDs of Jobs that are using this holiday.
	// Requests may specify either IDs or names. Responses will always contain IDs.
	Jobs []string `json:"jobs"`
}

Holiday is a collection of time periods indicating where a time series will behave differently than normal.

A holiday may be specified using either an iCal (in which case both the ICalURL and ICalTimeZone fields must be provided), or using a set of custom time periods directly (in which case the CustomPeriods field must be provided).

Holidays can be linked to jobs in three ways: - at holiday creation time, if the job already exists, using the Jobs field - at job creation time, if the holiday already exists, using the Holidays field - using the LinkHolidaysToJob method on a Client.

type Job

type Job struct {
	ID string `json:"id,omitempty"`
	// Name is a human readable name for the job.
	Name string `json:"name"`
	// Metric is the metric name used to query the job. Must match Prometheus
	// naming requirements:
	// https://prometheus.io/docs/concepts/data_model/#metric-names-and-labels.
	Metric      string `json:"metric"`
	Description string `json:"description"`

	// GrafanaURL is the full URL to the Grafana instance. For example,
	// https://myinstance.grafana.net/.
	GrafanaURL string `json:"grafanaUrl"`
	// DatasourceID is the numeric ID of the datasource to query when training
	// data.
	DatasourceID uint `json:"datasourceId"`
	// DatasourceUID is the string UID of the datasource to query when training
	// data.
	DatasourceUID  string                 `json:"datasourceUid"`
	DatasourceType string                 `json:"datasourceType"`
	CustomLabels   map[string]interface{} `json:"customLabels"`
	QueryParams    map[string]interface{} `json:"queryParams"`
	// Interval is the data resolution in seconds.
	Interval uint `json:"interval"`
	// TrainingWindow is the lookback window to train on in seconds.
	TrainingWindow uint `json:"trainingWindow"`
	// TrainingFrequency is how often to re-train a model in seconds.
	TrainingFrequency uint `json:"trainingFrequency"`

	// Algorithm is the algorithm to use for machine learning.
	// https://grafana.com/docs/grafana-cloud/machine-learning/models/ contains
	// information on all supported algorithms.
	Algorithm string `json:"algorithm"`
	// HyperParams are the hyperparameters that can be specified. See
	// https://grafana.com/docs/grafana-cloud/machine-learning/models/ for the
	// various hyperparameters that can be changed.
	HyperParams map[string]interface{} `json:"hyperParams"`

	// Holidays is a slice of IDs or names of Holidays to be linked to this job.
	// Requests may specify either IDs or names. Responses will always contain IDs.
	Holidays []string `json:"holidays"`
}

Job is a job that will be scheduled.

type OutlierAlgorithm added in v0.4.0

type OutlierAlgorithm struct {
	Name        string                  `json:"name"`
	Sensitivity float64                 `json:"sensitivity"` // used by MAD
	Config      *OutlierAlgorithmConfig `json:"config"`      // used by DBSCAN
}

type OutlierAlgorithmConfig added in v0.4.0

type OutlierAlgorithmConfig struct {
	Epsilon float64 `json:"epsilon"`
}

type OutlierDetector added in v0.4.0

type OutlierDetector struct {
	ID string `json:"id,omitempty"`
	// Name is a human readable name for the outlier detector.
	Name string `json:"name"`
	// Metric is the metric name used to query the outlier detector. Must match Prometheus
	// naming requirements:
	// https://prometheus.io/docs/concepts/data_model/#metric-names-and-labels.
	Metric      string `json:"metric"`
	Description string `json:"description"`

	// GrafanaURL is the full URL to the Grafana instance. For example,
	// https://myinstance.grafana.net/.
	GrafanaURL string `json:"grafanaUrl"`
	// DatasourceID is the numeric ID of the datasource to query when training
	// data.
	DatasourceID uint `json:"datasourceId"`
	// DatasourceUID is the string UID of the datasource to query when training
	// data.
	DatasourceUID  string                 `json:"datasourceUid"`
	DatasourceType string                 `json:"datasourceType"`
	QueryParams    map[string]interface{} `json:"queryParams"`
	// Interval is the data resolution in seconds.
	Interval uint `json:"interval"`

	// Algorithm specifies the algorithm to use and its configuration. See
	// https://grafana.com/docs/grafana-cloud/machine-learning/outlier-detection/ for the
	// options.
	Algorithm OutlierAlgorithm `json:"algorithm"`
}

OutlierDetector defines an outlier detector instance

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL