11// Variables to store chart instances
22let cpuTimeChart , memoryTimeChart , batteryTimeChart , networkTimeChart , dashboardMemoryTimeChart , cpuFrequencyTimeChart , currentTempTimeChart ;
33
4- // Function to fetch and display data
4+ function stringToDate ( dateString ) {
5+ const date = new Date ( dateString ) ;
6+ return date ;
7+ }
8+
59function fetchDataAndRenderCharts ( ) {
610 // Get the selected filter value
711 const filterValue = document . getElementById ( 'timeFilter' ) . value ;
@@ -11,20 +15,90 @@ function fetchDataAndRenderCharts() {
1115 . then ( response => response . json ( ) )
1216 . then ( data => {
1317 const cpuData = data . cpu ;
14- const timeData = data . time . map ( t => new Date ( t ) ) ; // Ensure date objects
1518 const memoryData = data . memory ;
1619 const batteryData = data . battery ;
1720 const networkSentData = data . network_sent ;
1821 const networkReceivedData = data . network_received ;
1922 const dashboardMemoryUsageData = data . dashboard_memory_usage ;
2023 const cpuFrequencyData = data . cpu_frequency ;
2124 const currentTempData = data . current_temp ;
25+ const currentTime = data . current_time ;
26+ const timeZoneName = Intl . DateTimeFormat ( ) . resolvedOptions ( ) . timeZone ;
27+ console . log ( currentTime ) ;
28+
29+ // Format the time data using the currentTime from backend
30+ const timeData = data . time . map ( time => formatDate ( time , currentTime ) ) ;
31+
32+ displayTimeAndTimeZone ( currentTime , timeZoneName ) ;
2233
2334 createCharts ( cpuData , timeData , memoryData , batteryData , networkSentData , networkReceivedData , dashboardMemoryUsageData , cpuFrequencyData , currentTempData ) ;
2435 } )
2536 . catch ( error => console . error ( 'Error fetching data:' , error ) ) ;
2637}
2738
39+ function formatDate ( dateString , currentTime ) {
40+ const date = new Date ( dateString ) ;
41+ const now = new Date ( currentTime ) ; // Use currentTime from backend
42+
43+ // Helper function to format with leading zeros
44+ const pad = ( num ) => String ( num ) . padStart ( 2 , '0' ) ;
45+
46+ // Manually extract UTC components
47+ const day = pad ( date . getUTCDate ( ) ) ; // e.g., 09
48+ const month = pad ( date . getUTCMonth ( ) + 1 ) ; // e.g., 04
49+ const year = date . getUTCFullYear ( ) ; // e.g., 2021
50+ const hours = pad ( date . getUTCHours ( ) ) ; // e.g., 11
51+ const minutes = pad ( date . getUTCMinutes ( ) ) ; // e.g., 33
52+
53+
54+ // Calculate time differences
55+ const diffDays = Math . floor ( ( now - date ) / ( 1000 * 60 * 60 * 24 ) ) ;
56+ const diffWeeks = Math . floor ( diffDays / 7 ) ;
57+ const diffMonths = now . getMonth ( ) - date . getUTCMonth ( ) + ( 12 * ( now . getFullYear ( ) - date . getUTCFullYear ( ) ) ) ;
58+ const diffYears = now . getFullYear ( ) - date . getUTCFullYear ( ) ;
59+
60+ // Determine the label based on time differences
61+ if ( diffDays === 0 ) {
62+ return `Today ${ hours } :${ minutes } ` ;
63+ } else if ( diffDays === 1 ) {
64+ return `Yesterday ${ hours } :${ minutes } ` ;
65+ } else if ( diffDays <= 3 ) {
66+ return `${ diffDays } Days Ago ${ hours } :${ minutes } ` ;
67+ } else if ( diffDays <= 7 ) {
68+ return `${ Math . ceil ( diffDays / 7 ) } Week${ diffDays > 7 ? 's' : '' } Ago ${ hours } :${ minutes } ` ;
69+ } else if ( diffDays <= 30 ) {
70+ return `${ Math . ceil ( diffDays / 7 ) } Weeks Ago ${ hours } :${ minutes } ` ;
71+ } else if ( diffMonths < 12 ) {
72+ return `${ diffMonths } Month${ diffMonths > 1 ? 's' : '' } Ago ${ hours } :${ minutes } ` ;
73+ } else if ( diffYears < 2 ) {
74+ return `Last Year ${ hours } :${ minutes } ` ;
75+ } else {
76+ return `${ year } /${ month } /${ day } ${ hours } :${ minutes } ` ;
77+ }
78+ }
79+
80+ function displayTimeAndTimeZone ( currentTime , timeZoneName ) {
81+ // Update current time and timezone
82+ function updateTime ( ) {
83+ const options = { hour : '2-digit' , minute : '2-digit' , second : '2-digit' , timeZone : timeZoneName } ;
84+ now = new Date ( ) ;
85+ const timeString = now . toLocaleTimeString ( 'en-US' , options ) ;
86+
87+ // Display the current time and timezone
88+ document . getElementById ( 'currentTime' ) . textContent = `Current Time: ${ timeString } ` ;
89+ document . getElementById ( 'timeZoneName' ) . textContent = `Time Zone: ${ timeZoneName } ` ;
90+ }
91+
92+ // Initial display
93+ updateTime ( ) ;
94+
95+ // Update time every second
96+ setInterval ( updateTime , 1000 ) ;
97+ }
98+
99+ // Display current time and time zone
100+
101+
28102// Function to create a chart with multiple datasets
29103function createChart ( ctx , labels , datasets , yLabel ) {
30104 if ( ctx . chart ) {
@@ -34,29 +108,22 @@ function createChart(ctx, labels, datasets, yLabel) {
34108 ctx . chart = new Chart ( ctx , {
35109 type : 'line' ,
36110 data : {
37- labels : labels , // Use timeData for x-axis labels
111+ labels : labels , // Use your timeData directly as labels
38112 datasets : datasets
39113 } ,
40114 options : {
41115 scales : {
42116 x : {
43- type : 'time' , // Use 'time' scale for proper date formatting
44- time : {
45- unit : 'minute' , // Adjust based on the granularity of your data
46- tooltipFormat : 'll HH:mm' , // Tooltip format for time
47- displayFormats : {
48- minute : 'HH:mm' , // Display format for minute-level granularity
49- hour : 'MMM D, HH:mm' , // Display format for hour-level granularity
50- day : 'MMM D' , // Display format for day-level granularity
51- week : 'MMM D' , // Display format for week-level granularity
52- month : 'MMM YYYY' , // Display format for month-level granularity
53- quarter : '[Q]Q YYYY' , // Display format for quarter-level granularity
54- year : 'YYYY' // Display format for year-level granularity
55- }
56- } ,
117+ type : 'category' , // Use 'category' scale to treat time as strings
57118 title : {
58119 display : true ,
59120 text : 'Time'
121+ } ,
122+ ticks : {
123+ autoSkip : true , // Automatically skip some labels to prevent overlap
124+ maxTicksLimit : 10 , // Maximum number of ticks to display
125+ maxRotation : 30 , // Prevent rotating the labels for better readability
126+ minRotation : 0
60127 }
61128 } ,
62129 y : {
@@ -71,6 +138,7 @@ function createChart(ctx, labels, datasets, yLabel) {
71138 } ) ;
72139}
73140
141+
74142// Function to create charts with the fetched data
75143function createCharts ( cpuData , timeData , memoryData , batteryData , networkSentData , networkReceivedData , dashboardMemoryUsageData , cpuFrequencyData , currentTempData ) {
76144 // CPU Usage Chart
0 commit comments