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
- func String(param string) string
- type Query
- func (q Query) And(tag, comparison, value string) Query
- func (q Query) Build() string
- func (q Query) CumulativeSum(function funcType, fieldname string, aliases ...string) Query
- func (q Query) Do(url string) (*influx.Response, error)
- func (q Query) Field(fieldname, aggregationMethod string) Querydeprecated
- func (q Query) Fill(value string) Query
- func (q Query) GroupByTag(tag ...string) Query
- func (q Query) GroupByTime(duration time.Duration) Query
- func (q Query) Last(fieldname string, aliases ...string) Query
- func (q Query) LastPoint() Query
- func (q Query) Limit(limit int) Query
- func (q Query) Max(fieldname string, aliases ...string) Query
- func (q Query) Mean(fieldname string, aliases ...string) Query
- func (q Query) Median(fieldname string, aliases ...string) Query
- func (q Query) Min(fieldname string, aliases ...string) Query
- func (q Query) NonNegativeDerivative(function funcType, fieldname string, duration time.Duration, aliases ...string) Query
- func (q Query) On(measurement string) Query
- func (q Query) OnSubquery(subquery Query) Query
- func (q Query) Or(tag, comparison, value string) Query
- func (q Query) OrderByTime(direction orderDirection) Query
- func (q Query) Where(tag, comparison, value string) Query
Constants ¶
const ( LessThan = "<" LessOrEqual = "<=" Equal = "=" MoreThan = ">" MoreOrEqual = ">=" Different = "!=" )
InfluxDB comparison operators
const ( None = "none" Null = "null" Previous = "previous" Linear = "linear" )
InfluxDB fill options
const ( Ascending = "ASC" Descending = "DESC" )
InfluxDB order by directions
const ( MeanType funcType = "mean" MaxType funcType = "max" MedianType funcType = "median" )
funcType list
Variables ¶
This section is empty.
Functions ¶
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 ¶
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) CumulativeSum ¶ added in v1.1.0
CumulativeSum adds the field `cumulative_sum` to the query.
func (Query) Do ¶
Do actually executes the query to the specified InfluxDB instance hosted at the url argument.
func (Query) Field
deprecated
func (Query) Fill ¶
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 ¶
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 ¶
GroupByTime groups query results by a time interval. Calling it twice will take the latest duration provided.
func (Query) LastPoint ¶ added in v1.2.0
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 ¶
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) 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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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.