From 8f51d5d837a71383287feadae251a4394d42361a Mon Sep 17 00:00:00 2001 From: SebClem Date: Thu, 16 Feb 2023 16:12:45 +0100 Subject: [PATCH] :hammer: Add home assistant backup list --- .../backend/src/routes/homeAssistant.ts | 16 +- .../src/services/homeAssistantService.ts | 5 +- .../src/types/services/ha_os_response.ts | 6 +- nextcloud_backup/frontend/src/App.vue | 6 +- .../components/cloud/CloudDeleteDialog.vue | 14 +- .../src/components/homeAssistant/HaList.vue | 61 ++++++ .../components/homeAssistant/HaListItem.vue | 197 ++++++++++++++++++ .../homeAssistant/HaListItemContent.vue | 16 ++ .../src/services/homeAssistantService.ts | 15 +- .../frontend/src/types/homeAssistant.ts | 34 +++ 10 files changed, 350 insertions(+), 20 deletions(-) create mode 100644 nextcloud_backup/frontend/src/components/homeAssistant/HaList.vue create mode 100644 nextcloud_backup/frontend/src/components/homeAssistant/HaListItem.vue create mode 100644 nextcloud_backup/frontend/src/components/homeAssistant/HaListItemContent.vue diff --git a/nextcloud_backup/backend/src/routes/homeAssistant.ts b/nextcloud_backup/backend/src/routes/homeAssistant.ts index 1abb9ff..10c4713 100644 --- a/nextcloud_backup/backend/src/routes/homeAssistant.ts +++ b/nextcloud_backup/backend/src/routes/homeAssistant.ts @@ -7,7 +7,19 @@ homeAssistantRouter.get("/backups/", (req, res, next) => { haOsService .getBackups() .then((value) => { - res.json(value.body.data.backups); + res.json(value.body.data.backups.sort((a, b)=> Date.parse(b.date) - Date.parse(a.date))); + }) + .catch((reason) => { + res.status(500); + res.json(reason); + }); +}); + +homeAssistantRouter.get("/backup/:slug", (req, res, next) => { + haOsService + .getBackupInfo(req.params.slug) + .then((value) => { + res.json(value.body.data); }) .catch((reason) => { res.status(500); @@ -29,6 +41,6 @@ homeAssistantRouter.get("/addons", (req, res, next) => { homeAssistantRouter.get("/folders", (req, res, next) => { res.json(haOsService.getFolderList()); -}) +}); export default homeAssistantRouter; diff --git a/nextcloud_backup/backend/src/services/homeAssistantService.ts b/nextcloud_backup/backend/src/services/homeAssistantService.ts index 43d6ca7..10a053e 100644 --- a/nextcloud_backup/backend/src/services/homeAssistantService.ts +++ b/nextcloud_backup/backend/src/services/homeAssistantService.ts @@ -185,7 +185,7 @@ function delSnap(id: string) { ); } -function checkSnap(id: string) { +function getBackupInfo(id: string) { const option: OptionsOfJSONResponseBody = { headers: { authorization: `Bearer ${token}` }, responseType: "json", @@ -269,7 +269,7 @@ function clean(backups: BackupModel[]) { return; } backups.sort((a, b) => { - return a.date < b.date ? 1 : -1; + return Date.parse(b.date) - Date.parse(a.date); }); const toDel = backups.slice(limit); for (const i of toDel) { @@ -511,4 +511,5 @@ export { startAddons, clean, publish_state, + getBackupInfo }; diff --git a/nextcloud_backup/backend/src/types/services/ha_os_response.ts b/nextcloud_backup/backend/src/types/services/ha_os_response.ts index b14a12b..e546d10 100644 --- a/nextcloud_backup/backend/src/types/services/ha_os_response.ts +++ b/nextcloud_backup/backend/src/types/services/ha_os_response.ts @@ -51,7 +51,7 @@ export interface BackupModel { slug: string; date: string; name: string; - type: string; + type: "full" | "partial"; protected: boolean; content: BackupContent; compressed: boolean; @@ -65,7 +65,7 @@ export interface BackupContent { export interface BackupDetailModel { slug: string; - type: string; + type: "full" | "partial"; name: string; date: string; size: string; @@ -76,7 +76,7 @@ export interface BackupDetailModel { name: string; version: string; size: number; - }; + }[]; repositories: string[]; folders: string[]; } diff --git a/nextcloud_backup/frontend/src/App.vue b/nextcloud_backup/frontend/src/App.vue index 3350235..e8450f0 100644 --- a/nextcloud_backup/frontend/src/App.vue +++ b/nextcloud_backup/frontend/src/App.vue @@ -7,7 +7,10 @@ - + + + + @@ -18,6 +21,7 @@ diff --git a/nextcloud_backup/frontend/src/components/homeAssistant/HaListItem.vue b/nextcloud_backup/frontend/src/components/homeAssistant/HaListItem.vue new file mode 100644 index 0000000..c4646a8 --- /dev/null +++ b/nextcloud_backup/frontend/src/components/homeAssistant/HaListItem.vue @@ -0,0 +1,197 @@ + + + diff --git a/nextcloud_backup/frontend/src/components/homeAssistant/HaListItemContent.vue b/nextcloud_backup/frontend/src/components/homeAssistant/HaListItemContent.vue new file mode 100644 index 0000000..141e9cd --- /dev/null +++ b/nextcloud_backup/frontend/src/components/homeAssistant/HaListItemContent.vue @@ -0,0 +1,16 @@ + + + diff --git a/nextcloud_backup/frontend/src/services/homeAssistantService.ts b/nextcloud_backup/frontend/src/services/homeAssistantService.ts index 7369e17..f78ce91 100644 --- a/nextcloud_backup/frontend/src/services/homeAssistantService.ts +++ b/nextcloud_backup/frontend/src/services/homeAssistantService.ts @@ -1,4 +1,9 @@ -import type { AddonData, Folder } from "@/types/homeAssistant"; +import type { + BackupDetailModel, + AddonData, + BackupModel, + Folder, +} from "@/types/homeAssistant"; import kyClient from "./kyClient"; export function getFolders() { @@ -8,3 +13,11 @@ export function getFolders() { export function getAddons() { return kyClient.get("homeAssistant/addons").json(); } + +export function getBackups() { + return kyClient.get("homeAssistant/backups").json(); +} + +export function getBackupDetail(slug: string) { + return kyClient.get(`homeAssistant/backup/${slug}`).json(); +} diff --git a/nextcloud_backup/frontend/src/types/homeAssistant.ts b/nextcloud_backup/frontend/src/types/homeAssistant.ts index 6800a4c..8623310 100644 --- a/nextcloud_backup/frontend/src/types/homeAssistant.ts +++ b/nextcloud_backup/frontend/src/types/homeAssistant.ts @@ -22,3 +22,37 @@ export interface AddonModel { logo: boolean; state: string; } + +export interface BackupModel { + slug: string; + date: string; + name: string; + type: "full" | "partial"; + protected: boolean; + content: BackupContent; + compressed: boolean; +} + +export interface BackupContent { + homeassistant: boolean; + addons: string[]; + folders: string[]; +} + +export interface BackupDetailModel { + slug: string; + type: "full" | "partial"; + name: string; + date: string; + size: string; + protected: boolean; + homeassistant: string; + addons: { + slug: string; + name: string; + version: string; + size: number; + }[]; + repositories: string[]; + folders: string[]; +}