Skip to content

Upload API Flight data

APIUploadFlightData

Bases: APIUploadInternal

Interface to upload flight data to server

Source code in src/rmlab/api/upload/flight_data.py
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
class APIUploadFlightData(APIUploadInternal):
    """Interface to upload flight data to server"""

    async def upload_flights(
        self,
        scen_id: int,
        items: Union[str, list],
        *,
        citysector_id: Optional[str] = None,
        sector_id: Optional[str] = None,
    ) -> None:
        """Upload a set of flights defined in a file.

        Args:
            scen_id (int): Scenario ID
            items (Union[str, list]): A filename (.csv or .json) or list of filenames (.json) defining the flights
            citysector_id (Optional[str], optional): A citysector ID if all flights belong to the same citysector. Defaults to None.
            sector_id (Optional[str], optional): A sector ID if all flights belong to the same sector. Defaults to None.

        Arguments `citysector_id` and `sector_id` are always optional. If any of them provided, concurrent uploads of flights
            files belonging to different citysectors/sectors are allowed.

        Where an item can refer to:
        * a **CSV file**, for instance:
        ```csv
        Airline,Aircraft,Origin,Destination,Flight number,On sale date,Departure date,Departure time,Duration
        MyCarrier,Airbus A320-b,MAD,GVA,2277,2022-04-01,2022-05-15,T16:30,T02:00
        MyCarrier,Airbus A320-b,MAD,GVA,2277,2022-04-01,2022-05-17,T20:30,T02:00
        ```

        * a **JSON file**, for instance:
        ```json
        [
            {"airline": "MyCarrier", "aircraft": "Airbus A320-b", "origin": "MAD", "destination": "GVA", "flight_number": "2277", "on_sale_date": "2022-04-01", "departure_date": "2022-05-15", "departure_time": "T16:30", "duration": "T02:00", },
            {"airline": "MyCarrier", "aircraft": "Airbus A320-b", "origin": "MAD", "destination": "GVA", "flight_number": "2277", "on_sale_date": "2022-04-01", "departure_date": "2022-05-17", "departure_time": "T20:30", "duration": "T02:00", },
        ]

        ```
        * a list of json filenames like the previous.
        """

        await self._upload_unbounded_items(
            scen_id, Flight, items, citysector_id=citysector_id, sector_id=sector_id
        )

    async def upload_flights_data(
        self,
        scen_id: int,
        flights_data: List[
            Union[
                FlightDataBooks,
                FlightDataThresholdSettings,
                FlightDataPricePerSeatSettings,
            ]
        ],
        *,
        citysector_id: Optional[str] = None,
        sector_id: Optional[str] = None,
    ) -> None:
        """Upload flight data of multiple flights belonging to a citysector or sector to server.

        Any previous existing data is overwritten.
        At least `citysector_id` and/or `sector_id` associated to the flights must be defined.

        Args:
            scen_id (int): Scenario ID
            flights_data (List[Union[FlightDataBooks, FlightDataThresholdSettings, FlightDataPricePerSeatSettings]]): List of flights data to upload.
            citysector_id (Optional[str], optional): Target citysector ID. Defaults to None.
            sector_id (Optional[str], optional): Target sector ID. Defaults to None.

        Raises:
            ValueError: If none of `citysector_id`, `sector_id` is defined
            ValueError: If one of flight data list elements has incorrect type.
            MultipleError: If several of flight data list elements have incorrect types.
        """

        if citysector_id is None and sector_id is None:
            raise ValueError(
                f"Require definition of either `citysector_id` or `sector_id`"
            )

        # TODO endpoint_limit = self._api_endpoints_limits.data_flight_post

        # if len(flights_data) > endpoint_limit:

        #     # Sequentially upload data in chunks to keep the request payloads and server load controlled

        #     for chunk_start in range(0, len(flights_data), endpoint_limit):

        #         chunk_end = chunk_start + endpoint_limit
        #         if chunk_end > len(flights_data):
        #             chunk_end = -1

        #         await run_async_tasks(
        #             [
        #                 self._upload_flight_data(
        #                     scen_id,
        #                     flight_data,
        #                     citysector_id=citysector_id,
        #                     sector_id=sector_id,
        #                 )
        #                 for flight_data in flights_data[chunk_start:chunk_end]
        #             ],
        #             return_results=False,
        #         )

        # else:

        await run_async_tasks(
            [
                self._upload_flight_data(
                    scen_id,
                    flight_data,
                    citysector_id=citysector_id,
                    sector_id=sector_id,
                )
                for flight_data in flights_data
            ],
            return_results=False,
        )

upload_flights(scen_id, items, *, citysector_id=None, sector_id=None) async

Upload a set of flights defined in a file.

Parameters:

NameTypeDescriptionDefault
scen_idint

Scenario ID

required
itemsUnion[str, list]

A filename (.csv or .json) or list of filenames (.json) defining the flights

required
citysector_idOptional[str]

A citysector ID if all flights belong to the same citysector. Defaults to None.

None
sector_idOptional[str]

A sector ID if all flights belong to the same sector. Defaults to None.

None

Arguments citysector_id and sector_id are always optional. If any of them provided, concurrent uploads of flights files belonging to different citysectors/sectors are allowed.

Where an item can refer to: * a CSV file, for instance:

