// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

syntax = "proto3";

package google.cloud.pubsublite.v1;

import "google/api/annotations.proto";
import "google/api/field_behavior.proto";
import "google/api/resource.proto";
import "google/cloud/pubsublite/v1/common.proto";
import "google/protobuf/timestamp.proto";
import "google/api/client.proto";

option go_package = "google.golang.org/genproto/googleapis/cloud/pubsublite/v1;pubsublite";
option java_multiple_files = true;
option java_outer_classname = "TopicStatsProto";
option java_package = "com.google.cloud.pubsublite.proto";

// This service allows users to get stats about messages in their topic.
service TopicStatsService {
  option (google.api.default_host) = "pubsublite.googleapis.com";
  option (google.api.oauth_scopes) = "https://www.googleapis.com/auth/cloud-platform";

  // Compute statistics about a range of messages in a given topic and
  // partition.
  rpc ComputeMessageStats(ComputeMessageStatsRequest) returns (ComputeMessageStatsResponse) {
    option (google.api.http) = {
      post: "/v1/topicStats/{topic=projects/*/locations/*/topics/*}:computeMessageStats"
      body: "*"
    };
  }
}

// Compute statistics about a range of messages in a given topic and partition.
message ComputeMessageStatsRequest {
  // Required. The topic for which we should compute message stats.
  string topic = 1 [
    (google.api.field_behavior) = REQUIRED,
    (google.api.resource_reference) = {
      type: "pubsublite.googleapis.com/Topic"
    }
  ];

  // Required. The partition for which we should compute message stats.
  int64 partition = 2 [(google.api.field_behavior) = REQUIRED];

  // The inclusive start of the range.
  Cursor start_cursor = 3;

  // The exclusive end of the range. The range is empty if end_cursor <=
  // start_cursor. Specifying a start_cursor before the first message and an
  // end_cursor after the last message will retrieve all messages.
  Cursor end_cursor = 4;
}

// Response containing stats for messages in the requested topic and partition.
message ComputeMessageStatsResponse {
  // The count of messages.
  int64 message_count = 1;

  // The number of quota bytes accounted to these messages.
  int64 message_bytes = 2;

  // The minimum publish timestamp across these messages. Note that publish
  // timestamps within a partition are non-decreasing. The timestamp will be
  // unset if there are no messages.
  google.protobuf.Timestamp minimum_publish_time = 3;

  // The minimum event timestamp across these messages. For the purposes of this
  // computation, if a message does not have an event time, we use the publish
  // time. The timestamp will be unset if there are no messages.
  google.protobuf.Timestamp minimum_event_time = 4;
}
