api: Add set_max_concurrent_bids helper.

This commit is contained in:
tecnovert
2024-11-23 18:48:46 +02:00
parent 01f6a1d877
commit 33105a832f
6 changed files with 203 additions and 14 deletions

View File

@@ -937,7 +937,9 @@ class BasicSwap(BaseApp):
def start(self):
import platform
self.log.info(f"Starting BasicSwap {__version__}, database v{self.db_version}\n\n")
self.log.info(
f"Starting BasicSwap {__version__}, database v{self.db_version}\n\n"
)
self.log.info(f"Python version: {platform.python_version()}")
self.log.info(f"SQLite version: {sqlite3.sqlite_version}")
self.log.info(f"Timezone offset: {time.timezone} ({time.tzname[0]})")
@@ -7308,6 +7310,9 @@ class BasicSwap(BaseApp):
if active_bid[2] != BidStates.SWAP_COMPLETED:
num_not_completed += 1
max_concurrent_bids = opts.get("max_concurrent_bids", 1)
self.log.debug(
f"active_bids {num_not_completed}, max_concurrent_bids {max_concurrent_bids}"
)
if num_not_completed >= max_concurrent_bids:
raise AutomationConstraint(
"Already have {} bids to complete".format(num_not_completed)
@@ -10524,6 +10529,7 @@ class BasicSwap(BaseApp):
if filter_bid_id is not None:
query_str += "AND bids.bid_id = :filter_bid_id "
query_data["filter_bid_id"] = filter_bid_id
if offer_id is not None:
query_str += "AND bids.offer_id = :filter_offer_id "
query_data["filter_offer_id"] = offer_id
@@ -10734,14 +10740,37 @@ class BasicSwap(BaseApp):
finally:
self.closeDB(cursor, commit=False)
def updateAutomationStrategy(self, strategy_id: int, data, note: str) -> None:
def updateAutomationStrategy(self, strategy_id: int, data: dict) -> None:
self.log.debug(f"updateAutomationStrategy {strategy_id}")
try:
cursor = self.openDB()
strategy = firstOrNone(
self.query(AutomationStrategy, cursor, {"record_id": strategy_id})
)
strategy.data = json.dumps(data).encode("utf-8")
strategy.note = note
if "data" in data:
strategy.data = json.dumps(data["data"]).encode("utf-8")
self.log.debug("data {}".format(data["data"]))
if "note" in data:
strategy.note = data["note"]
if "label" in data:
strategy.label = data["label"]
if "only_known_identities" in data:
strategy.only_known_identities = int(data["only_known_identities"])
if "set_max_concurrent_bids" in data:
new_max_concurrent_bids = data["set_max_concurrent_bids"]
ensure(
isinstance(new_max_concurrent_bids, int),
"set_max_concurrent_bids must be an integer",
)
strategy_data = (
{}
if strategy.data is None
else json.loads(strategy.data.decode("utf-8"))
)
strategy_data["max_concurrent_bids"] = new_max_concurrent_bids
strategy.data = json.dumps(strategy_data).encode("utf-8")
self.updateDB(strategy, cursor, ["record_id"])
finally:
self.closeDB(cursor)

View File

@@ -551,7 +551,7 @@ class AutomationStrategy(Table):
label = Column("string")
type_ind = Column("integer")
only_known_identities = Column("integer")
num_concurrent = Column("integer")
num_concurrent = Column("integer") # Deprecated, use data["max_concurrent"]
data = Column("blob")
note = Column("string")

View File

@@ -718,6 +718,8 @@ def js_automationstrategies(self, url_split, post_string: str, is_json: bool) ->
"sort_dir": "desc",
}
strat_id = int(url_split[3]) if len(url_split) > 3 else None
if post_string != "":
post_data = getFormData(post_string, is_json)
@@ -738,15 +740,36 @@ def js_automationstrategies(self, url_split, post_string: str, is_json: bool) ->
filters["limit"] > 0 and filters["limit"] <= PAGE_LIMIT
), "Invalid limit"
if len(url_split) > 3:
strat_id = int(url_split[3])
set_data = {}
if have_data_entry(post_data, "set_label"):
set_data["label"] = get_data_entry(post_data, "set_label")
if have_data_entry(post_data, "set_data"):
set_data["data"] = json.loads(get_data_entry(post_data, "set_data"))
if have_data_entry(post_data, "set_note"):
set_data["note"] = get_data_entry(post_data, "set_note")
if have_data_entry(post_data, "set_only_known_identities"):
set_data["only_known_identities"] = get_data_entry(
post_data, "set_only_known_identities"
)
if have_data_entry(post_data, "set_max_concurrent_bids"):
if "data" in set_data:
raise ValueError("set_max_concurrent_bids can't be used with set_data")
set_data["set_max_concurrent_bids"] = int(
get_data_entry(post_data, "set_max_concurrent_bids")
)
if set_data:
ensure(strat_id is not None, "Must specify a strategy to modify")
swap_client.updateAutomationStrategy(strat_id, set_data)
if strat_id is not None:
strat_data = swap_client.getAutomationStrategy(strat_id)
rv = {
"record_id": strat_data.record_id,
"label": strat_data.label,
"type_ind": strat_data.type_ind,
"only_known_identities": strat_data.only_known_identities,
"num_concurrent": strat_data.num_concurrent,
"data": json.loads(strat_data.data.decode("utf-8")),
"note": "" if strat_data.note is None else strat_data.note,
}

View File

@@ -115,9 +115,11 @@ def page_automation_strategy(self, url_split, post_string):
show_edit_form = True
if have_data_entry(form_data, "apply"):
try:
data = json.loads(get_data_entry_or(form_data, "data", ""))
note = get_data_entry_or(form_data, "note", "")
swap_client.updateAutomationStrategy(strategy_id, data, note)
data = {
"data": json.loads(get_data_entry_or(form_data, "data", "")),
"note": get_data_entry_or(form_data, "note", ""),
}
swap_client.updateAutomationStrategy(strategy_id, data)
messages.append("Updated")
except Exception as e:
err_messages.append(str(e))