syntax = "proto3";

option go_package = "v1";

option java_package = "io.stackrox.proto.api.v1";

import "google/api/annotations.proto";
import "google/protobuf/timestamp.proto";
import "api/v1/common.proto";
import "api/v1/empty.proto";
import "storage/policy.proto";
import "storage/alert.proto";

package v1;

message ListAlertsRequest {
    reserved 1 to 9; // Removed individual filtered fields and moved towards querying
    string query = 10;
}

message ResolveAlertRequest {
    string id = 1;
}

message SnoozeAlertRequest {
    string id = 1;
    google.protobuf.Timestamp snooze_till = 2;
}

message ListAlertsResponse {
    repeated storage.ListAlert alerts = 1;
}

message GetAlertsGroupResponse {
    message PolicyGroup {
        storage.ListAlertPolicy policy = 1;
        int64 num_alerts = 2;
    }

    repeated PolicyGroup alerts_by_policies = 1;
}

message GetAlertsCountsRequest {
    ListAlertsRequest request   = 1;
    enum RequestGroup {
        UNSET       = 0;
        CATEGORY    = 1;
        CLUSTER     = 2;
    }
    RequestGroup group_by       = 2;
}

message GetAlertsCountsResponse {
    message AlertGroup {
        string group                    = 1;
        message AlertCounts {
            storage.Severity severity   = 1;
            int64 count         = 2;
        }
        repeated AlertCounts counts     = 2;
    }
    repeated AlertGroup groups = 1;
}

enum Type {
    CREATED = 0;
    REMOVED = 1;
}

message AlertEvent {
    int64 time        = 1;
    Type type         = 2;
    string id         = 3;
}

message GetAlertTimeseriesResponse {
    message ClusterAlerts {
        string cluster                   = 1;
        message AlertEvents {
            storage.Severity severity          = 1;
            repeated AlertEvent events = 2;
        }
        repeated AlertEvents severities  = 2;
    }
    repeated ClusterAlerts clusters = 1;
}

service AlertService {

    rpc GetAlert (ResourceByID) returns (storage.Alert) {
        option (google.api.http) = {
            get: "/v1/alerts/{id}"
        };
    }

    rpc ListAlerts (ListAlertsRequest) returns (ListAlertsResponse) {
        option (google.api.http) = {
            get: "/v1/alerts"
        };
    }

    rpc GetAlertsGroup (ListAlertsRequest) returns (GetAlertsGroupResponse) {
        option (google.api.http) = {
            get: "/v1/alerts/summary/groups"
        };
    }

    rpc GetAlertsCounts (GetAlertsCountsRequest) returns (GetAlertsCountsResponse) {
        option (google.api.http) = {
            get: "/v1/alerts/summary/counts"
        };
    }

    rpc GetAlertTimeseries (ListAlertsRequest) returns (GetAlertTimeseriesResponse) {
        option (google.api.http) = {
            get: "/v1/alerts/summary/timeseries"
        };
    }

    rpc ResolveAlert (ResolveAlertRequest) returns (Empty) {
        option (google.api.http) = {
            patch: "/v1/alerts/{id}/resolve"
            body: "*"
        };
    }

    rpc SnoozeAlert (SnoozeAlertRequest) returns (Empty) {
        option (google.api.http) = {
            patch: "/v1/alerts/{id}/snooze"
            body: "*"
        };
    }

    // No deletion of alerts

}
