lib/gladys/time_series/counter.rb
# frozen_string_literal: true module Gladys module TimeSeries class Counter attr_reader :values def initialize @values = Concurrent::Hash.new end def increment key = hash_key @values[key] ||= 0 @values[key] += 1 end # Get the current value for the current time in the time series. def current @values[hash_key] || 0 end def any? @values.values.any?(&:positive?) end def between(start:, finish:) return @values.values if start.nil? || finish.nil? start = start.utc.to_i finish = finish.utc.to_i result = {} (start...finish).map do |time| result[time - start] = 0 end @values.each do |key, value| result[key - start] += value if result[key - start] end result end private def hash_key Time.now.utc.to_i end end end end