syntax = "proto3";

import "proto/acl.proto";
import "proto/build_event_stream.proto";
import "proto/cache.proto";
import "proto/command_line.proto";
import "proto/context.proto";
import "google/protobuf/timestamp.proto";

package invocation;

// Next tag: 25
message Invocation {
  // The invocation identifier itself.
  string invocation_id = 1;

  // The ordered set of build events generated by this invocation.
  repeated InvocationEvent event = 2;

  // Whether or not the build was successful.
  bool success = 3;

  // The unix-user who performed this build.
  string user = 4;

  // The duration of this build, from start to finish.
  int64 duration_usec = 5;

  // The host this build was executed on.
  string host = 6;

  // The command performed (usually "build" or "test").
  string command = 7;

  // The build patterns specified for this build.
  repeated string pattern = 8;

  // The number of actions performed.
  int64 action_count = 9;

  enum InvocationStatus {
    UNKNOWN_INVOCATION_STATUS = 0;
    COMPLETE_INVOCATION_STATUS = 1;
    PARTIAL_INVOCATION_STATUS = 2;
    DISCONNECTED_INVOCATION_STATUS = 3;
  }
  InvocationStatus invocation_status = 10;

  // The console buffer extracted from the build events in this invocation.
  // NB: This buffer may be incomplete if invocation_status is not equal to
  // COMPLETE_INVOCATION_STATUS.
  string console_buffer = 11;

  // The structured command line options for this invocation. These are
  // extracted from the raw BuildEvents and in some cases filtered to remove
  // sensitive information (like raw environment variables for non-logged-in
  // users).
  repeated command_line.CommandLine structured_command_line = 12;

  // The time this invocation was created and updated, respectively. Invocations
  // are created as soon as the first event is received from the client and
  // updated with subsequent events until they are finalized.
  int64 created_at_usec = 13;
  int64 updated_at_usec = 14;

  // A URL to the git repo this invocation was for.
  string repo_url = 15;

  // The commit SHA that this invocation was for.
  string commit_sha = 16;

  // Read permissions for this invocation.
  InvocationPermission read_permission = 17;

  // Any cache stats that were captured during this invocation.
  // May not be present if the buildbuddy cache was not used.
  cache.CacheStats cache_stats = 18;

  // The scorecard, indicating any action cache misses.
  // May not be present if the buildbuddy cache was not used.
  cache.ScoreCard score_card = 24;

  // The role played by this invocation. Ex: "CI"
  string role = 19;

  // Access control list for this invocation.
  acl.ACL acl = 20;

  string last_chunk_id = 21;

  // This field indicates to the client if we have chunked event logs
  bool has_chunked_event_logs = 22;

  // The name of the git branch for this invocation, if any
  string branch_name = 23;
}

message InvocationEvent {
  // The time this event occurred.
  google.protobuf.Timestamp event_time = 1;

  // The bazel build event.
  build_event_stream.BuildEvent build_event = 2;

  // The sequence number of the event in the stream.
  int64 sequence_number = 3;
}

enum InvocationPermission {
  UNKNOWN_PERMISSION = 0;
  OWNER = 1;
  GROUP = 2;
  PUBLIC = 3;
}

message InvocationLookup {
  // The invocation_id: a UUID generated by the bazel run.
  string invocation_id = 1;
}

message GetInvocationRequest {
  context.RequestContext request_context = 1;

  InvocationLookup lookup = 2;
}

message GetInvocationResponse {
  context.ResponseContext response_context = 1;

  repeated Invocation invocation = 2;
}

message GetInvocationOwnerRequest {
  context.RequestContext request_context = 1;

  // The invocation ID to get the owner of.
  string invocation_id = 2;
}

message GetInvocationOwnerResponse {
  context.ResponseContext response_context = 1;

  // Group ID that owns the invocation.
  string group_id = 2;
}

message UpdateInvocationRequest {
  context.RequestContext request_context = 1;

  // The ID of the invocation to be updated.
  string invocation_id = 2;

  // Permissions for the invocation.
  acl.ACL acl = 3;
}

message UpdateInvocationResponse {
  context.ResponseContext response_context = 1;
}

message DeleteInvocationRequest {
  context.RequestContext request_context = 1;

  // The ID of the invocation to be deleted.
  string invocation_id = 2;
}

