syntax = "proto3";

package smelt_telemetry;
import "google/protobuf/timestamp.proto";
import "executed_tests.proto";
import "google/protobuf/empty.proto";



service EventListener {
  // Sends an Event to the server.
  rpc SendEvent (Event) returns (google.protobuf.Empty) {}
  rpc SendOutputs(TaggedResult) returns (google.protobuf.Empty) {}
}



service EventSubscriber {
  rpc SubscribeReceivedEvents(ExecutionSubscribe) returns (stream Event);
  rpc SubscriptionComplete(ExecutionFinish) returns(google.protobuf.Empty);
}


// Event flows from server -> client only
message Event {
  google.protobuf.Timestamp time = 1;
  // A globally-unique ID (UUIDv4) of this trace. Required.
  string trace_id = 2;
  // type of event
  oneof et {
    CommandEvent command = 15;
    InvokeEvent invoke = 16;
    SmeltError error = 17;
  }
}

// CommandEvents covers activity happening on a per target basis
message CommandEvent {
  // test def id
  // this ref should be consistent for the same test being executed
  string command_ref = 1;
  oneof CommandVariant {
    CommandScheduled scheduled = 4;
    CommandStarted started = 5;
    CommandCancelled cancelled = 6;
    CommandFinished finished = 7;
    CommandStdout stdout = 8;
    CommandProfile profile = 9;
    CommandSkipped skipped = 10;
  }
}

message CommandScheduled {}
message CommandStarted {}
message CommandCancelled {}
message CommandSkipped {}
message CommandStdout { string output = 1; }
message CommandFinished {
  executed_tests.TestOutputs outputs = 1;
  string command_type = 2;
}
message CommandProfile {
  // memory used by the command, in bytes
  uint64 memory_used = 1;
  // cpu load as a fraction -- 1.0 is one cpu, 2.0 is 2 cpus, etc
  float cpu_load = 2;
  // Number of milliseconds since profiling began
  uint64 time_since_start_ms = 3;
}

// InvokeEvent demarcates the start of a graph execution.
message InvokeEvent {
  oneof InvokeVariant {
    ExecutionStart start = 5;
    AllCommandsDone done = 6;
    SetGraph set = 7;
  }
}

message ExecutionStart {
  string smelt_root = 1;
  string username = 2;
  string hostname = 3;
  string git_hash = 4;
  string git_repo = 5;
  string git_branch = 6;
}
message AllCommandsDone {}
message SetGraph {}

enum SmeltErrorType {
  // Client caused error
  CLIENT_ERROR = 0;
  // Internal error -- anything that is thrown by the smelt runtime
  INTERNAL_ERROR = 1;
  // Internal warning -- anything that the smelt runtime wants to broadcast back
  INTERNAL_WARN = 2;
}

message SmeltError {
  SmeltErrorType sig = 1;
  string error_payload = 2;
}


message TaggedResult {
  string trace_id = 1;
  executed_tests.TestResult results = 2;
}

message ExecutionSubscribe {
  string trace_id = 1;
}

message ExecutionFinish {
  string trace_id = 1;

}

