-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Various Report Buttons: View, Download + Run
- Loading branch information
Showing
24 changed files
with
565 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
<script setup lang="ts"> | ||
import { type Analysis, type AccessBlobContentReqItem, type AccessBlobContentResp } from '@/openapi/generated/pacta' | ||
import JSZip from 'jszip' | ||
const { t } = useI18n() | ||
const { public: { apiServerURL } } = useRuntimeConfig() | ||
const pactaClient = usePACTA() | ||
const { getMaybeMe } = useSession() | ||
const { isAdmin, isSuperAdmin, maybeMeOwnerId } = await getMaybeMe() | ||
interface Props { | ||
analysis: Analysis | ||
} | ||
const props = defineProps<Props>() | ||
const prefix = 'components/analysis/AccessButtons' | ||
const statePrefix = `${prefix}[${useStateIDGenerator().id()}]` | ||
const tt = (key: string) => t(`${prefix}.${key}`) | ||
const canAccessAsPublic = computed(() => props.analysis.artifacts.every((asset) => asset.sharedToPublic)) | ||
const canAccessAsAdmin = computed(() => { | ||
if (isAdmin.value || isSuperAdmin.value) { | ||
return props.analysis.artifacts.every((asset) => asset.adminDebugEnabled) | ||
} | ||
return false | ||
}) | ||
const canAccessAsOwner = computed(() => { | ||
if (maybeMeOwnerId.value) { | ||
return maybeMeOwnerId.value === props.analysis.ownerId | ||
} | ||
return false | ||
}) | ||
const canAccess = computed(() => { | ||
return canAccessAsPublic.value || canAccessAsAdmin.value || canAccessAsOwner.value | ||
}) | ||
const downloadInProgress = useState<boolean>(`${statePrefix}.downloadInProgress`, () => false) | ||
const doDownload = async () => { | ||
downloadInProgress.value = true | ||
await pactaClient.accessBlobContent({ | ||
items: props.analysis.artifacts.map((asset): AccessBlobContentReqItem => ({ | ||
blobId: asset.blob.id, | ||
})), | ||
}).then(async (response: AccessBlobContentResp) => { | ||
const zip = new JSZip() | ||
await Promise.all(response.items.map( | ||
async (item): Promise<void> => { | ||
const response = await fetch(item.downloadUrl) | ||
const data = await response.blob() | ||
const blob = presentOrFileBug(props.analysis.artifacts.find((artifact) => artifact.blob.id === item.blobId)).blob | ||
const fileName = `${blob.fileName}` | ||
zip.file(fileName, data) | ||
}), | ||
) | ||
return await zip.generateAsync({ type: 'blob' }) | ||
}).then((content) => { | ||
const element = document.createElement('a') | ||
element.href = URL.createObjectURL(content) | ||
const fileName = `${props.analysis.name}.zip` | ||
element.download = fileName | ||
document.body.appendChild(element) | ||
element.click() | ||
document.body.removeChild(element) | ||
downloadInProgress.value = false | ||
}) | ||
} | ||
const openReport = () => navigateTo(`${apiServerURL}/report/${props.analysis.id}/`, { | ||
open: { | ||
target: '_blank', | ||
}, | ||
external: true, | ||
}) | ||
</script> | ||
|
||
<template> | ||
<div | ||
v-tooltip="canAccess ? undefined : tt('Denied')" | ||
class="flex gap-1 align-items-center w-fit" | ||
> | ||
<PVButton | ||
icon="pi pi-external-link" | ||
:disabled="!canAccess" | ||
class="p-button-secondary p-button-outlined p-button-xs" | ||
:label="tt('View')" | ||
@click="openReport" | ||
/> | ||
<PVButton | ||
v-tooltip="canAccess ? tt('Download') : ''" | ||
:disabled="downloadInProgress || !canAccess" | ||
:loading="downloadInProgress" | ||
:icon="downloadInProgress ? 'pi pi-spinner pi-spin' : 'pi pi-download'" | ||
class="p-button-secondary p-button-text p-button-xs" | ||
@click="doDownload" | ||
/> | ||
</div> | ||
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
<script setup lang="ts"> | ||
import { type RunAnalysisReq, type Analysis, type AnalysisType } from '@/openapi/generated/pacta' | ||
const pactaClient = usePACTA() | ||
const { loading: { withLoading } } = useModal() | ||
const localePath = useLocalePath() | ||
const i18n = useI18n() | ||
const { t } = i18n | ||
const prefix = 'components/analysis/RunButton' | ||
const statePrefix = `${prefix}[${useStateIDGenerator().id()}]` | ||
const tt = (key: string) => t(`${prefix}.${key}`) | ||
interface Props { | ||
analysisType: AnalysisType | ||
name: string | ||
portfolioGroupId?: string | ||
portfolioId?: string | ||
initiativeId?: string | ||
} | ||
const props = defineProps<Props>() | ||
interface Emits { | ||
(e: 'started'): void | ||
(e: 'finished'): void | ||
} | ||
const emit = defineEmits<Emits>() | ||
const clicked = useState<boolean>(`${statePrefix}.clicked`, () => false) | ||
const analysisId = useState<string | null>(`${statePrefix}.analysisId`, () => null) | ||
const analysis = useState<Analysis | null>(`${statePrefix}.analysis`, () => null) | ||
const request = computed<RunAnalysisReq>(() => { | ||
const common = { | ||
analysisType: props.analysisType, | ||
name: `${tt(props.analysisType)}: ${props.name}`, | ||
description: `${tt(props.analysisType)} run at ${new Date().toLocaleString()}`, | ||
} | ||
if (props.portfolioId) { | ||
return { | ||
...common, | ||
portfolioId: props.portfolioId, | ||
} | ||
} else if (props.portfolioGroupId) { | ||
return { | ||
...common, | ||
portfolioGroupId: props.portfolioGroupId, | ||
} | ||
} else if (props.initiativeId) { | ||
return { | ||
...common, | ||
initiativeId: props.initiativeId, | ||
} | ||
} else { | ||
throw new Error('No portfolio, portfolio group or initiative ID provided') | ||
} | ||
}) | ||
const runAnalysis = () => { | ||
emit('started') | ||
return withLoading( | ||
() => pactaClient.runAnalysis(request.value) | ||
.then((resp) => { analysisId.value = resp.analysisId }) | ||
.then(() => { void refreshAnalysisState() }), | ||
`${prefix}.runAnalysis`, | ||
) | ||
} | ||
const refreshAnalysisState = async () => { | ||
const aid = analysisId.value | ||
if (!aid) { | ||
console.warn('No analysis ID set, but refresh requested') | ||
return | ||
} | ||
const resp = await pactaClient.findAnalysisById(aid) | ||
analysis.value = resp | ||
if (analysis.value?.completedAt) { | ||
emit('finished') | ||
return | ||
} | ||
setTimeout(() => { void refreshAnalysisState }, 2000) | ||
} | ||
const analysisCompleted = computed(() => analysis.value?.completedAt) | ||
const runBtnVisible = computed(() => !analysisCompleted.value) | ||
const runBtnDisabled = computed(() => analysisId.value !== null || clicked.value) | ||
const runBtnLoading = computed(() => runBtnDisabled.value) | ||
const runBtnIcon = computed(() => analysisId.value ? 'pi pi-spin pi-spinner' : 'pi pi-play') | ||
const runBtnLabel = computed(() => { | ||
if (!clicked.value) { | ||
return tt('Run') + ' ' + tt(props.analysisType) | ||
} | ||
if (!analysisId.value) { | ||
return tt('Starting') + ' ' + tt(props.analysisType) + '...' | ||
} | ||
if (analysis.value) { | ||
return tt('Running') + ' ' + tt(props.analysisType) + '...' | ||
} | ||
return 'Should not happen' | ||
}) | ||
const completeBtnTo = computed(() => { | ||
if (!analysisId.value) { | ||
return '' | ||
} | ||
return localePath(`/my-data?tab=a&analyses=${analysisId.value}`) | ||
}) | ||
const completeBtnLabel = computed(() => { | ||
if (!analysisId.value) { | ||
return '' | ||
} | ||
return tt(props.analysisType) + ' ' + tt('Completed') | ||
}) | ||
</script> | ||
|
||
<template> | ||
<PVButton | ||
v-if="runBtnVisible" | ||
:disabled="runBtnDisabled" | ||
:loading="runBtnLoading" | ||
:icon="runBtnIcon" | ||
:label="runBtnLabel" | ||
@click="runAnalysis" | ||
/> | ||
<LinkButton | ||
v-else | ||
:to="completeBtnTo" | ||
icon="pi pi-check" | ||
:label="completeBtnLabel" | ||
/> | ||
</template> |
Oops, something went wrong.