Compare commits
No commits in common. "a2b59b5bf48c38910a4f1daae7eb049f637cbfc5" and "0e20615412ce22c1e254715c1d564c0344db9ab5" have entirely different histories.
a2b59b5bf4
...
0e20615412
13
app.py
13
app.py
|
@ -4,7 +4,7 @@ import json
|
||||||
|
|
||||||
|
|
||||||
settings = Settings()
|
settings = Settings()
|
||||||
client = MongoDB(str(settings.DB_URI))
|
client = MongoDB(settings.DB_URI)
|
||||||
db = client.client[settings.DATABASE_NAME]
|
db = client.client[settings.DATABASE_NAME]
|
||||||
collection = db[settings.COLLECTION_NAME]
|
collection = db[settings.COLLECTION_NAME]
|
||||||
|
|
||||||
|
@ -17,14 +17,7 @@ json_str = '''
|
||||||
'''
|
'''
|
||||||
|
|
||||||
async def main(json_str):
|
async def main(json_str):
|
||||||
data = json.loads(json_str)
|
result = await aggregate_salaries()
|
||||||
dt_from = data["dt_from"]
|
pass
|
||||||
dt_upto = data["dt_upto"]
|
|
||||||
group_type = data["group_type"]
|
|
||||||
|
|
||||||
result = await aggregate_salaries(collection, dt_from, dt_upto, group_type)
|
|
||||||
|
|
||||||
print(result['dataset'], len(result['dataset']))
|
|
||||||
print(result['labels'], len(result['labels']))
|
|
||||||
|
|
||||||
asyncio.run(main(json_str))
|
asyncio.run(main(json_str))
|
|
@ -1,7 +1,4 @@
|
||||||
from pydantic_settings import BaseSettings, SettingsConfigDict
|
from pydantic_settings import BaseSettings, SettingsConfigDict
|
||||||
from pydantic import computed_field, MongoDsn
|
|
||||||
from pydantic_core import Url
|
|
||||||
|
|
||||||
|
|
||||||
class Settings(BaseSettings):
|
class Settings(BaseSettings):
|
||||||
API_TOKEN_TG: str
|
API_TOKEN_TG: str
|
||||||
|
@ -10,11 +7,6 @@ class Settings(BaseSettings):
|
||||||
COLLECTION_NAME: str
|
COLLECTION_NAME: str
|
||||||
USERNAME_MONGO: str
|
USERNAME_MONGO: str
|
||||||
PASSWORD_MONGO: str
|
PASSWORD_MONGO: str
|
||||||
|
DB_URI: str
|
||||||
@computed_field
|
|
||||||
def DB_URI(self) -> MongoDsn:
|
|
||||||
return Url(
|
|
||||||
f"mongodb+srv://{self.USERNAME_MONGO}:{self.PASSWORD_MONGO}@{self.HOST_MONGODB}"
|
|
||||||
)
|
|
||||||
|
|
||||||
model_config = SettingsConfigDict(env_file='.env', env_file_encoding='utf-8')
|
model_config = SettingsConfigDict(env_file='.env', env_file_encoding='utf-8')
|
|
@ -1,47 +1,8 @@
|
||||||
from typing import List, Tuple
|
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
|
async def aggregate_salaries(dt_from: str, dt_upto: str, group_type: str) -> Tuple[List[int], List[str]]:
|
||||||
group_type_format = "%Y-%m-%dT00:00:00" if group_type == "day" else "%Y-%m-01T00:00:00" if group_type == "month" else "%Y-%m-%dT%H:00:00"
|
|
||||||
pipeline = [
|
pipeline = [
|
||||||
{
|
|
||||||
"$match": {
|
|
||||||
"dt": {
|
|
||||||
"$gt": iso(dt_from),
|
|
||||||
"$lt": iso(dt_upto)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"$group": {
|
|
||||||
"_id": {
|
|
||||||
"$dateToString":
|
|
||||||
{
|
|
||||||
"format": group_type_format,
|
|
||||||
"date": "$dt"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"sum": {
|
|
||||||
"$sum": "$value"
|
|
||||||
},
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"$sort": {
|
|
||||||
"_id": 1
|
|
||||||
}
|
|
||||||
}
|
|
||||||
]
|
]
|
||||||
|
|
||||||
dataset = []
|
return {"dataset": None, "labels": None}
|
||||||
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)
|
|
Loading…
Reference in New Issue