Skip to content

Commit

Permalink
feature: Add file download functionality and reports page
Browse files Browse the repository at this point in the history
  • Loading branch information
abugraokkali committed Dec 28, 2023
1 parent 2391c70 commit 46cb1ab
Show file tree
Hide file tree
Showing 16 changed files with 417 additions and 27 deletions.
32 changes: 32 additions & 0 deletions app/Controllers/FileController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace App\Controllers;

class FileController
{
public function downloadFile()
{
$type = request('type');
$endpoint = request('endpoint');
$data = (array) json_decode(request('data'));

$random = str_random(16);
$fileName = "/tmp/" . $random . "." . $data['format'];
$file = fopen($fileName, "w+");

getResponse(function ($client) use ($type, $endpoint, $data, $file) {
return $client->request(strtoupper($type), getUrl($endpoint), [
'sink' => $file,
]);
});

if (!file_exists($fileName)) {
abort("File not found", 404);
} else {
header("Content-Type: " . mime_content_type($fileName));
header("Content-Disposition: attachment; filename=" . $random . "." . $data['format']);

readfile($fileName);
}
}
}
8 changes: 8 additions & 0 deletions db.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@
},
"url": "#/profiles",
"key": "profiles"
},
{
"name": {
"tr": "Raporlar",
"en": "Reports"
},
"url": "#/reports",
"key": "reports"
}
]
}
51 changes: 43 additions & 8 deletions frontend/src/localization/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@
"warning": "Warning",
"yes": "Yes",
"no": "No",
"are_you_sure": "Are you sure you want to proceed with the operation?"
"are_you_sure": "Are you sure you want to proceed with the operation?",
"status": {
"pending": "Pending",
"in_progress": "In Progress",
"done": "Done",
"error": "Error"
}
},
"table": {
"search": {
Expand Down Expand Up @@ -95,12 +101,6 @@
"message": "Message",
"updated_at": "Last Scan"
},
"status": {
"pending": "Pending",
"in_progress": "In Progress",
"done": "Done",
"error": "Error"
},
"fetch": {
"messages": {
"error": "An error occurred while fetching discoveries."
Expand Down Expand Up @@ -136,7 +136,7 @@
},
"package": {
"title": "Packages",
"description": "You can view your packages and their machine counts here.",
"description": "You can view your packages and their machine counts here. You can also create PDF/CSV reports.",
"table": {
"name": "Name",
"count": "Machine Count"
Expand All @@ -146,5 +146,40 @@
"error": "An error occurred while fetching packages."
}
}
},
"report": {
"title": "Reports",
"description": "You can view your previous reports and download them here.",
"table": {
"report_type": "Report Type",
"file_type": "File Type",
"status": "Status",
"message": "Message",
"created_at": "Created At"
},
"get": {
"messages": {
"error": "An error occurred while fetching reports."
}
},
"create": {
"title": "Create Report",
"messages": {
"success": "Report request sent successfully. You can view your report in the reports page.",
"error": "An error occurred while sending report request."
}
},
"download": {
"title": "Download Report",
"messages": {
"success": "Report downloaded successfully.",
"error": "An error occurred while downloading report."
}
},
"type": {
"package": "Packages",
"asset": "Assets",
"asset_packages": "Asset's Packages"
}
}
}
45 changes: 43 additions & 2 deletions frontend/src/localization/tr.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@
"warning": "Uyarı",
"yes": "Evet",
"no": "Hayır",
"are_you_sure": "İşlemi gerçekleştirmek istediğinizden emin misiniz?"
"are_you_sure": "İşlemi gerçekleştirmek istediğinizden emin misiniz?",
"status": {
"pending": "Beklemede",
"in_progress": "Devam Ediyor",
"done": "Tamamlandı",
"error": "Hata"
}
},
"table": {
"search": {
Expand Down Expand Up @@ -130,7 +136,7 @@
},
"package": {
"title": "Paketler",
"description": "Paketlerinizi ve yüklü oldukları makine sayılarını görüntüleyebilirsiniz.",
"description": "Paketlerinizi ve yüklü oldukları makine sayılarını görüntüleyebilirsiniz. Ayrıca PDF/CSV raporlar oluşturabilirsiniz.",
"table": {
"name": "Adı",
"count": "Makine Sayısı"
Expand All @@ -139,6 +145,41 @@
"messages": {
"error": "Paketler getirilirken bir hata oluştu."
}
},
"report": {
"title": "Raporlar",
"description": "Burada önceki raporlarınızı görüntüleyebilir ve indirebilirsiniz.",
"table": {
"report_type": "Rapor Türü",
"file_type": "Dosya Türü",
"status": "Durum",
"message": "Mesaj",
"created_at": "Oluşturulma Tarihi"
},
"get": {
"messages": {
"error": "Raporlar getirilirken bir hata oluştu."
}
},
"create": {
"title": "Rapor Oluştur",
"messages": {
"success": "Rapor isteği başarıyla gönderildi. Raporunuzu raporlar sayfasında görüntüleyebilirsiniz.",
"error": "Rapor isteği gönderilirken bir hata oluştu."
}
},
"download": {
"title": "Raporu İndir",
"messages": {
"success": "Rapor başarıyla indirildi.",
"error": "Rapor indirilirken bir hata oluştu."
}
},
"type": {
"package": "Paketler",
"asset": "Varlıklar",
"asset_packages": "Varlığın Paketleri"
}
}
}
}
12 changes: 12 additions & 0 deletions frontend/src/models/Report.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export interface IReport {
id: string
created_at: string
updated_at: string
deleted_at: string
status: string
job_type: string
file_type: string
report_type: string
message: string
path: string
}
5 changes: 5 additions & 0 deletions frontend/src/router/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ const router = createRouter({
name: "profiles",
component: () => import("@/views/pages/Profiles.vue"),
},
{
path: "/reports",
name: "reports",
component: () => import("@/views/pages/Reports.vue"),
},
{
path: "/error",
name: "error",
Expand Down
17 changes: 17 additions & 0 deletions frontend/src/stores/package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,22 @@ export const usePackageStore = defineStore({
}
})
},
async report(file_type: string) {
return http.get(`packages/report/${file_type}`).then((res) => {
if (res.status == 200) {
window.$notification.success({
title: i18n.t("common.success"),
content: i18n.t("report.create.messages.success"),
duration: 3000,
})
} else {
window.$notification.error({
title: i18n.t("common.error"),
content: i18n.t("report.create.messages.error"),
duration: 5000,
})
}
})
},
},
})
39 changes: 39 additions & 0 deletions frontend/src/stores/report.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import type { IFilter } from "@/models/Filter"
import type { IReport } from "@/models/Report"
import type { IPaginator } from "@/models/Paginator"
import http from "@/utils/http-common"
import { i18n } from "@/utils/i18n"
import { defineStore } from "pinia"

