Class: Ably::Models::Stats

Inherits:
Object
  • Object
show all
Extended by:
Ably::Modules::Enum
Includes:
Ably::Modules::ModelCommon
Defined in:
lib/ably/models/stats.rb,
lib/ably/models/stats_types.rb

Overview

A class representing an individual statistic for a specified #interval_id

Defined Under Namespace

Classes: ConnectionTypes, IntegerDefaultZero, MessageCount, MessageTraffic, MessageTypes, RequestCount, ResourceCount, StatsStruct

Constant Summary collapse

GRANULARITY =

Describes the interval unit over which statistics are gathered.

MINUTE Interval unit over which statistics are gathered as minutes. HOUR Interval unit over which statistics are gathered as hours. DAY Interval unit over which statistics are gathered as days. MONTH Interval unit over which statistics are gathered as months.

ruby_enum('GRANULARITY',
  :minute,
  :hour,
  :day,
  :month
)
INTERVAL_FORMAT_STRING =
[
  '%Y-%m-%d:%H:%M',
  '%Y-%m-%d:%H',
  '%Y-%m-%d',
  '%Y-%m'
]

Instance Attribute Summary

Attributes included from Ably::Modules::ModelCommon

#hash

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Ably::Modules::ModelCommon

#==, #[], included, #to_json, #to_s

Methods included from Ably::Modules::MessagePack

#to_msgpack

Constructor Details

#initialize(hash_object) ⇒ Stats

Ably::Models::Stats initializer

Parameters:

  • hash_object (Hash)

    object with the underlying stat details



108
109
110
111
# File 'lib/ably/models/stats.rb', line 108

def initialize(hash_object)
  @raw_hash_object  = hash_object
  set_attributes_object hash_object
end

Class Method Details

.from_interval_id(interval_id) ⇒ Time

Returns the UTC 0 start Time of an interval_id

Examples:

Stats.from_interval_id('2015-01-01:10') # => 2015-01-01 10:00:00 +0000

Parameters:

  • interval_id (String)

Returns:

  • (Time)

    start time of the provided interval_id

Raises:

  • (ArgumentError)


72
73
74
75
76
77
78
79
# File 'lib/ably/models/stats.rb', line 72

def from_interval_id(interval_id)
  raise ArgumentError, 'Interval ID must be a string' unless interval_id.kind_of?(String)

  format = INTERVAL_FORMAT_STRING.find { |fmt| expected_length(fmt) == interval_id.length }
  raise ArgumentError, 'Interval ID is an invalid length' unless format

  Time.strptime("#{interval_id} +0000", "#{format} %z").utc
end

.granularity_from_interval_id(interval_id) ⇒ Symbol

Returns the Symbol determined from the interval_id

Examples:

Stats.granularity_from_interval_id('2015-01-01:10') # => :hour

Parameters:

  • interval_id (String)

Returns:

  • (Symbol)

Raises:

  • (ArgumentError)


89
90
91
92
93
94
95
96
# File 'lib/ably/models/stats.rb', line 89

def granularity_from_interval_id(interval_id)
  raise ArgumentError, 'Interval ID must be a string' unless interval_id.kind_of?(String)

  format = INTERVAL_FORMAT_STRING.find { |fmt| expected_length(fmt) == interval_id.length }
  raise ArgumentError, 'Interval ID is an invalid length' unless format

  GRANULARITY[INTERVAL_FORMAT_STRING.index(format)]
end

.to_interval_id(time, granularity) ⇒ String

Convert a Time with the specified Granularity into an interval ID based on UTC 0 time

Examples:

Stats.to_interval_id(Time.now, :hour) # => '2015-01-01:10'

Parameters:

  • time (Time)

    Time used to determine the interval

  • granularity (GRANULARITY)

    Granularity of the metrics such as :hour, :day

Returns:

  • (String)

    interval ID used for stats

Raises:

  • (ArgumentError)


55
56
57
58
59
60
61
62
# File 'lib/ably/models/stats.rb', line 55