message DeleteInvocationResponse {
  context.ResponseContext response_context = 1;
}

message InvocationQuery {
  // The search parameters in this query will be ANDed when performing a
  // search -- so if a client species both "user" and "host", all results
  // returned must match both fields.

  // The unix-user who performed the build.
  string user = 1;

  // The host this build was executed on.
  string host = 2;

  // The group to search. The user must be a member of all named groups or an
  // error will be returned.
  string group_id = 3;

  // The git repo the build was for.
  string repo_url = 4;

  // The commit sha used for the build.
  string commit_sha = 5;

  // The ROLE metadata set on the build. If multiple filters are specified, they
  // are combined with "OR".
  repeated string role = 6;

  // The timestamp on or after which the build was last updated (inclusive).
  google.protobuf.Timestamp updated_after = 7;

  // The timestamp up to which the build was last updated (exclusive).
  google.protobuf.Timestamp updated_before = 8;

  // Status of the build. If multiple are specified, they are combined with
  // "OR".
  repeated OverallStatus status = 9;

  // The git branch used for the build.
  string branch_name = 10;
}

// OverallStatus is a status representing both the completion status and
// success status of an invocation.
enum OverallStatus {
  UNKNOWN_OVERALL_STATUS = 0;

  // Status representing a completed, successful invocation.
  SUCCESS = 1;

  // Status representing a completed, unsuccessful invocation.
  FAILURE = 2;

  // Status representing a partial invocation.
  IN_PROGRESS = 3;

  // Status representing a disconnected invocation.
  DISCONNECTED = 4;
}

message InvocationSort {
  enum SortField {
    UNKNOWN_SORT_FIELD = 0;
    CREATED_AT_USEC_SORT_FIELD = 1;
    UPDATED_AT_USEC_SORT_FIELD = 2;
  }

  // The field to sort results by.
  SortField sort_field = 1;

  // The sort direction.
  bool ascending = 2;
}

message SearchInvocationRequest {
  context.RequestContext request_context = 1;

  // Query params that determine which invocations will be matched. Required.
  InvocationQuery query = 2;

  // Sort parameters that determine result ordering. Optional.
  // If unset, the server will determine sort order.
  InvocationSort sort = 3;

  // The number of results to return. Optional.
  // If unset, the server will pick a reasonable page size.
  int32 count = 4;

  // The next_page_token value returned from a previous request, if any.
  string page_token = 5;
}

message SearchInvocationResponse {
  context.ResponseContext response_context = 1;

  // Note: For now, the invocations returned will not have the "event" field
  // set because it's a large amount of data. Clients that want to display
  // the full invocation should link to it or call GetInvocation directly on
  // the invocation they want to display.
  repeated Invocation invocation = 2;

  // Token to retrieve the next page of results, or empty if there are no
  // more results in the list.
  string next_page_token = 3;
}

enum AggType {
  UNKNOWN_AGGREGATION_TYPE = 0;
  USER_AGGREGATION_TYPE = 1;
  HOSTNAME_AGGREGATION_TYPE = 2;
  GROUP_ID_AGGREGATION_TYPE = 3;
  REPO_URL_AGGREGATION_TYPE = 4;
  COMMIT_SHA_AGGREGATION_TYPE = 5;
  DATE_AGGREGATION_TYPE = 6;
  BRANCH_AGGREGATION_TYPE = 7;
}

message InvocationStat {
  // The name of the entity used to aggregate these stats.
  string name = 2;

  // The sum of all invocation durations for this entity.
  int64 total_build_time_usec = 3;

  // The time (in usec since epoch) of the latest build by this entity.
  int64 latest_build_time_usec = 5;

  // The time (in usec since epoch) of the latest green build by this entity.
  int64 last_green_build_usec = 7;

  // The time (in usec since epoch) of the latest red build by this entity.
  int64 last_red_build_usec = 10;

  // The total number of invocations completed by this entity.
  int64 total_num_builds = 4;

  // The total number of invocations completed successfully by this entity.
  int64 total_num_sucessful_builds = 8;

  // The total number of invocations completed unsuccessfully by this entity.
  int64 total_num_failing_builds = 9;

  // The total number of actions completed by this entity.
  int64 total_actions = 6;
}

