// Licensed to Apache Software Foundation (ASF) under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Apache Software Foundation (ASF) licenses this file to you 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 banyandb.measure.v1;

import "banyandb/common/v1/trace.proto";
import "banyandb/model/v1/common.proto";
import "banyandb/model/v1/query.proto";
import "google/protobuf/timestamp.proto";
import "validate/validate.proto";

option go_package = "github.com/apache/skywalking-banyandb/api/proto/banyandb/measure/v1";
option java_package = "org.apache.skywalking.banyandb.measure.v1";

// TopNList contains a series of topN items
message TopNList {
  // timestamp is in the timeunit of milliseconds.
  google.protobuf.Timestamp timestamp = 1;
  message Item {
    repeated model.v1.Tag entity = 1;
    model.v1.FieldValue value = 2;
    int64 version = 3;
    google.protobuf.Timestamp timestamp = 4;
  }
  // items contains top-n items in a list
  repeated Item items = 2;
}

// TopNResponse is the response for a query to the Query module.
message TopNResponse {
  // lists contain a series topN lists ranked by timestamp
  // if agg_func in query request is specified, lists' size should be one.
  repeated TopNList lists = 1;
  // trace contains the trace information of the query when trace is enabled
  common.v1.Trace trace = 2;
}

// TopNRequest is the request contract for query.
message TopNRequest {
  // groups indicate where the data points are stored.
  repeated string groups = 1 [(validate.rules).repeated.min_items = 1];
  // name is the identity of a measure.
  string name = 2 [(validate.rules).string.min_len = 1];
  // time_range is a range query with begin/end time of entities in the timeunit of milliseconds.
  model.v1.TimeRange time_range = 3 [(validate.rules).message.required = true];
  // top_n set the how many items should be returned in each list.
  int32 top_n = 4 [(validate.rules).int32.gt = 0];
  // agg aggregates lists grouped by field names in the time_range
  model.v1.AggregationFunction agg = 5;
  // criteria select counters. Only equals are acceptable.
  repeated model.v1.Condition conditions = 6;
  // field_value_sort indicates how to sort fields
  model.v1.Sort field_value_sort = 7;
  // trace is used to enable trace for the query
  bool trace = 8;
  // stages is used to specify the stage of the data points in the lifecycle
  repeated string stages = 9;
}
