Fix totals returning 0 every time
This commit is contained in:
parent
e3b7c15f2a
commit
7a0ea052f1
@ -58,35 +58,51 @@ async def retrieve_totals(user: str, start_date: datetime = None, end_date: date
|
|||||||
if end_date is not None:
|
if end_date is not None:
|
||||||
match.setdefault("date", {}).setdefault("$lte", end_date)
|
match.setdefault("date", {}).setdefault("$lte", end_date)
|
||||||
|
|
||||||
pipeline = [
|
by_class_pipeline = [
|
||||||
{"$match": {"user": ObjectId(user)}},
|
{"$match": {"user": ObjectId(user)}},
|
||||||
{"$lookup": {
|
{"$lookup": {
|
||||||
"from": "flight",
|
"from": "flight",
|
||||||
"let": {"aircraft": "$tail_no"},
|
"let": {"aircraft": "$tail_no"},
|
||||||
"pipeline": [{"$match": {"$expr": {"$eq": ["$$aircraft", "$aircraft"]}}}],
|
"pipeline": [
|
||||||
|
{"$match": {
|
||||||
|
"$expr": {
|
||||||
|
"$eq": ["$$aircraft", "$aircraft"]
|
||||||
|
}
|
||||||
|
}}
|
||||||
|
],
|
||||||
"as": "flight_data"
|
"as": "flight_data"
|
||||||
}},
|
}},
|
||||||
{"$unwind": "$flight_data"},
|
{"$unwind": "$flight_data"},
|
||||||
{"$group": {
|
{"$group": {
|
||||||
# "_id": "$aircraft_category",
|
"_id": {
|
||||||
"_id": {"aircraft_category": "$aircraft_category", "aircraft_class": "$aircraft_class"},
|
"aircraft_category": "$aircraft_category",
|
||||||
"time_total": {"$sum": "$flight_data.time_total"},
|
"aircraft_class": "$aircraft_class"
|
||||||
|
},
|
||||||
|
"time_total": {
|
||||||
|
"$sum": "$flight_data.time_total"
|
||||||
|
},
|
||||||
}},
|
}},
|
||||||
{"$group": {
|
{"$group": {
|
||||||
"_id": "$_id.aircraft_category",
|
"_id": "$_id.aircraft_category",
|
||||||
"classes": {"$push": {
|
"classes": {
|
||||||
|
"$push": {
|
||||||
"aircraft_class": "$_id.aircraft_class",
|
"aircraft_class": "$_id.aircraft_class",
|
||||||
"time_total": "$time_total",
|
"time_total": "$time_total",
|
||||||
}},
|
}
|
||||||
|
},
|
||||||
}},
|
}},
|
||||||
{"$project": {
|
{"$project": {
|
||||||
"_id": 0,
|
"_id": 0,
|
||||||
"aircraft_category": "$_id",
|
"aircraft_category": "$_id",
|
||||||
"classes": 1,
|
"classes": 1,
|
||||||
}},
|
}},
|
||||||
{"$facet": {
|
]
|
||||||
"by_class": [{"$match": {}}],
|
|
||||||
"totals": [
|
class_cursor = aircraft_collection.aggregate(by_class_pipeline)
|
||||||
|
by_class_list = await class_cursor.to_list(None)
|
||||||
|
|
||||||
|
totals_pipeline = [
|
||||||
|
{"$match": {"user": ObjectId(user)}},
|
||||||
{"$group": {
|
{"$group": {
|
||||||
"_id": None,
|
"_id": None,
|
||||||
"time_total": {"$sum": "$time_total"},
|
"time_total": {"$sum": "$time_total"},
|
||||||
@ -107,28 +123,24 @@ async def retrieve_totals(user: str, start_date: datetime = None, end_date: date
|
|||||||
}},
|
}},
|
||||||
{"$project": {"_id": 0}},
|
{"$project": {"_id": 0}},
|
||||||
]
|
]
|
||||||
}},
|
|
||||||
{"$project": {
|
|
||||||
"by_class": 1,
|
|
||||||
"totals": {"$arrayElemAt": ["$totals", 0]}
|
|
||||||
}}
|
|
||||||
]
|
|
||||||
|
|
||||||
cursor = aircraft_collection.aggregate(pipeline)
|
totals_cursor = flight_collection.aggregate(totals_pipeline)
|
||||||
|
totals_list = await totals_cursor.to_list(None)
|
||||||
|
|
||||||
result_list = await cursor.to_list(None)
|
if not totals_list and not by_class_list:
|
||||||
|
|
||||||
if not result_list:
|
|
||||||
return {}
|
return {}
|
||||||
|
|
||||||
result = dict(result_list[0])
|
totals_dict = dict(totals_list[0])
|
||||||
|
|
||||||
for entry in result["by_class"]:
|
for entry in by_class_list:
|
||||||
entry["aircraft_category"] = aircraft_category_dict[entry["aircraft_category"]]
|
entry["aircraft_category"] = aircraft_category_dict[entry["aircraft_category"]]
|
||||||
for cls in entry["classes"]:
|
for cls in entry["classes"]:
|
||||||
cls["aircraft_class"] = aircraft_class_dict[cls["aircraft_class"]]
|
cls["aircraft_class"] = aircraft_class_dict[cls["aircraft_class"]]
|
||||||
|
|
||||||
print(result)
|
result = {
|
||||||
|
"by_class": by_class_list,
|
||||||
|
"totals": totals_dict
|
||||||
|
}
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user