(20250623) - Testing WSIO ticks and configure sentinel redis JSON for redis connection.

This commit is contained in:
yatmesh
2025-06-23 14:06:46 +05:30
parent 1593b01db0
commit 388b9bdd1b
2 changed files with 103 additions and 15 deletions
+90 -1
View File
@@ -39,6 +39,7 @@ sys.path.append("..")
# System-level activities:
import io
import os
import random
# My utils:
from utils_v2.string import json
@@ -254,6 +255,90 @@ async def ticks_from_kafka(
no_context_printer(ticks_str)
# ---------------------------------------------------------------------------------------------------------------------
# DUMMY TICKS WSIO TEST - 23 - 06 - 2025
# ----------------------------------------------------------------------------------------------------------------------
async def dummy_ticks(
) -> None:
"""
This function must run in the background forever and just keep listening for ticks on Kafka and keep relaying them
to all the connected clients as per their watchlists.
:return: None
"""
printer("Starting Kafka consumer (ticks).")
# Do the next part infinitely:
while True:
no_context_printer("DUMMY TICKS")
# Note the time:
now_utc = date_time.get_current_utc_date_time().timestamp()
# Get messages form Kafka:
ticks = [
{
"broker": "zerodhaKite",
"brokerToken": 999999,
"exchange": "NSE",
"exchangeToken": "999999",
"segment": "NSE",
"type": "EQ",
"symbol": "TCAOFF",
"name": "The CA Office",
"expiry": None,
"strike": 0,
"bidQty": 65,
"bidRate": 1715,
"askQty": 105,
"askRate": 1715.5,
"o": 1675,
"h": 1723.6,
"l": 1665,
"ltp": 1715.05*random.uniform(0.95, 1.05),
"qty": 20,
"chg": 29.200000000000045,
"pChg": 1.732115316170367,
"vwap": 1700.16,
"totVol": 4810200,
"rcvdTs": now_utc,
"tradeTs": now_utc,
"tradeTz": "Asia/Kolkata",
"exchgTs": now_utc,
"exchgTz": "Asia/Kolkata"
}
]
# If there are no updates to give:
if not ticks: continue
# Each message must be treated as an array of tick updates (list of dicts).
# In case the producer is sending each individual tick as a separate message,
# we normalize it to be a list:
tasks = [send_ticks(ticks)]
results = await asyncio.gather(*tasks)
await asyncio.sleep(0.25)
# 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)
# ---------------------------------------------------------------------------------------------------------------------
@@ -357,7 +442,7 @@ async def init(
# ┛┗┗ ┗┻┗┛ ┗┛┗┻┗┛┗┗
redis_cache = AsyncRedisCache(
sentinel_json=script_cred["redisCache"]["general"]["sentinelJson"],
connection_string=script_cred["redisCache"]["general"]["sentinelJson"],
# connection_string = script_cred["redisCache"]["general"]["connectionString"],
# serializer = JSONSerializer(),
debug = debug,
@@ -382,6 +467,10 @@ async def init(
fetch_timeout = 1.0
)
sio.start_background_task(
dummy_ticks
)
# ┳┓
# ┃┃┏┓┏┓┏┓
# ┻┛┗┛┛┗┗