Trifle::Stats / Usage / Get series
Learn how to work with series of values.

Get series

Getting series behaves same as getting values. The twist comes that series is values wrapped into an object that allows you to work further with transponders, aggregators and formatters.

series(key: String, from: Time, to: Time, range: Symbol, **options)

  • key - string identifier for the metrics
  • from - timestamp you want to get values from
  • to - timestamp you want to get values to
  • range - specific range you want to get values in
  • options - hash of optional arguments:
    • config - optional configuration instance of Trifle::Stats::Configuration. It defaults to global configuration, otherwise uses passed in configuration.

Using from and to gives you flexibility to exactly specify what data you are interested in.

Use range to specify sensitivity of returned data (aka how many data points you want to get). For example if you're trying to get daily values for current a specific month, use range: :day. This will give you ~30 data points. If you need to increase your sensitivity for chart, you may use range: :hour which will give you ~30 * 24 data points.

The more data you're fetching, the slower the response time. Thats a trade-off you need to decide.

Here is an example how to get today values for specific key.

series = Trifle::Stats.series(key: 'event::logs', from:, to:, range: :day)
=> #<Trifle::Stats::Series:0x0000ffffa14256e8 @series={:at=>[2021-01-25 00:00:00 +0200], :values=>[{"count"=>3, "duration"=>8, "lines"=>658}]}>
series.aggregate.sum(path: 'count')
=> 3

And here is another how to get daily values for a last 30 days for specific key.

series = Trifle::Stats.series(key: 'event::logs', from: - 60 * 60 * 24 * 30, to:, range: :day)
=> #<Trifle::Stats::Series:0x0000ffffa14256e8 @series={:at=>[2021-01-25 00:00:00 +0200, 2021-01-24 00:00:00 +0200, ...], values: [{"count"=>3, "duration"=>8, "lines"=>658}, {"count"=>1, "duration"=>3, "lines"=>311}, ...]}>
series.aggregate.sum(path: 'count')
=> 432

If you're integrating with Rails, don't forget you can use things like 1.month.ago or - 30.days.