From 0d20d07b29a908353894c54318264cb1504b3222 Mon Sep 17 00:00:00 2001 From: catborise Date: Tue, 10 Nov 2026 11:53:40 +0300 Subject: [PATCH] fix(ui): host and VM charts give their points the panel's time A host's charts and their "Updated at" line took their times from timezone.now(), which is UTC, while a VM's charts took time.strftime(), the panel's time zone. With TIME_ZONE=Europe/Istanbul the same moment read 09:00 on the host page and 12:00 on the VM page. Both now give the panel's time. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Kk4h2TcUiaWmU5MUszYSea --- computes/tests/test_cpu_graph.py | 10 +++++++++- computes/views.py | 4 ++-- instances/tests/test_stats_sample.py | 8 +++++++- instances/views.py | 3 +-- 4 files changed, 19 insertions(+), 6 deletions(-) diff --git a/computes/tests/test_cpu_graph.py b/computes/tests/test_cpu_graph.py index f9bf14a2..3b78c751 100644 --- a/computes/tests/test_cpu_graph.py +++ b/computes/tests/test_cpu_graph.py @@ -2,6 +2,7 @@ previous answer and now, without waiting, when that sample is recent and unaltered; otherwise over one second as before.""" +from datetime import datetime, timezone as dt_timezone import json import shutil import subprocess @@ -11,7 +12,7 @@ from unittest.mock import PropertyMock, patch from django.conf import settings from django.contrib.auth import get_user_model from django.core import signing -from django.test import TestCase +from django.test import TestCase, override_settings from django.urls import reverse from libvirt import libvirtError from lxml import html @@ -53,6 +54,13 @@ class GraphTestCase(TestCase): self.graph(cpu_sample=data["cpu_sample"]) self.assertEqual(self.previous(), (10, 20, 100.0)) + @override_settings(TIME_ZONE="Europe/Istanbul") + def test_the_time_of_a_point_is_the_panels(self): + with patch("django.utils.timezone.now", return_value=datetime(2026, 11, 10, 9, 0, tzinfo=dt_timezone.utc)): + self.assertEqual(self.graph()["timeline"], "12:00:00") + self.conn.get_cpu_usage.side_effect = libvirtError("unreachable") # its error answer too + self.assertEqual(self.graph()["timeline"], "12:00:00") + def test_an_altered_old_or_foreign_sample_is_not_used(self): token = self.graph()["cpu_sample"] foreign = self.graph(self.other)["cpu_sample"] diff --git a/computes/views.py b/computes/views.py index 200bc9e3..f2126c50 100644 --- a/computes/views.py +++ b/computes/views.py @@ -293,7 +293,7 @@ class ComputeManager: return data["idle"], data["total"], data["time"] def compute_graph(self, cpu_sample=None): - result = {"cpudata": None, "memdata": {"total": None, "usage": None}, "timeline": timezone.now().strftime("%H:%M:%S")} + result = {"cpudata": None, "memdata": {"total": None, "usage": None}, "timeline": timezone.localtime().strftime("%H:%M:%S")} try: conn = wvmHostDetails( self.compute.hostname, @@ -306,7 +306,7 @@ class ComputeManager: result = { "cpudata": cpu["usage"], "memdata": conn.get_memory_usage(), - "timeline": timezone.now().strftime("%H:%M:%S"), + "timeline": timezone.localtime().strftime("%H:%M:%S"), # the panel's time, as the VM charts "window": round(cpu["window"], 1), "cpu_sample": CPU_SAMPLE_SIGNER.sign_object( {"compute": self.compute.id, "idle": idle, "total": total, "time": taken} diff --git a/instances/tests/test_stats_sample.py b/instances/tests/test_stats_sample.py index c01715f7..16a7f42e 100644 --- a/instances/tests/test_stats_sample.py +++ b/instances/tests/test_stats_sample.py @@ -2,6 +2,7 @@ its compute; the next poll sends it back so that the rates are averages since then, without waiting.""" +from datetime import datetime, timezone as dt_timezone import json import shutil import subprocess @@ -13,7 +14,7 @@ from computes.models import Compute from django.conf import settings from django.contrib.auth import get_user_model from django.core import signing -from django.test import TestCase +from django.test import TestCase, override_settings from django.urls import reverse from lxml import html from instances.models import Instance @@ -63,6 +64,11 @@ class StatsViewTestCase(TestCase): self.stats(sample=data["sample"]) self.assertEqual(self.previous(), SAMPLE) + @override_settings(TIME_ZONE="Europe/Istanbul") + def test_the_time_of_a_point_is_the_panels(self): + with patch("django.utils.timezone.now", return_value=datetime(2026, 11, 10, 9, 0, tzinfo=dt_timezone.utc)): + self.assertEqual(self.stats()["timeline"], "12:00:00") + def test_an_altered_old_or_foreign_sample_is_not_used(self): token = self.stats()["sample"] foreign = self.stats(self.other)["sample"] diff --git a/instances/views.py b/instances/views.py index ad06070b..c334db53 100755 --- a/instances/views.py +++ b/instances/views.py @@ -8,7 +8,6 @@ import re import secrets import socket import subprocess -import time from ipaddress import ip_address from accounts.models import UserInstance, UserSSHKey @@ -480,7 +479,7 @@ def stats(request, pk): # MB/s and Mbit/s "blkdata": [{"dev": disk["dev"], "data": [mega(disk["rd"]), mega(disk["wr"])]} for disk in usage["disks"]], "netdata": [{"mac": nic["mac"], "data": [mega(nic["rx"]), mega(nic["tx"])]} for nic in usage["nics"]], - "timeline": time.strftime("%H:%M:%S"), + "timeline": timezone.localtime().strftime("%H:%M:%S"), # the panel's time, as the host charts } if "sample" in usage: result["window"] = None if usage["window"] is None else round(usage["window"], 1)