/**
 * Messages describing tasks and associated requests and responses.
 */
syntax = "proto3";

package armonik.api.grpc.v1.tasks;

import "google/protobuf/duration.proto";
import "google/protobuf/timestamp.proto";
import "objects.proto";
import "sort_direction.proto";
import "task_status.proto";
import "tasks_fields.proto";
import "tasks_filters.proto";

option csharp_namespace = "ArmoniK.Api.gRPC.V1.Tasks";

/**
 * A raw task object.
 *
 * Used when a single task is returned.
 */
message TaskDetailed {
  string id = 1; /** The task ID. */
  string session_id = 2; /** The session ID. A task have only one related session but a session have many tasks. */
  string owner_pod_id = 3; /** The owner pod ID. */

  string initial_task_id = 22; /** The initial task ID. Set when a task is submitted independently of retries. */
  repeated string parent_task_ids = 4; /** The parent task IDs. A tasks can be a child of another task. */
  repeated string data_dependencies = 5; /** The data dependencies. A task have data dependencies. */
  repeated string expected_output_ids = 6; /** The expected output IDs. A task have expected output IDs. */
  repeated string retry_of_ids = 7; /** The retry of IDs. When a task fail, retry will use these set of IDs. */

  task_status.TaskStatus status = 8; /** The task status. */
  string status_message = 9; /** The status message. */

  TaskOptions options = 10; /** The task options. */

  google.protobuf.Timestamp created_at = 11; /** The task creation date. */
  google.protobuf.Timestamp submitted_at = 12; /** The task submission date. */
  google.protobuf.Timestamp received_at = 18; /** When the task is received by the agent. */
  google.protobuf.Timestamp acquired_at = 19; /** When the task is acquired by the agent. */
  google.protobuf.Timestamp fetched_at = 25; /** Task data retrieval end date. */
  google.protobuf.Timestamp started_at = 13; /** The task start date. */
  google.protobuf.Timestamp processed_at = 24; /** The end of task processing date. */
  google.protobuf.Timestamp ended_at = 14; /** The task end date. Also used when task failed. */
  google.protobuf.Timestamp pod_ttl = 15; /** The pod TTL (Time To Live). */

  google.protobuf.Duration creation_to_end_duration = 20; /** The task duration. Between the creation date and the end date. */
  google.protobuf.Duration processing_to_end_duration = 21; /** The task calculated duration. Between the start date and the end date. */
  google.protobuf.Duration received_to_end_duration = 23; /** The task calculated duration. Between the received date and the end date. */

  string payload_id = 26; /** The ID of the Result that is used as a payload for this task. */
  string created_by = 27; /** The ID of the Task that as submitted this task empty if none.*/

  /**
   * Represents the task output.
   */
  message Output {
    bool success = 1; /** To know if a task have failed or succeed. */
    string error = 2; /** The error message. Only set if task have failed. */
  }

  Output output = 16; /** The task output. */

  string pod_hostname = 17; /** The hostname of the container running the task. */
}

/**
 * A summary task object.
 *
 * It contains only a subset of the fields from the underlying task object.
 * Used when a list of tasks are returned.
 */
message TaskSummary {
  string id = 1; /** The task ID. */
  string session_id = 2; /** The session ID. A task have only one related session but a session have many tasks. */
  string owner_pod_id = 10; /** The owner pod ID. */

  string initial_task_id = 22; /** The initial task ID. Set when a task is submitted independently of retries. */
  int64 count_parent_task_ids = 21; /** Count the parent task IDs. A tasks can be a child of another task. */
  int64 count_data_dependencies = 11; /** Count the data dependencies. A task have data dependencies. */
  int64 count_expected_output_ids = 13; /** Count the expected output IDs. A task have expected output IDs. */
  int64 count_retry_of_ids = 14; /** Count the retry of IDs. When a task fail, retry will use these set of IDs. */

  task_status.TaskStatus status = 4; /** The task status. */
  string status_message = 9; /** The status message. */

  TaskOptions options = 3; /** The task options. */

  google.protobuf.Timestamp created_at = 5; /** The task creation date. */
  google.protobuf.Timestamp submitted_at = 12; /** The task submission date. */
  google.protobuf.Timestamp received_at = 18; /** When the task is received by the agent. */
  google.protobuf.Timestamp acquired_at = 19; /** When the task is acquired by the agent. */
  google.protobuf.Timestamp fetched_at = 25; /** Task data retrieval end date. */
  google.protobuf.Timestamp started_at = 6; /** The task start date. */
  google.protobuf.Timestamp processed_at = 23; /** The end of task processing date. */
  google.protobuf.Timestamp ended_at = 7; /** The task end date. Also used when task failed. */
  google.protobuf.Timestamp pod_ttl = 20; /** The pod TTL (Time To Live). */

  google.protobuf.Duration creation_to_end_duration = 15; /** The task duration. Between the creation date and the end date. */
  google.protobuf.Duration processing_to_end_duration = 16; /** The task calculated duration. Between the start date and the end date. */
  google.protobuf.Duration received_to_end_duration = 24; /** The task calculated duration. Between the received date and the end date. */

  string payload_id = 26; /** The ID of the Result that is used as a payload for this task. */
  string created_by = 27; /** The ID of the Task that as submitted this task empty if none.*/

  string error = 8; /** The error message. Only set if task have failed. */
  string pod_hostname = 17; /** The hostname of the container running the task. */
}

