| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- from datetime import datetime, timedelta
- from pickletools import read_unicodestring1
- from pprint import pprint
- from config.settings import DECIMAL_PLACES
- from cruds.measures import get_finca_measures
- from database.mongo import mongo_measures
- from routes.calculations.pipelines import (
- count_measures_pipeline,
- daily_thermal_amplitude_pipeline,
- deg_daily_acc_avg_pipeline,
- get_percent,
- ltgt_pipeline,
- precip_acc_pipeline,
- )
- from schemas.fincas import FincaBase as FincaBaseSchema
- from schemas.tables import Summary as SummarySchema
- from schemas.tables import SummarySeason as SummarySeasonSchema
- def ensure_non_empty(result, key):
- """
- Ensures that the result list is not empty and that we are not rounding None
- """
- if len(result) > 0:
- value = result[0].get(key)
- if value is not None:
- return round(value, DECIMAL_PLACES)
- def general_summary(
- finca: FincaBaseSchema,
- start_datetime: datetime,
- end_datetime: datetime,
- ) -> SummarySchema:
- finca_data = mongo_measures[finca.station_code]
- lt10 = None
- gt30 = None
- gt33 = None
- deg_acc = None
- thermal_amplitude = None
- precip_accumulated = None
- # Compute <10C, >30C and >33C columns
- ltgt_agg = finca_data.aggregate(ltgt_pipeline(start_datetime, end_datetime))
- ltgt_agg_result = list(ltgt_agg)
- if len(ltgt_agg_result) > 0:
- lt10 = get_percent(
- ltgt_agg_result[0]["lt10_count"], ltgt_agg_result[0]["count"]
- )
- gt30 = get_percent(
- ltgt_agg_result[0]["gt30_count"], ltgt_agg_result[0]["count"]
- )
- gt33 = get_percent(
- ltgt_agg_result[0]["gt33_count"], ltgt_agg_result[0]["count"]
- )
- # Compute degrees accumulated column
- deg_acc_agg = finca_data.aggregate(
- deg_daily_acc_avg_pipeline(start_datetime, end_datetime)
- )
- deg_acc_agg_result = list(deg_acc_agg)
- deg_acc = ensure_non_empty(deg_acc_agg_result, "sum")
- deg_acc_avg = ensure_non_empty(deg_acc_agg_result, "avg")
- # Compute thermal amplitude column
- thermal_amplitude_agg = finca_data.aggregate(
- daily_thermal_amplitude_pipeline(start_datetime, end_datetime)
- )
- thermal_amplitude = ensure_non_empty(list(thermal_amplitude_agg), "avg")
- # Compute accumulated precipitations column
- precip_accumulated_agg = finca_data.aggregate(
- precip_acc_pipeline(start_datetime, end_datetime)
- )
- precip_accumulated = ensure_non_empty(list(precip_accumulated_agg), "sum")
- data_count = finca_data.count(count_measures_pipeline(start_datetime, end_datetime))
- total_count = (end_datetime - start_datetime).total_seconds() // (60 * 10)
- data_percentage = get_percent(data_count, total_count)
- return SummarySchema(
- station=FincaBaseSchema(
- station_code=finca.station_code,
- title=finca.title,
- ),
- initial_date=start_datetime,
- final_date=end_datetime,
- lt10=lt10,
- gt30=gt30,
- gt33=gt33,
- grados_acumulados=deg_acc,
- grados_acumulados_promedio=deg_acc_avg,
- amplitud_termica=thermal_amplitude,
- precip_acumulada=precip_accumulated,
- data_percentage=data_percentage,
- )
|