add disabled state

This commit is contained in:
null 2024-03-07 20:07:39 +01:00
parent 5bb4da7499
commit 2c96653753
3 changed files with 34 additions and 4 deletions

10
app.py
View File

@ -5,6 +5,16 @@ from messages.Bybit import Bybit
from messages.TickerData import TickerData from messages.TickerData import TickerData
def handle_tickerdata(data: TickerData, condition: Condition): def handle_tickerdata(data: TickerData, condition: Condition):
result = False
if condition.condition == "lt":
result = Condition.lower_than(condition.value, data.lastPrice)
elif condition.condition == "gt":
result = Condition.greater_than(condition.value, data.lastPrice)
if result:
DbConnector.disable_condition(condition.id)
print(data.symbol + ": " + data.lastPrice) print(data.symbol + ": " + data.lastPrice)
def main(): def main():

View File

@ -5,4 +5,13 @@ class Condition:
id: str id: str
symbol: str symbol: str
condition: str condition: str
value: float value: float
disabled: bool
@staticmethod
def lower_than(value: float, lastPrice: float):
return value < lastPrice
@staticmethod
def greater_than(value: float, lastPrice: float):
return value < lastPrice

View File

@ -1,5 +1,6 @@
import os import os
from typing import Callable from typing import Callable
from flask import jsonify
from rethinkdb import RethinkDB from rethinkdb import RethinkDB
from .Condition import Condition from .Condition import Condition
@ -33,16 +34,26 @@ def fetch_conditions() -> list[Condition]:
conditions: list[Condition] = list() conditions: list[Condition] = list()
class DbConnector: class DbConnector:
@staticmethod
def watch_conditions(callback: Callable[[Condition], None]): def watch_conditions(callback: Callable[[Condition], None]):
global conditions global conditions
conditions = fetch_conditions() conditions = fetch_conditions()
for cond in conditions: for cond in conditions:
callback(cond) if not cond.disabled:
callback(cond)
feed = r.table(TABLE_NAME).changes().run(get_connection()) feed = r.table(TABLE_NAME).changes().run(get_connection())
for change in feed: for change in feed:
if change['new_val'] and not change['old_val']: # New symbol added cond = Condition(**change['new_val'])
cond: Condition = change['new_val'] if not cond.disabled:
callback(cond) callback(cond)
@staticmethod
def disable_condition(condition_id: str):
cursor = r.table(TABLE_NAME).get(condition_id).run(get_connection())
json = jsonify(cursor)
json.disabled = True
r.table(TABLE_NAME).get(condition_id).update(json).run(get_connection())