1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-11-11 12:46:49 +00:00

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 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Kk4h2TcUiaWmU5MUszYSea
This commit is contained in:
catborise 2026-11-10 11:53:40 +03:00
parent a8f7c8aeaa
commit 0d20d07b29
4 changed files with 19 additions and 6 deletions

View file

@ -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"]

View file

@ -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}

View file

@ -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"]

View file

@ -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)