示例#1
0
def insert_states(self) -> None:  # type: ignore
    self.time_limit = 10
    logger.debug("Starting task insert_states")
    cur_time = int(time.time())

    api = Opensky()
    try:
        logger.debug(
            f"A request has been sent to API for the timestamp: {cur_time}")
        states: Optional[List[StateVector]] = api.get_current_states(
            time_sec=cur_time)
        logger.debug(
            f"A response has been received from API for the timestamp: {cur_time}"
        )
        if states:
            logger.debug(f"received {len(states)} states")
            with closing(DB(**common_conf.db_params)) as db:
                with db:
                    db.insert_current_states(states)

    except Exception as e:
        logger.exception(f"Exception: {e}")

    old_outs = sys.stdout, sys.stderr
    rlevel = app.conf.worker_redirect_stdouts_level

    try:
        app.log.redirect_stdouts_to_logger(logger, rlevel)
    finally:
        sys.stdout, sys.stderr = old_outs
示例#2
0
def fetch_current_flight(icao: str, params: Dict[str, Any]) -> Optional[Dict]:
    with closing(DB(**params)) as db:
        with db:
            flight: Optional[Dict] = db.find_unfinished_path_for_aircraft(icao)
            if flight:
                return flight
            else:
                return None
示例#3
0
def fetch_paths(icao: str, params: Dict[str, Any]) -> Optional[List[Dict]]:
    with closing(DB(**params)) as db:
        with db:
            flights: Optional[List[Dict]] = db.get_all_paths_for_icao(icao)
            if flights:
                return flights
            else:
                return None
示例#4
0
def update_stats(self) -> None:  # type: ignore
    self.time_limit = 10
    try:
        with closing(DB(**common_conf.db_params)) as db:
            with db:
                logger.debug("Starting airports statistics update")
                update_airport_stats()
    except Exception as e:
        logger.exception(f"Exception: {e}")
示例#5
0
def fill_airports(file: str) -> None:
    with open(file) as f:
        values = [line.split(",") for line in f.readlines()]
        airports = []
        for value in values:
            airports.append(Airport(*value)._asdict())

    with closing(DB(**common_conf.db_params)) as db:
        with db:
            db.insert_airports(airports)
示例#6
0
def update_flight_paths() -> None:
    start = time.time()
    with closing(DB(**params)) as db:
        with db:
            db.delete_outdated_paths()
            db.update_paths_when_finished()
        with db:
            states: Optional[List[Dict]] = db.get_current_states()

            if states is not None:
                logger.info(f"received {len(states)} states for paths update")
                for state in states:
                    icao = state["icao24"]
                    callsign = state["callsign"]
                    update_time = state["request_time"]

                    unfinished_path: Optional[
                        Dict] = db.find_unfinished_path_for_aircraft(icao)

                    current_location = {
                        "longitude": state["longitude"],
                        "latitude": state["latitude"],
                    }
                    path: str = json.dumps(current_location)

                    if callsign is not None:
                        airports = db.get_airports_for_callsign(callsign)
                        if airports is not None:
                            arrival_airport_icao, departure_airport_icao = airports
                        else:
                            arrival_airport_icao, departure_airport_icao = None, None  # type: ignore

                    if unfinished_path:
                        last_update = unfinished_path["last_update"]
                        db.update_unfinished_path(icao, last_update, path,
                                                  update_time,
                                                  arrival_airport_icao,
                                                  departure_airport_icao)
                    else:
                        flight_path = json.dumps([current_location])
                        new_path = FlightPath(
                            last_update=update_time,
                            icao24=icao,
                            callsign=callsign,
                            departure_airport_icao=departure_airport_icao,
                            arrival_airport_icao=arrival_airport_icao,
                            path=flight_path,
                            finished=False,
                            finished_at=0,
                        )

                        db.insert_path(new_path)
    finish = time.time()
    delta = finish - start
    logger.info(f"{delta} sec to update paths")
