use std::str::FromStr;
use cx_api::proto::com::coralogixapis::incidents::v1::{
AcknowledgeIncidentsRequest,
AcknowledgeIncidentsResponse,
AssignIncidentsRequest,
AssignIncidentsResponse,
BatchGetIncidentRequest,
BatchGetIncidentResponse,
CloseIncidentsRequest,
CloseIncidentsResponse,
GetFilterValuesRequest,
GetFilterValuesResponse,
GetIncidentEventsRequest,
GetIncidentEventsResponse,
GetIncidentRequest,
GetIncidentResponse,
GetIncidentUsingCorrelationKeyRequest,
GetIncidentUsingCorrelationKeyResponse,
ListIncidentAggregationsRequest,
ListIncidentAggregationsResponse,
ListIncidentEventsFilterValuesRequest,
ListIncidentEventsFilterValuesResponse,
ListIncidentEventsRequest,
ListIncidentEventsResponse,
ListIncidentEventsTotalCountRequest,
ListIncidentEventsTotalCountResponse,
ListIncidentsRequest,
ResolveIncidentsRequest,
ResolveIncidentsResponse,
UnassignIncidentsRequest,
UnassignIncidentsResponse,
incidents_service_client::IncidentsServiceClient,
};
pub use cx_api::proto::com::coralogixapis::incidents::v1::{
ContextualLabelValues,
FilterOperator,
GroupBy,
IncidentEventOrderByFieldType,
IncidentEventQueryFilter,
IncidentFields,
IncidentQueryFilter,
IncidentSearchQuery,
IncidentSeverity,
IncidentStatus,
LabelsFilter,
ListIncidentEventRequestOrderBy,
ListIncidentsResponse,
MetaLabel,
OrderBy,
OrderByDirection,
OrderByFields,
PaginationRequest,
TimeRange,
UserDetails,
incident_search_query::Field,
order_by::Field as OrderByField,
};
use time::OffsetDateTime;
use tokio::sync::Mutex;
use tonic::{
metadata::MetadataMap,
transport::{
Channel,
ClientTlsConfig,
Endpoint,
},
};
use crate::{
CoralogixRegion,
auth::AuthContext,
error::SdkApiError,
metadata::CallProperties,
util::make_request_with_metadata,
};
const INCIDENTS_FEATURE_GROUP_ID: &str = "incidents";
pub struct IncidentsClient {
service_client: Mutex<IncidentsServiceClient<Channel>>,
metadata_map: MetadataMap,
}
impl IncidentsClient {
pub async fn new(
auth_context: AuthContext,
region: CoralogixRegion,
) -> Result<Self, Box<dyn std::error::Error>> {
let channel: Channel = Endpoint::from_str(region.grpc_endpoint().as_str())?
.tls_config(ClientTlsConfig::new().with_native_roots())?
.connect_lazy();
let request_metadata: CallProperties = (&auth_context.team_level_api_key).into();
Ok(Self {
metadata_map: request_metadata.to_metadata_map(),
service_client: Mutex::new(IncidentsServiceClient::new(channel)),
})
}
pub async fn get_incident(
&self,
id: &str,
) -> Result<GetIncidentResponse, Box<dyn std::error::Error>> {
let request = make_request_with_metadata(
GetIncidentRequest {
id: Some(id.to_string()),
},
&self.metadata_map,
);
let response = self
.service_client
.lock()
.await
.get_incident(request)
.await
.map_err(|status| SdkApiError {
status,
endpoint: "/com.coralogixapis.incidents.v1.IncidentsService/GetIncident".into(),
feature_group: INCIDENTS_FEATURE_GROUP_ID.into(),
})?;
Ok(response.into_inner())
}
pub async fn batch_get_incident(
&self,
ids: Vec<String>,
) -> Result<BatchGetIncidentResponse, Box<dyn std::error::Error>> {
let request =
make_request_with_metadata(BatchGetIncidentRequest { ids }, &self.metadata_map);
let response = self
.service_client
.lock()
.await
.batch_get_incident(request)
.await
.map_err(|status| SdkApiError {
status,
endpoint: "/com.coralogixapis.incidents.v1.IncidentsService/BatchGetIncident"
.into(),
feature_group: INCIDENTS_FEATURE_GROUP_ID.into(),
})?;
Ok(response.into_inner())
}
pub async fn list_incidents(
&self,
filter: Option<IncidentQueryFilter>,
pagination: Option<PaginationRequest>,
order_bys: Vec<OrderBy>,
) -> Result<ListIncidentsResponse, Box<dyn std::error::Error>> {
let request = make_request_with_metadata(
ListIncidentsRequest {
filter,
pagination,
order_bys,
},
&self.metadata_map,
);
let response = self
.service_client
.lock()
.await
.list_incidents(request)
.await
.map_err(|status| SdkApiError {
status,
endpoint: "/com.coralogixapis.incidents.v1.IncidentsService/ListIncidents".into(),
feature_group: INCIDENTS_FEATURE_GROUP_ID.into(),
})?;
Ok(response.into_inner())
}
pub async fn list_incident_aggregations(
&self,
filter: Option<IncidentQueryFilter>,
group_bys: Vec<GroupBy>,
pagination: Option<PaginationRequest>,
) -> Result<ListIncidentAggregationsResponse, Box<dyn std::error::Error>> {
let request = make_request_with_metadata(
ListIncidentAggregationsRequest {
filter,
group_bys,
pagination,
},
&self.metadata_map,
);
let response = self
.service_client
.lock()
.await
.list_incident_aggregations(request)
.await
.map_err(|status| SdkApiError {
status,
endpoint:
"/com.coralogixapis.incidents.v1.IncidentsService/ListIncidentAggregations"
.into(),
feature_group: INCIDENTS_FEATURE_GROUP_ID.into(),
})?;
Ok(response.into_inner())
}
pub async fn get_filter_values(
&self,
filter: IncidentQueryFilter,
) -> Result<GetFilterValuesResponse, Box<dyn std::error::Error>> {
let request = make_request_with_metadata(
GetFilterValuesRequest {
filter: Some(filter),
},
&self.metadata_map,
);
let response = self
.service_client
.lock()
.await
.get_filter_values(request)
.await
.map_err(|status| SdkApiError {
status,
endpoint: "/com.coralogixapis.incidents.v1.IncidentsService/GetFilterValues".into(),
feature_group: INCIDENTS_FEATURE_GROUP_ID.into(),
})?;
Ok(response.into_inner())
}
pub async fn assign_incidents(
&self,
incident_ids: Vec<String>,
assigned_to: Option<UserDetails>,
) -> Result<AssignIncidentsResponse, Box<dyn std::error::Error>> {
let request = make_request_with_metadata(
AssignIncidentsRequest {
incident_ids,
assigned_to,
},
&self.metadata_map,
);
let response = self
.service_client
.lock()
.await
.assign_incidents(request)
.await?;
Ok(response.into_inner())
}
pub async fn unassign_incidents(
&self,
incident_ids: Vec<String>,
) -> Result<UnassignIncidentsResponse, Box<dyn std::error::Error>> {
let request = make_request_with_metadata(
UnassignIncidentsRequest { incident_ids },
&self.metadata_map,
);
let response = self
.service_client
.lock()
.await
.unassign_incidents(request)
.await
.map_err(|status| SdkApiError {
status,
endpoint: "/com.coralogixapis.incidents.v1.IncidentsService/UnassignIncidents"
.into(),
feature_group: INCIDENTS_FEATURE_GROUP_ID.into(),
})?;
Ok(response.into_inner())
}
pub async fn acknowledge_incidents(
&self,
incident_ids: Vec<String>,
) -> Result<AcknowledgeIncidentsResponse, Box<dyn std::error::Error>> {
let request = make_request_with_metadata(
AcknowledgeIncidentsRequest { incident_ids },
&self.metadata_map,
);
let response = self
.service_client
.lock()
.await
.acknowledge_incidents(request)
.await
.map_err(|status| SdkApiError {
status,
endpoint: "/com.coralogixapis.incidents.v1.IncidentsService/AcknowledgeIncidents"
.into(),
feature_group: INCIDENTS_FEATURE_GROUP_ID.into(),
})?;
Ok(response.into_inner())
}
pub async fn close_incidents(
&self,
incident_ids: Vec<String>,
) -> Result<CloseIncidentsResponse, Box<dyn std::error::Error>> {
let request =
make_request_with_metadata(CloseIncidentsRequest { incident_ids }, &self.metadata_map);
let response = self
.service_client
.lock()
.await
.close_incidents(request)
.await
.map_err(|status| SdkApiError {
status,
endpoint: "/com.coralogixapis.incidents.v1.IncidentsService/CloseIncidents".into(),
feature_group: INCIDENTS_FEATURE_GROUP_ID.into(),
})?;
Ok(response.into_inner())
}
pub async fn get_incident_events(
&self,
incident_id: Option<String>,
) -> Result<GetIncidentEventsResponse, Box<dyn std::error::Error>> {
let request = make_request_with_metadata(
GetIncidentEventsRequest { incident_id },
&self.metadata_map,
);
let response = self
.service_client
.lock()
.await
.get_incident_events(request)
.await?;
Ok(response.into_inner())
}
pub async fn resolve_incidents(
&self,
incident_ids: Vec<String>,
) -> Result<ResolveIncidentsResponse, Box<dyn std::error::Error>> {
let request = make_request_with_metadata(
ResolveIncidentsRequest { incident_ids },
&self.metadata_map,
);
let response = self
.service_client
.lock()
.await
.resolve_incidents(request)
.await
.map_err(|status| SdkApiError {
status,
endpoint: "/com.coralogixapis.incidents.v1.IncidentsService/ResolveIncidents"
.into(),
feature_group: INCIDENTS_FEATURE_GROUP_ID.into(),
})?;
Ok(response.into_inner())
}
pub async fn get_incident_using_correlation_key(
&self,
correlation_key: String,
timestamp: OffsetDateTime,
) -> Result<GetIncidentUsingCorrelationKeyResponse, Box<dyn std::error::Error>> {
let request = make_request_with_metadata(
GetIncidentUsingCorrelationKeyRequest {
correlation_key: Some(correlation_key),
incident_point_in_time: Some(timestamp.to_string().parse().unwrap()),
},
&self.metadata_map,
);
let response = self
.service_client
.lock()
.await
.get_incident_using_correlation_key(request)
.await
.map_err(|status| SdkApiError {
status,
endpoint:
"/com.coralogixapis.incidents.v1.IncidentsService/GetIncidentUsingCorrelationKey"
.into(),
feature_group: INCIDENTS_FEATURE_GROUP_ID.into(),
})?;
Ok(response.into_inner())
}
pub async fn list_incident_events(
&self,
filter: Option<IncidentEventQueryFilter>,
pagination: Option<PaginationRequest>,
order_by: Option<ListIncidentEventRequestOrderBy>,
) -> Result<ListIncidentEventsResponse, Box<dyn std::error::Error>> {
let request = make_request_with_metadata(
ListIncidentEventsRequest {
filter,
pagination,
order_by,
},
&self.metadata_map,
);
let response = self
.service_client
.lock()
.await
.list_incident_events(request)
.await
.map_err(|status| SdkApiError {
status,
endpoint: "/com.coralogixapis.incidents.v1.IncidentsService/ListIncidentEvents"
.into(),
feature_group: INCIDENTS_FEATURE_GROUP_ID.into(),
})?;
Ok(response.into_inner())
}
pub async fn list_incident_events_total_count(
&self,
filter: Option<IncidentEventQueryFilter>,
) -> Result<ListIncidentEventsTotalCountResponse, Box<dyn std::error::Error>> {
let request = make_request_with_metadata(
ListIncidentEventsTotalCountRequest { filter },
&self.metadata_map,
);
let response = self
.service_client
.lock()
.await
.list_incident_events_total_count(request)
.await
.map_err(|status| SdkApiError {
status,
endpoint:
"/com.coralogixapis.incidents.v1.IncidentsService/ListIncidentEventsTotalCount"
.into(),
feature_group: INCIDENTS_FEATURE_GROUP_ID.into(),
})?;
Ok(response.into_inner())
}
pub async fn list_incident_events_filter_values(
&self,
filter: Option<IncidentEventQueryFilter>,
) -> Result<ListIncidentEventsFilterValuesResponse, Box<dyn std::error::Error>> {
let request = make_request_with_metadata(
ListIncidentEventsFilterValuesRequest { filter },
&self.metadata_map,
);
let response = self
.service_client
.lock()
.await
.list_incident_events_filter_values(request)
.await
.map_err(|status| SdkApiError {
status,
endpoint:
"/com.coralogixapis.incidents.v1.IncidentsService/ListIncidentEventsFilterValues"
.into(),
feature_group: INCIDENTS_FEATURE_GROUP_ID.into(),
})?;
Ok(response.into_inner())
}
}