From 9628eb59982b113a68f5e61cb46eab75516db0d2 Mon Sep 17 00:00:00 2001 From: Matt Bostock Date: Mon, 12 Sep 2016 20:29:44 +0100 Subject: [PATCH 1/2] PromQL: Add minute() function Returns the minutes from the current time in UTC. Related to the `hour()` function. Fixes #1983. --- promql/functions.go | 14 ++++++++++++++ promql/testdata/functions.test | 3 +++ 2 files changed, 17 insertions(+) diff --git a/promql/functions.go b/promql/functions.go index 61d56d4ca..41ff6fe7b 100644 --- a/promql/functions.go +++ b/promql/functions.go @@ -909,6 +909,13 @@ func funcHour(ev *evaluator, args Expressions) model.Value { }) } +// === minute(v vector) scalar === +func funcMinute(ev *evaluator, args Expressions) model.Value { + return dateWrapper(ev, args, func(t time.Time) model.SampleValue { + return model.SampleValue(t.Minute()) + }) +} + // === month(v vector) scalar === func funcMonth(ev *evaluator, args Expressions) model.Value { return dateWrapper(ev, args, func(t time.Time) model.SampleValue { @@ -1102,6 +1109,13 @@ var functions = map[string]*Function{ ReturnType: model.ValVector, Call: funcMinOverTime, }, + "minute": { + Name: "minute", + ArgTypes: []model.ValueType{model.ValVector}, + OptionalArgs: 1, + ReturnType: model.ValVector, + Call: funcMinute, + }, "month": { Name: "month", ArgTypes: []model.ValueType{model.ValVector}, diff --git a/promql/testdata/functions.test b/promql/testdata/functions.test index 595161e9f..f041ffadc 100644 --- a/promql/testdata/functions.test +++ b/promql/testdata/functions.test @@ -383,6 +383,9 @@ eval instant at 0m day_of_week() eval instant at 0m hour() {} 0 +eval instant at 0m minute() + {} 0 + # 2008-12-31 23:59:59 just before leap second. eval instant at 0m year(vector(1230767999)) {} 2008 From a0201036fae06ec0cf10da829ea005bc9e15d9d3 Mon Sep 17 00:00:00 2001 From: Matt Bostock Date: Mon, 12 Sep 2016 23:12:43 +0100 Subject: [PATCH 2/2] PromQL: Add tests for time/date funcs with arg Add tests for the date and time functions where an argument is specified. Suggested by @grobie: https://github.com/prometheus/prometheus/pull/1984#issuecomment-246508286 `1136239445` is the reference time used by Go: https://golang.org/src/time/format.go --- promql/testdata/functions.test | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/promql/testdata/functions.test b/promql/testdata/functions.test index f041ffadc..cfabb3c9b 100644 --- a/promql/testdata/functions.test +++ b/promql/testdata/functions.test @@ -370,22 +370,40 @@ clear eval instant at 0m year() {} 1970 +eval instant at 0m year(vector(1136239445)) + {} 2006 + eval instant at 0m month() {} 1 +eval instant at 0m month(vector(1136239445)) + {} 1 + eval instant at 0m day_of_month() {} 1 +eval instant at 0m day_of_month(vector(1136239445)) + {} 2 + # Thursday. eval instant at 0m day_of_week() {} 4 +eval instant at 0m day_of_week(vector(1136239445)) + {} 1 + eval instant at 0m hour() {} 0 +eval instant at 0m hour(vector(1136239445)) + {} 22 + eval instant at 0m minute() {} 0 +eval instant at 0m minute(vector(1136239445)) + {} 4 + # 2008-12-31 23:59:59 just before leap second. eval instant at 0m year(vector(1230767999)) {} 2008