influx

package module
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: Jun 9, 2022 License: MIT Imports: 6 Imported by: 1

README

Package influx v1.2.1

The packge influx contains a query builder for the time series database InfluxDB.

Documentation

Overview

Package influx provides a query builder to ease the writing of query to InfluxDB with the InfluxQL language.

After instantiating a new query object with NewQuery, call the different methods to adapt the query to fit your needs. You can call Build to get the InfluxQL query in a string form. Eventually call Do to actually execute the query.

Example Usage

package models

func (a *Alert) InfluxQuery() influx.Query {
 	query := influx.NewQuery()

 	query = query.Where("time", influx.LessThan, "now() - 1m").And("time", influx.MoreThan, "now() - 5m")
 	query = query.And("application_id", influx.Equal, influx.String(a.AppID))
 	query = query.GroupByTime(5*time.Minute).Fill(influx.None).OrderBy("time", "DESC").Limit(1)

 	if a.IsRouterMetric() {
 		query = query.On("router").Field("value", "mean")
 		if a.Metric == metric.MetricRouterP95ResponseTime {
 			query = query.And("type", influx.Equal, influx.String("p95"))
 		}
 	} else {
 		query.And("container_type", influx.Equal, influx.String(a.ContainerType))
 		query = query.On(a.Metric).Field("value", "mean").Field("limit", "last")
 		query = query.GroupByTag("container_type").GroupByTag("container_index")
 	}
 	return query
 }

Index

Constants

View Source
const (
	LessThan    = "<"
	LessOrEqual = "<="
	Equal       = "="
	MoreThan    = ">"
	MoreOrEqual = ">="
	Different   = "!="
)

InfluxDB comparison operators

View Source
const (
	None     = "none"
	Null     = "null"
	Previous = "previous"
	Linear   = "linear"
)

InfluxDB fill options

View Source
const (
	Ascending  = "ASC"
	Descending = "DESC"
)

InfluxDB order by directions

View Source
const (
	MeanType   funcType = "mean"
	MaxType    funcType = "max"
	MedianType funcType = "median"
)

funcType list

Variables

This section is empty.

Functions

func String

func String(param string) string

String returns the parameter surround with single quote. You should use this method when dealing with values of conditions (third parameter of Where, And and Or methods).

Types

type Query

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

Query is the main structure for the query builder. All methods apply to it.

func NewQuery

func NewQuery() Query

NewQuery makes a new Query object. You MUST use this method to instantiate a new Query structure.

func (Query) And

func (q Query) And(tag, comparison, value string) Query

And add a condition to the query separated from the previous with AND.

The value parameter must be surrounded with single quote if it does not represent a number. You can use the influx.String method to add these.

func (Query) Build

func (q Query) Build() string

Build constructs the InfluxQL query in a string form.

func (Query) CumulativeSum added in v1.1.0

func (q Query) CumulativeSum(function funcType, fieldname string, aliases ...string) Query

CumulativeSum adds the field `cumulative_sum` to the query.

func (Query) Do

func (q Query) Do(url string) (*influx.Response, error)

Do actually executes the query to the specified InfluxDB instance hosted at the url argument.

func (Query) Field deprecated

func (q Query) Field(fieldname, aggregationMethod string) Query

Deprecated: instead use the individual functions. Field adds the given field to the list of fields with the given aggregation method applied. It is possible to add multiple fields with the same name but is highly discouraged.

func (Query) Fill

func (q Query) Fill(value string) Query

Fill sets the fill behaviour of the current query. Calling it twice will take the latest fill provided. It changes the value reported for time intervals that have no data.

func (Query) GroupByTag

func (q Query) GroupByTag(tag ...string) Query

GroupByTag groups query results by a user-specified set of tags. Every call to this method adds the given slice of tags to the existing set of tags.

func (Query) GroupByTime

func (q Query) GroupByTime(duration time.Duration) Query

GroupByTime groups query results by a time interval. Calling it twice will take the latest duration provided.

func (Query) Last added in v1.1.0

func (q Query) Last(fieldname string, aliases ...string) Query

Last adds the field `last` to the query.

func (Query) LastPoint added in v1.2.0

func (q Query) LastPoint() Query

LastPoint limits the query to return only the last element. It sets a `ORDER BY` to the query and a `LIMIT 1`.

func (Query) Limit

func (q Query) Limit(limit int) Query

Limit sets the limit of the current query. Calling it twice will take the latest limit provided. It limits the number of points returned by the query.

func (Query) Max added in v1.1.0

func (q Query) Max(fieldname string, aliases ...string) Query

Max adds the field `max` to the query.

func (Query) Mean added in v1.1.0

func (q Query) Mean(fieldname string, aliases ...string) Query

Mean adds the field `mean` to the query.

func (Query) Median added in v1.1.0

func (q Query) Median(fieldname string, aliases ...string) Query

Median adds the field `median` to the query.

func (Query) Min added in v1.1.0

func (q Query) Min(fieldname string, aliases ...string) Query

Min adds the field `min` to the query.

func (Query) NonNegativeDerivative added in v1.1.0

func (q Query) NonNegativeDerivative(function funcType, fieldname string, duration time.Duration, aliases ...string) Query

NonNegativeDerivative adds the field `non_negative_derivative` to the query.

func (Query) On

func (q Query) On(measurement string) Query

On sets the measurement of the current query. Calling it twice will take the latest measurement provided. Calling it after a call to OnSubqueries will take this measurement over the subquery.

func (Query) OnSubquery

func (q Query) OnSubquery(subquery Query) Query

OnSubquery sets a subquery instead of a measurement. Calling it twice will take the latest subquery provided. Calling it after a call to On will take this subquery over the measurement.

func (Query) Or

func (q Query) Or(tag, comparison, value string) Query

Or add a condition to the query separated from the previous with OR.

The value parameter must be surrounded with single quote if it does not represent a number. You can use the influx.String method to add these.

func (Query) OrderByTime

func (q Query) OrderByTime(direction orderDirection) Query

OrderByTime modifies the sorting of the result. By default, InfluxDB returns results in ascending time order; the first point returned has the oldest timestamp and the last point returned has the most recent timestamp. Calling this method with "DESC" reverses that order such that InfluxDB returns the points with the most recent timestamps first.

func (Query) Where

func (q Query) Where(tag, comparison, value string) Query

Where adds a condition on tag to the query. The comparison argument must be one of the constants defined in this package. Where must be called for the first condition. Calling it twice would remove all the previously registered conditions with And and Or.

The value parameter must be surrounded with single quote if it does not represent a number. You can use the influx.String method to add these.

Jump to

Keyboard shortcuts

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