Skip to content

Commit

Permalink
Merge pull request #1259 from art-vasilyev/rateofchangesec-operator
Browse files Browse the repository at this point in the history
Added rateofchangesec operator to calculate change of value per second
  • Loading branch information
mergify[bot] authored Jun 27, 2022
2 parents 39d48dd + 980b691 commit b83c4a8
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
14 changes: 13 additions & 1 deletion gnocchi/rest/aggregates/operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,20 @@ def _inner_rated_agg(values, axis):
}


def rateofchangesec(t, v):
"""Calculate change of value per second."""
values = numpy.divide(
numpy.diff(v.T, prepend=numpy.NaN),
numpy.divide(
numpy.diff(t, prepend=numpy.datetime64("NaT")),
numpy.timedelta64(1, 's'))
).T
return t, values


unary_operators_with_timestamps = {
u"rateofchange": lambda t, v: (t[1:], numpy.diff(v.T).T)
u"rateofchange": lambda t, v: (t[1:], numpy.diff(v.T).T),
u"rateofchangesec": rateofchangesec,
}


Expand Down
29 changes: 29 additions & 0 deletions gnocchi/tests/test_aggregates.py
Original file line number Diff line number Diff line change
Expand Up @@ -1642,3 +1642,32 @@ def test_unary_operator(self):
numpy.timedelta64(1, 'h'), 4),
]}
}, values)

def test_rateofchangesec_operator(self):
self.incoming.add_measures(self.metric.id, [
incoming.Measure(datetime64(2014, 1, 1, 12, 0, 1), 0),
incoming.Measure(datetime64(2014, 1, 1, 12, 5, 10), 450),
incoming.Measure(datetime64(2014, 1, 1, 12, 10, 5), 900),
incoming.Measure(datetime64(2014, 1, 1, 12, 15, 30), 1350),
])
self.trigger_processing([self.metric])

values = processor.get_measures(
self.storage,
[processor.MetricReference(self.metric, "mean")],
["rateofchangesec", ["metric", str(self.metric.id), "mean"]],
granularities=[numpy.timedelta64(5, 'm')],
)

self.assertEqual({
str(self.metric.id): {
"mean": [(datetime64(2014, 1, 1, 12, 0, 0),
numpy.timedelta64(5, 'm'), eq_nan),
(datetime64(2014, 1, 1, 12, 5, 0),
numpy.timedelta64(5, 'm'), 1.5),
(datetime64(2014, 1, 1, 12, 10, 0),
numpy.timedelta64(5, 'm'), 1.5),
(datetime64(2014, 1, 1, 12, 15, 0),
numpy.timedelta64(5, 'm'), 1.5),
]}
}, values)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
features:
- |
Added ``rateofchangesec`` operator to calculate changes between metric measures per second.
Example: ``(* (/ (rateofchangesec (metric cpu mean)) 1000000000) 100))``

0 comments on commit b83c4a8

Please sign in to comment.