// 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.stream.v1;

import "banyandb/common/v1/trace.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/stream/v1";
option java_package = "org.apache.skywalking.banyandb.stream.v1";

// Element represents
// (stream context) a Span defined in Google Dapper paper or equivalently a Segment in Skywalking.
// (Log context) a log
message Element {
  // element_id could be span_id of a Span or segment_id of a Segment in the context of stream
  string element_id = 1;
  // timestamp represents a millisecond
  // 1) either the start time of a Span/Segment,
  // 2) or the timestamp of a log
  google.protobuf.Timestamp timestamp = 2;
  // fields contains all indexed Field. Some typical names,
  // - stream_id
  // - duration
  // - service_name
  // - service_instance_id
  // - end_time_milliseconds
  repeated model.v1.TagFamily tag_families = 3;
}

// QueryResponse is the response for a query to the Query module.
message QueryResponse {
  // elements are the actual data returned
  repeated Element elements = 1;
  // trace contains the trace information of the query when trace is enabled
  common.v1.Trace trace = 2;
}

// QueryRequest is the request contract for query.
message QueryRequest {
  // groups indicate where the elements are stored.
  repeated string groups = 1 [(validate.rules).repeated.min_items = 1];
  // name is the identity of a stream.
  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.
  // In the context of stream, it represents the range of the `startTime` for spans/segments,
  // while in the context of Log, it means the range of the timestamp(s) for logs.
  // it is always recommended to specify time range for performance reason
  model.v1.TimeRange time_range = 3;
  // offset is used to support pagination, together with the following limit
  uint32 offset = 4;
  // limit is used to impose a boundary on the number of records being returned
  uint32 limit = 5;
  // order_by is given to specify the sort for a field. So far, only fields in the type of Integer are supported
  model.v1.QueryOrder order_by = 6;
  // tag_families are indexed.
  model.v1.Criteria criteria = 7;
  // projection can be used to select the key names of the element in the response
  model.v1.TagProjection projection = 8 [(validate.rules).message.required = true];
  // trace is used to enable trace for the query
  bool trace = 9;
  // stage is used to specify the stage of the query in the lifecycle
  repeated string stages = 10;
}