Airline,Aircraft,Origin,Destination,Flight number,On sale date,Departure date,Departure time,Duration
MyCarrier,Airbus A320-b,MAD,GVA,2277,2022-04-01,2022-05-15,T16:30,T02:00
MyCarrier,Airbus A320-b,MAD,GVA,2277,2022-04-01,2022-05-17,T20:30,T02:00

  • a JSON file, for instance:
    [
        {"airline": "MyCarrier", "aircraft": "Airbus A320-b", "origin": "MAD", "destination": "GVA", "flight_number": "2277", "on_sale_date": "2022-04-01", "departure_date": "2022-05-15", "departure_time": "T16:30", "duration": "T02:00", },
        {"airline": "MyCarrier", "aircraft": "Airbus A320-b", "origin": "MAD", "destination": "GVA", "flight_number": "2277", "on_sale_date": "2022-04-01", "departure_date": "2022-05-17", "departure_time": "T20:30", "duration": "T02:00", },
    ]
    
  • a list of json filenames like the previous.
Source code in src/rmlab/api/upload/flight_data.py
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
async def upload_flights(
    self,
    scen_id: int,
    items: Union[str, list],
    *,
    citysector_id: Optional[str] = None,
    sector_id: Optional[str] = None,
) -> None:
    """Upload a set of flights defined in a file.

    Args:
        scen_id (int): Scenario ID
        items (Union[str, list]): A filename (.csv or .json) or list of filenames (.json) defining the flights
        citysector_id (Optional[str], optional): A citysector ID if all flights belong to the same citysector. Defaults to None.
        sector_id (Optional[str], optional): A sector ID if all flights belong to the same sector. Defaults to None.

    Arguments `citysector_id` and `sector_id` are always optional. If any of them provided, concurrent uploads of flights
        files belonging to different citysectors/sectors are allowed.

    Where an item can refer to:
    * a **CSV file**, for instance:
    ```csv
    Airline,Aircraft,Origin,Destination,Flight number,On sale date,Departure date,Departure time,Duration
    MyCarrier,Airbus A320-b,MAD,GVA,2277,2022-04-01,2022-05-15,T16:30,T02:00
    MyCarrier,Airbus A320-b,MAD,GVA,2277,2022-04-01,2022-05-17,T20:30,T02:00
    ```

    * a **JSON file**, for instance:
    ```json
    [
        {"airline": "MyCarrier", "aircraft": "Airbus A320-b", "origin": "MAD", "destination": "GVA", "flight_number": "2277", "on_sale_date": "2022-04-01", "departure_date": "2022-05-15", "departure_time": "T16:30", "duration": "T02:00", },
        {"airline": "MyCarrier", "aircraft": "Airbus A320-b", "origin": "MAD", "destination": "GVA", "flight_number": "2277", "on_sale_date": "2022-04-01", "departure_date": "2022-05-17", "departure_time": "T20:30", "duration": "T02:00", },
    ]

    ```
    * a list of json filenames like the previous.
    """

    await self._upload_unbounded_items(
        scen_id, Flight, items, citysector_id=citysector_id, sector_id=sector_id
    )

upload_flights_data(scen_id, flights_data, *, citysector_id=None, sector_id=None) async

Upload flight data of multiple flights belonging to a citysector or sector to server.

Any previous existing data is overwritten. At least citysector_id and/or sector_id associated to the flights must be defined.

Parameters:

NameTypeDescriptionDefault
scen_idint

Scenario ID

required
flights_dataList[Union[FlightDataBooks, FlightDataThresholdSettings, FlightDataPricePerSeatSettings]]

List of flights data to upload.

required
citysector_idOptional[str]

Target citysector ID. Defaults to None.

None
sector_idOptional[str]

Target sector ID. Defaults to None.

None

Raises:

TypeDescription
ValueError

If none of citysector_id, sector_id is defined

ValueError

If one of flight data list elements has incorrect type.

MultipleError

If several of flight data list elements have incorrect types.

Source code in src/rmlab/api/upload/flight_data.py
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
async def upload_flights_data(
    self,
    scen_id: int,
    flights_data: List[
        Union[
            FlightDataBooks,
            FlightDataThresholdSettings,
            FlightDataPricePerSeatSettings,
        ]
    ],
    *,
    citysector_id: Optional[str] = None,
    sector_id: Optional[str] = None,
) -> None:
    """Upload flight data of multiple flights belonging to a citysector or sector to server.

    Any previous existing data is overwritten.
    At least `citysector_id` and/or `sector_id` associated to the flights must be defined.

    Args:
        scen_id (int): Scenario ID
        flights_data (List[Union[FlightDataBooks, FlightDataThresholdSettings, FlightDataPricePerSeatSettings]]): List of flights data to upload.
        citysector_id (Optional[str], optional): Target citysector ID. Defaults to None.
        sector_id (Optional[str], optional): Target sector ID. Defaults to None.

    Raises:
        ValueError: If none of `citysector_id`, `sector_id` is defined
        ValueError: If one of flight data list elements has incorrect type.
        MultipleError: If several of flight data list elements have incorrect types.
    """

    if citysector_id is None and sector_id is None:
        raise ValueError(
            f"Require definition of either `citysector_id` or `sector_id`"
        )

    # TODO endpoint_limit = self._api_endpoints_limits.data_flight_post

    # if len(flights_data) > endpoint_limit:

    #     # Sequentially upload data in chunks to keep the request payloads and server load controlled

    #     for chunk_start in range(0, len(flights_data), endpoint_limit):

    #         chunk_end = chunk_start + endpoint_limit
    #         if chunk_end > len(flights_data):
    #             chunk_end = -1

    #         await run_async_tasks(
    #             [
    #                 self._upload_flight_data(
    #                     scen_id,
    #                     flight_data,
    #                     citysector_id=citysector_id,
    #                     sector_id=sector_id,
    #                 )
    #                 for flight_data in flights_data[chunk_start:chunk_end]
    #             ],
    #             return_results=False,
    #         )

    # else:

    await run_async_tasks(
        [
            self._upload_flight_data(
                scen_id,
                flight_data,
                citysector_id=citysector_id,
                sector_id=sector_id,
            )
            for flight_data in flights_data
        ],
        return_results=False,
    )