(20250108) Tick-In script handles the buffer better.
This commit is contained in:
@@ -44,6 +44,7 @@ import os
|
||||
from utils_v2.string import json
|
||||
from utils_v2.string import regex
|
||||
from utils_v2.system import files
|
||||
from utils_v2.date_time import date_time
|
||||
from utils_v2.database.async_mongo_v2 import AsyncMongo
|
||||
from utils_v2.queue.kafka.controllers.async_kafka import ConsumerKafka, get_ssl_context
|
||||
from utils_v2.cache.async_redis_cache_v2 import AsyncRedisCache
|
||||
@@ -82,7 +83,7 @@ from icecream import IceCreamDebugger
|
||||
|
||||
# Debugging:
|
||||
printer = IceCreamDebugger(prefix = "Tick-Out | ", includeContext = True)
|
||||
# printer.disable()
|
||||
no_context_printer = IceCreamDebugger(prefix = "Tick-Out | ", includeContext = False)
|
||||
|
||||
# To make API calls:
|
||||
http_client = httpx.AsyncClient(
|
||||
@@ -219,12 +220,14 @@ async def ticks_from_kafka(
|
||||
# Do the next part infinitely:
|
||||
while True:
|
||||
|
||||
# Note the time:
|
||||
now_utc = date_time.get_current_utc_date_time().timestamp()
|
||||
|
||||
# Get messages form Kafka:
|
||||
ticks = await consumer.consume(
|
||||
count = fetch_count,
|
||||
timeout = fetch_timeout
|
||||
)
|
||||
printer(len(ticks))
|
||||
|
||||
# If there are no updates to give:
|
||||
if not ticks: continue
|
||||
@@ -235,6 +238,21 @@ async def ticks_from_kafka(
|
||||
tasks = [send_ticks(t.value if isinstance(t.value, list) else [t.value]) for t in ticks]
|
||||
results = await asyncio.gather(*tasks)
|
||||
|
||||
# Analyze the ticks:
|
||||
# latency = [abs(now_utc - t.value.get("rcvdTs", t.value["tradeTs"])) for t in ticks]
|
||||
latency = [abs(now_utc - t.ts.timestamp()) for t in ticks]
|
||||
avg_latency = sum(latency) / len(latency)
|
||||
total_ticks = len(ticks)
|
||||
# late_cutoff_seconds = 3.0
|
||||
#
|
||||
# late_ticks = 0
|
||||
# for tick in ticks:
|
||||
# if now_utc - tick.value["tradeTs"] > late_cutoff_seconds:
|
||||
# late_ticks += 1
|
||||
# ticks_str = f"COUNT: {total_ticks: >5,} | LATE: {late_ticks: >5,} ({(late_ticks/total_ticks)*100.0:.2f}%)"
|
||||
ticks_str = f"COUNT: {total_ticks: >5,} | AVG. LATENCY: {avg_latency:.5f}"
|
||||
no_context_printer(ticks_str)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
|
||||
Reference in New Issue
Block a user