File size: 9,801 Bytes
f6213fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
import {
  clonePlan,
  refreshPlan,
  refreshServerPlan,
} from './models.mjs';
import { fetchJson, showError } from './ui/components.mjs';
import { renderData } from './ui/data-tables.mjs';
import { createLayout } from './ui/layout.mjs';
import { sameRouteIdentity, syncLifecycleDataset } from './ui/lifecycle.mjs';
import { buildAnalysisBody, buildRecommendationBody } from './ui/modals.mjs';
import { renderMap, renderRouteList, renderSummary, renderTimelines } from './ui/overview.mjs';

const DEFAULT_DEMO = 'PHILADELPHIA';

export async function boot() {
  const config = await fetchJson('/sf-config.json');
  const backend = SF.createBackend({ baseUrl: '' });
  const app = document.getElementById('sf-app');

  let currentPlan = null;
  let currentRoutes = null;
  let currentDemo = DEFAULT_DEMO;
  let mapCtrl = null;
  let focusedVehicleId = null;
  let routeRequestToken = 0;
  // Route geometry is only trustworthy for one retained job, snapshot revision,
  // and routing mode. This identity prevents stale map lines from surviving
  // dataset switches, routing-mode changes, or newer solver snapshots.
  let activeRouteIdentity = null;
  let activeTab = 'overview';
  let mapLocationSignature = null;

  const statusBar = SF.createStatusBar({
    constraints: [
      'all_deliveries_assigned',
      'vehicle_capacity',
      'delivery_time_windows',
      'total_travel_time',
    ],
  });

  const layout = createLayout({
    app,
    config,
    statusBar,
    actions: {
      onSolve: () => loadAndSolve(),
      onPause: () => solver.pause().catch(showError),
      onResume: () => solver.resume().catch(showError),
      onCancel: () => solver.cancel().catch(showError),
      onAnalyze: () => openAnalysis(),
    },
    onTabChange: (tabId) => setActiveTab(tabId),
  });

  const solver = SF.createSolver({
    backend,
    statusBar,
    onProgress: syncLifecycle,
    onPauseRequested: syncLifecycle,
    onSolution: async (snapshot, meta) => handleSnapshotEvent(snapshot, meta),
    onPaused: async (snapshot, meta) => handleSnapshotEvent(snapshot, meta),
    onResumed: syncLifecycle,
    onCancelled: async (snapshot, meta) => handleSnapshotEvent(snapshot, meta),
    onComplete: async (snapshot, meta) => handleSnapshotEvent(snapshot, meta),
    onFailure: async (_message, meta, snapshot, analysis) => {
      await handleSnapshotEvent(snapshot, meta);
      if (analysis) layout.analysisModal.setBody(buildAnalysisBody(analysis));
    },
    onAnalysis: (analysis) => layout.analysisModal.setBody(buildAnalysisBody(analysis)),
    onError: showError,
  });

  layout.reloadButton.addEventListener('click', async () => loadDemoData(layout.demoField.select.value));
  layout.demoField.select.addEventListener('change', async () => loadDemoData(layout.demoField.select.value));
  layout.routingField.select.addEventListener('change', () => {
    if (!currentPlan) return;
    invalidateRoutes();
    currentPlan.routingMode = layout.routingField.select.value;
    renderDraftPlan(currentPlan);
  });

  setActiveTab(activeTab);
  await loadDemoData(DEFAULT_DEMO);

  function setActiveTab(tabId) {
    activeTab = tabId;
    Object.entries(layout.panels).forEach(([id, panel]) => {
      panel.classList.toggle('is-active', id === tabId);
    });
  }

  async function loadDemoData(demoId) {
    currentDemo = demoId;
    invalidateRoutes();
    if (currentPlan) {
      renderMapView();
      renderRouteListView();
    }
    const plan = await fetchJson(`/demo-data/${demoId}`);
    plan.routingMode = plan.routingMode || layout.routingField.select.value;
    renderServerPlan(plan);
  }

  async function loadAndSolve() {
    if (!currentPlan) return;
    invalidateRoutes();
    renderDraftPlan({ ...currentPlan, routingMode: layout.routingField.select.value });
    await cleanupTerminalJob();
    await solver.start(clonePlan(currentPlan));
    syncLifecycle();
  }

  async function cleanupTerminalJob() {
    const state = solver.getLifecycleState();
    if (!solver.getJobId() || state === 'IDLE' || state === 'PAUSED' || solver.isRunning()) return;
    try {
      await solver.delete();
    } catch (_error) {
      // Retained terminal cleanup is opportunistic before starting a new job.
    }
  }

  async function handleSnapshotEvent(snapshot, meta) {
    if (snapshot?.solution) {
      renderServerPlan(snapshot.solution, meta);
    }
    await loadRoutes(meta);
    syncLifecycle(meta);
  }

  async function loadRoutes(meta) {
    const requested = routeIdentityFrom(meta);
    if (!requested || !activeRouteIdentity || !sameRouteIdentity(requested, activeRouteIdentity)) return;
    const token = ++routeRequestToken;
    try {
      const routes = await fetchJson(`/jobs/${requested.jobId}/routes?snapshot_revision=${requested.snapshotRevision}`);
      const routingMode = routes.routingMode || requested.routingMode;
      if (!routeResponseStillCurrent(token, requested, routingMode)) return;
      currentRoutes = { ...routes, jobId: requested.jobId, snapshotRevision: requested.snapshotRevision, routingMode };
      renderMapView();
      renderRouteListView();
    } catch (_error) {
      if (token === routeRequestToken && activeRouteIdentity && sameRouteIdentity(requested, activeRouteIdentity)) {
        currentRoutes = null;
        renderMapView();
        renderRouteListView();
      }
    }
  }

  function routeResponseStillCurrent(token, requested, routingMode) {
    return (
      token === routeRequestToken &&
      activeRouteIdentity &&
      sameRouteIdentity(requested, activeRouteIdentity) &&
      currentPlan?.routingMode === requested.routingMode &&
      routingMode === requested.routingMode
    );
  }

  async function openAnalysis() {
    if (!solver.getJobId()) return;
    const analysis = await solver.analyzeSnapshot();
    layout.analysisModal.setBody(buildAnalysisBody(analysis));
    layout.analysisModal.open();
  }

  async function openRecommendations(deliveryId) {
    if (!currentPlan) return;
    const response = await fetch('/recommendations/delivery-insertions', {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({ plan: currentPlan, deliveryId, limit: 8 }),
    });
    if (!response.ok) throw new Error('Failed to load recommendations');
    const payload = await response.json();
    layout.recommendationModal.setBody(buildRecommendationBody(payload, (previewPlan) => {
      renderServerPlan(previewPlan);
      layout.recommendationModal.close();
    }));
    layout.recommendationModal.open();
  }

  function renderDraftPlan(plan) {
    renderPlan(plan, refreshPlan);
  }

  function renderServerPlan(plan, meta = null) {
    invalidateRoutes();
    renderPlan(plan, refreshServerPlan);
    // A solver snapshot is the only source that can authorize `/routes` reads.
    activeRouteIdentity = meta ? routeIdentityFrom(meta) : null;
  }

  function renderPlan(plan, refresh) {
    const nextMapLocationSignature = locationSignature(plan);
    const shouldFitMap = nextMapLocationSignature !== mapLocationSignature;
    currentPlan = refresh({ ...plan, routingMode: plan.routingMode || layout.routingField.select.value });
    mapLocationSignature = nextMapLocationSignature;
    layout.routingField.select.value = currentPlan.routingMode || 'road_network';
    renderSummaryView();
    renderRouteListView();
    renderMapView({ fitBounds: shouldFitMap });
    renderTimelinesView();
    renderDataView();
  }

  function invalidateRoutes() {
    routeRequestToken += 1;
    currentRoutes = null;
    focusedVehicleId = null;
    // Route tables and map geometry must be reloaded for the next snapshot.
    activeRouteIdentity = null;
  }

  function routeIdentityFrom(meta) {
    const jobId = meta?.jobId != null ? String(meta.jobId) : solver.getJobId();
    const snapshotRevision =
      meta?.snapshotRevision != null ? meta.snapshotRevision : solver.getSnapshotRevision();
    const routingMode = currentPlan?.routingMode || null;
    if (!jobId || snapshotRevision == null || !routingMode) return null;
    return { jobId: String(jobId), snapshotRevision: String(snapshotRevision), routingMode };
  }

  function renderSummaryView() {
    renderSummary({ currentDemo, currentPlan, summaryCard: layout.summaryCard, summaryMetrics: layout.summaryMetrics });
  }

  function renderRouteListView() {
    renderRouteList({
      currentPlan,
      focusedVehicleId,
      routeList: layout.routeList,
      onFocusVehicle: (vehicleId) => {
        focusedVehicleId = focusedVehicleId === vehicleId ? null : vehicleId;
        renderMapView();
        renderRouteListView();
      },
    });
  }

  function renderMapView({ fitBounds = false } = {}) {
    renderMap({
      currentPlan,
      currentRoutes,
      focusedVehicleId,
      fitBounds,
      mapCtrl,
      setMapCtrl: (next) => {
        mapCtrl = next;
      },
    });
  }

  function renderTimelinesView() {
    renderTimelines({
      currentPlan,
      vehicleTimeline: layout.vehicleTimeline,
      deliveryTimeline: layout.deliveryTimeline,
    });
  }

  function renderDataView() {
    renderData({
      currentPlan,
      dataBody: layout.dataBody,
      onRecommend: async (deliveryId) => {
        try {
          await openRecommendations(deliveryId);
        } catch (error) {
          showError(error);
        }
      },
    });
  }

  function syncLifecycle(meta) {
    syncLifecycleDataset(app, solver, meta);
  }

  function locationSignature(plan) {
    const deliveries = (plan.deliveries || []).map((delivery) => `${delivery.id}:${delivery.lat}:${delivery.lng}`);
    const vehicles = (plan.vehicles || []).map((vehicle) => `${vehicle.id}:${vehicle.homeLat}:${vehicle.homeLng}`);
    return `${deliveries.join('|')}::${vehicles.join('|')}`;
  }
}