Browse Source

api save task add parameter status

master
filesite 7 months ago
parent
commit
b58d54f0c8
  1. 16
      heroUnion.mjs
  2. 52
      public/index.html
  3. 7
      router_api.mjs

16
heroUnion.mjs

@ -253,7 +253,8 @@ class HeroUnion { @@ -253,7 +253,8 @@ class HeroUnion {
}
//保存处理中任务结果
saveTaskById(bot_name, id, data) {
//增加失败状态设置
saveTaskById(bot_name, id, data, status) {
let done = false;
let taskIndex = this.tasks.findIndex((item) => item.id == id && item.status == 'running');
@ -276,9 +277,16 @@ class HeroUnion { @@ -276,9 +277,16 @@ class HeroUnion {
//更新统计数据
this.taskStatus.running --;
this.taskStatus.done ++;
this.tasks[taskIndex].status = 'done';
if (typeof(status) == 'undefined' || status == 'done') {
this.taskStatus.done ++;
this.tasks[taskIndex].status = 'done';
}else if (typeof(status) != 'undefined' && status == 'failed') {
this.taskStatus.failed ++;
this.tasks[taskIndex].status = 'failed';
this.tasks[taskIndex].error = typeof(data.error) != 'undefined' && data.error ?
data.error : 'HeroBot says it failed.';
common.error('Task %s is failed, save by bot %s', id, bot_name);
}
common.log('Task %s save data done by bot %s.', id, bot_name);
done = true;

52
public/index.html

@ -163,26 +163,38 @@ @@ -163,26 +163,38 @@
$('#herobots').html(html);
};
var api = '/api/stats/';
$.ajax({
url: api,
method: 'GET',
dataType: 'json'
}).done(function(data) {
$('#herounion_stats').text(JSON.stringify(data, null, 4));
$('.run_time').text( formatSeconds(data.run_seconds) );
renderStats('taskStatus', data.taskStatus);
renderStats('heroStatus', data.heroStatus);
});
api = '/api/heros/';
$.ajax({
url: api,
method: 'GET',
dataType: 'json'
}).done(function(data) {
renderHeros(data);
});
var loadUnionStats = function() {
$('.run_time').text('...');
var api = '/api/stats/';
$.ajax({
url: api,
method: 'GET',
dataType: 'json'
}).done(function(data) {
$('#herounion_stats').text(JSON.stringify(data, null, 4));
$('.run_time').text( formatSeconds(data.run_seconds) );
renderStats('taskStatus', data.taskStatus);
renderStats('heroStatus', data.heroStatus);
});
};
var loadHeros = function() {
$('#herobots').text('...');
var api = '/api/heros/';
$.ajax({
url: api,
method: 'GET',
dataType: 'json'
}).done(function(data) {
renderHeros(data);
});
};
//init
loadUnionStats();
loadHeros();
setInterval(loadUnionStats, 5000);
</script>
</body>
</html>

7
router_api.mjs

@ -230,6 +230,7 @@ router.post('/savetask/', async (req, res) => { @@ -230,6 +230,7 @@ router.post('/savetask/', async (req, res) => {
let name = req.body.name,
task_id = req.body.task_id,
task_result = req.body.task_result,
task_status = req.body.status,
sign = req.body.sign;
let data = {code: 0, message: ''};
@ -261,11 +262,15 @@ router.post('/savetask/', async (req, res) => { @@ -261,11 +262,15 @@ router.post('/savetask/', async (req, res) => {
task_result: task_result
};
if (typeof(task_status) != 'undefined' && task_status == 'failed') {
paramsCheck.status = task_status;
}
let mySign = common.sign(paramsCheck, task.token);
if (mySign.toLowerCase() != sign.toLowerCase()) {
data.message = `签名 ${sign} 不匹配,请确保token正确及签名方法跟文档一致`;
}else {
let saved = heroUnion.saveTaskById(name, task_id, task_result);
let saved = heroUnion.saveTaskById(name, task_id, task_result, task_status);
if (saved) {
data.code = 1;
data.message = '保存任务数据完成';

Loading…
Cancel
Save