]>
Commit | Line | Data |
---|---|---|
063ca5be DM |
1 | use failure::*; |
2 | ||
3 | //use crate::tools; | |
4 | use crate::api_schema::*; | |
5 | use crate::api_schema::router::*; | |
6 | use serde_json::{json, Value}; | |
7 | ||
8 | use crate::server; | |
9 | ||
10 | fn list_tasks( | |
11 | param: Value, | |
12 | _info: &ApiMethod, | |
13 | rpcenv: &mut RpcEnvironment, | |
14 | ) -> Result<Value, Error> { | |
15 | ||
16 | let start = param["start"].as_u64().unwrap_or(0); | |
17 | let limit = param["limit"].as_u64().unwrap_or(50); | |
18 | let errors = param["errors"].as_bool().unwrap_or(false); | |
19 | ||
20 | let list = server::read_task_list()?; | |
21 | ||
22 | let mut result = vec![]; | |
23 | ||
24 | let mut count = 0; | |
25 | ||
26 | for info in list.iter() { | |
27 | let mut entry = json!({ | |
28 | "upid": info.upid_str, | |
29 | "node": "localhost", | |
30 | "pid": info.upid.pid, | |
31 | "pstart": info.upid.pstart, | |
32 | "starttime": info.upid.starttime, | |
33 | "type": info.upid.worker_type, | |
34 | "id": info.upid.worker_id, | |
35 | "user": info.upid.username, | |
36 | }); | |
37 | ||
38 | if let Some(ref state) = info.state { | |
39 | if errors && state.1 == "OK" { | |
40 | continue; | |
41 | } | |
42 | ||
43 | entry["endtime"] = Value::from(state.0); | |
44 | entry["status"] = Value::from(state.1.clone()); | |
45 | } | |
46 | ||
47 | if (count as u64) <= start { | |
48 | count += 1; | |
49 | continue; | |
50 | } else { | |
51 | count += 1; | |
52 | } | |
53 | ||
54 | if (result.len() as u64) < limit { result.push(entry); }; | |
55 | } | |
56 | ||
57 | rpcenv.set_result_attrib("total", Value::from(count)); | |
58 | ||
59 | Ok(json!(result)) | |
60 | } | |
61 | ||
62 | pub fn router() -> Router { | |
63 | ||
64 | let route = Router::new() | |
65 | .get(ApiMethod::new( | |
66 | list_tasks, | |
67 | ObjectSchema::new("List tasks.") | |
68 | .optional( | |
69 | "start", | |
70 | IntegerSchema::new("List tasks beginning from this offset.") | |
71 | .minimum(0) | |
72 | .default(0) | |
73 | ) | |
74 | .optional( | |
75 | "limit", | |
76 | IntegerSchema::new("Only list this amount of tasks.") | |
77 | .minimum(0) | |
78 | .default(50) | |
79 | ) | |
80 | .optional( | |
81 | "errors", | |
82 | BooleanSchema::new("Only list erroneous tasks.") | |
83 | ) | |
84 | ) | |
85 | ); | |
86 | ||
87 | route | |
88 | } |