1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
use bson::serde_helpers::hex_string_as_object_id;
use serde::{Deserialize, Serialize};
use strum_macros::{Display, EnumString};
use typeshare::typeshare;

use crate::{monitor_timestamp, Build, Deployment, Group, Operation, Procedure, Server, Version};

#[typeshare]
#[derive(Serialize, Deserialize, Debug, Clone, Default)]
pub struct Update {
    #[serde(
        default,
        rename = "_id",
        skip_serializing_if = "String::is_empty",
        with = "hex_string_as_object_id"
    )]
    pub id: String,
    pub target: UpdateTarget,
    pub operation: Operation,
    pub logs: Vec<Log>,
    pub start_ts: String,
    pub end_ts: Option<String>,
    pub status: UpdateStatus,
    pub success: bool,
    pub operator: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub version: Option<Version>,
}

#[typeshare]
#[derive(Serialize, Deserialize, Debug, Clone, Default)]
pub struct Log {
    pub stage: String,
    pub command: String,
    pub stdout: String,
    pub stderr: String,
    pub success: bool,
    pub start_ts: String,
    pub end_ts: String,
}

impl Log {
    pub fn simple(stage: &str, msg: String) -> Log {
        let ts = monitor_timestamp();
        Log {
            stage: stage.to_string(),
            stdout: msg,
            success: true,
            start_ts: ts.clone(),
            end_ts: ts,
            ..Default::default()
        }
    }

    pub fn error(stage: &str, msg: String) -> Log {
        let ts = monitor_timestamp();
        Log {
            stage: stage.to_string(),
            stderr: msg,
            start_ts: ts.clone(),
            end_ts: ts,
            success: false,
            ..Default::default()
        }
    }
}

#[typeshare]
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(tag = "type", content = "id")]
pub enum UpdateTarget {
    System,
    Build(String),
    Deployment(String),
    Server(String),
    Procedure(String),
    Group(String),
}

impl Default for UpdateTarget {
    fn default() -> Self {
        UpdateTarget::System
    }
}

impl From<&Build> for UpdateTarget {
    fn from(build: &Build) -> Self {
        Self::Build(build.id.clone())
    }
}

impl From<&Build> for Option<UpdateTarget> {
    fn from(build: &Build) -> Self {
        Some(UpdateTarget::Build(build.id.clone()))
    }
}

impl From<&Deployment> for UpdateTarget {
    fn from(deployment: &Deployment) -> Self {
        Self::Deployment(deployment.id.clone())
    }
}

impl From<&Deployment> for Option<UpdateTarget> {
    fn from(deployment: &Deployment) -> Self {
        Some(UpdateTarget::Deployment(deployment.id.clone()))
    }
}

impl From<&Server> for UpdateTarget {
    fn from(server: &Server) -> Self {
        Self::Server(server.id.clone())
    }
}

impl From<&Server> for Option<UpdateTarget> {
    fn from(server: &Server) -> Self {
        Some(UpdateTarget::Server(server.id.clone()))
    }
}

impl From<&Procedure> for UpdateTarget {
    fn from(procedure: &Procedure) -> Self {
        Self::Procedure(procedure.id.clone())
    }
}

impl From<&Procedure> for Option<UpdateTarget> {
    fn from(procedure: &Procedure) -> Self {
        Some(UpdateTarget::Procedure(procedure.id.clone()))
    }
}

impl From<&Group> for UpdateTarget {
    fn from(group: &Group) -> Self {
        Self::Group(group.id.clone())
    }
}

impl From<&Group> for Option<UpdateTarget> {
    fn from(group: &Group) -> Self {
        Some(UpdateTarget::Group(group.id.clone()))
    }
}

#[typeshare]
#[derive(Serialize, Deserialize, Debug, Display, EnumString, PartialEq, Hash, Eq, Clone, Copy)]
#[serde(rename_all = "snake_case")]
#[strum(serialize_all = "snake_case")]
pub enum UpdateStatus {
    Queued,
    InProgress,
    Complete,
}

impl Default for UpdateStatus {
    fn default() -> Self {
        UpdateStatus::Complete
    }
}