renewed development
This commit is contained in:
+43
-12
@@ -1,6 +1,7 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Dict, Any, List, Optional
|
||||
import asyncio
|
||||
from typing import Callable, Dict, Any, List, Optional
|
||||
import time
|
||||
|
||||
import requests
|
||||
@@ -63,7 +64,6 @@ class RESTSender(NamedObject):
|
||||
f"Failed to send status={excpt.response.status_code} {excpt.response.text}" # type: ignore
|
||||
) from excpt
|
||||
|
||||
|
||||
class MdSummary(HistMdBar):
|
||||
def __init__(
|
||||
self,
|
||||
@@ -105,6 +105,7 @@ class MdSummary(HistMdBar):
|
||||
)
|
||||
return res
|
||||
|
||||
MdSummaryCallbackT = Callable[[List[MdSummary]], None]
|
||||
|
||||
class MdSummaryCollector(NamedObject):
|
||||
sender_: RESTSender
|
||||
@@ -114,8 +115,9 @@ class MdSummaryCollector(NamedObject):
|
||||
history_depth_sec_: int
|
||||
|
||||
history_: List[MdSummary]
|
||||
callbacks_: List[MdSummaryCallbackT]
|
||||
timer_: Optional[Timer]
|
||||
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
sender: RESTSender,
|
||||
@@ -130,8 +132,12 @@ class MdSummaryCollector(NamedObject):
|
||||
self.interval_sec_ = interval_sec
|
||||
self.history_depth_sec_ = history_depth_sec
|
||||
|
||||
self.history_depth_sec_ = []
|
||||
self.history_ = []
|
||||
self.callbacks_ = []
|
||||
self.timer_ = None
|
||||
|
||||
def add_callback(self, cb: MdSummaryCallbackT) -> None:
|
||||
self.callbacks_.append(cb)
|
||||
|
||||
def rqst_data(self) -> Dict[str, Any]:
|
||||
return {
|
||||
@@ -145,22 +151,32 @@ class MdSummaryCollector(NamedObject):
|
||||
response: requests.Response = self.sender_.send_post(
|
||||
endpoint="md_summary", post_body=self.rqst_data()
|
||||
)
|
||||
if response.status_code not in (200, 201):
|
||||
Log.error(f"{self.fname()}: Received error: {response.status_code} - {response.text}")
|
||||
return []
|
||||
return MdSummary.from_REST_response(response=response)
|
||||
|
||||
def get_last(self) -> Optional[MdSummary]:
|
||||
rqst_data = self.rqst_data()
|
||||
rqst_data["history_depth_sec"] = self.interval_sec_
|
||||
rqst_data["history_depth_sec"] = self.interval_sec_ * 2
|
||||
response: requests.Response = self.sender_.send_post(
|
||||
endpoint="md_summary", post_body=rqst_data
|
||||
)
|
||||
if response.status_code not in (200, 201):
|
||||
Log.error(f"{self.fname()}: Received error: {response.status_code} - {response.text}")
|
||||
return None
|
||||
res = MdSummary.from_REST_response(response=response)
|
||||
return None if len(res) == 0 else res[-1]
|
||||
|
||||
def is_empty(self) -> bool:
|
||||
return len(self.history_) == 0
|
||||
|
||||
async def start(self) -> None:
|
||||
if self.timer_:
|
||||
Log.error(f"{self.fname()}: Timer is already started")
|
||||
return
|
||||
self.history_ = self.get_history()
|
||||
self.run_callbacks()
|
||||
self.timer_ = Timer(
|
||||
start_in_sec=self.interval_sec_,
|
||||
is_periodic=True,
|
||||
@@ -169,15 +185,19 @@ class MdSummaryCollector(NamedObject):
|
||||
)
|
||||
|
||||
async def _load_new(self) -> None:
|
||||
|
||||
last: Optional[MdSummary] = self.get_last()
|
||||
if not last:
|
||||
# URGENT logging
|
||||
Log.warning(f"{self.fname()}: did not get last update")
|
||||
return
|
||||
if last.ts_ns_ <= self.history_[-1].ts_ns_:
|
||||
# URGENT logging
|
||||
if not self.is_empty() and last.ts_ns_ <= self.history_[-1].ts_ns_:
|
||||
Log.info(f"{self.fname()}: Received {last}. Already Have: {self.history_[-1]}")
|
||||
return
|
||||
self.history_.append(last)
|
||||
# URGENT implement notification
|
||||
self.run_callbacks()
|
||||
|
||||
def run_callbacks(self) -> None:
|
||||
[cb(self.history_) for cb in self.callbacks_]
|
||||
|
||||
def stop(self) -> None:
|
||||
if self.timer_:
|
||||
@@ -196,7 +216,8 @@ class CvttRESTClient(NamedObject):
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
config = Config(json_src={"cvtt_base_url": "http://cvtt-tester-01.cvtt.vpn:23456"})
|
||||
# config = Config(json_src={"cvtt_base_url": "http://cvtt-tester-01.cvtt.vpn:23456"})
|
||||
config = Config(json_src={"cvtt_base_url": "http://dev-server-02.cvtt.vpn:23456"})
|
||||
|
||||
cvtt_client = CvttRESTClient(config)
|
||||
|
||||
@@ -208,6 +229,16 @@ if __name__ == "__main__":
|
||||
history_depth_sec=24 * 3600,
|
||||
)
|
||||
|
||||
hist = mdsc.get_history()
|
||||
last = mdsc.get_last()
|
||||
def _calback(history: List[MdSummary]) -> None:
|
||||
Log.info(f"MdSummary Hist Length is {len(history)}. Last summary: {history[-1] if len(history) > 0 else '[]'}")
|
||||
|
||||
mdsc.add_callback(_calback)
|
||||
|
||||
async def __run() -> None:
|
||||
Log.info("Starting...")
|
||||
await mdsc.start()
|
||||
while True:
|
||||
await asyncio.sleep(5)
|
||||
|
||||
asyncio.run(__run())
|
||||
pass
|
||||
|
||||
Reference in New Issue
Block a user