forked from googlemaps/fleet-debugger
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTask.js
More file actions
74 lines (70 loc) · 2.42 KB
/
Task.js
File metadata and controls
74 lines (70 loc) · 2.42 KB
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
/*
* src/Task.js
*
* Processed log for a task. Handles computing the state of
* a task at a specified time.
*/
import _ from "lodash";
class Task {
constructor(date, taskIdx, taskId, taskReq, taskResp) {
this.taskIdx = taskIdx;
this.taskId = taskId;
this.updates = [];
this.firstUpdate = date;
this.addUpdate(date, taskReq, taskResp);
}
/**
* Returns the status of the task at the specified date. Note that
* many task changes are actually done as side-effects of vehicle changes
* and thus the debugger only has visibily into a task change if there
* is an update_task call made.
*/
getTaskInfo(maxDate) {
const taskInfo = {
taskid: this.taskId,
};
const lastUpdate = _(this.updates)
.filter((update) => update.date <= maxDate)
.last();
if (lastUpdate) {
// The create vs update task input and output protos are annoyingly
// different. The following code attemps to handle both.
taskInfo.type = lastUpdate.taskResp.type || lastUpdate.taskReq.task.type;
taskInfo.plannedlocation = lastUpdate.taskResp.plannedlocation || lastUpdate.taskReq.task.plannedlocation;
taskInfo.taskoutcome = lastUpdate.taskResp.taskoutcome;
taskInfo.state = lastUpdate.taskResp.state;
taskInfo.taskoutcomelocationsource = lastUpdate.taskResp.taskoutcomelocationsource;
taskInfo.taskoutcomelocation = lastUpdate.taskResp.taskoutcomelocation;
taskInfo.taskoutcometime = lastUpdate.taskResp.taskoutcometime;
taskInfo.trackingid = lastUpdate.taskResp.trackingid || lastUpdate.taskReq.task.trackingid;
if (
taskInfo.taskoutcomelocationsource &&
taskInfo.plannedlocation &&
taskInfo.plannedlocation.point &&
taskInfo.taskoutcomelocation &&
taskInfo.taskoutcomelocation.point
) {
taskInfo.plannedVsActualDeltaMeters = window.google.maps.geometry.spherical.computeDistanceBetween(
{
lat: taskInfo.plannedlocation.point.latitude,
lng: taskInfo.plannedlocation.point.longitude,
},
{
lat: taskInfo.taskoutcomelocation.point.latitude,
lng: taskInfo.taskoutcomelocation.point.longitude,
}
);
}
}
return taskInfo;
}
addUpdate(date, taskReq, taskResp) {
this.lastUpdate = date;
this.updates.push({
date,
taskReq,
taskResp,
});
}
}
export { Task as default };