From 06060f394f9f5a19272be63f58f2ef11443f11ed Mon Sep 17 00:00:00 2001 From: Johan Ohly Date: Thu, 26 Sep 2024 11:06:10 +0200 Subject: [PATCH] feat: export flights (#46) * feat: export flights * fix: export to csv and json * fix: import from AirTrail export * docs: add guide to importing from AirTrail export --- docs/docs/features/export.md | 93 ++++++++++++++ docs/docs/features/import.md | 26 +++- .../modals/settings/SettingsModal.svelte | 4 + .../modals/settings/pages/ExportPage.svelte | 64 ++++++++++ .../modals/settings/pages/ImportPage.svelte | 6 +- .../modals/settings/pages/PageHeader.svelte | 19 ++- .../components/modals/settings/pages/index.ts | 1 + src/lib/import/airtrail.ts | 116 ++++++++++++++++++ src/lib/import/index.ts | 5 +- src/lib/server/routes/flight.ts | 40 +++++- src/lib/utils/csv.ts | 28 +++++ 11 files changed, 387 insertions(+), 15 deletions(-) create mode 100644 docs/docs/features/export.md create mode 100644 src/lib/components/modals/settings/pages/ExportPage.svelte create mode 100644 src/lib/import/airtrail.ts diff --git a/docs/docs/features/export.md b/docs/docs/features/export.md new file mode 100644 index 0000000..a330848 --- /dev/null +++ b/docs/docs/features/export.md @@ -0,0 +1,93 @@ +--- +sidebar_position: 2 +--- + +# Export + +The export feature allows you to export your flight data from AirTrail. + +## Export flights + +To export your flights, follow these steps: + +1. Go to the AirTrail application. +2. Go to the settings page. +3. Click on the "Export" tab. +4. Choose your desired export format (CSV or JSON). + +## Export formats + +### CSV + +The CSV export option allows you to export your flights as a CSV file, which can be opened in any spreadsheet +application like Microsoft Excel or Google Sheets. It is a simple and easy-to-use format that can be used to +analyze your flight data. + +#### Format + +The CSV file contains the following columns: + +- `date`: The date of the flight (YYYY-MM-DD format). +- `from`: The IATA code of the departure airport. +- `to`: The IATA code of the arrival airport. +- `departure`: The departure time in ISO 8601 format (if available). +- `arrival`: The arrival time in ISO 8601 format (if available). +- `duration`: The duration of the flight in seconds. +- `flightNumber`: The flight number (if available). +- `flightReason`: The reason for the flight (if provided). +- `airline`: The airline operating the flight (if available). +- `aircraft`: The type of aircraft used (if available). +- `aircraftReg`: The registration number of the aircraft (if available). +- `note`: Any additional notes about the flight. +- `seat`: The type of seat (e.g., window, aisle, etc.). +- `seatNumber`: The seat number (if available). +- `seatClass`: The class of the seat (e.g., economy, business). + +### JSON + +:::tip +The JSON format can be reimported into AirTrail using the import feature. +::: + +The JSON export option provides a more structured format that is ideal for developers or when integrating the data into +other systems. It contains nested objects for each flight and detailed data for each user and their seat information. + +#### Format + +The JSON file follows this structure: + +```json +{ + "users": [ + { + "id": "user_id", + "displayName": "User Name", + "username": "username" + } + ], + "flights": [ + { + "date": "YYYY-MM-DD", + "from": "ICAO_CODE", + "to": "ICAO_CODE", + "departure": "ISO_8601_DATETIME", + "arrival": "ISO_8601_DATETIME", + "duration": flight_duration_in_seconds, + "flightNumber": "FLIGHT_NUMBER", + "flightReason": "FLIGHT_REASON", + "airline": "ICAO_AIRLINE_CODE", + "aircraft": "ICAO_AIRCRAFT_TYPE", + "aircraftReg": "AIRCRAFT_REGISTRATION", + "note": "FLIGHT_NOTE", + "seats": [ + { + "userId": "USER_ID", + "guestName": "GUEST_NAME", + "seat": "SEAT_TYPE", + "seatNumber": "SEAT_NUMBER", + "seatClass": "SEAT_CLASS" + } + ] + } + ] +} \ No newline at end of file diff --git a/docs/docs/features/import.md b/docs/docs/features/import.md index 2323e75..22c177a 100644 --- a/docs/docs/features/import.md +++ b/docs/docs/features/import.md @@ -6,7 +6,8 @@ sidebar_position: 1 The import feature allows you to import flight data from other sources into AirTrail. Currently, AirTrail supports importing flights from [MyFlightradar24](https://my.flightradar24.com) -, [App in the Air](https://appintheair.com) and [JetLog](https://github.com/pbogre/jetlog). +, [App in the Air](https://appintheair.com), [JetLog](https://github.com/pbogre/jetlog) +and [AirTrail JSON files](/docs/features/export). ## Import flights from MyFlightradar24 @@ -22,7 +23,7 @@ Once you have the CSV file, you can import it into AirTrail by following these s 1. Go to the AirTrail application. 2. Go to the settings page. 3. Click on the "Import" tab. -4. Click on the "Choose File" button and select the CSV file you downloaded from MyFlightradar24. +4. Click on the "Select file" button and select the CSV file you downloaded from MyFlightradar24. 5. Click on the "Import" button to start the import process. After the import process is complete, you will see your flights on the map. @@ -41,7 +42,7 @@ Once you have the text file, you can import it into AirTrail by following these 1. Go to the AirTrail application. 2. Go to the settings page. 3. Click on the "Import" tab. -4. Click on the "Choose File" button and select the text file you received from App in the Air. +4. Click on the "Select file" button and select the text file you received from App in the Air. 5. Click on the "Import" button to start the import process. After the import process is complete, you will see your flights on the map. @@ -63,7 +64,24 @@ Once you have the CSV file, you can import it into AirTrail by following these s 1. Go to the AirTrail application. 2. Go to the settings page. 3. Click on the "Import" tab. -4. Click on the "Choose File" button and select the CSV file you downloaded from JetLog. +4. Click on the "Select file" button and select the CSV file you downloaded from JetLog. +5. Click on the "Import" button to start the import process. + +After the import process is complete, you will see your flights on the map. + +## Import flights from AirTrail JSON files + +:::tip +Make sure the file you are importing is called `airtrail.json`. If it is not, rename it to `airtrail.json` before +importing. +::: + +Once you have the JSON file, you can import it into AirTrail by following these steps: + +1. Go to the AirTrail application. +2. Go to the settings page. +3. Click on the "Import" tab. +4. Click on the "Select file" button and select the JSON file you want to import. 5. Click on the "Import" button to start the import process. After the import process is complete, you will see your flights on the map. diff --git a/src/lib/components/modals/settings/SettingsModal.svelte b/src/lib/components/modals/settings/SettingsModal.svelte index e4461f8..b4f3f69 100644 --- a/src/lib/components/modals/settings/SettingsModal.svelte +++ b/src/lib/components/modals/settings/SettingsModal.svelte @@ -10,6 +10,7 @@ GeneralPage, AppearancePage, UsersPage, + ExportPage, OAuthPage, SecurityPage, } from './pages'; @@ -22,6 +23,7 @@ { title: 'Security', id: 'security' }, { title: 'Appearance', id: 'appearance' }, { title: 'Import', id: 'import' }, + { title: 'Export', id: 'export' }, ] as const; const ADMIN_SETTINGS = [ { title: 'Users', id: 'users' }, @@ -131,6 +133,8 @@ {:else if activeTab === 'import'} + {:else if activeTab === 'export'} + {:else if activeTab === 'users'} {:else if activeTab === 'oauth'} diff --git a/src/lib/components/modals/settings/pages/ExportPage.svelte b/src/lib/components/modals/settings/pages/ExportPage.svelte new file mode 100644 index 0000000..b46513a --- /dev/null +++ b/src/lib/components/modals/settings/pages/ExportPage.svelte @@ -0,0 +1,64 @@ + + + + {#snippet subtitleHtml()} +

+ Export your data. Learn more about the data formats in the documentation. +

+ {/snippet} +
+ + +
+
diff --git a/src/lib/components/modals/settings/pages/ImportPage.svelte b/src/lib/components/modals/settings/pages/ImportPage.svelte index e004f8b..732367d 100644 --- a/src/lib/components/modals/settings/pages/ImportPage.svelte +++ b/src/lib/components/modals/settings/pages/ImportPage.svelte @@ -21,7 +21,7 @@ const file = files?.[0]; if (!file) return; - if (!file.name.endsWith('.csv') && !file.name.endsWith('.txt')) { + if (!file.name.endsWith('.csv') && !file.name.endsWith('.txt') && !file.name.endsWith('.json')) { fileError = 'File type not supported'; } else if (file.size > 5 * 1024 * 1024) { fileError = 'File must be less than 5MB'; @@ -53,7 +53,7 @@