예제 #1
0
 def get(self, department_id: Optional[UUID] = None) -> Dict:
     with session_scope() as sess:
         query = sess.query(Department).options(
             subqueryload(Department.massifs))
         if department_id:
             query.filter(Department.d_id == department_id)
         return query.all()
예제 #2
0
 def get(self, record_id: UUID) -> Dict:
     """
     Return a specific BRA with it's ID.
     """
     with session_scope() as sess:
         global_rec = sess.query(
             BraRecord,
             func.lag(BraRecord.br_id).over(
                 order_by=BraRecord.br_production_date,
                 partition_by=BraRecord.br_massif,
             ).label("previous_bra_id"),
             func.lead(BraRecord.br_id).over(
                 order_by=BraRecord.br_production_date,
                 partition_by=BraRecord.br_massif,
             ).label("next_bra_id"),
         ).subquery()
         result_filtered = (sess.query(global_rec).filter(
             global_rec.c.br_id == record_id).first())
         record_as_dict = result_filtered._asdict()
         record_as_dict["massif"] = (sess.query(Massif).filter(
             Massif.m_id == result_filtered.br_massif).first())
         record_as_dict["risks"] = sess.query(Risk).filter(
             Risk.r_record_id == result_filtered.br_id)
         record_as_dict["snow_records"] = sess.query(SnowRecord).filter(
             SnowRecord.s_bra_record == result_filtered.br_id)
         record_as_dict["fresh_snow_records"] = sess.query(
             FreshSnowRecord).filter(
                 FreshSnowRecord.fsr_bra_record == result_filtered.br_id)
         record_as_dict["weather_forecasts"] = sess.query(
             WeatherForecast).filter(
                 WeatherForecast.wf_bra_record == result_filtered.br_id)
         record_as_dict["risk_forecasts"] = sess.query(RiskForecast).filter(
             RiskForecast.rf_bra_record == result_filtered.br_id)
         return marshal(record_as_dict, bra_model)
예제 #3
0
 def get(self) -> List[Dict]:
     """
     Return the last record for all massifs.
     """
     with session_scope() as sess:
         results = (sess.query(
             BraRecord,
             func.lag(BraRecord.br_id).over(
                 order_by=BraRecord.br_production_date,
                 partition_by=BraRecord.br_massif,
             ).label("previous_bra_id"),
             func.lead(BraRecord.br_id).over(
                 order_by=BraRecord.br_production_date,
                 partition_by=BraRecord.br_massif,
             ).label("next_bra_id"),
         ).filter(
             BraRecord.br_production_date.cast(Date) == select([
                 func.max(BraRecord.br_production_date.cast(Date))
             ])).options(subqueryload(BraRecord.risks)).options(
                 subqueryload(BraRecord.snow_records)).options(
                     subqueryload(BraRecord.fresh_snow_records)).options(
                         subqueryload(BraRecord.weather_forecasts)).options(
                             subqueryload(
                                 BraRecord.risk_forecasts)).options(
                                     subqueryload(BraRecord.massif)))
         final = []
         for res in results.all():
             encoded_bra_record = marshal(res.BraRecord, bra_model)
             encoded_bra_record["previous_bra_id"] = (str(
                 res.previous_bra_id) if res.previous_bra_id else None)
             encoded_bra_record["next_bra_id"] = (str(res.next_bra_id) if
                                                  res.next_bra_id else None)
             final.append(encoded_bra_record)
         return final
예제 #4
0
 def get(self, zone_id: Optional[UUID] = None) -> Union[List[Dict], Dict]:
     with session_scope() as sess:
         query = sess.query(Zone).options(subqueryload(Zone.departments))
         if zone_id:
             query.filter(Zone.z_id == zone_id)
         res = query.all()
         if len(res) == 1:
             return res[0]
         return res
예제 #5
0
 def get(self, station_id: UUID):
     with session_scope() as sess:
         return (
             sess.query(NivoRecord)
             .join(SensorStation)
             .filter(SensorStation.nss_id == station_id)
             .order_by(NivoRecord.nr_date.desc())
             .limit(1)
             .first()
         )
