2019-04-07 10:18:58 +00:00
|
|
|
use failure::*;
|
|
|
|
|
|
|
|
//use crate::tools;
|
|
|
|
use crate::api_schema::*;
|
|
|
|
use crate::api_schema::router::*;
|
|
|
|
use serde_json::{json, Value};
|
|
|
|
|
|
|
|
use crate::server;
|
|
|
|
|
|
|
|
fn list_tasks(
|
|
|
|
param: Value,
|
|
|
|
_info: &ApiMethod,
|
|
|
|
rpcenv: &mut RpcEnvironment,
|
|
|
|
) -> Result<Value, Error> {
|
|
|
|
|
|
|
|
let start = param["start"].as_u64().unwrap_or(0);
|
|
|
|
let limit = param["limit"].as_u64().unwrap_or(50);
|
|
|
|
let errors = param["errors"].as_bool().unwrap_or(false);
|
|
|
|
|
2019-04-07 11:17:19 +00:00
|
|
|
let userfilter = param["userfilter"].as_str();
|
|
|
|
|
2019-04-07 10:18:58 +00:00
|
|
|
let list = server::read_task_list()?;
|
|
|
|
|
|
|
|
let mut result = vec![];
|
|
|
|
|
|
|
|
let mut count = 0;
|
|
|
|
|
|
|
|
for info in list.iter() {
|
|
|
|
let mut entry = json!({
|
|
|
|
"upid": info.upid_str,
|
|
|
|
"node": "localhost",
|
|
|
|
"pid": info.upid.pid,
|
|
|
|
"pstart": info.upid.pstart,
|
|
|
|
"starttime": info.upid.starttime,
|
|
|
|
"type": info.upid.worker_type,
|
|
|
|
"id": info.upid.worker_id,
|
|
|
|
"user": info.upid.username,
|
|
|
|
});
|
|
|
|
|
2019-04-07 11:17:19 +00:00
|
|
|
if let Some(username) = userfilter {
|
|
|
|
if !info.upid.username.contains(username) { continue; }
|
|
|
|
}
|
|
|
|
|
2019-04-07 10:18:58 +00:00
|
|
|
if let Some(ref state) = info.state {
|
|
|
|
if errors && state.1 == "OK" {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
entry["endtime"] = Value::from(state.0);
|
|
|
|
entry["status"] = Value::from(state.1.clone());
|
|
|
|
}
|
|
|
|
|
2019-04-07 10:41:24 +00:00
|
|
|
if (count as u64) < start {
|
2019-04-07 10:18:58 +00:00
|
|
|
count += 1;
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
count += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (result.len() as u64) < limit { result.push(entry); };
|
|
|
|
}
|
|
|
|
|
|
|
|
rpcenv.set_result_attrib("total", Value::from(count));
|
|
|
|
|
|
|
|
Ok(json!(result))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn router() -> Router {
|
|
|
|
|
|
|
|
let route = Router::new()
|
|
|
|
.get(ApiMethod::new(
|
|
|
|
list_tasks,
|
|
|
|
ObjectSchema::new("List tasks.")
|
|
|
|
.optional(
|
|
|
|
"start",
|
|
|
|
IntegerSchema::new("List tasks beginning from this offset.")
|
|
|
|
.minimum(0)
|
|
|
|
.default(0)
|
|
|
|
)
|
|
|
|
.optional(
|
|
|
|
"limit",
|
|
|
|
IntegerSchema::new("Only list this amount of tasks.")
|
|
|
|
.minimum(0)
|
|
|
|
.default(50)
|
|
|
|
)
|
|
|
|
.optional(
|
|
|
|
"errors",
|
|
|
|
BooleanSchema::new("Only list erroneous tasks.")
|
|
|
|
)
|
2019-04-07 11:17:19 +00:00
|
|
|
.optional(
|
|
|
|
"userfilter",
|
|
|
|
StringSchema::new("Only list tasks from this user.")
|
|
|
|
)
|
|
|
|
)
|
2019-04-07 10:18:58 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
route
|
|
|
|
}
|