def to_interval_id(time, granularity)
  raise ArgumentError, 'Time object required as first argument' unless time.kind_of?(Time)

  granularity = GRANULARITY(granularity)
  format = INTERVAL_FORMAT_STRING[granularity.to_i]

  time.utc.strftime(format)
end

Instance Method Details

#allStats::MessageTypes

A MessageTypes object containing the aggregate count of all message stats.

Returns:



119
120
121
# File 'lib/ably/models/stats.rb', line 119

def all
  @all ||= Stats::MessageTypes.new(attributes[:all])
end

#api_requestsAbly::Models::Stats::RequestCount

A RequestCount object containing a breakdown of API Requests.



179
180
181
# File 'lib/ably/models/stats.rb', line 179

def api_requests
  @api_requests ||= Stats::RequestCount.new(attributes[:api_requests])
end

#as_json(*args) ⇒ Object



229
230
231
# File 'lib/ably/models/stats.rb', line 229

def as_json(*args)
  attributes.as_json(*args).reject { |key, val| val.nil? }
end

#attributesObject



225
226
227
# File 'lib/ably/models/stats.rb', line 225

def attributes
  @attributes
end

#channelsAbly::Models::Stats::ResourceCount

A ResourceCount object containing a breakdown of connection related stats, such as min, mean and peak connections.



169
170
171
# File 'lib/ably/models/stats.rb', line 169

def channels
  @channels ||= Stats::ResourceCount.new(attributes[:channels])
end

#connectionsAbly::Models::Stats::ConnectionTypes

A ConnectionTypes object containing a breakdown of connection related stats, such as min, mean and peak connections.



159
160
161
# File 'lib/ably/models/stats.rb', line 159

def connections
  @connections ||= Stats::ConnectionTypes.new(attributes[:connections])
end

#inboundAbly::Models::Stats::MessageTraffic

A MessageTraffic object containing the aggregate count of inbound message stats.



129
130
131
# File 'lib/ably/models/stats.rb', line 129

def inbound
  @inbound ||= Stats::MessageTraffic.new(attributes[:inbound])
end

#interval_granularityGRANULARITY

The length of the interval the stats span. Values will be a [StatsIntervalGranularity]StatsIntervalGranularity.

Returns:

  • (GRANULARITY)

    The granularity of the interval for the stat such as :day, :hour, :minute, see GRANULARITY



221
222
223
# File 'lib/ably/models/stats.rb', line 221

def interval_granularity
  self.class.granularity_from_interval_id(interval_id)
end

#interval_idString

The UTC time at which the time period covered begins. If unit is set to minute this will be in the format YYYY-mm-dd:HH:MM, if hour it will be YYYY-mm-dd:HH, if day it will be YYYY-mm-dd:00 and if month it will be YYYY-mm-01:00.

Returns:

  • (String)


201
202
203
# File 'lib/ably/models/stats.rb', line 201

def interval_id
  attributes.fetch(:interval_id)
end

#interval_timeTime

Represents the intervalId as a time object.

Returns:

  • (Time)


211
212
213
# File 'lib/ably/models/stats.rb', line 211

def interval_time
  self.class.from_interval_id(interval_id)
end

#outboundAbly::Models::Stats::MessageTraffic

A MessageTraffic object containing the aggregate count of outbound message stats.



139
140
141
# File 'lib/ably/models/stats.rb', line 139

def outbound
  @outbound ||= Stats::MessageTraffic.new(attributes[:outbound])
end

#persistedAbly::Models::Stats::MessageTraffic

A MessageTraffic object containing the aggregate count of persisted message stats.



149
150
151
# File 'lib/ably/models/stats.rb', line 149

def persisted
  @persisted ||= Stats::MessageTypes.new(attributes[:persisted])
end

#token_requestsAbly::Models::Stats::RequestCount

A RequestCount object containing a breakdown of Ably Token requests.



189
190
191
# File 'lib/ably/models/stats.rb', line 189

def token_requests
  @token_requests ||= Stats::RequestCount.new(attributes[:token_requests])
end