Class: Wiq::Commands::Metrics

Inherits:
Base
  • Object
show all
Defined in:
lib/wiq/commands/metrics.rb

Constant Summary collapse

NAMES =
%w[
  active_subscribers
  cancelled_subscriptions
  category_breakdown_net
  charge_avg
  charge_count
  gross_volume
  mrr
  net_volume
  new_subscriptions
  renewed_subscriptions
  revenue_per_subscriber
].freeze
CURRENCY_METRICS =
%w[
  gross_volume net_volume mrr charge_avg revenue_per_subscriber
  category_breakdown_net
].freeze
VALID_RANGES =
["today", "7d", "4w", "3m", "12m", "year to date", "custom"].freeze
VALID_INTERVALS =
%w[hourly daily weekly monthly].freeze

Instance Method Summary collapse

Methods inherited from Base

exit_on_failure?

Instance Method Details

#listObject



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/wiq/commands/metrics.rb', line 37

def list
  rows = NAMES.map do |n|
    { "name" => n, "currency" => CURRENCY_METRICS.include?(n) }
  end
  render_index(rows,
               summary: "Use `wiq metrics show <name>` to fetch a specific metric. " \
                        "Currency values are returned in integer cents.",
               breadcrumbs: [
                 { "cmd" => "wiq metrics show mrr", "description" => "Example: monthly recurring revenue" }
               ])
end

#show(name) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/wiq/commands/metrics.rb', line 84

def show(name)
  unless VALID_RANGES.include?(options[:range])
    raise Wiq::Error.new("Invalid --range #{options[:range].inspect}",
                         code: "invalid_range",
                         hint: "Valid: #{VALID_RANGES.join(", ")}")
  end

  params = {
    "range" => options[:range],
    "interval_group" => options[:interval]
  }
  if options[:range] == "custom"
    unless options[:start_date] && options[:end_date]
      raise Wiq::Error.new("--start-date and --end-date are required when --range=custom.",
                           code: "missing_custom_dates")
    end
    params["start_date"] = options[:start_date]
    params["end_date"] = options[:end_date]
  end
  params["location_id"] = options[:location] if options[:location]

  payload = client.get("/api/v1/metrics/#{name}", params)
  metrics = payload["metrics"] || {}

  data = {
    "name" => name,
    "currency" => CURRENCY_METRICS.include?(name),
    "primary_series" => metrics["primary_series"],
    "primary_total" => metrics["primary_total"]
  }
  unless options[:no_comparison]
    data["comparison_series"] = metrics["comparison_series"]
    data["comparison_total"] = metrics["comparison_total"]
  end

  meta = { "currency_unit" => CURRENCY_METRICS.include?(name) ? "cents" : "count" }
  meta["location_id"] = options[:location] if options[:location]

  render(data,
         summary: "metric=#{name} range=#{options[:range]} interval=#{options[:interval]}" \
                  "#{options[:location] ? " location=#{options[:location]}" : ""}",
         meta: meta,
         breadcrumbs: [
           { "cmd" => "wiq metrics list", "description" => "See all supported metrics" }
         ])
end