Last active
January 5, 2021 14:44
-
-
Save someshkoli/cb836ff1880c0f6e622cbc89bde8c73c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def hygiene_index_list(outlet_code, interval=None, _to=None, _from=None): | |
td = datetime.date.today() | |
temp_group_id = None | |
timeInterval = None | |
duration_filter = None | |
if interval: | |
if interval == "day": | |
temp_group_id = { | |
"year": {"$year": "$hygiene_index_records.timestamp"}, | |
"month": {"$month": "$hygiene_index_records.timestamp"}, | |
"day": {"$dayOfMonth": "$hygiene_index_records.timestamp"}, | |
"hour": {"$hour": "$hygiene_index_records.timestamp"}, | |
} | |
timeInterval = datetime.datetime( | |
year=td.year, | |
month=td.month, | |
day=td.day, | |
) | |
else: | |
temp_group_id = { | |
"year": {"$year": "$hygiene_index_records.timestamp"}, | |
"month": {"$month": "$hygiene_index_records.timestamp"}, | |
"day": {"$dayOfMonth": "$hygiene_index_records.timestamp"}, | |
} | |
if interval == "week": | |
timeInterval = datetime.datetime.now() - datetime.timedelta(days=7) | |
elif interval == "month": | |
timeInterval = datetime.datetime.now() - datetime.timedelta(days=30) | |
else: | |
timeInterval = None | |
duration_filter = {"$gte": timeInterval} | |
if _to and _from: | |
duration_filter = {"$gte": _from, "$lte": _to} | |
elif _from: | |
duration_filter = {"$gte": _from} | |
else: | |
raise HTTPException( | |
status_code=status.HTTP_400_BAD_REQUEST, detail="Improper filter" | |
) | |
qres = Outlets.aggregate( | |
[ | |
{ | |
"$match": { | |
"code": outlet_code, | |
} | |
}, | |
{"$unwind": "$hygiene_index_records"}, | |
{ | |
"$match": {"hygiene_index_records.timestamp": duration_filter}, | |
}, | |
{ | |
"$group": { | |
"_id": temp_group_id, | |
"hygiene_index": {"$avg": "$hygiene_index_records.hygiene_index"}, | |
} | |
}, | |
] | |
) | |
result = [] | |
for res in qres: | |
formatD = FormatDate() | |
if "hour" not in res["_id"]: | |
res["_id"]["houd"] = 0 | |
data_time_format = formatD.formatHourWise( | |
year=res["_id"]["year"], | |
month=res["_id"]["month"], | |
day=res["_id"]["day"], | |
hour=res["_id"]["hour"], | |
) | |
temp = { | |
"ts": data_time_format, | |
"hygiene_index": res["hygiene_index"], | |
} | |
result.append(temp) | |
return result |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment