Class: Fastlane::Helper::AppSizeMetricsHelper
- Inherits:
-
Object
- Object
- Fastlane::Helper::AppSizeMetricsHelper
- Defined in:
- lib/fastlane/plugin/wpmreleasetoolkit/helper/app_size_metrics_helper.rb
Overview
A helper class to build an App Size Metrics payload and send it to a server (or write it to disk)
The payload generated (and sent) by this helper conforms to the API for grouped metrics described in github.com/Automattic/apps-metrics
Instance Method Summary collapse
-
#add_metric(name:, value:, metadata: nil) ⇒ Object
Adds a single metric to the group of metrics.
-
#initialize(metadata = {}) ⇒ AppSizeMetricsHelper
constructor
A new instance of AppSizeMetricsHelper.
-
#metadata=(hash) ⇒ Object
Sets the metadata common to the whole group of metrics in the payload being built by this helper instance.
-
#send_metrics(to:, api_token:, use_gzip: true) ⇒ Integer
Send the metrics to the given App Metrics endpoint.
- #to_h ⇒ Object
Constructor Details
#initialize(metadata = {}) ⇒ AppSizeMetricsHelper
Returns a new instance of AppSizeMetricsHelper.
15 16 17 18 |
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/app_size_metrics_helper.rb', line 15 def initialize( = {}) self. = @metrics = [] end |
Instance Method Details
#add_metric(name:, value:, metadata: nil) ⇒ Object
Adds a single metric to the group of metrics
34 35 36 37 38 39 |
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/app_size_metrics_helper.rb', line 34 def add_metric(name:, value:, metadata: nil) metric = { name: name, value: value } = &.compact || {} # Remove nil values if any (and use empty Hash if nil was provided) metric[:meta] = .map { |, | { name: .to_s, value: } } unless .empty? @metrics.append(metric) end |
#metadata=(hash) ⇒ Object
Sets the metadata common to the whole group of metrics in the payload being built by this helper instance
24 25 26 |
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/app_size_metrics_helper.rb', line 24 def (hash) @metadata = (hash.compact || {}).map { |key, value| { name: key.to_s, value: value } } end |
#send_metrics(to:, api_token:, use_gzip: true) ⇒ Integer
Send the metrics to the given App Metrics endpoint.
Must conform to the API described in github.com/Automattic/apps-metrics/wiki/Queue-Group-of-Metrics
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/app_size_metrics_helper.rb', line 56 def send_metrics(to:, api_token:, use_gzip: true) uri = URI(to) json_payload = use_gzip ? Zlib.gzip(to_h.to_json) : to_h.to_json # Allow using a `file:` URI for debugging if uri.is_a?(URI::File) UI.("Writing metrics payload to file #{uri.path} (instead of sending it to a remote API endpoint)") File.write(uri.path, json_payload) return 201 # To make it easy at call site to check for pseudo-status code 201 even in non-HTTP cases end UI.("Sending metrics to #{uri}...") headers = { Authorization: "Bearer #{api_token}", Accept: 'application/json', 'Content-Type': 'application/json' } headers[:'Content-Encoding'] = 'gzip' if use_gzip request = Net::HTTP::Post.new(uri, headers) request.body = json_payload response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: uri.scheme == 'https') do |http| http.request(request) end if response.is_a?(Net::HTTPSuccess) UI.success("Metrics sent. (#{response.code} #{response.})") else UI.error("Metrics failed to send. Received: #{response.code} #{response.}") UI.("Request was #{request.method} to #{request.uri}") UI.("Request headers were: #{headers}") UI.("Request body was #{request.body.length} bytes") UI.("Response was #{response.body}") end response.code.to_i end |
#to_h ⇒ Object
41 42 43 44 45 46 |
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/app_size_metrics_helper.rb', line 41 def to_h { meta: @metadata, metrics: @metrics } end |