gnral_summary.py 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. from datetime import datetime, timedelta
  2. from pickletools import read_unicodestring1
  3. from pprint import pprint
  4. from config.settings import DECIMAL_PLACES
  5. from cruds.measures import get_finca_measures
  6. from database.mongo import mongo_measures
  7. from routes.calculations.pipelines import (
  8. count_measures_pipeline,
  9. daily_thermal_amplitude_pipeline,
  10. deg_daily_acc_avg_pipeline,
  11. get_percent,
  12. ltgt_pipeline,
  13. precip_acc_pipeline,
  14. )
  15. from schemas.fincas import FincaBase as FincaBaseSchema
  16. from schemas.tables import Summary as SummarySchema
  17. from schemas.tables import SummarySeason as SummarySeasonSchema
  18. def ensure_non_empty(result, key):
  19. """
  20. Ensures that the result list is not empty and that we are not rounding None
  21. """
  22. if len(result) > 0:
  23. value = result[0].get(key)
  24. if value is not None:
  25. return round(value, DECIMAL_PLACES)
  26. def general_summary(
  27. finca: FincaBaseSchema,
  28. start_datetime: datetime,
  29. end_datetime: datetime,
  30. ) -> SummarySchema:
  31. finca_data = mongo_measures[finca.station_code]
  32. lt10 = None
  33. gt30 = None
  34. gt33 = None
  35. deg_acc = None
  36. thermal_amplitude = None
  37. precip_accumulated = None
  38. # Compute <10C, >30C and >33C columns
  39. ltgt_agg = finca_data.aggregate(ltgt_pipeline(start_datetime, end_datetime))
  40. ltgt_agg_result = list(ltgt_agg)
  41. if len(ltgt_agg_result) > 0:
  42. lt10 = get_percent(
  43. ltgt_agg_result[0]["lt10_count"], ltgt_agg_result[0]["count"]
  44. )
  45. gt30 = get_percent(
  46. ltgt_agg_result[0]["gt30_count"], ltgt_agg_result[0]["count"]
  47. )
  48. gt33 = get_percent(
  49. ltgt_agg_result[0]["gt33_count"], ltgt_agg_result[0]["count"]
  50. )
  51. # Compute degrees accumulated column
  52. deg_acc_agg = finca_data.aggregate(
  53. deg_daily_acc_avg_pipeline(start_datetime, end_datetime)
  54. )
  55. deg_acc_agg_result = list(deg_acc_agg)
  56. deg_acc = ensure_non_empty(deg_acc_agg_result, "sum")
  57. deg_acc_avg = ensure_non_empty(deg_acc_agg_result, "avg")
  58. # Compute thermal amplitude column
  59. thermal_amplitude_agg = finca_data.aggregate(
  60. daily_thermal_amplitude_pipeline(start_datetime, end_datetime)
  61. )
  62. thermal_amplitude = ensure_non_empty(list(thermal_amplitude_agg), "avg")
  63. # Compute accumulated precipitations column
  64. precip_accumulated_agg = finca_data.aggregate(
  65. precip_acc_pipeline(start_datetime, end_datetime)
  66. )
  67. precip_accumulated = ensure_non_empty(list(precip_accumulated_agg), "sum")
  68. data_count = finca_data.count(count_measures_pipeline(start_datetime, end_datetime))
  69. total_count = (end_datetime - start_datetime).total_seconds() // (60 * 10)
  70. data_percentage = get_percent(data_count, total_count)
  71. return SummarySchema(
  72. station=FincaBaseSchema(
  73. station_code=finca.station_code,
  74. title=finca.title,
  75. ),
  76. initial_date=start_datetime,
  77. final_date=end_datetime,
  78. lt10=lt10,
  79. gt30=gt30,
  80. gt33=gt33,
  81. grados_acumulados=deg_acc,
  82. grados_acumulados_promedio=deg_acc_avg,
  83. amplitud_termica=thermal_amplitude,
  84. precip_acumulada=precip_accumulated,
  85. data_percentage=data_percentage,
  86. )