/*
 * Copyright 2020-2020 Exactpro (Exactpro Systems Limited)
 *
 * 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 th2.data_provider.lw;

import "th2_grpc_common/common.proto";
import "google/protobuf/timestamp.proto";
import "google/protobuf/any.proto";
import "google/protobuf/wrappers.proto";

option java_multiple_files = true;
option java_package = "com.exactpro.th2.dataprovider.lw.grpc";


service DataProvider {

  /* returns a single event with the specified id */
  rpc GetEvent (EventID) returns (EventResponse);

  /* returns a single message with the specified id */
  rpc GetMessage (MessageID) returns (MessageGroupResponse);

  /* returns a list of message stream names */
  rpc GetMessageStreams (MessageStreamsRequest) returns (MessageStreamsResponse);

  /* creates a message stream that matches the filter. */
  rpc SearchMessages (MessageSearchRequest) returns (stream MessageSearchResponse);

  /* creates an event or an event metadata stream that matches the filter. */
  rpc SearchEvents (EventSearchRequest) returns (stream EventSearchResponse);

  /*
  Searches for messages groups in specified timestamp
   */
  rpc SearchMessageGroups (MessageGroupsSearchRequest) returns (stream MessageSearchResponse);

  // Returns the set of books stored in cradle cache
  rpc GetBooks(BooksRequest) returns (BooksResponse);

  rpc GetPageInfo(PageInfoRequest) returns (stream PageInfoResponse);
}

// The scope for events to request
message EventScope {
  string name = 1;
}

message BookId {
  // Name of the book to request data from
  string name = 1;
}

message MessageGroupsSearchRequest {
  message Group {
    string name = 1;
  }
  google.protobuf.Timestamp start_timestamp = 1;
  google.protobuf.Timestamp end_timestamp = 2;
  BookId book_id = 7;
  repeated Group message_group = 3;
  google.protobuf.BoolValue sort = 4;
  bool raw_only = 5;
  bool keep_open = 6;
}

message MessageStreamsRequest {
  BookId book_id = 1;
}

message EventResponse {
  EventID event_id = 1; // Current event id
  EventID parent_event_id = 2; // Event id of a parent event. It is absent for root event

  EventID batch_id = 3; // Event id of a parent event. It is absent for root event

  bool is_batched = 4; // Flag indicating that the event is in batch

  google.protobuf.Timestamp start_timestamp = 5;
  google.protobuf.Timestamp end_timestamp = 6; // May be absent.
  EventStatus status = 7; // Aggregated status of current and children events which sync written.
  string event_name = 8;
  string event_type = 9; // May be absent.
  bytes body = 10; // May be absent.
  repeated MessageID attached_message_id = 11; // ids of messages linked to this event
}

message MessageGroupItem {
  Message message = 1;
  bool match = 2;
}

message MessageGroupResponse {
  MessageID message_id = 1; // Message id should be unique in session
  google.protobuf.Timestamp timestamp = 2 [deprecated = true]; // Message creation timestamp. Deprecated. Moved to MessageID
  map<string, string> message_properties = 6; // Additional properties for the source raw message
  bytes body_raw = 3;
  repeated EventID attached_event_id = 4; // ids of events linked to this message group
  repeated MessageGroupItem message_item = 5; // May be absent.
}

message FilterName {
  string name = 1;
}

message Filter {
  FilterName name = 1;
  bool negative = 2;
  repeated string value = 3;
  bool conjunct = 4;
}

message EventSearchRequest {
  google.protobuf.Timestamp start_timestamp = 1; // unix timestamp - Sets the search starting point. One of the 'start_timestamp' or 'resume_from_id' must not absent
  google.protobuf.Timestamp end_timestamp = 2; // unix timestamp - Sets the timestamp to which the search will be performed, starting with 'start_timestamp'. When 'search_direction' is previous, end_timestamp must be less than start_timestamp. By default it is absent (the search is carried out endlessly into the past or into the future).

  EventID parent_event = 3; // Match events to the specified parent. Only the immediate children of the specified event will be returned.
  TimeRelation search_direction = 4; // Sets the lookup direction. Can be used for pagination. Set by default to NEXT.

  google.protobuf.Int32Value result_count_limit = 5; // Sets the maximum amount of events to return. By default it is absent (unlimited).

  repeated Filter filter = 6; // List of event search filters. (See 'search/sse/events' HTTP method https://github.com/th2-net/th2-rpt-data-provider)

  BookId book_id = 7;
  EventScope scope = 8;
}

message EventSearchResponse {
  oneof data {
    EventResponse event = 1;
  }
}

message MessageSearchRequest {
  google.protobuf.Timestamp start_timestamp = 1; // unix timestamp - Sets the search starting point. One of the 'start_timestamp' or 'resume_from_id' or 'message_id' must not absent
  google.protobuf.Timestamp end_timestamp = 2; // unix timestamp - Sets the timestamp to which the search will be performed, starting with 'start_timestamp'. When search_direction is 'previous', 'end_timestamp' must be less than 'start_timestamp'. By default it is absent (the search is carried out endlessly into the past or into the future).

  TimeRelation search_direction = 3; // Sets the lookup direction. Can be used for pagination. Set by default to NEXT.

  google.protobuf.Int32Value result_count_limit = 4; // Sets the maximum amount of messages to return. By default it is absent (unlimited)

  repeated MessageStream stream = 5; // Sets the stream names and directions to search in. Case-sensitive. Required.
  google.protobuf.BoolValue keep_open = 6; //  If the search has reached the current moment, it is necessary to wait further for the appearance of new data. Set by default to false.

  repeated MessageStreamPointer stream_pointer = 7; // List of stream pointers to restore the search from. start_timestamp will be ignored if this parameter is specified. By default it is absent.

  repeated string response_formats = 8; // List of possible response formats (e.g. PARSED or BASE_64)
  BookId book_id = 9;
}

message MessageSearchResponse {
  oneof data {
    MessageGroupResponse message = 1;
    MessageStreamPointers message_stream_pointers = 2;
  }
}

message MessageStreamPointers {
  repeated MessageStreamPointer message_stream_pointer = 1;
}

/* This object contains the data of the last message in the stream on which
 * the search has stopped, it is necessary to continue the search from the same point */
 message MessageStreamPointer {
  MessageStream message_stream = 1;
  MessageID last_id = 2; // Might be absent if the stream has no messages
}

message MessageStream {
  string name = 1; // Case sensitive. Required.
  Direction direction = 2; // Required.
}

message MessageStreamsResponse {
  repeated MessageStream message_stream = 1;
}

enum TimeRelation {
  NEXT = 0;
  PREVIOUS = 1;
}

message BooksRequest {
}

message BooksResponse {
  repeated BookId book_ids = 1;
}

message PageInfoRequest {
  BookId book_id = 1;
  google.protobuf.Timestamp start_timestamp = 2;
  google.protobuf.Timestamp end_timestamp = 3;
  google.protobuf.Int32Value result_limit = 4;
}

message PageInfoResponse {
  PageId id = 1;
  google.protobuf.StringValue comment = 2;
  google.protobuf.Timestamp started = 3;
  google.protobuf.Timestamp ended = 4;
  google.protobuf.Timestamp updated = 5;
  google.protobuf.Timestamp removed = 6;
}

message PageId {
  BookId book_id = 1;
  string name = 2;
}