Skip to content

Commit 5b640c6

Browse files
Fix device list display on Dashboard and Analytics pages
- Update deviceService.ts to use correct MQTT endpoint (/devices instead of /devices/list) - Fix Dashboard page to fetch device data directly for accurate device counts - Fix Analytics page to augment analytics data with correct device counts - Both pages now show 5 devices instead of 0 for Total/Online/Active devices - Device counts now update in real-time based on actual device status
1 parent 17f308a commit 5b640c6

File tree

3 files changed

+32
-5
lines changed

3 files changed

+32
-5
lines changed

frontend/src/pages/Analytics.tsx

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import React, { useState } from 'react';
2+
import { useQuery } from 'react-query';
23
import TimeIntervalSelector, { TimeIntervalOption, timeIntervals } from '../components/TimeIntervalSelector';
34
import AnalyticsSummaryCards from '../components/analytics/AnalyticsSummaryCards';
45
import ConsumptionTrendsChart from '../components/analytics/ConsumptionTrendsChart';
@@ -10,6 +11,7 @@ import ComparativeAnalysis from '../components/analytics/ComparativeAnalysis';
1011
import DevicePerformanceTable from '../components/analytics/DevicePerformanceTable';
1112
import EnergyReportsGenerator from '../components/analytics/EnergyReportsGenerator';
1213
import { useAnalyticsSummary } from '../hooks/useAnalyticsData';
14+
import deviceService from '../services/deviceService';
1315
import { ChartParams } from '../types/analytics';
1416

1517
const Analytics: React.FC = () => {
@@ -25,6 +27,21 @@ const Analytics: React.FC = () => {
2527
// Fetch analytics summary data
2628
const { data: summaryData, isLoading: summaryLoading, error: summaryError } = useAnalyticsSummary();
2729

30+
// Fetch device data to get accurate device counts for analytics
31+
const { data: devices } = useQuery(
32+
'analytics-devices',
33+
() => deviceService.getDevices(),
34+
{
35+
refetchInterval: 5 * 60 * 1000, // Refresh every 5 minutes
36+
}
37+
);
38+
39+
// Augment analytics summary with correct device count
40+
const augmentedSummaryData = summaryData && devices ? {
41+
...summaryData,
42+
active_devices: devices.filter(device => device.status === 'online').length
43+
} : summaryData;
44+
2845
if (summaryLoading && !summaryData) {
2946
return (
3047
<div className="flex items-center justify-center h-64">
@@ -69,7 +86,7 @@ const Analytics: React.FC = () => {
6986
</div>
7087
) : (
7188
<AnalyticsSummaryCards
72-
data={summaryData}
89+
data={augmentedSummaryData}
7390
isLoading={summaryLoading}
7491
onCardClick={handleCardClick}
7592
/>

frontend/src/pages/Dashboard.tsx

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import React, { useState } from 'react';
22
import { useQuery } from 'react-query';
33
import { Activity, Zap, TrendingUp, AlertTriangle } from 'lucide-react';
44
import dashboardService from '../services/dashboardService';
5+
import deviceService from '../services/deviceService';
56
import { formatPower, formatEnergy } from '../utils';
67
import PowerChart from '../components/charts/PowerChart';
78
import EnergyChart from '../components/charts/EnergyChart';
@@ -19,6 +20,15 @@ const Dashboard: React.FC = () => {
1920
}
2021
);
2122

23+
// Fetch device data to get accurate device counts
24+
const { data: devices } = useQuery(
25+
'dashboard-devices',
26+
() => deviceService.getDevices(),
27+
{
28+
refetchInterval: 5000, // Refresh every 5 seconds for real-time updates
29+
}
30+
);
31+
2232
const { data: powerChartData, isLoading: powerLoading } = useQuery(
2333
['powerChart', selectedInterval.interval, selectedInterval.timeRange],
2434
() => {
@@ -66,14 +76,14 @@ const Dashboard: React.FC = () => {
6676
const stats = [
6777
{
6878
name: 'Total Devices',
69-
value: dashboardData?.totalDevices || 0,
79+
value: devices?.length || 0,
7080
icon: Activity,
7181
color: 'text-blue-600',
7282
bgColor: 'bg-blue-100',
7383
},
7484
{
7585
name: 'Online Devices',
76-
value: dashboardData?.onlineDevices || 0,
86+
value: devices?.filter(device => device.status === 'online').length || 0,
7787
icon: Zap,
7888
color: 'text-green-600',
7989
bgColor: 'bg-green-100',

frontend/src/services/deviceService.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import { Device, DeviceCreate, DeviceUpdate, EnergyReading, ApiResponse } from '
33

44
class DeviceService {
55
async getDevices(): Promise<Device[]> {
6-
// Use the new data-ingestion endpoint
7-
const response = await api.get<ApiResponse<Device[]>>('/api/v1/data-ingestion/devices/list');
6+
// Use the MQTT data-ingestion endpoint with real-time device data
7+
const response = await api.get<ApiResponse<Device[]>>('/api/v1/data-ingestion/devices');
88
if (response.data.success && response.data.data) {
99
return response.data.data;
1010
}

0 commit comments

Comments
 (0)