syntax = "proto3";

import "google/protobuf/timestamp.proto";

service HistogrammerService {
  rpc Init(InitRequest) returns (InitResponse) {}
  rpc Describe(DescribeRequest) returns (DescribeResponse) {}
  rpc Exists(ExistsRequest) returns (ExistsResponse) {}
  rpc Fill(FillRequest) returns (FillResponse) {}
  rpc FillMany(FillManyRequest) returns (FillResponse) {}
  rpc Snapshot(SnapshotRequest) returns (SnapshotResponse) {}
  rpc Delete(DeleteRequest) returns (DeleteResponse) {}
  rpc Reset(ResetRequest) returns (ResetResponse) {}
  rpc Flush(FlushRequest) returns (FlushResponse) {}
  rpc Stats(StatsRequest) returns (StatsResponse) {}
}

enum CompressionCodec {
  COMPRESSION_CODEC_NONE = 0;
  COMPRESSION_CODEC_ZSTD = 1;
}

message ChunkScalar {
  oneof value {
    string string_value = 1;
    int64 int_value = 2;
  }
}

message ChunkPayload {
  repeated ChunkScalar chunk_key = 1;
  bytes dense_view = 2;
}

message ChunkedHistPayload {
  string hist_json = 1;
  repeated ChunkPayload chunks = 2;
  CompressionCodec dense_view_codec = 3;
}

message InitRequest {
  ChunkedHistPayload payload = 1;
}

message InitResponse {
  string hist_id = 1;
}

message DescribeRequest {
  string hist_id = 1;
}

message DescribeResponse {
  string hist_json = 1;
}

message ExistsRequest {
  string hist_id = 1;
}

message ExistsResponse {
  bool exists = 1;
}

message FillRequest {
  string hist_id = 1;
  optional bytes unique_id = 2;
  repeated ChunkScalar chunk_key = 3;
  bytes dense_view = 4;
  CompressionCodec dense_view_codec = 5;
}

message FillResponse {}

message FillManyRequest {
  string hist_id = 1;
  optional bytes unique_id = 2;
  repeated ChunkPayload chunks = 3;
  CompressionCodec dense_view_codec = 4;
}

message ChunkSelector {
  string axis = 1;
  repeated ChunkScalar values = 2;
}

message SnapshotRequest {
  string hist_id = 1;
  bool delete_from_server = 2;
  repeated ChunkSelector chunk_selectors = 3;
}

message SnapshotResponse {
  ChunkedHistPayload payload = 1;
}

message DeleteRequest {
  string hist_id = 1;
}

message DeleteResponse {}

message ResetRequest {
  string hist_id = 1;
}

message ResetResponse {}

message FlushRequest {
  string hist_id = 1;
  string destination = 2;
}

message FlushResponse {}

message StatsRequest {}

message StatsResponse {
  uint64 histogram_count = 1;
  uint64 histogram_bytes = 2;
  uint64 active_rpcs = 3;
  string version = 4;
  uint64 uptime_seconds = 5;
  double user_cpu_seconds = 6;
  double system_cpu_seconds = 7;
  map<string, uint64> rpc_calls_total = 8;
  google.protobuf.Timestamp observed_at = 9;
  TokenScopedStats token_scoped = 10;
}

message TokenScopedStats {
  uint64 histogram_count = 1;
  uint64 histogram_bytes = 2;
  map<string, uint64> rpc_calls_total = 3;
}
