Skip to main content

acts_next/export/
executor.rs

1mod act_executor;
2mod event_executor;
3mod message_executor;
4mod model_executor;
5mod package_executor;
6mod process_executor;
7mod task_executor;
8
9use serde::Serialize;
10use serde_json::json;
11
12use crate::{scheduler::Runtime, store::query::*};
13use std::sync::Arc;
14
15#[derive(Default, Debug)]
16pub struct ExecutorQuery {
17    pub query_by: Vec<(String, serde_json::Value)>,
18    pub order_by: Vec<(String, bool)>,
19
20    pub offset: usize,
21    pub count: usize,
22}
23
24#[derive(Clone)]
25pub struct Executor {
26    msg: message_executor::MessageExecutor,
27    act: act_executor::ActExecutor,
28    model: model_executor::ModelExecutor,
29    proc: process_executor::ProcessExecutor,
30    task: task_executor::TaskExecutor,
31    pack: package_executor::PackageExecutor,
32    evt: event_executor::EventExecutor,
33}
34
35impl ExecutorQuery {
36    pub fn new() -> Self {
37        Self::default()
38    }
39
40    pub fn with_offset(mut self, offset: usize) -> Self {
41        self.offset = offset;
42        self
43    }
44
45    pub fn with_count(mut self, count: usize) -> Self {
46        self.count = count;
47        self
48    }
49
50    pub fn with_order(mut self, order: &str, rev: bool) -> Self {
51        self.order_by.push((order.to_string(), rev));
52        self
53    }
54
55    pub fn with_query<T: Serialize>(mut self, key: &str, value: T) -> Self {
56        self.query_by.push((key.to_string(), json!(value)));
57        self
58    }
59
60    pub fn into_cond(&self) -> Cond {
61        let mut cond = Cond::and();
62        for (k, v) in self.query_by.iter() {
63            let mut key: &str = k;
64            if k == "type" {
65                key = "kind";
66            }
67            cond = cond.push(Expr::eq(key, v))
68        }
69        cond
70    }
71
72    pub fn into_query(&self) -> Query {
73        let mut query = Query::new().set_offset(self.offset).set_limit(self.count);
74        if !self.query_by.is_empty() {
75            query = query.push(self.into_cond())
76        }
77        query.set_order(&self.order_by)
78    }
79}
80
81impl Executor {
82    pub(crate) fn new(rt: &Arc<Runtime>) -> Self {
83        Self {
84            msg: message_executor::MessageExecutor::new(rt),
85            act: act_executor::ActExecutor::new(rt),
86            model: model_executor::ModelExecutor::new(rt),
87            proc: process_executor::ProcessExecutor::new(rt),
88            task: task_executor::TaskExecutor::new(rt),
89            pack: package_executor::PackageExecutor::new(rt),
90            evt: event_executor::EventExecutor::new(rt),
91        }
92    }
93
94    /// executor for related message functions
95    pub fn msg(&self) -> &message_executor::MessageExecutor {
96        &self.msg
97    }
98
99    /// executor for related act operations
100    /// such as 'complete', 'back', 'cancel' ..
101    pub fn act(&self) -> &act_executor::ActExecutor {
102        &self.act
103    }
104
105    /// executor for related model functions
106    pub fn model(&self) -> &model_executor::ModelExecutor {
107        &self.model
108    }
109
110    /// executor for related process functions
111    pub fn proc(&self) -> &process_executor::ProcessExecutor {
112        &self.proc
113    }
114
115    /// executor for related task functions
116    pub fn task(&self) -> &task_executor::TaskExecutor {
117        &self.task
118    }
119
120    /// executor for related package functions
121    pub fn pack(&self) -> &package_executor::PackageExecutor {
122        &self.pack
123    }
124
125    /// executor for related event functions
126    pub fn evt(&self) -> &event_executor::EventExecutor {
127        &self.evt
128    }
129}