|
1 | 1 | <script lang="ts"> |
2 | 2 | import { onMount, onDestroy } from 'svelte'; |
3 | 3 | import { Line } from 'svelte-chartjs'; |
4 | | - import { goto, invalidateAll } from '$app/navigation'; |
| 4 | + import { invalidateAll } from '$app/navigation'; |
5 | 5 | import { |
6 | 6 | Chart as ChartJS, |
7 | 7 | CategoryScale, |
|
27 | 27 |
|
28 | 28 | $: ({ running, config } = data); |
29 | 29 |
|
| 30 | + let workerCountChartData = { |
| 31 | + labels: [] as string[], |
| 32 | + datasets: [ |
| 33 | + { |
| 34 | + label: 'Order', |
| 35 | + data: [] as number[], |
| 36 | + borderColor: 'green', |
| 37 | + tension: 0.1 |
| 38 | + }, |
| 39 | + { |
| 40 | + label: 'Shipment', |
| 41 | + data: [] as number[], |
| 42 | + borderColor: 'blue', |
| 43 | + tension: 0.1 |
| 44 | + }, |
| 45 | + { |
| 46 | + label: 'Charge', |
| 47 | + data: [] as number[], |
| 48 | + borderColor: 'orange', |
| 49 | + tension: 0.1 |
| 50 | + } |
| 51 | + ] |
| 52 | + } |
| 53 | +
|
30 | 54 | let completeChartData = { |
31 | | - labels: [], |
| 55 | + labels: [] as string[], |
32 | 56 | datasets: [ |
33 | 57 | { |
34 | | - label: 'Order Complete RPS', |
35 | | - data: [], |
| 58 | + label: 'Order', |
| 59 | + data: [] as number[], |
36 | 60 | borderColor: 'green', |
37 | 61 | tension: 0.1 |
38 | 62 | }, |
| 63 | + { |
| 64 | + label: 'Shipment', |
| 65 | + data: [] as number[], |
| 66 | + borderColor: 'blue', |
| 67 | + tension: 0.1 |
| 68 | + }, |
| 69 | + { |
| 70 | + label: 'Charge', |
| 71 | + data: [] as number[], |
| 72 | + borderColor: 'orange', |
| 73 | + tension: 0.1 |
| 74 | + } |
39 | 75 | ] |
40 | 76 | }; |
41 | 77 |
|
42 | 78 | let backlogChartData = { |
43 | | - labels: [], |
| 79 | + labels: [] as string[], |
44 | 80 | datasets: [ |
45 | 81 | { |
46 | | - label: 'Order Backlog', |
47 | | - data: [], |
48 | | - borderColor: 'red', |
| 82 | + label: 'Order', |
| 83 | + data: [] as number[], |
| 84 | + borderColor: 'green', |
49 | 85 | tension: 0.1 |
50 | 86 | }, |
| 87 | + { |
| 88 | + label: 'Shipment', |
| 89 | + data: [] as number[], |
| 90 | + borderColor: 'blue', |
| 91 | + tension: 0.1 |
| 92 | + }, |
| 93 | + { |
| 94 | + label: 'Charge', |
| 95 | + data: [] as number[], |
| 96 | + borderColor: 'orange', |
| 97 | + tension: 0.1 |
| 98 | + } |
51 | 99 | ] |
52 | 100 | }; |
53 | 101 |
|
54 | 102 | let refreshInterval: number; |
55 | 103 |
|
56 | 104 | const updateChartData = async() => { |
57 | | - const response = await fetch('/api/order/stats'); |
58 | | - const { completeRate, backlog } = await response.json(); |
59 | | -
|
60 | 105 | const now = new Date().toLocaleTimeString(); |
61 | 106 |
|
| 107 | + workerCountChartData.labels = [...workerCountChartData.labels.slice(-20), now]; |
62 | 108 | completeChartData.labels = [...completeChartData.labels.slice(-20), now]; |
63 | 109 | backlogChartData.labels = [...backlogChartData.labels.slice(-20), now]; |
64 | | - completeChartData.datasets[0].data = [...completeChartData.datasets[0].data.slice(-20), completeRate]; |
65 | | - backlogChartData.datasets[0].data = [...backlogChartData.datasets[0].data.slice(-20), backlog]; |
66 | | - chartData = chartData; // trigger reactivity |
| 110 | +
|
| 111 | + const responses = await Promise.all([ |
| 112 | + fetch('/api/order/stats'), |
| 113 | + fetch('/api/shipment/stats'), |
| 114 | + fetch('/api/charge/stats') |
| 115 | + ]); |
| 116 | +
|
| 117 | + const allStats = await Promise.all(responses.map(response => response.json())); |
| 118 | +
|
| 119 | + allStats.forEach((stats, i) => { |
| 120 | + let { workerCount, completeRate, backlog } = stats; |
| 121 | + workerCountChartData.datasets[i].data = [...workerCountChartData.datasets[i].data.slice(-20), workerCount]; |
| 122 | + completeChartData.datasets[i].data = [...completeChartData.datasets[i].data.slice(-20), completeRate]; |
| 123 | + backlogChartData.datasets[i].data = [...backlogChartData.datasets[i].data.slice(-20), backlog]; |
| 124 | + }); |
| 125 | +
|
| 126 | + workerCountChartData = workerCountChartData |
| 127 | + completeChartData = completeChartData |
| 128 | + backlogChartData = backlogChartData |
67 | 129 | } |
68 | 130 |
|
69 | 131 | onMount(() => { |
|
113 | 175 | </div> |
114 | 176 | <div class="flex gap-2 w-full"> |
115 | 177 | <div class="w-full p-4 flex flex-col gap-4 bg-white border border-black rounded"> |
116 | | - <h3 class="text-xl font-bold">Order Completions</h3> |
| 178 | + <h3 class="text-xl font-bold">Completions</h3> |
117 | 179 | <div class="w-full h-[300px]"> |
118 | 180 | <Line |
119 | 181 | data={completeChartData} |
|
130 | 192 | /> |
131 | 193 | </div> |
132 | 194 | </div> |
| 195 | + </div> |
| 196 | + <div class="flex gap-2 w-full"> |
| 197 | + <div class="w-full p-4 flex flex-col gap-4 bg-white border border-black rounded"> |
| 198 | + <h3 class="text-xl font-bold">Workers</h3> |
| 199 | + <div class="w-full h-[300px]"> |
| 200 | + <Line |
| 201 | + data={workerCountChartData} |
| 202 | + options={{ |
| 203 | + responsive: true, |
| 204 | + maintainAspectRatio: false, |
| 205 | + animation: false, |
| 206 | + scales: { |
| 207 | + y: { |
| 208 | + beginAtZero: true |
| 209 | + } |
| 210 | + } |
| 211 | + }} |
| 212 | + /> |
| 213 | + </div> |
| 214 | + </div> |
133 | 215 | <div class="w-full p-4 flex flex-col gap-4 bg-white border border-black rounded"> |
134 | | - <h3 class="text-xl font-bold">Order Backlog</h3> |
| 216 | + <h3 class="text-xl font-bold">Backlog</h3> |
135 | 217 | <div class="w-full h-[300px]"> |
136 | 218 | <Line |
137 | 219 | data={backlogChartData} |
|
0 commit comments