1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
<template>
  <el-card shadow="never" :body-style="{ padding: 0 }">
    <template #header>
      <el-row justify="space-between">
        <div>
          <div><el-text tag="b" size="large">后台任务</el-text></div>
          <el-text size="small" type="info">显示当前正在进行的后台耗时任务状态</el-text>
        </div>
        <el-button
          icon="Refresh"
          type="primary"
          size="default"
          :loading="loading"
          @click="fetchTask"
          >刷新任务</el-button
        >
      </el-row>
      <!-- <el-row>
        <el-button type="default" size="default" @click="newTestTask">新增测试任务</el-button>
        <el-button type="default" size="default" @click="startNewTestTask"
          >新建并运行一个测试任务</el-button
        >
        <el-button type="default" size="default" @click="shutDownTask"
          >强制关闭所有测试任务</el-button
        >
      </el-row> -->
    </template>
    <el-scrollbar height="70vh" class="scrollbar">
      <template v-for="(v, i) in taskList" :key="i">
        <FYBgTaskItem
          :model="v"
          :index="i"
          @start="startTask"
          @shutDown="shutDownTask"
          @remove="removeTask"
          @gotoResult="gotoResult"
        ></FYBgTaskItem>
      </template>
    </el-scrollbar>
  </el-card>
</template>
<script>
/**
 * 自动评估任务管理
 */
import { useFetchData } from '@/composables/fetchData';
import bgtaskApi from '@/api/fysp/bgtaskApi';
import { enumBgTask, BG_TASK_TYPE, BG_TASK_STATUS } from '@/enum/bgTask';
import { SOCKET_MESSAGE_TYPE } from '@/enum/socketMessage';
import MessageManager from '@/socket/MessageManager.js';
 
export default {
  setup() {
    const { loading, fetchData } = useFetchData();
    return { loading, fetchData };
  },
  props: {
    modelValue: Number
  },
  emits: ['update:modelValue'],
  data() {
    return {
      taskList: [],
      taskIndex: 0
    };
  },
  watch: {
    taskList: {
      handler(nV) {
        let count = 0;
        for (const e of nV) {
          if (e.status == BG_TASK_STATUS.RUNNING.name) {
            count++;
          }
        }
        this.$emit('update:modelValue', count);
      },
      deep: true
    }
  },
  methods: {
    registerBgTaskMessage() {
      MessageManager.register(SOCKET_MESSAGE_TYPE.BACKGROUND_TASK.name, (data) => {
        this.refreshTaskById(data)
      })
    },
    computeRunTime(data) {
      const taskStatus = data.status
      const startTime = new Date(data.startTime).getTime()
      const endTime = new Date(data.endTime).getTime()
      const now = (new Date()).getTime();
      switch (taskStatus) {
        case BG_TASK_STATUS.WAITING.name:
          return 0;
        case BG_TASK_STATUS.RUNNING.name:
          return (now - startTime) / 1000; // 转换为秒
        case BG_TASK_STATUS.SUCCESS.name:
        case BG_TASK_STATUS.FAIL.name:
        case BG_TASK_STATUS.SHUTDOWN.name:
          return (endTime - startTime) / 1000; // 转换为秒
      }
    },
    /**
     * 刷新一个任务通过id,如果是新的任务则添加到任务列表taskList中
     * @param data 
     */
    refreshTaskById(data) {
      if (!data || data == {}) {
        return;
      }
      let isNewTask = true
      for (let index = 0; index < this.taskList.length; index++) {
        const task = this.taskList[index];
        if (task.id == data.id) {
          this.taskList[index] = data
          // 时间转换
          const hasStartTime = 'startTime' in data && data.startTime != {}
          const hasEndTime = 'endTime' in data && data.endTime != {}
          if (hasEndTime) {
            this.taskList[index].endTime = this.formatDate(data.endTime)
          }
          if (hasStartTime) {
            this.taskList[index].startTime = this.formatDate(data.startTime)
          }
          if (hasStartTime && hasEndTime) {
            this.taskList[index].runTime = this.computeRunTime(this.taskList[index])
          }
          isNewTask = false
          break
        }
      }
      if (isNewTask) {
        this.taskList.push(data)
      }
    },
    /**
     * 拷贝属性
     * @param from
     * @param to 
     */
    copyProperties(from, to) {
      for (const prop in to) {
        if (prop in from) {
          to[prop] = from[prop]
        }
      }
    },
    formatDate(date) {
      const dateObj = date.date
      const timeObj = date.time
      let year = dateObj.year;
      let month = dateObj.month < 10 ? '0' + dateObj.month : dateObj.month;
      let day = dateObj.day < 10 ? '0' + dateObj.day : dateObj.day;
      let hour = timeObj.hour < 10 ? '0' + timeObj.hour : timeObj.hour;
      let minute = timeObj.minute < 10 ? '0' + timeObj.minute : timeObj.minute;
      let second = timeObj.second < 10 ? '0' + timeObj.second : timeObj.second;
 
      return `${year}-${month}-${day} ${hour}:${minute}:${second}`;
    },
    addTask() {},
    newTestTask() {
      this.fetchData((page, pageSize) => {
        return bgtaskApi.newTestTask(`Test-Task-${++this.taskIndex}`).then((res) => {
          this.taskList.push(res.data);
        });
      });
    },
    startNewTestTask() {
      this.fetchData((page, pageSize) => {
        return bgtaskApi.startNewTestTask(`Test-Task-${++this.taskIndex}`).then((res) => {
          this.taskList.push(res.data);
        });
      });
    },
 
    _getParam(taskStatus) {
      return {
        type: taskStatus.type,
        id: taskStatus.id
      };
    },
    fetchTask() {
      this.fetchData((page, pageSize) => {
        return bgtaskApi
          .fetchTaskStatus({
            // type: BG_TASK_TYPE.AUTO_SCORE.name
          })
          .then((res) => {
            this.taskList = res.data;
          });
      });
    },
    startTask(index, callback) {
      this.fetchData((page, pageSize) => {
        const param = this._getParam(this.taskList[index]);
        return bgtaskApi.startTask(param).then((res) => {
          this.taskList[index] = res.data;
          callback(true);
        });
      });
    },
    shutDownTask(index, callback) {
      this.fetchData((page, pageSize) => {
        const param = this._getParam(this.taskList[index]);
        return bgtaskApi.shutDownTask(param).then((res) => {
          if (index && res.data && res.data.length == 1) {
            this.taskList[index] = res.data[0];
          } else {
            res.data.forEach((e) => {
              let v = this.taskList.find((value) => {
                return value.id == e.id;
              });
              const i = this.taskList.indexOf(v);
              this.taskList[i] = e;
            });
          }
          callback(true);
        });
      });
    },
    removeTask(index, callback) {
      this.fetchData((page, pageSize) => {
        const param = this._getParam(this.taskList[index]);
        return bgtaskApi.removeTask(param).then((res) => {
          if (res.data) {
            this.taskList.splice(index, 1);
            callback(true);
          }
        });
      });
    },
    gotoResult(index) {}
  },
  created() {
    this.registerBgTaskMessage()
  },
  mounted() {
    this.fetchTask();
    // setInterval(() => {
    //   this.fetchTask();
    // }, 10000);
  }
};
</script>
<style scoped>
.scrollbar {
  padding: 8px;
}
</style>