Adds day_of_year function to PromQL

Signed-off-by: Ivo Gosemann <ivo.gosemann@sap.com>
This commit is contained in:
Ivo Gosemann 2022-05-20 12:24:19 +02:00 committed by Julien Pivotto
parent 89de30a0b7
commit e22b54e253
8 changed files with 51 additions and 2 deletions

View file

@ -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 each of the given times in UTC. Returned values are from 0 to 6, where 0 means
Sunday etc. 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()`
`days_in_month(v=vector(time()) instant-vector)` returns number of days in the `days_in_month(v=vector(time()) instant-vector)` returns number of days in the

View file

@ -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 === // === hour(v Vector) Scalar ===
func funcHour(vals []parser.Value, args parser.Expressions, enh *EvalNodeHelper) Vector { func funcHour(vals []parser.Value, args parser.Expressions, enh *EvalNodeHelper) Vector {
return dateWrapper(vals, enh, func(t time.Time) float64 { return dateWrapper(vals, enh, func(t time.Time) float64 {
@ -1089,6 +1096,7 @@ var FunctionCalls = map[string]FunctionCall{
"days_in_month": funcDaysInMonth, "days_in_month": funcDaysInMonth,
"day_of_month": funcDayOfMonth, "day_of_month": funcDayOfMonth,
"day_of_week": funcDayOfWeek, "day_of_week": funcDayOfWeek,
"day_of_year": funcDayOfYear,
"deg": funcDeg, "deg": funcDeg,
"delta": funcDelta, "delta": funcDelta,
"deriv": funcDeriv, "deriv": funcDeriv,
@ -1143,7 +1151,7 @@ var FunctionCalls = map[string]FunctionCall{
// that can also change with change in eval time. // that can also change with change in eval time.
var AtModifierUnsafeFunctions = map[string]struct{}{ var AtModifierUnsafeFunctions = map[string]struct{}{
// Step invariant functions. // 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": {}, "hour": {}, "minute": {}, "month": {}, "year": {},
"predict_linear": {}, "time": {}, "predict_linear": {}, "time": {},
// Uses timestamp of the argument for the result, // Uses timestamp of the argument for the result,

View file

@ -132,6 +132,12 @@ var Functions = map[string]*Function{
Variadic: 1, Variadic: 1,
ReturnType: ValueTypeVector, ReturnType: ValueTypeVector,
}, },
"day_of_year": {
Name: "day_of_year",
ArgTypes: []ValueType{ValueTypeVector},
Variadic: 1,
ReturnType: ValueTypeVector,
},
"deg": { "deg": {
Name: "deg", Name: "deg",
ArgTypes: []ValueType{ValueTypeVector}, ArgTypes: []ValueType{ValueTypeVector},

View file

@ -704,6 +704,12 @@ eval instant at 0m day_of_month()
eval instant at 0m day_of_month(vector(1136239445)) eval instant at 0m day_of_month(vector(1136239445))
{} 2 {} 2
eval instant at 0m day_of_year()
{} 1
eval instant at 0m day_of_year(vector(1136239445))
{} 2
# Thursday. # Thursday.
eval instant at 0m day_of_week() eval instant at 0m day_of_week()
{} 4 {} 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 eval instant at 0m month(vector(1456790400)) + day_of_month(vector(1456790400)) / 100
{} 3.01 {} 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. # February 1st 2016 in leap year.
eval instant at 0m days_in_month(vector(1454284800)) eval instant at 0m days_in_month(vector(1454284800))
{} 29 {} 29

View file

@ -177,6 +177,12 @@ export const functionIdentifierTerms = [
info: 'Return the day of the week for provided timestamps', info: 'Return the day of the week for provided timestamps',
type: 'function', type: 'function',
}, },
{
label: 'day_of_year',
detail: 'function',
info: 'Return the day of the year for provided timestamps',
type: 'function',
},
{ {
label: 'deg', label: 'deg',
detail: 'function', detail: 'function',

View file

@ -35,7 +35,7 @@ export function promQLLanguage(top: LanguageType): LRLanguage {
StringLiteral: tags.string, StringLiteral: tags.string,
NumberLiteral: tags.number, NumberLiteral: tags.number,
Duration: 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), tags.function(tags.variableName),
'Avg Bottomk Count Count_values Group Max Min Quantile Stddev Stdvar Sum Topk': tags.operatorKeyword, '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, 'By Without Bool On Ignoring GroupLeft GroupRight Offset Start End': tags.modifier,

View file

@ -32,6 +32,7 @@ import {
CountOverTime, CountOverTime,
DayOfMonth, DayOfMonth,
DayOfWeek, DayOfWeek,
DayOfYear,
DaysInMonth, DaysInMonth,
Deg, Deg,
Delta, Delta,
@ -224,6 +225,12 @@ const promqlFunctions: { [key: number]: PromQLFunction } = {
variadic: 1, variadic: 1,
returnType: ValueType.vector, returnType: ValueType.vector,
}, },
[DayOfYear]: {
name: 'day_of_year',
argTypes: [ValueType.vector],
variadic: 1,
returnType: ValueType.vector,
},
[Deg]: { [Deg]: {
name: 'deg', name: 'deg',
argTypes: [ValueType.vector], argTypes: [ValueType.vector],

View file

@ -140,6 +140,7 @@ FunctionIdentifier {
DaysInMonth | DaysInMonth |
DayOfMonth | DayOfMonth |
DayOfWeek | DayOfWeek |
DayOfYear |
Deg | Deg |
Delta | Delta |
Deriv | Deriv |
@ -378,6 +379,7 @@ NumberLiteral {
DaysInMonth { condFn<"days_in_month"> } DaysInMonth { condFn<"days_in_month"> }
DayOfMonth { condFn<"day_of_month"> } DayOfMonth { condFn<"day_of_month"> }
DayOfWeek { condFn<"day_of_week"> } DayOfWeek { condFn<"day_of_week"> }
DayOfYear { condFn<"day_of_year"> }
Deg { condFn<"deg"> } Deg { condFn<"deg"> }
Delta { condFn<"delta"> } Delta { condFn<"delta"> }
Deriv { condFn<"deriv"> } Deriv { condFn<"deriv"> }