message InvocationStatQuery {
  // The search parameters in this query will be ANDed when performing a
  // query -- so if a client specifies both "user" and "host", all results
  // returned must match both fields.

  // The unix-user who performed the build.
  string user = 1;

  // The host this build was executed on.
  string host = 2;

  // The git repo the build was for.
  string repo_url = 4;

  // The commit sha used for the build.
  string commit_sha = 5;

  // The role played by the build. Ex: "CI". If multiple role filters are
  // specified, they are combined with "OR".
  repeated string role = 6;

  // The timestamp on or after which the build was last updated (inclusive).
  google.protobuf.Timestamp updated_after = 7;

  // The timestamp up to which the build was last updated (exclusive).
  google.protobuf.Timestamp updated_before = 8;

  // Status of the build. If multiple are specified, they are combined with
  // "OR".
  repeated OverallStatus status = 9;

  // The git branch used for the build.
  string branch_name = 10;
}

message GetInvocationStatRequest {
  context.RequestContext request_context = 1;

  // The requested aggregation type.
  AggType aggregation_type = 2;

  // The maximum number of stats to return. If not set, the server will
  // determine a reasonable limit.
  int32 limit = 3;

  // Query used to filter invocation stats.
  InvocationStatQuery query = 4;
}

message GetInvocationStatResponse {
  context.ResponseContext response_context = 1;

  // The list of invocation stats found.
  repeated InvocationStat invocation_stat = 2;
}

message TrendStat {
  string name = 1;

  // The sum of all invocation durations for this entity.
  int64 total_build_time_usec = 2;

  // The total number of invocations completed by this entity.
  int64 total_num_builds = 3;

  // The number of invocations with a duration longer than 0 seconds.
  int64 completed_invocation_count = 4;

  // The number of unique users who stared a build.
  int64 user_count = 5;

  // The number of unique commits that caused a build.
  int64 commit_count = 6;

  // The number of unique hosts that ran a build.
  int64 host_count = 7;

  // The number of unique repos that were built.
  int64 repo_count = 8;

  // The duration (in microseconds) of the longest build.
  int64 max_duration_usec = 9;

  // Server-side Action-cache stats.
  int64 action_cache_hits = 10;
  int64 action_cache_misses = 11;
  int64 action_cache_uploads = 12;

  // Server-side CAS-cache stats.
  int64 cas_cache_hits = 13;
  int64 cas_cache_misses = 14;
  int64 cas_cache_uploads = 15;

  // Download / upload stats.
  int64 total_download_size_bytes = 16;
  int64 total_upload_size_bytes = 17;
  int64 total_download_usec = 18;
  int64 total_upload_usec = 19;

  // The number of unique branches that were built.
  int64 branch_count = 20;
}

message TrendQuery {
  // The search parameters in this query will be ANDed when performing a
  // query -- so if a client specifies both "user" and "host", all results
  // returned must match both fields.

  // The unix-user who performed the build.
  string user = 1;

  // The host this build was executed on.
  string host = 2;

  // The git repo the build was for.
  string repo_url = 4;

  // The commit sha used for the build.
  string commit_sha = 5;

  // The role played by the build. Ex: "CI". If multiple filters are specified,
  // they are combined with "OR".
  repeated string role = 6;

  // The timestamp on or after which the build was last updated (inclusive).
  google.protobuf.Timestamp updated_after = 7;

  // The timestamp up to which the build was last updated (exclusive).
  google.protobuf.Timestamp updated_before = 8;

  // Status of the build. If multiple are specified, they are combined with
  // "OR".
  repeated OverallStatus status = 9;

  // The git branch used for the build.
  string branch_name = 10;
}

message GetTrendRequest {
  context.RequestContext request_context = 1;

  TrendQuery query = 2;

  // The maximum number of past days to aggregate. If not set, the server will
  // pick an appropriate value. Probably 7.
  // DEPRECATED: Use `query.updated_after` and `query.updated_before` instead.
  // TODO(bduffany): Delete this once clients no longer use it.
  int32 lookback_window_days = 3 [deprecated = true];
}

message GetTrendResponse {
  context.ResponseContext response_context = 1;

  // The list of trend stats found.
  repeated TrendStat trend_stat = 2;
}
