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)
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(), ))
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 _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 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) }
def __init__(self, year: Optional[int]) -> None: if year is None: year = DateUtil.now().year self.year: int = year
def __init__(self, month: Optional[date]) -> None: if month is None: month = DateUtil.now() self.month: date = month
def for_yesterday(cls) -> DaySummary: return DaySummary(DateUtil.now().date())
def __init__(self, day: Optional[date]) -> None: if day is None: day = DateUtil.now() self.day: date = day
def _test(): print(get_all_times_rwc()) d = DateUtil.now()