41 lines
1.1 KiB
Python
41 lines
1.1 KiB
Python
from typing import List, Tuple
|
|
from motor.motor_asyncio import AsyncIOMotorCollection
|
|
import datetime as dt
|
|
|
|
async def aggregate_salaries(collection: AsyncIOMotorCollection, dt_from: str, dt_upto: str, group_type: str) -> Tuple[List[int], List[str]]: # type: ignore
|
|
pipeline = [
|
|
{
|
|
"$match": {
|
|
"dt": {
|
|
"$gt": iso(dt_from),
|
|
"$lt": iso(dt_upto)
|
|
}
|
|
}
|
|
},
|
|
{
|
|
"$group": {
|
|
"_id": {
|
|
"$dateToString":
|
|
{
|
|
"format": "%Y-%m-%dT00:00:00",
|
|
"date": "$dt"
|
|
}
|
|
},
|
|
"sum": {
|
|
"$sum": "$value"
|
|
},
|
|
}
|
|
}
|
|
]
|
|
|
|
dataset = []
|
|
labels = []
|
|
|
|
async for document in collection.aggregate(pipeline):
|
|
dataset.append(document["sum"])
|
|
labels.append(document["_id"] )
|
|
|
|
return {"dataset": dataset, "labels": labels}
|
|
|
|
def iso(date: str) -> dt.datetime:
|
|
return dt.datetime.fromisoformat(date) |