Exemple #1
0
def _wrap_result(res, **extras) -> dict:
    now = DateUtil.now()
    return {
        "result":
        _dictify_obs_result(res),
        "server":
        dict(datetime=now,
             epoch=now.timestamp(),
             localtime=DateUtil.now_local_string(),
             date=(now.year, now.month, now.day),
             offset=int(now.utcoffset().total_seconds() // 3600)),
        **extras
    }
    def update_avg_ext(d: date = None) -> None:
        if d is None:
            d = DateUtil.now().date()
        logger.info(f"Avg/extr update date: {d}")
        try:
            day_summary = DaySummary(d).stats()
        except ValueError:
            logger.warn("No records for day (yet)")
            return

        records = []
        for obsVar, summary in day_summary.items():
            params = dict(d=d,
                          var=obsVar.db_field,
                          period="day",
                          t_mod=datetime.utcnow(),
                          cnt=summary.count)

            for typ, val, at in [
                ("avg", summary.avg, None),
                ("total", summary.total, None),
                ("min", summary.min_val, summary.min_at),
                ("max", summary.max_val, summary.max_at),
            ]:
                all_params = dict(type=typ, val=val, at=at)
                all_params.update(params)
                # TODO: don't override already overidden entries (check first)
                records.append(
                    insert(m.avg_extreme).values(
                        **all_params, ).on_duplicate_key_update(**all_params))
        db.execute_batch(records)
Exemple #3
0
def dashboard_summary():
    """
    Amalgamates all useful data for a live dashboard to reduce number of http calls
    """
    return _wrap_result(
        dict(
            yesterday=DaySummary(DateUtil.yesterday().date()).stats_json(),
            month=MonthSummary.for_this_month().stats(),
            year=YearSummary.for_this_year().stats(),
            astronomy=get_all_times_rwc(),
        ))
Exemple #4
0
def dashboard_live():
    """
    Amalgamates all useful data for a live dashboard to reduce number of http calls
    """
    return _wrap_result(
        dict(
            now=ObsQ.latest(1)[0],
            trends=ObsQ.trend([10, 60, 1440]),
            today=DaySummary(DateUtil.now().date()).stats_json(),
            last_rain=ObsQ.last_rain(),
        ))
Exemple #5
0
def _var_avg_exts_from_request(var: str, typ: str) -> List[AvgExt]:
    if "year" in request.args:
        year = int(request.args.get("year", DateUtil.now().year))
        avg_ext = AvgExtQ.for_var_and_year(var, typ, year)
    else:
        if "start" in request.args:
            start = DateStampConverter.from_str(request.args["start"])
        else:
            n = int(request.args.get("n", "31"))
            start = DateUtil.now() - timedelta(days=n)

        if "end" in request.args:
            end = DateStampConverter.from_str(request.args["end"])
        elif "include_today" in request.args or DateUtil.now().hour >= 21:
            end = None
        else:
            end = DateUtil.yesterday()

        avg_ext = AvgExtQ.for_var_between_dates(var, typ, start, end)

    return avg_ext
def get_all_times_rwc(dt: datetime = None) -> Dict[str, datetime]:
    lat, lng = 37.470, -122.265
    dt_original = dt or datetime.now()
    dt = dt or DateUtil.now()
    dt_midnight = datetime(dt.year, dt.month, dt.day,
                           tzinfo=TZ).astimezone(UTC)

    # moon_times_utc_today = getMoonTimes(dt, lat, lng)
    # moon_times_utc_yest = getMoonTimes(dt - timedelta(days=1), lat, lng)

    return {
        **getTimes(dt_midnight.replace(hour=12), lat, lng),
        **getMoonTimes(dt_midnight, lat, lng),
        **getMoonPosition(dt_original, lat, lng),
        **getMoonIllumination(dt_original)
    }
Exemple #7
0
 def latest(mins: int, optimized=False) -> List[Obs]:
     """
     Most recent :mins: records, most recent first
     Caps the oldest data
     """
     if mins >= 60:
         st = DateUtil.utc_now() - timedelta(minutes=mins * 1.05)  # Get slightly more data
         q = db.s.query(Obs).filter(Obs.t > st).order_by(Obs.t.desc())
         if optimized and mins > 720:
             # Get every nth record for efficiency
             nth = min(max(round(mins / 2000), 2), 15)
             nth_filter = text(f"(unix_timestamp(t) / 60) % {nth} = 0")
             q = q.filter(nth_filter)
         q = q.all()
     else:
         q = db.s.query(Obs).order_by(Obs.t.desc())[:mins]
     return q
Exemple #8
0
 def __init__(self, year: Optional[int]) -> None:
     if year is None:
         year = DateUtil.now().year
     self.year: int = year
Exemple #9
0
 def __init__(self, month: Optional[date]) -> None:
     if month is None:
         month = DateUtil.now()
     self.month: date = month
Exemple #10
0
 def for_yesterday(cls) -> DaySummary:
     return DaySummary(DateUtil.now().date())
Exemple #11
0
 def __init__(self, day: Optional[date]) -> None:
     if day is None:
         day = DateUtil.now()
     self.day: date = day
Exemple #12
0
 def all_for_year(year: int) -> List[AvgExt]:
     start_date, end_date = DateUtil.year_start_end_dates(year)
     q = db.s.query(AvgExt).filter(AvgExt.d >= start_date).filter(AvgExt.d <= end_date)
     return q.all()
Exemple #13
0
 def all_for_month(month: date) -> List[AvgExt]:
     start_date = month.replace(day=1)
     end_date = DateUtil.last_date_of_month(month)
     q = db.s.query(AvgExt).filter(AvgExt.d >= start_date).filter(AvgExt.d <= end_date)
     return q.all()
Exemple #14
0
 def for_var_and_year(var: str, typ: str, year: int) -> List[AvgExt]:
     start_date, end_date = DateUtil.year_start_end_dates(year)
     q = (db.s.query(AvgExt).filter(AvgExt.var == var).filter(AvgExt.type == typ)
          .filter(AvgExt.d >= start_date).filter(AvgExt.d <= end_date))
     return q.order_by(AvgExt.d.desc(), AvgExt.type.asc()).all()
def _test():
    print(get_all_times_rwc())
    d = DateUtil.now()