Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added rateofchangesec operator to calculate change of value per second #1259

Merged
merged 4 commits into from
Jun 27, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)