Skip to content

Commit 8054cdf

Browse files
Merge pull request #337 from CropWatchDevelopment/develop
increased decimal limt, added daily values for temp sensor
2 parents d9c3c9a + ab33972 commit 8054cdf

5 files changed

Lines changed: 122 additions & 4 deletions

File tree

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
<script>
2+
import { _ } from 'svelte-i18n';
3+
let { historicalData } = $props();
4+
5+
let dataArray = $derived(historicalData); // Assume data is an array of objects
6+
7+
// Filter data for current day
8+
function getCurrentDayData(dataArray) {
9+
const today = new Date();
10+
today.setHours(0, 0, 0, 0);
11+
12+
return dataArray.filter((item) => {
13+
const itemDate = new Date(item.created_at);
14+
itemDate.setHours(0, 0, 0, 0);
15+
return itemDate.getTime() === today.getTime();
16+
});
17+
}
18+
19+
// Get visible columns
20+
function getVisibleColumns(dataArray) {
21+
const excludedColumns = ['is_simulated', 'dev_eui', 'smoke_detected', 'id', 'vape_detected'];
22+
const allKeys = dataArray.length > 0 ? Object.keys(dataArray[0]) : [];
23+
24+
return allKeys.filter((key) => {
25+
if (excludedColumns.includes(key)) return false;
26+
const hasNonNullValue = dataArray.some((item) => item[key] !== null);
27+
return hasNonNullValue;
28+
});
29+
}
30+
31+
// Format column names
32+
function formatColumnName(key) {
33+
return key
34+
.split('_')
35+
.map((word) => word.charAt(0).toUpperCase() + word.slice(1))
36+
.join(' ');
37+
}
38+
39+
// Format cell value
40+
function formatCellValue(key, value) {
41+
if (key === 'created_at') {
42+
return new Date(value).toLocaleString();
43+
}
44+
if (typeof value === 'boolean') {
45+
return value ? 'Yes' : 'No';
46+
}
47+
if (typeof value === 'number') {
48+
return value.toFixed(2);
49+
}
50+
return value;
51+
}
52+
53+
let filteredData = $derived(getCurrentDayData(dataArray));
54+
let visibleColumns = $derived(getVisibleColumns(filteredData));
55+
</script>
56+
57+
<div class="min-h-screen w-full text-gray-900">
58+
<div class="mx-auto w-full p-4">
59+
<div
60+
class="overflow-hidden rounded-lg border border-gray-200/70 bg-white shadow-lg dark:border-white/10 dark:bg-neutral-900"
61+
>
62+
<!-- Header -->
63+
<div
64+
class="bg-gradient-to-r from-blue-600 to-blue-700 px-6 py-4 dark:from-blue-700 dark:to-indigo-800"
65+
>
66+
<h1 class="text-2xl font-bold text-white">{$_('Sensor Data - Today')}</h1>
67+
</div>
68+
69+
<!-- Table wrapper -->
70+
<div class="overflow-x-auto">
71+
{#if filteredData.length === 0}
72+
<div class="p-12 text-center text-gray-500 dark:text-gray-400">
73+
<p class="text-lg">{$_('No data available for today')}</p>
74+
</div>
75+
{:else}
76+
<table class="w-full text-sm">
77+
<thead
78+
class="border-b border-gray-200 bg-gray-50 dark:border-white/10 dark:bg-neutral-800"
79+
>
80+
<tr>
81+
{#each visibleColumns as column}
82+
<th
83+
class="text-md px-6 py-3 text-left font-semibold tracking-wide text-gray-700 uppercase dark:text-gray-200"
84+
>
85+
{$_(column)}
86+
</th>
87+
{/each}
88+
</tr>
89+
</thead>
90+
91+
<tbody class="divide-y divide-gray-200 dark:divide-white/10">
92+
{#each filteredData as row, rowIndex}
93+
<tr
94+
class="transition-colors duration-150 even:bg-transparent hover:bg-gray-50 dark:even:bg-white/5 dark:hover:bg-white/10"
95+
>
96+
{#each visibleColumns as column}
97+
<td
98+
class="px-6 py-3 text-lg whitespace-nowrap text-gray-900 dark:text-gray-100"
99+
>
100+
{formatCellValue(column, row[column])}
101+
</td>
102+
{/each}
103+
</tr>
104+
{/each}
105+
</tbody>
106+
</table>
107+
{/if}
108+
</div>
109+
</div>
110+
</div>
111+
</div>

src/lib/i18n/locales/en.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export const strings = {
2+
'Sensor Data - Today': 'Sensor Data - Today',
23
soil_moisture: 'Volumetric Water Content',
34
moisture: 'Volumetric Water Content',
45
Moisture: 'Volumetric Water Content',

src/lib/i18n/locales/ja.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export const strings = {
2+
'Sensor Data - Today': 'センサーデータ - 本日',
23
soil_moisture: '土壌水分量',
34
moisture: '土壌水分量',
45
Moisture: '土壌水分量',

src/lib/utilities/stats.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,9 @@ export const getTextColorByKey = (key: string, isMetadata: boolean = false): str
9393
};
9494

9595
/**
96-
* Default options for number formatting. This sets the maximum number of fraction digits to 1.
96+
* Default options for number formatting. This sets the maximum number of fraction digits to 2.
9797
*/
98-
const defaultNumberFormatOptions: Intl.NumberFormatOptions = { maximumFractionDigits: 1 };
98+
const defaultNumberFormatOptions: Intl.NumberFormatOptions = { maximumFractionDigits: 2 };
9999

100100
/**
101101
* Custom format options for various statistics keys.

src/routes/app/dashboard/location/[location_id]/devices/[devEui]/+page.svelte

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,7 @@
410410
// Cleanup on destroy
411411
import { onDestroy } from 'svelte';
412412
import BatteryLevel from '$lib/components/BatteryLevel.svelte';
413+
import DataTable from '$lib/components/DataTable.svelte';
413414
onDestroy(() => {
414415
teardownRealtime();
415416
if (staleCheckIntervalId) clearInterval(staleCheckIntervalId);
@@ -599,8 +600,12 @@
599600

600601
<!-- Full-width Calendar Section -->
601602
<section class="mb-12 px-4">
602-
<h2>{$_('Weather & Data')}</h2>
603-
<WeatherCalendar events={calendarEvents} />
603+
{#if device.cw_device_type?.data_table_v2 === 'cw_air_data'}
604+
<DataTable {historicalData} />
605+
{:else}
606+
<h2>{$_('Weather & Data')}</h2>
607+
<WeatherCalendar events={calendarEvents} />
608+
{/if}
604609
</section>
605610

606611
<style lang="postcss">

0 commit comments

Comments
 (0)