syntax = "proto3";
package rockpaperscissors;
option go_package = "proto";

// https://cloud.google.com/service-management/reference/rpc/google.api#http
import "google/api/annotations.proto";
// https://github.com/google/protobuf/blob/master/src/google/protobuf/struct.proto
import "google/protobuf/struct.proto";

import "code.justin.tv/dta/rockpaperscissors/proto/metric.proto";
import "code.justin.tv/dta/rockpaperscissors/proto/timerange.proto";

message ListMetricsRequest {
}

message ListMetricsResponse {
  repeated MetricInfo metrics = 3;
}

message GetMetricInfoRequest {
  string metric_id = 1;
}

message GetMetricInfoResponse {
  MetricInfo metric = 3;
}

message GetMetricRequest {
  string metric_id = 1;

  TimeRange timerange = 2;

  enum BucketSize {
    UNSET = 0;
    DAY = 1;
    WEEK = 2;
    MONTH = 3;
    QUARTER = 4;
    YEAR = 5;
  }
  BucketSize bucket_size = 3;
  string bucket_size_name = 4;  // Enums not suported in grpc-gateway paths, yet.

  // One use one of these. Can't use "oneof" because not supported in grpc-gateway paths, yet.
  string project_id = 5;
  string developer = 6;
  string team = 7;
  string org = 8;

  // Calculate start and end times for each entry in the result time series
  // relative to the given IANA Time Zone name (e.g. "America/Los_Angeles").
  // The default will use UTC.
  string iana_time_zone = 9;
}

message GetMetricResponse {
  MetricInfo metric_info = 1;

  message TimeSeriesEntry {
    google.protobuf.Value value = 1;
    TimeRange timerange = 2;
  }
  repeated TimeSeriesEntry time_series = 2;
  string time_series_units = 3;
}

service MetricService {
  // Get list of available metrics.
  rpc ListMetrics(ListMetricsRequest) returns (ListMetricsResponse) {
    option (google.api.http) = {
      get: "/api/v1/metrics"
    };
  }

  // Get details about a particular metric.
  rpc GetMetricInfo(GetMetricInfoRequest) returns (GetMetricInfoResponse) {
    option (google.api.http) = {
      get: "/api/v1/metrics/{metric_id}/info"
    };
  }

  // Get a metric timeseries for time range, filtered by selector, and aggregated into buckets.
  rpc GetMetric(GetMetricRequest) returns (GetMetricResponse) {
    option (google.api.http) = {
      get: "/api/v1/metrics/{metric_id}/project/{project_id}/{timerange.start.seconds}/{timerange.end.seconds}/{bucket_size_name}"

      additional_bindings {
        get: "/api/v1/metrics/{metric_id}/developer/{developer}/{timerange.start.seconds}/{timerange.end.seconds}/{bucket_size_name}"
      }

      additional_bindings {
        get: "/api/v1/metrics/{metric_id}/team/{team}/{timerange.start.seconds}/{timerange.end.seconds}/{bucket_size_name}"
      }

      additional_bindings {
        get: "/api/v1/metrics/{metric_id}/org/{org}/{timerange.start.seconds}/{timerange.end.seconds}/{bucket_size_name}"
      }
    };
  }
}