/**
 * Request to list tasks.
 *
 * Use pagination, filtering and sorting.
 */
message ListTasksRequest {
  int32 page = 1; /** The page number. Start at 0.*/
  int32 page_size = 2; /** The page size. */

  Filters filters = 3; /** The filters. */

  /**
   * Represents the sort object.
   */
  message Sort {
    TaskField field = 1; /** The field to sort on. */
    sort_direction.SortDirection direction = 2; /** The sort direction. */
  }

  /** The sort.
   *
   * Must be set for every request.
   */
  Sort sort = 4;

  // Request error message in case of error in task
  bool with_errors = 5;
}

/**
 * Response to list tasks.
 *
 * Use pagination, filtering and sorting from the request.
 * Return a list of formatted tasks.
 */
message ListTasksResponse {
  repeated TaskSummary tasks = 1; /** The list of tasks. */

  int32 page = 2; /** The page number. Start at 0. */
  int32 page_size = 3; /** The page size. */
  int32 total = 4; /** The total number of tasks. */
}

/**
 * Response to list tasks.
 *
 * Use pagination, filtering and sorting from the request.
 * Return a list of formatted tasks.
 */
message ListTasksDetailedResponse {
  repeated TaskDetailed tasks = 1; /** The list of tasks. */

  int32 page = 2; /** The page number. Start at 0. */
  int32 page_size = 3; /** The page size. */
  int32 total = 4; /** The total number of tasks. */
}

/**
 * Request for getting a single task.
 */
message GetTaskRequest {
  string task_id = 1; /** The task ID. */
}

/**
 * Response for getting a single task.
 *
 * Return a raw task.
 */
message GetTaskResponse {
  TaskDetailed task = 1; /** The task. */
}

/**
 * Request to cancel one or many tasks
 */
message CancelTasksRequest {
  // Ids of the tasks to cancel
  repeated string task_ids = 1;
}

/**
 * Response from canceling one or many tasks
 */
message CancelTasksResponse {
  // Tasks that have been asked to cancel
  repeated TaskSummary tasks = 1;
}

/*
*  Request for getting result ids of tasks ids.
*/
message GetResultIdsRequest {
  repeated string task_id = 1; /** The task IDs. */
}

/*
* Response for getting result ids of tasks ids.
*/
message GetResultIdsResponse {
  /**
   * Represents a task result.
  */
  message MapTaskResult {
    string task_id = 1; /** The task ID. */
    repeated string result_ids = 2; /** The result IDs. */
  }

  repeated MapTaskResult task_results = 1; /** The task results. */
}

/**
 * Request to get count from tasks by status
 */
message CountTasksByStatusRequest {
  Filters filters = 1; /** The filters. */
}

/**
 * Response to get count from tasks by status
 */
message CountTasksByStatusResponse {
  repeated StatusCount status = 1; /** Number of tasks by status. Expected to have only 1 object by tasks status. */
}

/**
* Request to create tasks.
*/
message SubmitTasksRequest {
  message TaskCreation {
    repeated string expected_output_keys = 1; /** Unique ID of the results that will be produced by the task. Results should be created using ResultsService. */
    repeated string data_dependencies = 2; /** Unique ID of the results that will be used as datadependencies. Results should be created using ResultsService. */
    string payload_id = 3; /** Unique ID of the result that will be used as payload. Result should created using ResultsService. */
    TaskOptions task_options = 4; /** Optional task options. */
  }

  string session_id = 1; /** The session ID. */
  TaskOptions task_options = 2; /** The options for the tasks. Each task will have the same. Options are merged with the one from the session. */
  repeated TaskCreation task_creations = 3; /** Task creation requests. */
}

/**
* Response to create tasks.
*
* expected_output_ids and data_dependencies must be created through ResultsService.
*
* Remark : this may have to be enriched to a better management of errors but
* will the client application be able to manage a missing data dependency or expected output ?
*/
message SubmitTasksResponse {
  message TaskInfo {
    string task_id = 1; /** The task ID. */
    repeated string expected_output_ids = 2; /** The expected output IDs. A task have expected output IDs. */
    repeated string data_dependencies = 3; /** The data dependencies IDs (inputs). A task have data dependencies. */
    string payload_id = 4; /** Unique ID of the result that will be used as payload. Result should created using ResultsService. */
  }

  repeated TaskInfo task_infos = 1; /** List of task infos if submission successful, else throw gRPC exception. */
}
