Skip to content

Commit 31a7a67

Browse files
committed
[#1690] fix: rename _type property to instance_type in Instance class and get the right key in instances data when fetching it
1 parent f0646eb commit 31a7a67

File tree

1 file changed

+35
-15
lines changed

1 file changed

+35
-15
lines changed

src/ui/src/Instances.py

+35-15
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class Instance:
1515
_id: str
1616
name: str
1717
hostname: str
18-
_type: str
18+
instance_type: str
1919
health: bool
2020
env: Any
2121
apiCaller: ApiCaller
@@ -25,17 +25,19 @@ def __init__(
2525
_id: str,
2626
name: str,
2727
hostname: str,
28-
_type: str,
28+
instance_type: str,
2929
status: str,
3030
data: Any = None,
3131
apiCaller: Optional[ApiCaller] = None,
3232
) -> None:
3333
self._id = _id
3434
self.name = name
3535
self.hostname = hostname
36-
self._type = _type
36+
self.instance_type = instance_type
3737
self.health = status == "up" and (
38-
(data.attrs["State"]["Health"]["Status"] == "healthy" if "Health" in data.attrs["State"] else False) if _type == "container" and data else True
38+
(data.attrs["State"]["Health"]["Status"] == "healthy" if "Health" in data.attrs["State"] else False)
39+
if instance_type == "container" and data
40+
else True
3941
)
4042
self.env = data
4143
self.apiCaller = apiCaller or ApiCaller()
@@ -45,7 +47,7 @@ def id(self) -> str:
4547
return self._id
4648

4749
def reload(self) -> bool:
48-
if self._type == "local":
50+
if self.instance_type == "local":
4951
return (
5052
run(
5153
[join(sep, "usr", "sbin", "nginx"), "-s", "reload"],
@@ -59,7 +61,7 @@ def reload(self) -> bool:
5961
return self.apiCaller.send_to_apis("POST", "/reload")
6062

6163
def start(self) -> bool:
62-
if self._type == "local":
64+
if self.instance_type == "local":
6365
return (
6466
run(
6567
[join(sep, "usr", "sbin", "nginx"), "-e", "/var/log/bunkerweb/error.log"],
@@ -73,7 +75,7 @@ def start(self) -> bool:
7375
return self.apiCaller.send_to_apis("POST", "/start")
7476

7577
def stop(self) -> bool:
76-
if self._type == "local":
78+
if self.instance_type == "local":
7779
return (
7880
run(
7981
[join(sep, "usr", "sbin", "nginx"), "-s", "stop"],
@@ -87,7 +89,7 @@ def stop(self) -> bool:
8789
return self.apiCaller.send_to_apis("POST", "/stop")
8890

8991
def restart(self) -> bool:
90-
if self._type == "local":
92+
if self.instance_type == "local":
9193
proc = run(
9294
[join(sep, "usr", "sbin", "nginx"), "-s", "stop"],
9395
stdin=DEVNULL,
@@ -368,7 +370,9 @@ def get_bans(self, _id: Optional[int] = None) -> List[dict[str, Any]]:
368370
return []
369371
if not resp:
370372
return []
371-
return instance_bans[instance.name if instance.name != "local" else "127.0.0.1"].get("data", [])
373+
return instance_bans[(instance.name if instance.instance_type != "pod" else instance.hostname) if instance.name != "local" else "127.0.0.1"].get(
374+
"data", []
375+
)
372376

373377
bans: List[dict[str, Any]] = []
374378
for instance in self.get_instances():
@@ -378,7 +382,11 @@ def get_bans(self, _id: Optional[int] = None) -> List[dict[str, Any]]:
378382
continue
379383
if not resp:
380384
continue
381-
bans.extend(instance_bans[instance.name if instance.name != "local" else "127.0.0.1"].get("data", []))
385+
bans.extend(
386+
instance_bans[(instance.name if instance.instance_type != "pod" else instance.hostname) if instance.name != "local" else "127.0.0.1"].get(
387+
"data", []
388+
)
389+
)
382390

383391
bans.sort(key=itemgetter("exp"))
384392

@@ -425,7 +433,12 @@ def get_reports(self, _id: Optional[int] = None) -> List[dict[str, Any]]:
425433
return []
426434
if not resp:
427435
return []
428-
return (instance_reports[instance.name if instance.name != "local" else "127.0.0.1"].get("msg") or {"requests": []})["requests"]
436+
return (
437+
instance_reports[(instance.name if instance.instance_type != "pod" else instance.hostname) if instance.name != "local" else "127.0.0.1"].get(
438+
"msg"
439+
)
440+
or {"requests": []}
441+
)["requests"]
429442

430443
reports: List[dict[str, Any]] = []
431444
for instance in self.get_instances():
@@ -436,7 +449,14 @@ def get_reports(self, _id: Optional[int] = None) -> List[dict[str, Any]]:
436449

437450
if not resp:
438451
continue
439-
reports.extend((instance_reports[instance.name if instance.name != "local" else "127.0.0.1"].get("msg") or {"requests": []})["requests"])
452+
reports.extend(
453+
(
454+
instance_reports[
455+
(instance.name if instance.instance_type != "pod" else instance.hostname) if instance.name != "local" else "127.0.0.1"
456+
].get("msg")
457+
or {"requests": []}
458+
)["requests"]
459+
)
440460

441461
reports.sort(key=itemgetter("date"), reverse=True)
442462

@@ -446,7 +466,7 @@ def get_metrics(self, plugin_id: str):
446466
# Get metrics from all instances
447467
metrics = {}
448468
for instance in self.get_instances():
449-
instance_name = instance.name if instance.name != "local" else "127.0.0.1"
469+
instance_name = (instance.name if instance.instance_type != "pod" else instance.hostname) if instance.name != "local" else "127.0.0.1"
450470

451471
try:
452472
if plugin_id == "redis":
@@ -505,7 +525,7 @@ def get_ping(self, plugin_id: str):
505525
# Need at least one instance to get a success ping to return success
506526
ping = {"status": "error"}
507527
for instance in self.get_instances():
508-
instance_name = instance.name if instance.name != "local" else "127.0.0.1"
528+
instance_name = (instance.name if instance.instance_type != "pod" else instance.hostname) if instance.name != "local" else "127.0.0.1"
509529

510530
try:
511531
resp, ping_data = instance.ping(plugin_id)
@@ -527,7 +547,7 @@ def get_data(self, plugin_endpoint: str):
527547
data = []
528548
for instance in self.get_instances():
529549

530-
instance_name = instance.name if instance.name != "local" else "127.0.0.1"
550+
instance_name = (instance.name if instance.instance_type != "pod" else instance.hostname) if instance.name != "local" else "127.0.0.1"
531551

532552
try:
533553
resp, instance_data = instance.data(plugin_endpoint)

0 commit comments

Comments
 (0)