FilamentTrends.tsx 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. import { useMemo, useState } from 'react';
  2. import { useTranslation } from 'react-i18next';
  3. import {
  4. AreaChart,
  5. Area,
  6. XAxis,
  7. YAxis,
  8. CartesianGrid,
  9. Tooltip,
  10. ResponsiveContainer,
  11. PieChart,
  12. Pie,
  13. Cell,
  14. } from 'recharts';
  15. import type { ArchiveSlim } from '../api/client';
  16. import { MetricToggle, type Metric } from './MetricToggle';
  17. import { parseUTCDate } from '../utils/date';
  18. import { formatWeight } from '../utils/weight';
  19. interface FilamentTrendsProps {
  20. archives: ArchiveSlim[];
  21. currency?: string;
  22. dateFrom?: string;
  23. dateTo?: string;
  24. }
  25. const COLORS = ['#00ae42', '#3b82f6', '#f59e0b', '#ef4444', '#8b5cf6', '#ec4899', '#14b8a6', '#f97316'];
  26. const DAY_NAMES = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
  27. const HOUR_SUFFIXES = ['12am', '1am', '2am', '3am', '4am', '5am', '6am', '7am', '8am', '9am', '10am', '11am', '12pm', '1pm', '2pm', '3pm', '4pm', '5pm', '6pm', '7pm', '8pm', '9pm', '10pm', '11pm'];
  28. export function FilamentTrends({ archives, currency = '$', dateFrom, dateTo }: FilamentTrendsProps) {
  29. const { t } = useTranslation();
  30. const [filamentTypeMetric, setFilamentTypeMetric] = useState<Metric>('weight');
  31. const [colorMetric, setColorMetric] = useState<Metric>('weight');
  32. // Calculate daily usage data
  33. const dailyData = useMemo(() => {
  34. const dataMap = new Map<string, { date: string; filament: number; cost: number; prints: number }>();
  35. archives.forEach(archive => {
  36. const date = parseUTCDate(archive.completed_at || archive.created_at) || new Date();
  37. // Use local date string for grouping
  38. const key = `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, '0')}-${String(date.getDate()).padStart(2, '0')}`;
  39. const existing = dataMap.get(key) || { date: key, filament: 0, cost: 0, prints: 0 };
  40. existing.filament += archive.filament_used_grams || 0;
  41. existing.cost += archive.cost || 0;
  42. existing.prints += archive.quantity || 1;
  43. dataMap.set(key, existing);
  44. });
  45. return Array.from(dataMap.values())
  46. .sort((a, b) => a.date.localeCompare(b.date))
  47. .map(d => ({
  48. ...d,
  49. dateLabel: new Date(d.date).toLocaleDateString('en-US', { month: 'short', day: 'numeric' }),
  50. }));
  51. }, [archives]);
  52. // Compute effective span in days from props or archive spread
  53. const spanDays = useMemo(() => {
  54. if (dateFrom && dateTo) {
  55. return Math.max((new Date(dateTo).getTime() - new Date(dateFrom).getTime()) / 86400000, 0) + 1;
  56. }
  57. if (dateFrom) {
  58. return Math.max((Date.now() - new Date(dateFrom).getTime()) / 86400000, 0) + 1;
  59. }
  60. if (archives.length < 2) return 0;
  61. const times = archives.map(a => new Date(a.completed_at || a.created_at).getTime());
  62. return (Math.max(...times) - Math.min(...times)) / 86400000;
  63. }, [archives, dateFrom, dateTo]);
  64. // Calculate hourly data for short timeframes (≤ 7 days)
  65. const hourlyData = useMemo(() => {
  66. if (spanDays > 7) return [];
  67. const dataMap = new Map<string, { date: string; filament: number; cost: number; prints: number }>();
  68. const multiDay = spanDays > 1;
  69. archives.forEach(archive => {
  70. const date = parseUTCDate(archive.completed_at || archive.created_at) || new Date();
  71. const h = date.getHours();
  72. const key = `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, '0')}-${String(date.getDate()).padStart(2, '0')}T${String(h).padStart(2, '0')}`;
  73. const existing = dataMap.get(key) || { date: key, filament: 0, cost: 0, prints: 0 };
  74. existing.filament += archive.filament_used_grams || 0;
  75. existing.cost += archive.cost || 0;
  76. existing.prints += archive.quantity || 1;
  77. dataMap.set(key, existing);
  78. });
  79. return Array.from(dataMap.values())
  80. .sort((a, b) => a.date.localeCompare(b.date))
  81. .map(d => {
  82. const [datePart, hourPart] = d.date.split('T');
  83. const dt = new Date(datePart);
  84. const h = parseInt(hourPart, 10);
  85. const label = multiDay
  86. ? `${DAY_NAMES[dt.getDay()]} ${HOUR_SUFFIXES[h]}`
  87. : HOUR_SUFFIXES[h];
  88. return { ...d, dateLabel: label };
  89. });
  90. }, [archives, spanDays]);
  91. // Calculate weekly aggregated data when there are many daily points
  92. const weeklyData = useMemo(() => {
  93. if (dailyData.length <= 60) return dailyData;
  94. const dataMap = new Map<string, { week: string; filament: number; cost: number; prints: number }>();
  95. dailyData.forEach(day => {
  96. const date = new Date(day.date);
  97. const weekStart = new Date(date);
  98. weekStart.setDate(date.getDate() - date.getDay());
  99. const key = `${weekStart.getFullYear()}-${String(weekStart.getMonth() + 1).padStart(2, '0')}-${String(weekStart.getDate()).padStart(2, '0')}`;
  100. const existing = dataMap.get(key) || { week: key, filament: 0, cost: 0, prints: 0 };
  101. existing.filament += day.filament;
  102. existing.cost += day.cost;
  103. existing.prints += day.prints;
  104. dataMap.set(key, existing);
  105. });
  106. return Array.from(dataMap.values())
  107. .sort((a, b) => a.week.localeCompare(b.week))
  108. .map(d => ({
  109. date: d.week,
  110. dateLabel: `Week of ${new Date(d.week).toLocaleDateString('en-US', { month: 'short', day: 'numeric' })}`,
  111. ...d,
  112. }));
  113. }, [dailyData]);
  114. // Usage by filament type
  115. const filamentTypeData = useMemo(() => {
  116. const dataMap = new Map<string, number>();
  117. archives.forEach(archive => {
  118. const type = archive.filament_type || 'Unknown';
  119. // Handle multiple types (e.g., "PLA, PETG")
  120. const types = type.split(', ');
  121. types.forEach(t => {
  122. const grams = (archive.filament_used_grams || 0) / types.length;
  123. dataMap.set(t, (dataMap.get(t) || 0) + grams);
  124. });
  125. });
  126. return Array.from(dataMap.entries())
  127. .map(([name, value]) => ({ name, value: Math.round(value) }))
  128. .sort((a, b) => b.value - a.value);
  129. }, [archives]);
  130. // Usage by filament type (print count)
  131. const filamentTypePrintData = useMemo(() => {
  132. const dataMap = new Map<string, number>();
  133. archives.forEach(archive => {
  134. const type = archive.filament_type || 'Unknown';
  135. const types = type.split(', ');
  136. types.forEach(t => {
  137. dataMap.set(t, (dataMap.get(t) || 0) + 1);
  138. });
  139. });
  140. return Array.from(dataMap.entries())
  141. .map(([name, value]) => ({ name, value }))
  142. .sort((a, b) => b.value - a.value);
  143. }, [archives]);
  144. // Usage by filament type (print time in hours)
  145. const filamentTypeTimeData = useMemo(() => {
  146. const dataMap = new Map<string, number>();
  147. archives.forEach(archive => {
  148. const type = archive.filament_type || 'Unknown';
  149. const types = type.split(', ');
  150. const seconds = (archive.actual_time_seconds || archive.print_time_seconds || 0) / types.length;
  151. types.forEach(t => {
  152. dataMap.set(t, (dataMap.get(t) || 0) + seconds);
  153. });
  154. });
  155. return Array.from(dataMap.entries())
  156. .map(([name, seconds]) => ({ name, value: Math.round((seconds / 3600) * 10) / 10 }))
  157. .sort((a, b) => b.value - a.value);
  158. }, [archives]);
  159. // Success rate by filament type
  160. const filamentSuccessData = useMemo(() => {
  161. const map = new Map<string, { completed: number; failed: number }>();
  162. archives.forEach(a => {
  163. if (a.status !== 'completed' && a.status !== 'failed') return;
  164. const types = (a.filament_type || 'Unknown').split(', ');
  165. types.forEach(type => {
  166. const entry = map.get(type) || { completed: 0, failed: 0 };
  167. if (a.status === 'completed') entry.completed++;
  168. else entry.failed++;
  169. map.set(type, entry);
  170. });
  171. });
  172. return Array.from(map.entries())
  173. .filter(([, v]) => v.completed + v.failed >= 2)
  174. .map(([name, v]) => {
  175. const total = v.completed + v.failed;
  176. const rate = Math.round((v.completed / total) * 100);
  177. return { name, rate, total };
  178. })
  179. .sort((a, b) => b.rate - a.rate);
  180. }, [archives]);
  181. // Color distribution
  182. const colorData = useMemo(() => {
  183. const colorMap = new Map<string, { count: number; weight: number }>();
  184. archives.forEach(a => {
  185. if (!a.filament_color) return;
  186. const colors = a.filament_color.split(',').map(c => c.trim());
  187. const weightPerColor = (a.filament_used_grams || 0) / colors.length;
  188. colors.forEach(hex => {
  189. const entry = colorMap.get(hex) || { count: 0, weight: 0 };
  190. entry.count++;
  191. entry.weight += weightPerColor;
  192. colorMap.set(hex, entry);
  193. });
  194. });
  195. return Array.from(colorMap.entries())
  196. .map(([hex, data]) => ({
  197. hex,
  198. value: colorMetric === 'prints' ? data.count : Math.round(data.weight),
  199. }))
  200. .sort((a, b) => b.value - a.value);
  201. }, [archives, colorMetric]);
  202. const activeFilamentTypeData =
  203. filamentTypeMetric === 'weight' ? filamentTypeData :
  204. filamentTypeMetric === 'prints' ? filamentTypePrintData :
  205. filamentTypeTimeData;
  206. const chartData = spanDays <= 7 && hourlyData.length > 0 ? hourlyData : weeklyData;
  207. const totalFilament = archives.reduce((sum, a) => sum + (a.filament_used_grams || 0), 0);
  208. const totalCost = archives.reduce((sum, a) => sum + (a.cost || 0), 0);
  209. const totalPrints = archives.reduce((sum, a) => sum + (a.quantity || 1), 0);
  210. const printerCount = new Set(archives.map(a => a.printer_id).filter(Boolean)).size;
  211. return (
  212. <div className="space-y-4">
  213. {/* Summary Cards */}
  214. <div className="grid grid-cols-3 gap-2 max-[640px]:grid-cols-1">
  215. <div className="bg-bambu-dark rounded-lg p-4">
  216. <div className="flex items-center justify-between gap-2">
  217. <p className="text-sm text-bambu-gray leading-none">{t('stats.periodFilament')}</p>
  218. <p className="text-2xl font-bold text-white leading-none">{formatWeight(totalFilament)}</p>
  219. </div>
  220. <p className="text-xs text-bambu-gray">{printerCount} {t('nav.printers').toLowerCase()}</p>
  221. </div>
  222. <div className="bg-bambu-dark rounded-lg p-4">
  223. <div className="flex items-center justify-between gap-2">
  224. <p className="text-sm text-bambu-gray leading-none">{t('stats.periodCost')}</p>
  225. <p className="text-2xl font-bold text-white leading-none">{currency}{totalCost.toFixed(2)}</p>
  226. </div>
  227. <p className="text-xs text-bambu-gray">{totalPrints} {t('common.prints')}</p>
  228. </div>
  229. <div className="bg-bambu-dark rounded-lg p-4">
  230. <div className="flex items-center justify-between gap-2">
  231. <p className="text-sm text-bambu-gray leading-none">{t('stats.avgPerPrint')}</p>
  232. <p className="text-2xl font-bold text-white leading-none">
  233. {totalPrints > 0
  234. ? (totalFilament / totalPrints).toFixed(0)
  235. : 0}g
  236. </p>
  237. </div>
  238. <p className="text-xs text-bambu-gray">
  239. {currency}{totalPrints > 0 ? (totalCost / totalPrints).toFixed(2) : '0.00'} avg
  240. </p>
  241. </div>
  242. </div>
  243. {/* Usage Over Time Chart */}
  244. {chartData.length > 0 ? (
  245. <div className="bg-bambu-dark rounded-lg p-4">
  246. <h4 className="text-sm font-medium text-bambu-gray mb-4">{t('stats.usageOverTime')}</h4>
  247. <ResponsiveContainer width="100%" height={250}>
  248. <AreaChart data={chartData}>
  249. <defs>
  250. <linearGradient id="colorFilament" x1="0" y1="0" x2="0" y2="1">
  251. <stop offset="5%" stopColor="#00ae42" stopOpacity={0.3}/>
  252. <stop offset="95%" stopColor="#00ae42" stopOpacity={0}/>
  253. </linearGradient>
  254. </defs>
  255. <CartesianGrid strokeDasharray="3 3" stroke="#3d3d3d" />
  256. <XAxis
  257. dataKey="dateLabel"
  258. stroke="#9ca3af"
  259. tick={{ fontSize: 12 }}
  260. interval="preserveStartEnd"
  261. />
  262. <YAxis
  263. stroke="#9ca3af"
  264. tick={{ fontSize: 12 }}
  265. tickFormatter={(value) => `${value}g`}
  266. />
  267. <Tooltip
  268. contentStyle={{
  269. backgroundColor: '#2d2d2d',
  270. border: '1px solid #3d3d3d',
  271. borderRadius: '8px',
  272. }}
  273. labelStyle={{ color: '#fff' }}
  274. formatter={(value) => [`${Number(value ?? 0).toFixed(0)}g`, 'Filament']}
  275. />
  276. <Area
  277. type="monotone"
  278. dataKey="filament"
  279. stroke="#00ae42"
  280. strokeWidth={2}
  281. fillOpacity={1}
  282. fill="url(#colorFilament)"
  283. />
  284. </AreaChart>
  285. </ResponsiveContainer>
  286. </div>
  287. ) : (
  288. <div className="bg-bambu-dark rounded-lg p-8 text-center text-bambu-gray">
  289. {t('stats.noPrintDataInRange')}
  290. </div>
  291. )}
  292. {/* Bottom Charts */}
  293. <div className="grid grid-cols-1 lg:grid-cols-3 gap-4">
  294. {/* Filament Type Distribution */}
  295. <div className="bg-bambu-dark rounded-lg p-4">
  296. <div className="flex items-center justify-between mb-4">
  297. <h4 className="text-sm font-medium text-bambu-gray">{t('stats.byMaterial')}</h4>
  298. <MetricToggle value={filamentTypeMetric} onChange={setFilamentTypeMetric} />
  299. </div>
  300. {activeFilamentTypeData.length > 0 ? (
  301. <div className="flex items-center gap-4">
  302. <ResponsiveContainer width={160} height={160}>
  303. <PieChart>
  304. <Pie
  305. data={activeFilamentTypeData}
  306. cx="50%"
  307. cy="50%"
  308. innerRadius={40}
  309. outerRadius={70}
  310. paddingAngle={2}
  311. dataKey="value"
  312. >
  313. {activeFilamentTypeData.map((_, index) => (
  314. <Cell key={`cell-${index}`} fill={COLORS[index % COLORS.length]} />
  315. ))}
  316. </Pie>
  317. <Tooltip
  318. contentStyle={{
  319. backgroundColor: '#2d2d2d',
  320. border: '1px solid #3d3d3d',
  321. borderRadius: '8px',
  322. }}
  323. formatter={(value) => [
  324. filamentTypeMetric === 'weight' ? formatWeight(Number(value ?? 0)) :
  325. filamentTypeMetric === 'time' ? `${Number(value ?? 0)}h` :
  326. `${value ?? 0}`,
  327. filamentTypeMetric === 'weight' ? 'Usage' : filamentTypeMetric === 'time' ? 'Time' : 'Prints',
  328. ]}
  329. />
  330. </PieChart>
  331. </ResponsiveContainer>
  332. <div className="flex-1 space-y-2 overflow-hidden">
  333. {activeFilamentTypeData.map((entry, index) => {
  334. const total = activeFilamentTypeData.reduce((sum, e) => sum + e.value, 0);
  335. const percent = total > 0 ? ((entry.value / total) * 100).toFixed(0) : 0;
  336. return (
  337. <div key={entry.name} className="flex items-center gap-2 text-sm">
  338. <div
  339. className="w-3 h-3 rounded-sm flex-shrink-0"
  340. style={{ backgroundColor: COLORS[index % COLORS.length] }}
  341. />
  342. <span className="text-white truncate flex-1">{entry.name}</span>
  343. <span className="text-bambu-gray flex-shrink-0">
  344. {filamentTypeMetric === 'weight' ? formatWeight(entry.value) :
  345. filamentTypeMetric === 'time' ? `${entry.value}h` :
  346. entry.value} · {percent}%
  347. </span>
  348. </div>
  349. );
  350. })}
  351. </div>
  352. </div>
  353. ) : (
  354. <div className="h-[160px] flex items-center justify-center text-bambu-gray">
  355. {t('stats.noFilamentData')}
  356. </div>
  357. )}
  358. </div>
  359. {/* Success by Material */}
  360. <div className="bg-bambu-dark rounded-lg p-4">
  361. <h4 className="text-sm font-medium text-bambu-gray mb-4">{t('stats.filamentSuccess')}</h4>
  362. {filamentSuccessData.length > 0 ? (
  363. <div className="space-y-1.5">
  364. {filamentSuccessData.map(d => (
  365. <div key={d.name} className="flex items-center gap-2 text-sm">
  366. <span className="text-white truncate w-20 flex-shrink-0">{d.name}</span>
  367. <div className="flex-1 h-1.5 bg-bambu-dark-secondary rounded-full">
  368. <div
  369. className={`h-full rounded-full transition-all ${
  370. d.rate >= 90 ? 'bg-status-ok' : d.rate >= 70 ? 'bg-status-warning' : 'bg-status-error'
  371. }`}
  372. style={{ width: `${d.rate}%` }}
  373. />
  374. </div>
  375. <span className={`font-medium flex-shrink-0 tabular-nums ${
  376. d.rate >= 90 ? 'text-status-ok' : d.rate >= 70 ? 'text-status-warning' : 'text-status-error'
  377. }`}>
  378. {d.rate}%
  379. </span>
  380. <span className="text-bambu-gray flex-shrink-0 text-xs">({d.total})</span>
  381. </div>
  382. ))}
  383. </div>
  384. ) : (
  385. <div className="h-[160px] flex items-center justify-center text-bambu-gray">
  386. {t('stats.noArchiveData')}
  387. </div>
  388. )}
  389. </div>
  390. {/* Color Distribution */}
  391. <div className="bg-bambu-dark rounded-lg p-4">
  392. <div className="flex items-center justify-between mb-4">
  393. <h4 className="text-sm font-medium text-bambu-gray">{t('stats.colorDistribution')}</h4>
  394. <MetricToggle value={colorMetric} onChange={setColorMetric} exclude={['time']} />
  395. </div>
  396. {colorData.length > 0 ? (() => {
  397. const colorTotal = colorData.reduce((sum, e) => sum + e.value, 0);
  398. return (
  399. <div>
  400. <div className="relative mx-auto" style={{ width: 160, height: 160 }}>
  401. <ResponsiveContainer width="100%" height="100%">
  402. <PieChart>
  403. <Pie
  404. data={colorData}
  405. cx="50%"
  406. cy="50%"
  407. innerRadius={45}
  408. outerRadius={70}
  409. paddingAngle={2}
  410. dataKey="value"
  411. >
  412. {colorData.map((entry, index) => (
  413. <Cell key={`color-${index}`} fill={entry.hex} stroke="#1a1a1a" strokeWidth={1} />
  414. ))}
  415. </Pie>
  416. <Tooltip
  417. contentStyle={{
  418. backgroundColor: '#2d2d2d',
  419. border: '1px solid #3d3d3d',
  420. borderRadius: '8px',
  421. }}
  422. formatter={(value) => [
  423. colorMetric === 'weight' ? formatWeight(Number(value ?? 0)) : `${value ?? 0}`,
  424. colorMetric === 'weight' ? t('stats.filamentByWeight') : t('stats.filamentByPrints'),
  425. ]}
  426. />
  427. </PieChart>
  428. </ResponsiveContainer>
  429. <div className="absolute inset-0 flex flex-col items-center justify-center">
  430. <span className="text-lg font-bold text-white">
  431. {colorMetric === 'weight' ? formatWeight(colorTotal) : colorTotal}
  432. </span>
  433. <span className="text-[10px] text-bambu-gray">
  434. {colorData.length} {colorData.length === 1 ? 'color' : 'colors'}
  435. </span>
  436. </div>
  437. </div>
  438. <div className="grid grid-cols-2 gap-x-3 gap-y-1 mt-2">
  439. {colorData.slice(0, 8).map((entry) => {
  440. const percent = colorTotal > 0 ? ((entry.value / colorTotal) * 100).toFixed(0) : 0;
  441. return (
  442. <div key={entry.hex} className="flex items-center gap-1.5 text-xs min-w-0">
  443. <div className="w-2.5 h-2.5 rounded-full flex-shrink-0 border border-black/20"
  444. style={{ backgroundColor: entry.hex }} />
  445. <span className="text-bambu-gray truncate">
  446. {percent}%
  447. </span>
  448. </div>
  449. );
  450. })}
  451. </div>
  452. {colorData.length > 8 && (
  453. <p className="text-[10px] text-bambu-gray mt-1 text-center">+{colorData.length - 8} more</p>
  454. )}
  455. </div>
  456. );
  457. })() : (
  458. <div className="h-[160px] flex items-center justify-center text-bambu-gray">
  459. {t('stats.noColorData')}
  460. </div>
  461. )}
  462. </div>
  463. </div>
  464. </div>
  465. );
  466. }