Skip to content

Commit 10ff991

Browse files
committed
API tier endpt changes needed to accomodate scores from DB
1 parent 959adc8 commit 10ff991

1 file changed

Lines changed: 14 additions & 47 deletions

File tree

api/coeqwal-api/routes/tier_endpoints.py

Lines changed: 14 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -79,35 +79,6 @@ async def get_db():
7979
yield connection
8080

8181

82-
def calculate_tier_scores(
83-
total_value: Optional[float],
84-
total_count: Optional[int]
85-
) -> dict:
86-
"""
87-
Calculate the two scores used by the Scenario Explorer for multi-value
88-
tier rows.
89-
90-
Inputs may be None when the ETL row is missing or partial. If both
91-
inputs are None, both outputs are None (no data). Otherwise None values
92-
are treated as 0 in the arithmetic so partial rows still yield scores.
93-
94-
Returns:
95-
- weighted_score: 1.0 (best) to 5.0 (worst), drives sort comparators
96-
- normalized_score: 0.0 to 1.0 (higher = better), Y-axis for the
97-
parallel plot
98-
"""
99-
100-
weighted_score = round(total_value / total_count, 3)
101-
102-
# Map weighted_score 1.0 to normalized 1.0 (best), 5.0 to 0.0 (worst)
103-
normalized_score = round((5.0 - weighted_score) / 4.0, 3)
104-
105-
return {
106-
"weighted_score": weighted_score,
107-
"normalized_score": normalized_score,
108-
}
109-
110-
11182
@router.get("/definitions", summary="Get tier descriptions")
11283
async def get_tier_definitions(
11384
response: Response,
@@ -248,6 +219,8 @@ async def get_all_scenario_tiers(
248219
tr.norm_tier_4,
249220
tr.total_value,
250221
tr.total_count,
222+
tr.weighted_score,
223+
tr.normalized_score,
251224
tr.single_tier_level
252225
FROM tier_result tr
253226
JOIN tier_definition td ON tr.tier_short_code = td.short_code
@@ -278,16 +251,12 @@ async def get_all_scenario_tiers(
278251
norm_2 = safe_float(row["norm_tier_2"])
279252
norm_3 = safe_float(row["norm_tier_3"])
280253
norm_4 = safe_float(row["norm_tier_4"])
281-
total_value = safe_float(row["total_value"])
282-
total_count = safe_int(row["total_count"])
283-
284-
scores = calculate_tier_scores(total_value, total_count)
285254

286255
tiers[tier_code] = {
287256
"name": row["name"],
288257
"type": "multi_value",
289-
"weighted_score": scores["weighted_score"],
290-
"normalized_score": scores["normalized_score"],
258+
"weighted_score": safe_float(row["weighted_score"]),
259+
"normalized_score": safe_float(row["normalized_score"]),
291260
# Fixed-length 4-element array, index i corresponds to
292261
# tier level i+1. Clients derive the tier label from the
293262
# index so we don't ship "tier1".."tier4" on every row
@@ -297,16 +266,16 @@ async def get_all_scenario_tiers(
297266
{"value": safe_float(row["tier_3_value"]), "normalized": norm_3},
298267
{"value": safe_float(row["tier_4_value"]), "normalized": norm_4},
299268
],
300-
"total": safe_float(row["total_value"]),
269+
"total": safe_int(row["total_count"]),
301270
}
302271
else:
303272
level = safe_int(row["single_tier_level"])
304273
if level is None:
305274
weighted = None
306275
normalized = None
307276
else:
308-
weighted = level
309-
normalized = round((5.0 - weighted) / 4.0, 3)
277+
weighted = safe_float(row["weighted_score"])
278+
normalized = safe_float(row["normalized_score"])
310279
tiers[tier_code] = {
311280
"name": row["name"],
312281
"type": "single_value",
@@ -377,6 +346,8 @@ async def get_batch_scenario_tiers(
377346
tr.norm_tier_4,
378347
tr.total_value,
379348
tr.total_count,
349+
tr.weighted_score,
350+
tr.normalized_score,
380351
tr.single_tier_level
381352
FROM tier_result tr
382353
JOIN tier_definition td ON tr.tier_short_code = td.short_code
@@ -401,16 +372,12 @@ async def get_batch_scenario_tiers(
401372
norm_2 = safe_float(row["norm_tier_2"])
402373
norm_3 = safe_float(row["norm_tier_3"])
403374
norm_4 = safe_float(row["norm_tier_4"])
404-
total_value = safe_float(row["total_value"])
405-
total_count = safe_int(row["total_count"])
406-
407-
scores = calculate_tier_scores(total_value, total_count)
408375

409376
result[scenario_id][tier_code] = {
410377
"name": row["name"],
411378
"type": "multi_value",
412-
"weighted_score": scores["weighted_score"],
413-
"normalized_score": scores["normalized_score"],
379+
"weighted_score": safe_float(row["weighted_score"]),
380+
"normalized_score": safe_float(row["normalized_score"]),
414381
# Fixed-length 4-element array, index i corresponds to
415382
# tier level i+1 (see per-scenario handler for context)
416383
"data": [
@@ -419,16 +386,16 @@ async def get_batch_scenario_tiers(
419386
{"value": safe_float(row["tier_3_value"]), "normalized": norm_3},
420387
{"value": safe_float(row["tier_4_value"]), "normalized": norm_4},
421388
],
422-
"total": safe_float(row["total_value"]),
389+
"total": safe_int(row["total_count"]),
423390
}
424391
else:
425392
level = safe_int(row["single_tier_level"])
426393
if level is None:
427394
weighted = None
428395
normalized = None
429396
else:
430-
weighted = level
431-
normalized = round((5.0 - weighted) / 4.0, 3)
397+
weighted = safe_float(row["weighted_score"])
398+
normalized = safe_float(row["normalized_score"])
432399
result[scenario_id][tier_code] = {
433400
"name": row["name"],
434401
"type": "single_value",

0 commit comments

Comments
 (0)