From 06f7537422b9e22978becb795189d4ffd2f86e30 Mon Sep 17 00:00:00 2001 From: rafaeldpsilva Date: Wed, 1 Oct 2025 12:41:12 +0100 Subject: [PATCH] Add computed sensor stats and refactor AnalyticsView to use stores --- src/stores/sensor.ts | 30 +++++++++- src/views/AnalyticsView.vue | 107 ++++++++++++++++++++---------------- 2 files changed, 90 insertions(+), 47 deletions(-) diff --git a/src/stores/sensor.ts b/src/stores/sensor.ts index 9b70978..4ffcd96 100644 --- a/src/stores/sensor.ts +++ b/src/stores/sensor.ts @@ -1,5 +1,5 @@ import { defineStore } from 'pinia' -import { ref, reactive } from 'vue' +import { ref, reactive, computed } from 'vue' import { sensorsApi, SensorType, @@ -14,9 +14,19 @@ export const useSensorStore = defineStore('sensor', () => { const latestReadings = reactive>(new Map()) const sensorsData = reactive>(new Map()) // Legacy support const recentlyUpdatedSensors = reactive>(new Set()) // Track recently updated sensors + const totalReadings = ref(0) // Total number of readings across all sensors const apiLoading = ref(false) const apiError = ref(null) + // Computed properties + const totalSensors = computed(() => sensorDevices.size) + + const activeSensors = computed(() => { + return Array.from(sensorDevices.values()).filter( + sensor => sensor.status === 'active' || sensor.status === 'online' + ).length + }) + // Actions function updateSensorRoom(sensorId: string, newRoom: string) { const sensor = sensorDevices.get(sensorId) @@ -56,6 +66,9 @@ export const useSensorStore = defineStore('sensor', () => { function updateLatestReading(reading: SensorReading) { latestReadings.set(reading.sensor_id, reading) + // Increment total readings count + totalReadings.value++ + // Mark sensor as recently updated recentlyUpdatedSensors.add(reading.sensor_id) // Remove from recently updated after 2 seconds @@ -178,11 +191,18 @@ export const useSensorStore = defineStore('sensor', () => { console.log(result) // Check if result has a sensors property (common API pattern) if (result.sensors && Array.isArray(result.sensors)) { + let totalReadingsCount = 0 + result.sensors.forEach((sensor) => { const sensorKey = sensor._id || sensor.sensor_id const sensorType = sensor.sensor_type || sensor.type const sensorName = sensor.name || '' + // Accumulate total readings + if (sensor.total_readings) { + totalReadingsCount += sensor.total_readings + } + const normalizedSensor = { ...sensor, id: sensorKey, @@ -207,6 +227,9 @@ export const useSensorStore = defineStore('sensor', () => { sensorDevices.set(sensorKey, normalizedSensor) }) + + // Update total readings + totalReadings.value = totalReadingsCount } else { console.warn('Unexpected result format:', typeof result, result) } @@ -247,9 +270,14 @@ export const useSensorStore = defineStore('sensor', () => { latestReadings, sensorsData, recentlyUpdatedSensors, + totalReadings, apiLoading, apiError, + // Computed + totalSensors, + activeSensors, + // Actions updateEnergySensors, updateSensorRoom, diff --git a/src/views/AnalyticsView.vue b/src/views/AnalyticsView.vue index d17a242..17f729d 100644 --- a/src/views/AnalyticsView.vue +++ b/src/views/AnalyticsView.vue @@ -33,7 +33,7 @@

Total Sensors

-

{{ healthStatus?.total_sensors || 0 }}

+

{{ sensorStore.totalSensors }}

@@ -47,7 +47,7 @@

Active Sensors

-

{{ healthStatus?.active_sensors || 0 }}

+

{{ sensorStore.activeSensors }}

@@ -61,14 +61,14 @@

Total Readings

-

{{ formatNumber(healthStatus?.total_readings || 0) }}

+

{{ formatNumber(sensorStore.totalReadings) }}

-
+

Loading API data...

@@ -76,7 +76,7 @@
-
+
@@ -85,38 +85,33 @@

API Error

-

{{ energyStore.apiError }}

+

{{ apiError }}

- -
-

Analytics Summary (Last 24 Hours)

+ +
+

Energy Metrics

-

Total Energy Consumption

+

Current Energy Consumption

- {{ analyticsData.summary.total_energy_consumption.value.toFixed(2) }} - {{ analyticsData.summary.total_energy_consumption.unit }} + {{ energyStore.currentEnergyValue.toFixed(2) }} kWh

-

Average Power

+

Average Energy Usage

- {{ analyticsData.summary.average_power.value.toFixed(2) }} - {{ analyticsData.summary.average_power.unit }} + {{ energyStore.averageEnergyUsage.toFixed(2) }} kWh

-

Peak Power

-

- {{ analyticsData.summary.peak_power.value.toFixed(2) }} - {{ analyticsData.summary.peak_power.unit }} -

-

- at {{ new Date(analyticsData.summary.peak_power.timestamp * 1000).toLocaleString() }} +

Total Consumption

+

+ {{ energyStore.currentConsumption.toFixed(2) }} kWh

+

Cumulative

@@ -164,14 +159,14 @@ No rooms found from API
-
-

{{ room.room }}

+

{{ room.name || room.room }}

{{ room.sensor_count }} sensors
-

Types: {{ room.sensor_types.join(', ') }}

+

Types: {{ room.sensor_types.join(', ') }}

Energy: {{ room.latest_metrics.energy.current }} {{ room.latest_metrics.energy.unit }} @@ -180,6 +175,7 @@ CO2: {{ room.latest_metrics.co2.current }} {{ room.latest_metrics.co2.unit }}
+

No metrics available

@@ -191,31 +187,31 @@

API Actions

- - - - - - -
@@ -226,15 +222,29 @@ \ No newline at end of file