Spaces:
Sleeping
Sleeping
File size: 7,695 Bytes
66c0efc ae32abe 66c0efc ae32abe 66c0efc | 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 | /* app-utils.js — shared helpers for the Bergamo FSR demo */
(function () {
'use strict';
var FSR = window.FSR = window.FSR || {};
FSR.utils = {
assignedVisitSet: assignedVisitSet,
buildCurl: buildCurl,
clonePlan: clonePlan,
fetchDemoCatalog: fetchDemoCatalog,
fetchDemoPlan: fetchDemoPlan,
fetchJobRoutes: fetchJobRoutes,
findHeaderButton: findHeaderButton,
formatDuration: formatDuration,
iconForVisit: iconForVisit,
legFor: legFor,
locationLat: locationLat,
locationLng: locationLng,
maskContains: maskContains,
routeBadges: routeBadges,
routeSchedule: routeSchedule,
routeStats: routeStats,
timeLabel: timeLabel,
title: title,
toneForRoute: toneForRoute,
};
function buildCurl(method, path, json) {
var parts = ['curl'];
if (method && method !== 'GET') parts.push('-X', method);
if (json) parts.push('-H', '"Content-Type: application/json"', '-d', '@plan.json');
parts.push(window.location.origin + path);
return parts.join(' ');
}
function fetchDemoCatalog() {
return requestJson('/demo-data', 'demo data catalog').then(function (catalog) {
if (!catalog || typeof catalog.defaultId !== 'string' || !Array.isArray(catalog.availableIds)) {
throw new Error('demo data catalog is missing defaultId or availableIds');
}
return { defaultId: catalog.defaultId, availableIds: catalog.availableIds.slice() };
});
}
function fetchDemoPlan(demoId) {
return requestJson('/demo-data/' + encodeURIComponent(demoId), 'demo data "' + demoId + '"');
}
function fetchJobRoutes(jobId, snapshotRevision) {
return requestJson(
'/jobs/' + encodeURIComponent(jobId) + '/routes?snapshot_revision=' + encodeURIComponent(snapshotRevision),
'route geometry for job "' + jobId + '"'
);
}
function requestJson(path, label) {
return fetch(path).then(function (response) {
if (!response.ok) throw new Error(label + ' returned HTTP ' + response.status);
return response.json();
});
}
function routeSchedule(plan, route) {
var visits = plan.service_visits || [];
var entries = [];
var clock = route.shift_start_minute;
var previous = route.start_location_idx;
(route.visits || []).forEach(function (visitIdx) {
var visit = visits[visitIdx];
if (!visit) return;
var leg = legFor(plan, previous, visit.location_idx);
clock += leg && leg.reachable ? Math.ceil((leg.duration_seconds || 0) / 60) : 0;
if (clock < visit.earliest_minute) clock = visit.earliest_minute;
var start = clock;
var end = start + Math.max(0, visit.duration_minutes || 0);
entries.push({ visit: visit, start: start, end: end });
clock = end;
previous = visit.location_idx;
});
return entries;
}
function routeStats(plan, route) {
var visits = plan.service_visits || [];
var travelMinutes = 0;
var serviceMinutes = 0;
var lateMinutes = 0;
var overtimeMinutes = 0;
var missingSkills = 0;
var missingParts = 0;
var unreachable = 0;
var clock = route.shift_start_minute;
var previous = route.start_location_idx;
(route.visits || []).forEach(function (visitIdx) {
var visit = visits[visitIdx];
if (!visit) {
unreachable += 1;
return;
}
var leg = legFor(plan, previous, visit.location_idx);
if (!leg || !leg.reachable) {
unreachable += 1;
} else {
var legMinutes = Math.ceil((leg.duration_seconds || 0) / 60);
travelMinutes += legMinutes;
clock += legMinutes;
}
if (clock < visit.earliest_minute) clock = visit.earliest_minute;
if (clock > visit.latest_minute) lateMinutes += clock - visit.latest_minute;
if (!maskContains(route.skill_mask, visit.required_skill_mask)) missingSkills += 1;
if (!maskContains(route.inventory_mask, visit.required_parts_mask)) missingParts += 1;
serviceMinutes += Math.max(0, visit.duration_minutes || 0);
clock += Math.max(0, visit.duration_minutes || 0);
previous = visit.location_idx;
});
var returnLeg = legFor(plan, previous, route.end_location_idx);
if (!returnLeg || !returnLeg.reachable) {
unreachable += 1;
} else {
var returnMinutes = Math.ceil((returnLeg.duration_seconds || 0) / 60);
travelMinutes += returnMinutes;
clock += returnMinutes;
}
var routeMinutes = Math.max(0, clock - route.shift_start_minute);
overtimeMinutes += Math.max(0, clock - route.shift_end_minute);
overtimeMinutes += Math.max(0, routeMinutes - route.max_route_minutes);
return {
travelMinutes: travelMinutes,
serviceMinutes: serviceMinutes,
lateMinutes: lateMinutes,
overtimeMinutes: overtimeMinutes,
missingSkills: missingSkills,
missingParts: missingParts,
unreachable: unreachable,
};
}
function legFor(plan, from, to) {
var width = (plan.locations || []).length;
var direct = (plan.travel_legs || [])[from * width + to];
if (direct && direct.from_location_idx === from && direct.to_location_idx === to) {
return direct;
}
return (plan.travel_legs || []).find(function (leg) {
return leg.from_location_idx === from && leg.to_location_idx === to;
});
}
function assignedVisitSet(routes) {
var assigned = {};
(routes || []).forEach(function (route) {
(route.visits || []).forEach(function (visitIdx) {
assigned[visitIdx] = route;
});
});
return assigned;
}
function locationLat(location) {
if (location.lat != null) return Number(location.lat);
return Number(location.lat_e6 || 0) / 1000000;
}
function locationLng(location) {
if (location.lng != null) return Number(location.lng);
return Number(location.lng_e6 || 0) / 1000000;
}
function routeBadges(stats) {
var badges = [];
if (stats.unreachable) badges.push('Routing');
if (stats.missingSkills) badges.push('Skills');
if (stats.missingParts) badges.push('Parts');
if (stats.lateMinutes) badges.push('Late');
if (stats.overtimeMinutes) badges.push('Overtime');
return badges.length ? badges : ['Feasible'];
}
function iconForVisit(visit) {
if ((visit.required_skill_mask & 8) === 8) return 'fa-elevator';
if ((visit.required_skill_mask & 4) === 4) return 'fa-faucet';
if ((visit.required_skill_mask & 2) === 2) return 'fa-bolt';
return 'fa-screwdriver-wrench';
}
function maskContains(available, required) {
return ((available || 0) & (required || 0)) === (required || 0);
}
function toneForRoute(index) {
return ['blue', 'emerald', 'amber', 'rose', 'violet', 'slate'][index % 6];
}
function formatDuration(minutes) {
var value = Math.max(0, Math.round(minutes || 0));
var h = Math.floor(value / 60);
var m = value % 60;
if (!h) return String(m) + 'm';
return String(h) + 'h ' + String(m).padStart(2, '0') + 'm';
}
function timeLabel(minute) {
var h = Math.floor(minute / 60);
var m = minute % 60;
return String(h).padStart(2, '0') + ':' + String(m).padStart(2, '0');
}
function findHeaderButton(header, label) {
var buttons = header.querySelectorAll('button');
for (var i = 0; i < buttons.length; i += 1) {
if ((buttons[i].textContent || '').trim() === label) return buttons[i];
}
return null;
}
function clonePlan(data) {
return JSON.parse(JSON.stringify(data));
}
function title(text) {
return String(text || '')
.replace(/_/g, ' ')
.replace(/\b\w/g, function (match) { return match.toUpperCase(); });
}
})();
|