예제 #6
0
 def _get_bra_by_date_or_404(self, massif: str, date: str) -> _Element:
     with session_scope() as sess:
         try:
             parsed_date = datetime.strptime(date, "%Y-%m-%d")
             query = (sess.query(BraRecord.br_raw_xml).join(Massif).filter(
                 Massif.m_name == massif.upper()).filter(
                     cast(BraRecord.br_production_date, Date) ==
                     parsed_date))
             return query.one().br_raw_xml
         except NoResultFound:
             bra_api.abort(HTTPStatus.NOT_FOUND,
                           "BRA for this date or name cannot be found.")
         except ValueError:
             # strptime fail
             bra_api.abort(HTTPStatus.NOT_FOUND,
                           "BRA for this date or name cannot be found.")
예제 #7
0
 def get(self, massif_id: UUID) -> Dict:
     """
     Return a record for a massifs. With all the associated metadata.
     """
     with session_scope() as sess:
         s = (
             sess.query(BraRecord)
             .filter(BraRecord.br_massif == massif_id)
             .order_by(BraRecord.br_production_date.desc())
             .limit(1)
             .options(subqueryload(BraRecord.risks))
             .options(subqueryload(BraRecord.snow_records))
             .options(subqueryload(BraRecord.fresh_snow_records))
             .options(subqueryload(BraRecord.weather_forcasts))
             .options(subqueryload(BraRecord.risk_forcasts))
             .options(subqueryload(BraRecord.massif))
         )
         res = s.first()
         return marshal(res, bra_model)
예제 #8
0
 def get(self) -> Dict:
     """
     Return the last record for all massifs.
     """
     with session_scope() as sess:
         res = (
             sess.query(BraRecord)
             .join(Massif)
             .filter(
                 BraRecord.br_production_date.cast(Date)
                 == select([func.max(BraRecord.br_production_date.cast(Date))])
             )
             .options(subqueryload(BraRecord.risks))
             .options(subqueryload(BraRecord.snow_records))
             .options(subqueryload(BraRecord.fresh_snow_records))
             .options(subqueryload(BraRecord.weather_forcasts))
             .options(subqueryload(BraRecord.risk_forcasts))
             .all()
         )
         return marshal(res, bra_model)
예제 #9
0
 def get(self, massif_id: UUID) -> List[Dict]:
     """
     Return a list of records for a massif. Limit to last 50 records (last 50 bra)
     """
     args = massif_record_parser.parse_args()
     with session_scope() as sess:
         res = (
             sess.query(BraRecord)
             .filter(BraRecord.br_massif == massif_id)
             .order_by(BraRecord.br_production_date.desc())
             .limit(args["limit"])
             .options(subqueryload(BraRecord.risks))
             .options(subqueryload(BraRecord.snow_records))
             .options(subqueryload(BraRecord.fresh_snow_records))
             .options(subqueryload(BraRecord.weather_forcasts))
             .options(subqueryload(BraRecord.risk_forcasts))
             .options(subqueryload(BraRecord.massif))
             .all()
         )
         return marshal(res, bra_model)
예제 #10
0
    def get(self, station_id: UUID):
        args = record_limit_parser.parse_args()
        with session_scope() as sess:
            limit = args.get("limit", None)
            limit_by_day = args.get("day_limit", None)

            req = (sess.query(NivoRecord).filter(
                NivoRecord.nr_nivo_sensor == station_id).order_by(
                    NivoRecord.nr_date.desc()))
            if limit_by_day:
                last_date = sess.query(
                    func.max(NivoRecord.nr_date).label('max')).filter(
                        NivoRecord.nr_nivo_sensor == station_id).subquery(
                            'last_date')
                req = req.filter(
                    func.age(last_date.c.max, NivoRecord.nr_date) < timedelta(
                        days=limit_by_day))
            if limit:
                req = req.limit(limit)
            return req.all()
