1use crate::{
2 ActError, ActRunAs, MessageState, Result, Workflow,
3 package::ActPackageCatalog,
4 scheduler::{self, NodeData},
5 store::data,
6};
7use serde::{Deserialize, Serialize};
8use std::sync::Arc;
9
10#[derive(Debug, Deserialize, Serialize, Clone)]
11pub struct PackageInfo {
12 pub id: String,
13
14 pub desc: String,
15 pub icon: String,
16 pub doc: String,
17 pub version: String,
18 pub schema: String,
19 pub run_as: ActRunAs,
20 pub resources: String,
21 pub catalog: ActPackageCatalog,
22
23 pub create_time: i64,
24 pub update_time: i64,
25 pub timestamp: i64,
26}
27
28#[derive(Debug, Deserialize, Serialize, Clone)]
29pub struct ProcInfo {
30 pub id: String,
31 pub name: String,
32 pub mid: String,
33 pub state: String,
34 pub start_time: i64,
35 pub end_time: i64,
36 pub timestamp: i64,
37 pub tasks: Vec<TaskInfo>,
38}
39
40#[derive(Debug, Deserialize, Serialize, Clone)]
41pub struct TaskInfo {
42 pub id: String,
43 pub prev: Option<String>,
44 pub name: String,
45 pub tag: String,
46 pub key: String,
47 pub pid: String,
48 pub nid: String,
49 pub r#type: String,
50 pub state: String,
51 pub data: String,
52 pub start_time: i64,
53 pub end_time: i64,
54 pub timestamp: i64,
55}
56
57#[derive(Debug, Deserialize, Serialize, Clone)]
58pub struct ModelInfo {
59 pub id: String,
60 pub name: String,
61 pub ver: i32,
62 pub size: i32,
63 pub create_time: i64,
64 pub update_time: i64,
65 pub data: String,
66}
67
68#[derive(Debug, Deserialize, Serialize, Clone)]
69pub struct MessageInfo {
70 pub id: String,
71 pub tid: String,
72 pub name: String,
73 pub state: MessageState,
74 pub r#type: String,
75 pub pid: String,
76 pub nid: String,
77 pub key: String,
78 pub inputs: String,
79 pub outputs: String,
80 pub tag: String,
81 pub create_time: i64,
82 pub update_time: i64,
83 pub retry_times: i32,
84 pub status: String,
85 pub timestamp: i64,
86 pub uses: String,
87}
88
89#[derive(Debug, Deserialize, Serialize, Clone)]
90pub struct EventInfo {
91 pub id: String,
92 pub name: String,
93 pub mid: String,
94 pub ver: i32,
95
96 pub uses: String,
97 pub params: String,
98
99 pub create_time: i64,
100 pub timestamp: i64,
101}
102
103impl From<&data::Package> for PackageInfo {
104 fn from(m: &data::Package) -> Self {
105 Self {
106 id: m.id.clone(),
107 desc: m.desc.clone(),
108 icon: m.icon.clone(),
109 doc: m.doc.clone(),
110 version: m.version.clone(),
111 schema: m.schema.clone(),
112 run_as: m.run_as,
113 resources: m.resources.clone(),
114 catalog: m.catalog,
115
116 timestamp: m.timestamp,
117 create_time: m.create_time,
118 update_time: m.update_time,
119 }
120 }
121}
122
123impl ModelInfo {
124 pub fn workflow(&self) -> Result<Workflow> {
125 let m = serde_yaml::from_str::<Workflow>(&self.data);
126 match m {
127 Ok(mut m) => {
128 m.set_ver(self.ver);
129 Ok(m)
130 }
131 Err(err) => Err(ActError::Convert(err.to_string())),
132 }
133 }
134}
135
136impl From<data::Model> for ModelInfo {
137 fn from(m: data::Model) -> Self {
138 Self {
139 id: m.id,
140 name: m.name,
141 ver: m.ver,
142 size: m.size,
143 create_time: m.create_time,
144 update_time: m.update_time,
145 data: m.data,
146 }
147 }
148}
149
150impl From<&data::Model> for ModelInfo {
151 fn from(m: &data::Model) -> Self {
152 m.clone().into()
153 }
154}
155
156impl From<&data::Proc> for ProcInfo {
157 fn from(p: &data::Proc) -> Self {
158 Self {
159 id: p.id.clone(),
160 name: p.name.clone(),
161 mid: p.mid.clone(),
162 state: p.state.clone(),
163 start_time: p.start_time,
164 end_time: p.end_time,
165 timestamp: p.timestamp,
166 tasks: Vec::new(),
167 }
168 }
169}
170
171impl From<data::Task> for TaskInfo {
172 fn from(t: data::Task) -> Self {
173 let node_data: NodeData = serde_json::from_str(&t.node_data).unwrap();
174 Self {
175 id: t.tid,
176 prev: t.prev,
177 name: t.name,
178 pid: t.pid,
179 nid: node_data.id,
180 r#type: t.kind,
181 state: t.state,
182 data: t.data,
183 start_time: t.start_time,
184 end_time: t.end_time,
185 timestamp: t.timestamp,
186 key: node_data.content.key(),
187 tag: node_data.content.tag(),
188 }
189 }
190}
191
192impl From<&data::Task> for TaskInfo {
193 fn from(t: &data::Task) -> Self {
194 t.clone().into()
195 }
196}
197
198impl From<&Arc<scheduler::Task>> for TaskInfo {
199 fn from(t: &Arc<scheduler::Task>) -> Self {
200 Self {
201 id: t.id.clone(),
202 prev: t.prev(),
203 name: t.node().content.name(),
204 pid: t.pid.clone(),
205 nid: t.node().id().to_string(),
206 r#type: t.node().kind().to_string(),
207 state: t.state().into(),
208 data: t.data().to_string(),
209 start_time: t.start_time(),
210 end_time: t.end_time(),
211 timestamp: t.timestamp,
212 tag: t.node().tag(),
213 key: t.node().key(),
214 }
215 }
216}
217
218impl From<&data::Message> for MessageInfo {
219 fn from(m: &data::Message) -> Self {
220 Self {
221 id: m.id.clone(),
222 name: m.name.clone(),
223 pid: m.pid.clone(),
224 tid: m.tid.clone(),
225 nid: m.nid.clone(),
226 timestamp: m.timestamp,
227 create_time: m.create_time,
228 update_time: m.update_time,
229 state: m.state,
230 r#type: m.r#type.clone(),
231 key: m.key.clone(),
232 tag: m.tag.clone(),
233
234 inputs: m.inputs.clone(),
235 outputs: m.outputs.clone(),
236 retry_times: m.retry_times,
237 status: m.status.to_string(),
238 uses: m.uses.clone(),
239 }
240 }
241}
242
243impl From<PackageInfo> for serde_json::Value {
244 fn from(val: PackageInfo) -> Self {
245 serde_json::to_value(val).unwrap()
246 }
247}
248
249impl From<TaskInfo> for serde_json::Value {
250 fn from(val: TaskInfo) -> Self {
251 serde_json::to_value(val).unwrap()
252 }
253}
254
255impl From<ProcInfo> for serde_json::Value {
256 fn from(val: ProcInfo) -> Self {
257 serde_json::to_value(val).unwrap()
258 }
259}
260
261impl From<ModelInfo> for serde_json::Value {
262 fn from(val: ModelInfo) -> Self {
263 serde_json::to_value(val).unwrap()
264 }
265}
266
267impl From<MessageInfo> for serde_json::Value {
268 fn from(val: MessageInfo) -> Self {
269 serde_json::to_value(val).unwrap()
270 }
271}
272
273impl From<&data::Event> for EventInfo {
274 fn from(m: &data::Event) -> Self {
275 Self {
276 id: m.id.clone(),
277 name: m.name.clone(),
278 timestamp: m.timestamp,
279 create_time: m.create_time,
280 mid: m.mid.clone(),
281 ver: m.ver,
282 uses: m.uses.clone(),
283 params: m.params.clone(),
284 }
285 }
286}
287
288impl From<EventInfo> for serde_json::Value {
289 fn from(val: EventInfo) -> Self {
290 serde_json::to_value(val).unwrap()
291 }
292}