diff --git a/docs/querying/functions.md b/docs/querying/functions.md index 1de8bb2aa..94bd48632 100644 --- a/docs/querying/functions.md +++ b/docs/querying/functions.md @@ -103,6 +103,12 @@ for each of the given times in UTC. Returned values are from 1 to 31. each of the given times in UTC. Returned values are from 0 to 6, where 0 means Sunday etc. +## `day_of_year()` + +`day_of_year(v=vector(time()) instant-vector)` returns the day of the year for +each of the given times in UTC. Returned values are from 1 to 365 for non-leap years, +and 1 to 366 in leap years. + ## `days_in_month()` `days_in_month(v=vector(time()) instant-vector)` returns number of days in the diff --git a/promql/functions.go b/promql/functions.go index 423c33f66..0b3a04251 100644 --- a/promql/functions.go +++ b/promql/functions.go @@ -1038,6 +1038,13 @@ func funcDayOfWeek(vals []parser.Value, args parser.Expressions, enh *EvalNodeHe }) } +// === day_of_year(v Vector) Scalar === +func funcDayOfYear(vals []parser.Value, args parser.Expressions, enh *EvalNodeHelper) Vector { + return dateWrapper(vals, enh, func(t time.Time) float64 { + return float64(t.YearDay()) + }) +} + // === hour(v Vector) Scalar === func funcHour(vals []parser.Value, args parser.Expressions, enh *EvalNodeHelper) Vector { return dateWrapper(vals, enh, func(t time.Time) float64 { @@ -1089,6 +1096,7 @@ var FunctionCalls = map[string]FunctionCall{ "days_in_month": funcDaysInMonth, "day_of_month": funcDayOfMonth, "day_of_week": funcDayOfWeek, + "day_of_year": funcDayOfYear, "deg": funcDeg, "delta": funcDelta, "deriv": funcDeriv, @@ -1143,7 +1151,7 @@ var FunctionCalls = map[string]FunctionCall{ // that can also change with change in eval time. var AtModifierUnsafeFunctions = map[string]struct{}{ // Step invariant functions. - "days_in_month": {}, "day_of_month": {}, "day_of_week": {}, + "days_in_month": {}, "day_of_month": {}, "day_of_week": {}, "day_of_year": {}, "hour": {}, "minute": {}, "month": {}, "year": {}, "predict_linear": {}, "time": {}, // Uses timestamp of the argument for the result, diff --git a/promql/parser/functions.go b/promql/parser/functions.go index c4bc5ddb8..92afff8b2 100644 --- a/promql/parser/functions.go +++ b/promql/parser/functions.go @@ -132,6 +132,12 @@ var Functions = map[string]*Function{ Variadic: 1, ReturnType: ValueTypeVector, }, + "day_of_year": { + Name: "day_of_year", + ArgTypes: []ValueType{ValueTypeVector}, + Variadic: 1, + ReturnType: ValueTypeVector, + }, "deg": { Name: "deg", ArgTypes: []ValueType{ValueTypeVector}, diff --git a/promql/testdata/functions.test b/promql/testdata/functions.test index b216c42c7..02e6a3219 100644 --- a/promql/testdata/functions.test +++ b/promql/testdata/functions.test @@ -704,6 +704,12 @@ eval instant at 0m day_of_month() eval instant at 0m day_of_month(vector(1136239445)) {} 2 +eval instant at 0m day_of_year() + {} 1 + +eval instant at 0m day_of_year(vector(1136239445)) + {} 2 + # Thursday. eval instant at 0m day_of_week() {} 4 @@ -739,6 +745,14 @@ eval instant at 0m month(vector(1456790399)) + day_of_month(vector(1456790399)) eval instant at 0m month(vector(1456790400)) + day_of_month(vector(1456790400)) / 100 {} 3.01 +# 2016-12-31 13:37:00 366th day in leap year. +eval instant at 0m day_of_year(vector(1483191420)) + {} 366 + +# 2022-12-31 13:37:00 365th day in non-leap year. +eval instant at 0m day_of_year(vector(1672493820)) + {} 365 + # February 1st 2016 in leap year. eval instant at 0m days_in_month(vector(1454284800)) {} 29 diff --git a/web/ui/module/codemirror-promql/src/complete/promql.terms.ts b/web/ui/module/codemirror-promql/src/complete/promql.terms.ts index 5b2da2088..f793ddbd2 100644 --- a/web/ui/module/codemirror-promql/src/complete/promql.terms.ts +++ b/web/ui/module/codemirror-promql/src/complete/promql.terms.ts @@ -177,6 +177,12 @@ export const functionIdentifierTerms = [ info: 'Return the day of the week for provided timestamps', type: 'function', }, + { + label: 'day_of_year', + detail: 'function', + info: 'Return the day of the year for provided timestamps', + type: 'function', + }, { label: 'deg', detail: 'function', diff --git a/web/ui/module/codemirror-promql/src/promql.ts b/web/ui/module/codemirror-promql/src/promql.ts index d9b573071..e20a15c97 100644 --- a/web/ui/module/codemirror-promql/src/promql.ts +++ b/web/ui/module/codemirror-promql/src/promql.ts @@ -35,7 +35,7 @@ export function promQLLanguage(top: LanguageType): LRLanguage { StringLiteral: tags.string, NumberLiteral: tags.number, Duration: tags.number, - 'Abs Absent AbsentOverTime Acos Acosh Asin Asinh Atan Atanh AvgOverTime Ceil Changes Clamp ClampMax ClampMin Cos Cosh CountOverTime DaysInMonth DayOfMonth DayOfWeek Deg Delta Deriv Exp Floor HistogramQuantile HoltWinters Hour Idelta Increase Irate LabelReplace LabelJoin LastOverTime Ln Log10 Log2 MaxOverTime MinOverTime Minute Month Pi PredictLinear PresentOverTime QuantileOverTime Rad Rate Resets Round Scalar Sgn Sin Sinh Sort SortDesc Sqrt StddevOverTime StdvarOverTime SumOverTime Tan Tanh Time Timestamp Vector Year': + 'Abs Absent AbsentOverTime Acos Acosh Asin Asinh Atan Atanh AvgOverTime Ceil Changes Clamp ClampMax ClampMin Cos Cosh CountOverTime DaysInMonth DayOfMonth DayOfWeek DayOfYear Deg Delta Deriv Exp Floor HistogramQuantile HoltWinters Hour Idelta Increase Irate LabelReplace LabelJoin LastOverTime Ln Log10 Log2 MaxOverTime MinOverTime Minute Month Pi PredictLinear PresentOverTime QuantileOverTime Rad Rate Resets Round Scalar Sgn Sin Sinh Sort SortDesc Sqrt StddevOverTime StdvarOverTime SumOverTime Tan Tanh Time Timestamp Vector Year': tags.function(tags.variableName), 'Avg Bottomk Count Count_values Group Max Min Quantile Stddev Stdvar Sum Topk': tags.operatorKeyword, 'By Without Bool On Ignoring GroupLeft GroupRight Offset Start End': tags.modifier, diff --git a/web/ui/module/codemirror-promql/src/types/function.ts b/web/ui/module/codemirror-promql/src/types/function.ts index 2c8723f8c..9f99d359d 100644 --- a/web/ui/module/codemirror-promql/src/types/function.ts +++ b/web/ui/module/codemirror-promql/src/types/function.ts @@ -32,6 +32,7 @@ import { CountOverTime, DayOfMonth, DayOfWeek, + DayOfYear, DaysInMonth, Deg, Delta, @@ -224,6 +225,12 @@ const promqlFunctions: { [key: number]: PromQLFunction } = { variadic: 1, returnType: ValueType.vector, }, + [DayOfYear]: { + name: 'day_of_year', + argTypes: [ValueType.vector], + variadic: 1, + returnType: ValueType.vector, + }, [Deg]: { name: 'deg', argTypes: [ValueType.vector], diff --git a/web/ui/module/lezer-promql/src/promql.grammar b/web/ui/module/lezer-promql/src/promql.grammar index 642d25f10..8efc9a8a1 100644 --- a/web/ui/module/lezer-promql/src/promql.grammar +++ b/web/ui/module/lezer-promql/src/promql.grammar @@ -140,6 +140,7 @@ FunctionIdentifier { DaysInMonth | DayOfMonth | DayOfWeek | + DayOfYear | Deg | Delta | Deriv | @@ -378,6 +379,7 @@ NumberLiteral { DaysInMonth { condFn<"days_in_month"> } DayOfMonth { condFn<"day_of_month"> } DayOfWeek { condFn<"day_of_week"> } + DayOfYear { condFn<"day_of_year"> } Deg { condFn<"deg"> } Delta { condFn<"delta"> } Deriv { condFn<"deriv"> }