Skip to content

Commit

Permalink
Merge pull request #83 from ecmwf-projects/dynamic-qos-rules-values
Browse files Browse the repository at this point in the history
Dynamic qos rules values
  • Loading branch information
mcucchi9 authored Oct 17, 2023
2 parents 3d81d03 + 7fa6ce0 commit c6ce8de
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 17 deletions.
22 changes: 13 additions & 9 deletions cads_broker/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,34 +307,38 @@ def count_users(status: str, entry_point: str, session: sa.orm.Session) -> int:
)


def get_qos_status_from_request(request: dict[str, Any]) -> dict[str, list[str]]:
def get_qos_status_from_request(
request: dict[str, Any]
) -> dict[str, list[tuple[str, str]]]:
ret_value: dict[str, list[str]] = {}
for rule_name, rules in request["qos_status"].items():
ret_value[rule_name] = []
for rule in rules.values():
ret_value[rule_name].append(rule.get("info", ""))
ret_value[rule_name].append(
(rule.get("info", ""), rule.get("conclusion", ""))
)
return ret_value


def set_request_qos_rule(
request_uid: str,
request: SystemRequest,
rule,
session: sa.orm.Session,
):
request = get_request(request_uid=request_uid, session=session)
qos_status = request.qos_status
old_rules = qos_status.get(rule.name, {})
if rule.uid in old_rules:
rule_uid = rule.get_uid(request)
if rule_uid in old_rules:
return
old_rules[rule.uid] = {
"conclusion": str(rule.conclusion),
"info": str(rule.info),
old_rules[rule_uid] = {
"conclusion": str(rule.evaluate(request)),
"info": str(rule.info).replace('"', ""),
"condition": str(rule.condition),
}
qos_status[rule.name] = old_rules
session.execute(
sa.update(SystemRequest)
.filter_by(request_uid=request_uid)
.filter_by(request_uid=request.request_uid)
.values(qos_status=qos_status)
)

Expand Down
4 changes: 2 additions & 2 deletions cads_broker/qos/QoS.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ def can_run(self, request, session):
for i, limit in enumerate(properties.limits):
if limit.full(request):
# performance. avoid interacting with db if limit is already there
if limit.uid not in request.qos_status.get(limit.name, []):
database.set_request_qos_rule(request.request_uid, limit, session)
if limit.get_uid(request) not in request.qos_status.get(limit.name, []):
database.set_request_qos_rule(request, limit, session)
limits.append(limit)
session.commit()
permissions = []
Expand Down
5 changes: 2 additions & 3 deletions cads_broker/qos/Rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,8 @@ def match(self, request):
def dump(self, out):
out(self)

@property
def uid(self):
return str(hash(self.__repr__()))
def get_uid(self, request):
return str(hash(f"{self.name} {self.info} {self.condition} : {self.evaluate(request)}"))

def __repr__(self):
return f"{self.name} {self.info} {self.condition} : {self.conclusion}"
Expand Down
15 changes: 12 additions & 3 deletions tests/test_02_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ def __init__(self, name, uid, conclusion, info, condition):
self.info = info
self.condition = condition

def get_uid(self, request):
return self.uid

def evaluate(self, request):
return self.uid


def mock_config(hash: str = "", config: dict[str, Any] = {}, form: dict[str, Any] = {}):
adaptor_properties = db.AdaptorProperties(
Expand Down Expand Up @@ -505,8 +511,8 @@ def test_set_request_qos_rule(session_obj: sa.orm.sessionmaker) -> None:
session.add(adaptor_properties)
session.add(request)
session.commit()
db.set_request_qos_rule(request_uid=request_uid, rule=limit_1, session=session)
db.set_request_qos_rule(request_uid=request_uid, rule=limit_2, session=session)
db.set_request_qos_rule(request=request, rule=limit_1, session=session)
db.set_request_qos_rule(request=request, rule=limit_2, session=session)
session.commit()
with session_obj() as session:
request = db.get_request(request_uid=request_uid, session=session)
Expand All @@ -532,7 +538,10 @@ def test_get_qos_status_from_request() -> None:
"rule_name_2": {"rule_key_2_1": {}},
}
}
exp_qos_status = {"rule_name_1": ["info_1_1", "info_1_2"], "rule_name_2": [""]}
exp_qos_status = {
"rule_name_1": [("info_1_1", "conclusion_1_1"), ("info_1_2", "conclusion_1_2")],
"rule_name_2": [("", "")],
}
res_qos_staus = db.get_qos_status_from_request(test_request)
assert exp_qos_status == res_qos_staus

Expand Down

0 comments on commit c6ce8de

Please sign in to comment.