예제 #11
0
 def get(self, massif_id: UUID) -> Dict:
     """
     Return a record for a massifs. With all the associated metadata.
     """
     with session_scope() as sess:
         request = (sess.query(
             BraRecord,
             func.lag(BraRecord.br_id).over(
                 order_by=BraRecord.br_production_date,
                 partition_by=BraRecord.br_massif,
             ).label("previous_bra_id"),
             func.lead(BraRecord.br_id).over(
                 order_by=BraRecord.br_production_date,
                 partition_by=BraRecord.br_massif,
             ).label("next_bra_id")).filter(
                 BraRecord.br_massif == massif_id).order_by(
                     BraRecord.br_production_date.desc()).limit(1))
         result = request.first()
         json = marshal(result.BraRecord, bra_model)
         json["previous_bra_id"] = result.previous_bra_id
         json["next_bra_id"] = result.next_bra_id
         return json
예제 #12
0
 def get(self, massif_id, bra_date) -> Dict:
     parsed_date = date.fromisoformat(bra_date)
     with session_scope() as sess:
         global_rec = sess.query(
             BraRecord,
             func.lag(BraRecord.br_id).over(
                 order_by=BraRecord.br_production_date,
                 partition_by=BraRecord.br_massif,
             ).label("previous_bra_id"),
             func.lead(BraRecord.br_id).over(
                 order_by=BraRecord.br_production_date,
                 partition_by=BraRecord.br_massif,
             ).label("next_bra_id"),
         ).subquery()
         result_filtered = (sess.query(global_rec).filter(
             global_rec.c.br_production_date.cast(Date) == parsed_date
         ).filter(global_rec.c.br_massif == massif_id).first())
         if result_filtered is None:
             return bra_api.abort(
                 404,
                 f"Record for massif id {massif_id} for the date {bra_date} could not be found."
             )
         record_as_dict = result_filtered._asdict()
         record_as_dict["massif"] = (sess.query(Massif).filter(
             Massif.m_id == result_filtered.br_massif).first())
         record_as_dict["risks"] = sess.query(Risk).filter(
             Risk.r_record_id == result_filtered.br_id)
         record_as_dict["snow_records"] = sess.query(SnowRecord).filter(
             SnowRecord.s_bra_record == result_filtered.br_id)
         record_as_dict["fresh_snow_records"] = sess.query(
             FreshSnowRecord).filter(
                 FreshSnowRecord.fsr_bra_record == result_filtered.br_id)
         record_as_dict["weather_forecasts"] = sess.query(
             WeatherForecast).filter(
                 WeatherForecast.wf_bra_record == result_filtered.br_id)
         record_as_dict["risk_forecasts"] = sess.query(RiskForecast).filter(
             RiskForecast.rf_bra_record == result_filtered.br_id)
         return marshal(record_as_dict, bra_model)
예제 #13
0
 def get(self, massif_id: UUID) -> List[Dict]:
     """
     Return a list of records for a massif. Limit to last 50 days by default.
     """
     args = massif_record_parser.parse_args()
     with session_scope() as sess:
         results = (sess.query(
             BraRecord,
             func.lag(BraRecord.br_id).over(
                 order_by=BraRecord.br_production_date,
                 partition_by=BraRecord.br_massif,
             ).label("previous_bra_id"),
             func.lead(BraRecord.br_id).over(
                 order_by=BraRecord.br_production_date,
                 partition_by=BraRecord.br_massif,
             ).label("next_bra_id"),
         ).filter(BraRecord.br_production_date > (
             func.now() -
             func.cast(concat(args["limit"], "DAYS"), INTERVAL))).filter(
                 BraRecord.br_massif == massif_id).order_by(
                     BraRecord.br_production_date.desc()).options(
                         subqueryload(BraRecord.risks)).options(
                             subqueryload(BraRecord.snow_records)).options(
                                 subqueryload(
                                     BraRecord.fresh_snow_records)).
                    options(subqueryload(
                        BraRecord.weather_forecasts)).options(
                            subqueryload(BraRecord.risk_forecasts)).options(
                                subqueryload(BraRecord.massif)).all())
         final = []
         for res in results:
             encoded_bra_record = marshal(res.BraRecord, bra_model)
             encoded_bra_record["previous_bra_id"] = (str(
                 res.previous_bra_id) if res.previous_bra_id else None)
             encoded_bra_record["next_bra_id"] = (str(res.next_bra_id) if
                                                  res.next_bra_id else None)
             final.append(encoded_bra_record)
         return final