Merge pull request #1908 from prometheus/on-dates

Add various time and date functions
This commit is contained in:
Fabian Reinartz 2016-08-30 11:03:23 +02:00 committed by GitHub
commit ab88057063
2 changed files with 160 additions and 11 deletions

View file

@ -18,6 +18,7 @@ import (
"regexp"
"sort"
"strconv"
"time"
"github.com/prometheus/common/model"
@ -859,6 +860,69 @@ func funcVector(ev *evaluator, args Expressions) model.Value {
}
}
// Common code for date related functions.
func dateWrapper(ev *evaluator, args Expressions, f func(time.Time) model.SampleValue) model.Value {
var v vector
if len(args) == 0 {
v = vector{
&sample{
Metric: metric.Metric{},
Value: model.SampleValue(ev.Timestamp.Unix()),
},
}
} else {
v = ev.evalVector(args[0])
}
for _, el := range v {
el.Metric.Del(model.MetricNameLabel)
t := time.Unix(int64(el.Value), 0).UTC()
el.Value = f(t)
}
return v
}
// === days_in_month(v vector) scalar ===
func funcDaysInMonth(ev *evaluator, args Expressions) model.Value {
return dateWrapper(ev, args, func(t time.Time) model.SampleValue {
return model.SampleValue(32 - time.Date(t.Year(), t.Month(), 32, 0, 0, 0, 0, time.UTC).Day())
})
}
// === day_of_month(v vector) scalar ===
func funcDayOfMonth(ev *evaluator, args Expressions) model.Value {
return dateWrapper(ev, args, func(t time.Time) model.SampleValue {
return model.SampleValue(t.Day())
})
}
// === day_of_week(v vector) scalar ===
func funcDayOfWeek(ev *evaluator, args Expressions) model.Value {
return dateWrapper(ev, args, func(t time.Time) model.SampleValue {
return model.SampleValue(t.Weekday())
})
}
// === hour(v vector) scalar ===
func funcHour(ev *evaluator, args Expressions) model.Value {
return dateWrapper(ev, args, func(t time.Time) model.SampleValue {
return model.SampleValue(t.Hour())
})
}
// === month(v vector) scalar ===
func funcMonth(ev *evaluator, args Expressions) model.Value {
return dateWrapper(ev, args, func(t time.Time) model.SampleValue {
return model.SampleValue(t.Month())
})
}
// === year(v vector) scalar ===
func funcYear(ev *evaluator, args Expressions) model.Value {
return dateWrapper(ev, args, func(t time.Time) model.SampleValue {
return model.SampleValue(t.Year())
})
}
var functions = map[string]*Function{
"abs": {
Name: "abs",
@ -872,12 +936,6 @@ var functions = map[string]*Function{
ReturnType: model.ValVector,
Call: funcAbsent,
},
"increase": {
Name: "increase",
ArgTypes: []model.ValueType{model.ValMatrix},
ReturnType: model.ValVector,
Call: funcIncrease,
},
"avg_over_time": {
Name: "avg_over_time",
ArgTypes: []model.ValueType{model.ValMatrix},
@ -920,6 +978,27 @@ var functions = map[string]*Function{
ReturnType: model.ValScalar,
Call: funcCountScalar,
},
"days_in_month": {
Name: "days_in_month",
ArgTypes: []model.ValueType{model.ValVector},
OptionalArgs: 1,
ReturnType: model.ValVector,
Call: funcDaysInMonth,
},
"day_of_month": {
Name: "day_of_month",
ArgTypes: []model.ValueType{model.ValVector},
OptionalArgs: 1,
ReturnType: model.ValVector,
Call: funcDayOfMonth,
},
"day_of_week": {
Name: "day_of_week",
ArgTypes: []model.ValueType{model.ValVector},
OptionalArgs: 1,
ReturnType: model.ValVector,
Call: funcDayOfWeek,
},
"delta": {
Name: "delta",
ArgTypes: []model.ValueType{model.ValMatrix},
@ -962,11 +1041,12 @@ var functions = map[string]*Function{
ReturnType: model.ValVector,
Call: funcHoltWinters,
},
"irate": {
Name: "irate",
ArgTypes: []model.ValueType{model.ValMatrix},
ReturnType: model.ValVector,
Call: funcIrate,
"hour": {
Name: "hour",
ArgTypes: []model.ValueType{model.ValVector},
OptionalArgs: 1,
ReturnType: model.ValVector,
Call: funcHour,
},
"idelta": {
Name: "idelta",
@ -974,6 +1054,18 @@ var functions = map[string]*Function{
ReturnType: model.ValVector,
Call: funcIdelta,
},
"increase": {
Name: "increase",
ArgTypes: []model.ValueType{model.ValMatrix},
ReturnType: model.ValVector,
Call: funcIncrease,
},
"irate": {
Name: "irate",
ArgTypes: []model.ValueType{model.ValMatrix},
ReturnType: model.ValVector,
Call: funcIrate,
},
"label_replace": {
Name: "label_replace",
ArgTypes: []model.ValueType{model.ValVector, model.ValString, model.ValString, model.ValString, model.ValString},
@ -1010,6 +1102,13 @@ var functions = map[string]*Function{
ReturnType: model.ValVector,
Call: funcMinOverTime,
},
"month": {
Name: "month",
ArgTypes: []model.ValueType{model.ValVector},
OptionalArgs: 1,
ReturnType: model.ValVector,
Call: funcMonth,
},
"predict_linear": {
Name: "predict_linear",
ArgTypes: []model.ValueType{model.ValMatrix, model.ValScalar},
@ -1095,6 +1194,13 @@ var functions = map[string]*Function{
ReturnType: model.ValVector,
Call: funcVector,
},
"year": {
Name: "year",
ArgTypes: []model.ValueType{model.ValVector},
OptionalArgs: 1,
ReturnType: model.ValVector,
Call: funcYear,
},
}
// getFunction returns a predefined Function object for the given name.

View file

@ -363,3 +363,46 @@ eval instant at 1m quantile_over_time(2, data[1m])
{test="two samples"} +Inf
{test="three samples"} +Inf
{test="uneven samples"} +Inf
clear
# Test time-related functions.
eval instant at 0m year()
{} 1970
eval instant at 0m month()
{} 1
eval instant at 0m day_of_month()
{} 1
# Thursday.
eval instant at 0m day_of_week()
{} 4
eval instant at 0m hour()
{} 0
# 2008-12-31 23:59:59 just before leap second.
eval instant at 0m year(vector(1230767999))
{} 2008
# 2009-01-01 00:00:00 just after leap second.
eval instant at 0m year(vector(1230768000))
{} 2009
# 2016-02-29 23:59:59 Febuary 29th in leap year.
eval instant at 0m month(vector(1456790399)) + day_of_month(vector(1456790399)) / 100
{} 2.29
# 2016-03-01 00:00:00 March 1st in leap year.
eval instant at 0m month(vector(1456790400)) + day_of_month(vector(1456790400)) / 100
{} 3.01
# Febuary 1st 2016 in leap year.
eval instant at 0m days_in_month(vector(1454284800))
{} 29
# Febuary 1st 2017 not in leap year.
eval instant at 0m days_in_month(vector(1485907200))
{} 28