pipelines.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. from config.settings import DECIMAL_PLACES
  2. def truncate_100(num):
  3. return 100 if num > 100 else num
  4. def get_percent(count, total):
  5. return truncate_100(round((count * 100) / total, DECIMAL_PLACES))
  6. id_daily = {
  7. "day": {"$dayOfMonth": "$date"},
  8. "month": {"$month": "$date"},
  9. "year": {"$year": "$date"},
  10. }
  11. def ltgt_pipeline(start_datetime, end_datetime):
  12. return [
  13. {
  14. "$match": {
  15. "date": {
  16. "$gte": start_datetime,
  17. "$lte": end_datetime,
  18. },
  19. "temp": {
  20. "$ne": None,
  21. },
  22. },
  23. },
  24. {
  25. "$group": {
  26. "_id": None,
  27. "lt10_count": {
  28. "$sum": {"$cond": [{"$lt": ["$temp", 10]}, 1, 0]},
  29. },
  30. "gt30_count": {
  31. "$sum": {"$cond": [{"$gt": ["$temp", 30]}, 1, 0]},
  32. },
  33. "gt33_count": {
  34. "$sum": {"$cond": [{"$gt": ["$temp", 33]}, 1, 0]},
  35. },
  36. "count": {
  37. "$sum": 1,
  38. },
  39. },
  40. },
  41. ]
  42. def deg_daily_acc_avg_pipeline(start_datetime, end_datetime):
  43. return [
  44. {
  45. "$match": { # Conseguimos las muestras
  46. "date": {
  47. "$gte": start_datetime,
  48. "$lte": end_datetime,
  49. },
  50. "temp": {
  51. "$ne": None,
  52. },
  53. },
  54. },
  55. {
  56. "$group": { # Agrupamos por dia y calculamos el promedio de temperatura(de ese dia)
  57. "_id": id_daily,
  58. "avg": {
  59. "$avg": "$temp",
  60. },
  61. }
  62. },
  63. {
  64. "$addFields": { # Pisamos el campo promedio que calculamos antes y lo usamos para los grados dia de ese dia(medio feo)
  65. "avg": {
  66. "$cond": [
  67. {"$gt": ["$avg", 10]},
  68. {"$add": ["$avg", -10] # Cantidad de grados por encima de la temperatura base
  69. },
  70. 0,
  71. ],
  72. },
  73. },
  74. },
  75. {
  76. "$group": {
  77. "_id": None,
  78. "sum": { # Grados dias acumulados
  79. "$sum": "$avg",
  80. },
  81. "avg": { # grados dias promedio
  82. "$avg": "$avg",
  83. },
  84. },
  85. },
  86. ]
  87. def daily_thermal_amplitude_pipeline(start_datetime, end_datetime):
  88. return [
  89. {
  90. "$match": {
  91. "date": {
  92. "$gte": start_datetime,
  93. "$lte": end_datetime,
  94. },
  95. "temp": {"$ne": None},
  96. },
  97. },
  98. {
  99. "$group": {
  100. "_id": id_daily,
  101. "min": {
  102. "$min": "$temp",
  103. },
  104. "max": {
  105. "$max": "$temp",
  106. },
  107. }
  108. },
  109. {
  110. "$addFields": {
  111. "amplitude": {
  112. "$subtract": ["$max", "$min"],
  113. },
  114. },
  115. },
  116. {
  117. "$group": {
  118. "_id": None,
  119. "avg": {
  120. "$avg": "$amplitude",
  121. },
  122. },
  123. },
  124. ]
  125. def precip_acc_pipeline(start_datetime, end_datetime):
  126. return [
  127. {
  128. "$match": {
  129. "date": {
  130. "$gte": start_datetime,
  131. "$lte": end_datetime,
  132. },
  133. "precip": {"$ne": None},
  134. },
  135. },
  136. {
  137. "$group": {
  138. "_id": None,
  139. "sum": {
  140. "$sum": "$precip",
  141. },
  142. },
  143. },
  144. ]
  145. def count_measures_pipeline(start_datetime, end_datetime) -> dict:
  146. return {
  147. "date": {
  148. "$gte": start_datetime,
  149. "$lte": end_datetime,
  150. },
  151. "$or": [
  152. {
  153. "temp": {
  154. "$ne": None,
  155. },
  156. },
  157. {
  158. "precip": {
  159. "$ne": None,
  160. },
  161. },
  162. ],
  163. }
  164. def count_temp_measures_pipeline(start_datetime, end_datetime) -> dict:
  165. return {
  166. "date": {
  167. "$gte": start_datetime,
  168. "$lte": end_datetime,
  169. },
  170. "temp": {
  171. "$ne": None,
  172. },
  173. }
  174. def count_precip_measures_pipeline(start_datetime, end_datetime) -> dict:
  175. return {
  176. "date": {
  177. "$gte": start_datetime,
  178. "$lte": end_datetime,
  179. },
  180. "precip": {
  181. "$ne": None,
  182. },
  183. }