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

import "banyandb/common/v1/trace.proto";
import "banyandb/model/v1/query.proto";
import "banyandb/model/v1/write.proto";
import "validate/validate.proto";

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

// Span is a single operation within a trace.
message Span {
  // tags are the indexed tags of the span.
  repeated model.v1.Tag tags = 1;
  // span is the raw span data.
  bytes span = 2;
  // span_id is the unique identifier of the span.
  string span_id = 3;
}

// InternalTrace is the trace that is used for internal use.
message InternalTrace {
  // spans are the spans that belong to this trace.
  repeated Span spans = 1;
  // trace_id is the unique identifier of the trace.
  string trace_id = 2;
  // key is used for sorting.
  int64 key = 3;
}

// Trace contains all spans that belong to a single trace ID.
message Trace {
  // spans is the list of spans that belong to this trace.
  repeated Span spans = 1;
  // trace_id is the unique identifier of the trace.
  string trace_id = 2;
}

// QueryResponse is the response of a query.
message QueryResponse {
  // traces is a list of traces that match the query, with spans grouped by trace ID.
  repeated Trace traces = 1;
  // trace_query_result contains the trace of the query execution if tracing is enabled.
  common.v1.Trace trace_query_result = 2;
  // group_statuses reports the per-group gate outcome. Populated even when the query
  // short-circuits because any group failed the gate.
  map<string, model.v1.Status> group_statuses = 50;
}

// InternalQueryResponse is the response of an internal query.
message InternalQueryResponse {
  // internal_traces is a list of internal traces that match the query.
  repeated InternalTrace internal_traces = 1;
  // trace_query_result contains the trace of the query execution if tracing is enabled.
  common.v1.Trace trace_query_result = 2;
}

// QueryRequest is the request contract for query.
message QueryRequest {
  // groups indicates the physical data location.
  repeated string groups = 1 [(validate.rules).repeated.min_items = 1];
  // name is the identity of a trace.
  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 trace, it represents the range of the `startTime` for spans/segments,
  // 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 spans being returned
  uint32 limit = 5;
  // order_by is given to specify the sort for a tag. So far, only tags in the type of Integer are supported
  model.v1.QueryOrder order_by = 6;
  // criteria is the filter criteria.
  model.v1.Criteria criteria = 7;
  // projection can be used to select the names of the tags in the response
  repeated string tag_projection = 8;
  // 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;
  // group_mod_revisions gates the query per group. Keys match entries in `groups`;
  // values are the client's known mod_revision for that group. Empty map or value 0
  // means "don't gate". A group not listed in the map is not gated.
  map<string, int64> group_mod_revisions = 50;
}