示例#7
0
def make_tables() -> None:
    path = os.path.join(os.path.dirname(__file__), "make_tables.yaml")

    with closing(DB(**common_conf.db_params)) as db:
        with db:

            db.set_timezone()

            with open(path, "r") as f:
                scripts = yaml.load(f, Loader=yaml.FullLoader)
                for key in scripts.keys():
                    db.execute_script(scripts[key])
示例#8
0
def update_callsigns(self) -> None:  # type: ignore
    logger.debug("Starting task update_callsigns")
    self.time_limit = 15
    api = Opensky()
    flights: Optional[List[OpenskyFlight]] = api.get_flights_for_period(
        int(time.time()) - 259200)

    if flights:
        logger.debug(f"Received {len(flights)} flights")
        with closing(DB(**common_conf.db_params)) as db:
            with db:
                db.upsert_callsigns(flights)
示例#9
0
def update_airport_stats() -> None:
    with closing(DB(**params)) as db:
        with db:
            db.delete_outdated_stats()

            last_update: int = db.get_stats_last_update()

            not_considered_paths = db.get_not_considered_paths(last_update)
            logger.debug(
                f"Quantity of not considered in stats calculation paths: {len(not_considered_paths)}"
            )

            max_update = 0

            for path in not_considered_paths:
                path = FlightPath(*path[1:])
                update = path.last_update
                update_day = datetime.utcfromtimestamp(update).strftime(
                    "%Y-%m-%d")

                arrival_airport = path.arrival_airport_icao
                if arrival_airport:
                    arr_stats = db.get_stats_for_airport(
                        arrival_airport, update_day)
                    if arr_stats:
                        db.update_stats_for_arrival_airport(
                            arrival_airport, update_day)
                    else:
                        db.insert_arrival_airport_stats(
                            arrival_airport, update_day)

                departure_airport = path.departure_airport_icao
                if departure_airport:
                    dep_stats = db.get_stats_for_airport(
                        departure_airport, update_day)
                    if dep_stats:
                        db.update_stats_for_departure_airport(
                            departure_airport, update_day)
                    else:
                        db.insert_departure_airport_stats(
                            departure_airport, update_day)

                if update > max_update:
                    max_update = update

            if max_update != 0:
                db.update_airport_stats_last_update(max_update)
示例#10
0
def fill_callsigns() -> None:
    """Fill callsigns for the period [yesterday - 2 weeks, yesterday].
    There is a delay of when finished flights appear in /flights/all"""
    api = Opensky()
    with closing(DB(**common_conf.db_params)) as db:
        end = int(time.time()) - 17280  # 2 days ago
        begin = end - 604800  # 1 week before the end

        while begin < end:
            with db:
                flights: Optional[
                    List[OpenskyFlight]] = api.get_flights_for_period(begin)
                if flights:
                    db.upsert_callsigns(flights)
                    begin += 3600
                    logger.info(f"{len(flights)} flights upserted")
                    time.sleep(30)
示例#11
0
def fetch_aircraft_states(params: Dict[str, Any]) -> None:
    with closing(DB(**params)) as db:
        while True:
            with db:
                vectors: Optional[List[Dict]] = db.get_current_states()
            if vectors is not None:
                for vector in vectors:
                    airports: Optional[Tuple[
                        str, str]] = db.get_airports_for_callsign(
                            vector["callsign"])
                    # logger.info(f"airports for callsign {vector['callsign']}: {airports}")
                    if airports:
                        est_arrival_airport = airports[0]
                        est_departure_airport = airports[1]
                        vector["est_arrival_airport"] = est_arrival_airport
                        vector["est_departure_airport"] = est_departure_airport

                quantity = len(vectors)
                global states_memo
                states_memo = vectors
                logger.info(
                    f"{quantity} states fetched from the DB for the time {vectors[0]['request_time']}"
                )
                time.sleep(4)
示例#12
0
def fetch_airports(params: Dict[str, Any]) -> Optional[List[Dict]]:
    with closing(DB(**params)) as db:
        with db:
            airports: Optional[List[Dict]] = db.get_all_airports()
    return airports