add disabled state
This commit is contained in:
parent
5bb4da7499
commit
2c96653753
10
app.py
10
app.py
|
|
@ -5,6 +5,16 @@ from messages.Bybit import Bybit
|
|||
from messages.TickerData import TickerData
|
||||
|
||||
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)
|
||||
|
||||
def main():
|
||||
|
|
|
|||
|
|
@ -5,4 +5,13 @@ class Condition:
|
|||
id: str
|
||||
symbol: 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
|
||||
|
|
@ -1,5 +1,6 @@
|
|||
import os
|
||||
from typing import Callable
|
||||
from flask import jsonify
|
||||
from rethinkdb import RethinkDB
|
||||
from .Condition import Condition
|
||||
|
||||
|
|
@ -33,16 +34,26 @@ def fetch_conditions() -> list[Condition]:
|
|||
conditions: list[Condition] = list()
|
||||
|
||||
class DbConnector:
|
||||
@staticmethod
|
||||
def watch_conditions(callback: Callable[[Condition], None]):
|
||||
global conditions
|
||||
|
||||
conditions = fetch_conditions()
|
||||
|
||||
for cond in conditions:
|
||||
callback(cond)
|
||||
if not cond.disabled:
|
||||
callback(cond)
|
||||
|
||||
feed = r.table(TABLE_NAME).changes().run(get_connection())
|
||||
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)
|
||||
|
||||
@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())
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue