syntax = "proto3";

import "google/protobuf/empty.proto";
import "google/protobuf/timestamp.proto";

package accumulator.v1;

// AccumulatorWindow describes a special kind of SessionWindow (similar to Global Window) where output should
// always have monotonically increasing WM but it can be manipulated through event-time by reordering the messages.
// NOTE: Quite powerful, should not be abused; it can cause stalling of pipelines and leaks
service Accumulator {
  // AccumulateFn applies a accumulate function to a request stream.
  rpc AccumulateFn(stream AccumulatorRequest) returns (stream AccumulatorResponse);

  // IsReady is the heartbeat endpoint for gRPC.
  rpc IsReady(google.protobuf.Empty) returns (ReadyResponse);
}

// Payload represents a payload element.
message Payload {
  repeated string keys = 1;
  bytes value = 2;
  google.protobuf.Timestamp event_time = 3;
  google.protobuf.Timestamp watermark = 4;
  string id = 5;
  map<string, string> headers = 6;
}

// AccumulatorRequest represents a request element.
message AccumulatorRequest {
  // WindowOperation represents a window operation.
  // For Unaligned windows, OPEN, APPEND and CLOSE events are sent.
  message WindowOperation {
    enum Event {
      OPEN = 0;
      CLOSE = 1;
      APPEND = 2;
    }
    Event event = 1;
    KeyedWindow keyedWindow = 2;
  }

  Payload payload = 1;
  WindowOperation operation = 2;
}


// Window represents a window.
message KeyedWindow {
  google.protobuf.Timestamp start = 1;
  google.protobuf.Timestamp end = 2;
  string slot = 3;
  repeated string keys = 4;
}

// AccumulatorResponse represents a response element.
message AccumulatorResponse {
  Payload payload = 1;
  // window represents a window to which the result belongs.
  KeyedWindow window = 2;
  repeated string tags = 3;
  // EOF represents the end of the response for a window.
  bool EOF = 4;
}


// ReadyResponse is the health check result.
message ReadyResponse {
  bool ready = 1;
}