databuild/databuild/event_transforms.rs

245 lines
6.8 KiB
Rust

use crate::PartitionStatusCode::{PartitionFailed, PartitionLive};
use crate::data_build_event::Event;
use crate::job_run_state::{JobInfo, JobRunWithState, QueuedState};
use crate::util::current_timestamp;
use crate::{
CancelWantRequest, CancelWantResponse, CreateTaintRequest, CreateTaintResponse,
CreateWantRequest, CreateWantResponse, EventSource, GetWantResponse, JobRunBufferEventV1,
JobRunDetail, JobRunStatus, JobRunStatusCode, JobTriggeredEvent, ManuallyTriggeredEvent,
PartitionDetail, PartitionRef, PartitionStatus, PartitionStatusCode, TaintCancelEventV1,
TaintCreateEventV1, TaintDetail, WantAttributedPartitions, WantCancelEventV1,
WantCreateEventV1, WantDetail, WantStatus, WantStatusCode, event_source,
};
use uuid::Uuid;
impl From<&WantCreateEventV1> for WantDetail {
fn from(e: &WantCreateEventV1) -> Self {
e.clone().into()
}
}
impl From<WantCreateEventV1> for WantDetail {
fn from(e: WantCreateEventV1) -> Self {
WantDetail {
want_id: e.want_id,
partitions: e.partitions,
upstreams: vec![],
data_timestamp: e.data_timestamp,
ttl_seconds: e.ttl_seconds,
sla_seconds: e.sla_seconds,
source: e.source,
comment: e.comment,
status: Some(WantStatusCode::WantIdle.into()),
last_updated_timestamp: current_timestamp(),
}
}
}
impl From<WantCreateEventV1> for Event {
fn from(value: WantCreateEventV1) -> Self {
Event::WantCreateV1(value)
}
}
impl From<WantCancelEventV1> for Event {
fn from(value: WantCancelEventV1) -> Self {
Event::WantCancelV1(value)
}
}
impl From<TaintCreateEventV1> for Event {
fn from(value: TaintCreateEventV1) -> Self {
Event::TaintCreateV1(value)
}
}
impl From<TaintCancelEventV1> for Event {
fn from(value: TaintCancelEventV1) -> Self {
Event::TaintCancelV1(value)
}
}
impl From<WantCreateEventV1> for WantAttributedPartitions {
fn from(value: WantCreateEventV1) -> Self {
Self {
want_id: value.want_id,
partitions: value.partitions,
}
}
}
impl From<WantStatusCode> for WantStatus {
fn from(code: WantStatusCode) -> Self {
WantStatus {
code: code.into(),
name: code.as_str_name().to_string(),
}
}
}
impl From<JobRunBufferEventV1> for JobRunDetail {
fn from(value: JobRunBufferEventV1) -> Self {
Self {
id: value.job_run_id,
status: Some(JobRunStatusCode::JobRunQueued.into()),
last_heartbeat_at: None,
building_partitions: value.building_partitions,
servicing_wants: value.want_attributed_partitions,
}
}
}
impl From<JobRunBufferEventV1> for JobRunWithState<QueuedState> {
fn from(event: JobRunBufferEventV1) -> Self {
JobRunWithState {
info: JobInfo {
id: event.job_run_id,
building_partitions: event.building_partitions,
servicing_wants: event.want_attributed_partitions,
},
state: QueuedState {
queued_at: current_timestamp(),
},
}
}
}
pub fn want_status_matches_any(
pds: &Vec<Option<PartitionDetail>>,
status: PartitionStatusCode,
) -> bool {
pds.iter().any(|pd| {
pd.clone()
.map(|pd| pd.status == Some(status.into()))
.unwrap_or(false)
})
}
pub fn want_status_matches_all(
pds: &Vec<Option<PartitionDetail>>,
status: PartitionStatusCode,
) -> bool {
pds.iter().all(|pd| {
pd.clone()
.map(|pd| pd.status == Some(status.into()))
.unwrap_or(false)
})
}
/// Merges a list of partition details into a single status code.
/// Takes the lowest state as the want status.
impl Into<WantStatusCode> for Vec<Option<PartitionDetail>> {
fn into(self) -> WantStatusCode {
if want_status_matches_any(&self, PartitionFailed) {
WantStatusCode::WantFailed
} else if want_status_matches_all(&self, PartitionLive) {
WantStatusCode::WantSuccessful
} else if self.iter().any(|pd| pd.is_none()) {
WantStatusCode::WantBuilding
} else {
WantStatusCode::WantIdle
}
}
}
impl From<&str> for PartitionRef {
fn from(value: &str) -> Self {
Self {
r#ref: value.to_string(),
}
}
}
impl From<PartitionStatusCode> for PartitionStatus {
fn from(code: PartitionStatusCode) -> Self {
PartitionStatus {
code: code.into(),
name: code.as_str_name().to_string(),
}
}
}
impl From<JobRunStatusCode> for JobRunStatus {
fn from(code: JobRunStatusCode) -> Self {
JobRunStatus {
code: code.into(),
name: code.as_str_name().to_string(),
}
}
}
impl From<ManuallyTriggeredEvent> for EventSource {
fn from(value: ManuallyTriggeredEvent) -> Self {
Self {
source: Some(event_source::Source::ManuallyTriggered(value)),
}
}
}
impl From<JobTriggeredEvent> for EventSource {
fn from(value: JobTriggeredEvent) -> Self {
Self {
source: Some(event_source::Source::JobTriggered(value)),
}
}
}
impl From<&WantDetail> for WantAttributedPartitions {
fn from(value: &WantDetail) -> Self {
Self {
want_id: value.want_id.clone(),
partitions: value.partitions.clone(),
}
}
}
impl From<CreateWantRequest> for WantCreateEventV1 {
fn from(value: CreateWantRequest) -> Self {
WantCreateEventV1 {
want_id: Uuid::new_v4().into(),
partitions: value.partitions,
data_timestamp: value.data_timestamp,
ttl_seconds: value.ttl_seconds,
sla_seconds: value.sla_seconds,
source: value.source,
comment: value.comment,
}
}
}
impl Into<CreateWantResponse> for Option<WantDetail> {
fn into(self) -> CreateWantResponse {
CreateWantResponse { data: self }
}
}
impl Into<GetWantResponse> for Option<WantDetail> {
fn into(self) -> GetWantResponse {
GetWantResponse { data: self }
}
}
impl From<CancelWantRequest> for WantCancelEventV1 {
fn from(value: CancelWantRequest) -> Self {
WantCancelEventV1 {
want_id: value.want_id,
source: value.source,
comment: value.comment,
}
}
}
impl Into<CancelWantResponse> for Option<WantDetail> {
fn into(self) -> CancelWantResponse {
CancelWantResponse { data: self }
}
}
impl From<CreateTaintRequest> for TaintCreateEventV1 {
fn from(value: CreateTaintRequest) -> Self {
todo!()
}
}
impl Into<CreateTaintResponse> for Option<TaintDetail> {
fn into(self) -> CreateTaintResponse {
CreateTaintResponse {
// TODO
}
}
}