Esempio n. 1
0
def get_visa_status_detail(
        visa_type: VisaType = Query(...),
        embassy_code: List[EmbassyCode] = Query(...),
        timestamp: Optional[datetime] = datetime.now(timezone.utc),
):
    """ Return all of the historical data fetched for a given `(visa_type, embassy_code)` pair.
        If a given `since` or `or` date query is given, the historical data will be truncated to
        the specified dates.
    """
    if not isinstance(embassy_code, list):
        embassy_code = [embassy_code]
    embassy_code = list(set(embassy_code))
    time_range = [
        dt_to_utc((timestamp - timedelta(days=1)), remove_second=True),
        dt_to_utc(timestamp, remove_second=True),
    ]
    detail = []
    for e in embassy_code:
        single_result = DB.VisaStatus.find_visa_status_past24h_turning_point(
            visa_type, e, timestamp)
        if single_result:
            single_result = single_result['available_dates']
        else:
            single_result = [{
                'write_time': time_range[0],
                'available_date': None
            }]
        detail.append({'embassy_code': e, 'available_dates': single_result})
    return {
        'visa_type': visa_type,
        'embassy_code': embassy_code,
        'time_range': time_range,
        'detail': detail,
    }
async def get_newest_visa_status(websocket: WebSocket):
    """ Get the latest fetched visa status with the given query"""
    try:
        while True:
            visa_type, embassy_code = await websocket.receive_json()
            if not isinstance(visa_type, list):
                visa_type = [visa_type]
            if not isinstance(embassy_code, list):
                embassy_code = [embassy_code]

            if not (
                all([vt in G.VISA_TYPES for vt in visa_type]) and
                all([ec in EMBASSY_CODES for ec in embassy_code])
            ):
                await websocket.send_json(
                    {'error': f'In valid visa_type ({visa_type}) or embsssy_code ({embassy_code}) is given'}
                )
                continue

            latest_written = DB.VisaStatus.find_latest_written_visa_status(visa_type, embassy_code)
            ws_data = {
                'type': 'newest',
                'data': [{**lw, 'write_time': dt_to_utc(lw['write_time'])} for lw in latest_written]
            }
            await websocket.send_json(jsonable_encoder(ws_data))
    except WebSocketDisconnect:
        pass
def get_visa_status_detail(
    visa_type: VisaType = Query(...),
    embassy_code: List[EmbassyCode] = Query(...),
    timestamp: Optional[datetime] = datetime.now(timezone.utc),
):
    """ Return all of the historical data fetched for a given `(visa_type, embassy_code)` pair.
        If a given `since` or `or` date query is given, the historical data will be truncated to
        the specified dates.
    """
    if not isinstance(embassy_code, list):
        embassy_code = [embassy_code]
    embassy_code = list(set(embassy_code))
    now = datetime.now(timezone.utc)
    #if timestamp > now:
    #    timestamp = now
    time_range = [
        dt_to_utc((timestamp - timedelta(days=1)), remove_second=True),
        dt_to_utc(timestamp, remove_second=True),
    ]

    keys = visa_type + "".join(list(sorted(embassy_code)))
    cache_timestamp, cache_result = G.DETAIL_CACHE.get(keys, (None, None))
    if cache_result is None or abs(timestamp - cache_timestamp) > timedelta(minutes=1):
        detail = []
        for e in embassy_code:
            single_result = DB.VisaStatus.find_visa_status_past24h_turning_point(visa_type, e, timestamp)
            if single_result:
                single_result = single_result['available_dates']
            else:
                single_result = [{'write_time': time_range[0], 'available_date': None}]
            detail.append({'embassy_code': e, 'available_dates': single_result})
        G.DETAIL_CACHE[keys] = (timestamp, detail)
    else:
        detail = cache_result
    return {
        'visa_type': visa_type,
        'embassy_code': embassy_code,
        'time_range': time_range,
        'detail': detail,
    }
 def convert(dt: datetime):
     return dt_to_utc(dt, remove_second=True)