export const useReportStore = defineStore({
id: "report",
state: () => ({
filter: {} as IFilter,
reports: {} as IPaginator<IReport>,
}),
getters: {
get: (state) => state.reports,
},
actions: {
async fetch(payload: IFilter = {} as IFilter) {
let q = payload
if (Object.keys(payload).length < 1) {
q = this.filter
} else {
this.filter = q
}
const query = new URLSearchParams(q as Record<string, string>).toString()
return http.get(`jobs/?${query}`).then((res) => {
if (res.status == 200) {
this.reports = res.data
} else {
window.$notification.error({
title: i18n.t("common.error"),
content: i18n.t("report.get.messages.error"),
duration: 5000,
})
}
})
},
},
})
4 changes: 4 additions & 0 deletions frontend/src/utils/data/file_types.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[
{ "label": "CSV", "value": "csv" },
{ "label": "PDF", "value": "pdf" }
]
2 changes: 1 addition & 1 deletion frontend/src/utils/http-common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export class HttpClient {
{
...data,
lmntargetFunction: url,
type: "post",
type: "get",
endpoint,
},
{
Expand Down
3 changes: 1 addition & 2 deletions frontend/src/views/pages/Assets.vue
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
<script setup lang="ts">
import { reactive } from "vue"
import { h, reactive } from "vue"
import { useI18n } from "vue-i18n"
import AsyncStore from "@/components/Table/AsyncStore.vue"
import type { IColumn } from "@/models/Column"
import { useAssetStore } from "@/stores/asset"
import Header from "@/components/UIElements/Header.vue"
import type { IAsset } from "@/models/Asset"
import { h } from "vue"
const { t } = useI18n()
const store = useAssetStore()
Expand Down
16 changes: 8 additions & 8 deletions frontend/src/views/pages/Discoveries.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<script setup lang="ts">
import { h, reactive } from "vue"
import { useI18n } from "vue-i18n"
import { NButton, NTag } from "naive-ui"
import AsyncStore from "@/components/Table/AsyncStore.vue"
import DropdownMenu from "@/components/Table/DropdownMenu.vue"
import DiscoveryModal from "@/views/modals/Discovery.vue"
Expand All @@ -9,7 +10,6 @@ import useEmitter from "@/utils/emitter"
import type { IColumn } from "@/models/Column"
import type { IDiscovery } from "@/models/Discovery"
import Header from "@/components/UIElements/Header.vue"
import { NButton, NTag } from "naive-ui"
const { t } = useI18n()
const store = useDiscoveryStore()
Expand Down Expand Up @@ -43,10 +43,10 @@ const columns: IColumn[] = reactive([
tooltip: true,
},
filterOptions: [
{ label: t(`discovery.status.in_progress`), value: "in_progress" },
{ label: t(`discovery.status.pending`), value: "pending" },
{ label: t(`discovery.status.done`), value: "done" },
{ label: t(`discovery.status.error`), value: "error" },
{ label: t(`common.status.in_progress`), value: "in_progress" },
{ label: t(`common.status.pending`), value: "pending" },
{ label: t(`common.status.done`), value: "done" },
{ label: t(`common.status.error`), value: "error" },
],
filter(value: string, row: IDiscovery) {
return row.discovery_status === value
Expand All @@ -62,7 +62,7 @@ const columns: IColumn[] = reactive([
{
default: () => [
h("i", { class: "fas fa-spinner fa-spin mr-2" }),
t(`discovery.status.${row.discovery_status}`),
t(`common.status.${row.discovery_status}`),
],
},
)
Expand All @@ -73,7 +73,7 @@ const columns: IColumn[] = reactive([
{
default: () => [
h("i", { class: "fas fa-circle-check mr-2" }),
t(`discovery.status.${row.discovery_status}`),
t(`common.status.${row.discovery_status}`),
],
},
)
Expand All @@ -84,7 +84,7 @@ const columns: IColumn[] = reactive([
{
default: () => [
h("i", { class: "fas fa-circle-exclamation mr-2" }),
t(`discovery.status.${row.discovery_status}`),
t(`common.status.${row.discovery_status}`),
],
},
)
Expand Down
Loading

0 comments on commit 46cb1ab

Please sign in to comment.