|
| 1 | +<script setup lang="ts"> |
| 2 | +import { faHourglass } from "@fortawesome/free-solid-svg-icons"; |
| 3 | +import { FontAwesomeIcon } from "@fortawesome/vue-fontawesome"; |
| 4 | +import { BBadge } from "bootstrap-vue"; |
| 5 | +import { parseISO } from "date-fns"; |
| 6 | +import { storeToRefs } from "pinia"; |
| 7 | +import { computed } from "vue"; |
| 8 | +
|
| 9 | +import type { HDASummary, HDCASummary } from "@/api"; |
| 10 | +import { isHDA } from "@/api"; |
| 11 | +import { useObjectStoreStore } from "@/stores/objectStoreStore"; |
| 12 | +
|
| 13 | +interface ExpirableObjectStoreTime { |
| 14 | + objectStoreId: string; |
| 15 | + objectExpiresAfterDays: number; |
| 16 | + objectStoreName: string; |
| 17 | + oldestCreateTime: Date; |
| 18 | +} |
| 19 | +
|
| 20 | +const props = defineProps<{ |
| 21 | + item: HDASummary | HDCASummary; |
| 22 | +}>(); |
| 23 | +
|
| 24 | +const store = useObjectStoreStore(); |
| 25 | +const { selectableObjectStores } = storeToRefs(store); |
| 26 | +
|
| 27 | +const defaultName = "Unnamed Object Store"; |
| 28 | +
|
| 29 | +const expirableObjectStoreTime = computed<ExpirableObjectStoreTime | undefined>(() => { |
| 30 | + const item = props.item; |
| 31 | + if (isHDA(item)) { |
| 32 | + // Single object store case: check if it has an expiration policy |
| 33 | + const expirableObjectStore = selectableObjectStores.value?.find( |
| 34 | + (objectStore) => |
| 35 | + objectStore.object_store_id === item.object_store_id && |
| 36 | + (objectStore.object_expires_after_days ?? 0) > 0, |
| 37 | + ); |
| 38 | + if (!expirableObjectStore) { |
| 39 | + return undefined; |
| 40 | + } |
| 41 | + return { |
| 42 | + objectStoreId: expirableObjectStore.object_store_id ?? "default", |
| 43 | + objectExpiresAfterDays: expirableObjectStore.object_expires_after_days ?? 0, |
| 44 | + objectStoreName: expirableObjectStore.name ?? defaultName, |
| 45 | + oldestCreateTime: parseISO(`${item.create_time}Z`), |
| 46 | + }; |
| 47 | + } else if (item.store_times_summary !== undefined) { |
| 48 | + // Multiple object stores case: find the one with the shortest expiration date |
| 49 | + const expirableStoreTimes: ExpirableObjectStoreTime[] = (item.store_times_summary ?? []) |
| 50 | + .map((storeTime) => { |
| 51 | + const objectStore = selectableObjectStores.value?.find( |
| 52 | + (os) => os.object_store_id === storeTime.object_store_id, |
| 53 | + ); |
| 54 | + if (!objectStore || (objectStore.object_expires_after_days ?? 0) <= 0) { |
| 55 | + return null; |
| 56 | + } |
| 57 | + return { |
| 58 | + objectStoreId: storeTime.object_store_id, |
| 59 | + objectExpiresAfterDays: objectStore.object_expires_after_days ?? 0, |
| 60 | + objectStoreName: objectStore.name ?? defaultName, |
| 61 | + oldestCreateTime: parseISO(`${storeTime.oldest_create_time}Z`), |
| 62 | + }; |
| 63 | + }) |
| 64 | + .filter((storeTime): storeTime is ExpirableObjectStoreTime => storeTime !== null); |
| 65 | + if (expirableStoreTimes.length === 0) { |
| 66 | + return undefined; |
| 67 | + } |
| 68 | + // Find the store with the shortest expiration time according to the oldest creation time and expiration days |
| 69 | + expirableStoreTimes.sort((a, b) => { |
| 70 | + const aExpiration = new Date(a.oldestCreateTime); |
| 71 | + aExpiration.setDate(aExpiration.getDate() + a.objectExpiresAfterDays); |
| 72 | + const bExpiration = new Date(b.oldestCreateTime); |
| 73 | + bExpiration.setDate(bExpiration.getDate() + b.objectExpiresAfterDays); |
| 74 | + return aExpiration.getTime() - bExpiration.getTime(); |
| 75 | + }); |
| 76 | + return expirableStoreTimes[0]; |
| 77 | + } |
| 78 | + return undefined; |
| 79 | +}); |
| 80 | +
|
| 81 | +const objectStoreName = computed(() => { |
| 82 | + return expirableObjectStoreTime.value?.objectStoreName ?? defaultName; |
| 83 | +}); |
| 84 | +
|
| 85 | +const expirationDate = computed(() => { |
| 86 | + const target = expirableObjectStoreTime.value; |
| 87 | + if (!target) { |
| 88 | + return null; |
| 89 | + } |
| 90 | + // Calculate the expiration date based on the creation date and the expiration days of the object store |
| 91 | + const expirationDate = new Date(target.oldestCreateTime); |
| 92 | + expirationDate.setDate(expirationDate.getDate() + target.objectExpiresAfterDays); |
| 93 | + return expirationDate; |
| 94 | +}); |
| 95 | +
|
| 96 | +const timeToExpire = computed<number | null>(() => { |
| 97 | + if (!expirationDate.value) { |
| 98 | + return null; |
| 99 | + } |
| 100 | + // Calculate the difference in days between the expiration date and the current date |
| 101 | + return expirationDate.value.getTime() - new Date().getTime(); |
| 102 | +}); |
| 103 | +
|
| 104 | +const canExpire = computed(() => timeToExpire.value !== null); |
| 105 | +
|
| 106 | +const daysToExpire = computed(() => { |
| 107 | + if (timeToExpire.value === null) { |
| 108 | + return null; |
| 109 | + } |
| 110 | + return timeToExpire.value < 0 ? 0 : Math.floor(timeToExpire.value / (1000 * 60 * 60 * 24)); |
| 111 | +}); |
| 112 | +
|
| 113 | +const hasExpired = computed(() => { |
| 114 | + return expirationDate.value ? expirationDate.value < new Date() : false; |
| 115 | +}); |
| 116 | +
|
| 117 | +const expirationMessage = computed(() => { |
| 118 | + if (daysToExpire.value === null) { |
| 119 | + return undefined; |
| 120 | + } |
| 121 | + if (hasExpired.value) { |
| 122 | + return "Expired"; |
| 123 | + } |
| 124 | + if (daysToExpire.value !== null && daysToExpire.value <= 1) { |
| 125 | + return `Expires soon!`; |
| 126 | + } |
| 127 | + return `Expires in ${daysToExpire.value} days`; |
| 128 | +}); |
| 129 | +
|
| 130 | +const expirationTooltip = computed(() => { |
| 131 | + const itemType = isHDA(props.item) ? "dataset" : "dataset collection (or any of its datasets)"; |
| 132 | + if (!expirationDate.value) { |
| 133 | + return `This ${itemType} does not have an expiration date.`; |
| 134 | + } |
| 135 | + if (hasExpired.value) { |
| 136 | + return `This ${itemType} was stored in ${ |
| 137 | + objectStoreName.value |
| 138 | + } and has expired on ${expirationDate.value.toDateString()}.`; |
| 139 | + } |
| 140 | + return `This ${itemType} is stored in ${ |
| 141 | + objectStoreName.value |
| 142 | + } and expires on ${expirationDate.value.toDateString()}.`; |
| 143 | +}); |
| 144 | +
|
| 145 | +const variant = computed(() => { |
| 146 | + if (hasExpired.value) { |
| 147 | + return "danger"; |
| 148 | + } |
| 149 | + if (daysToExpire.value && daysToExpire.value <= 5) { |
| 150 | + return "warning"; |
| 151 | + } |
| 152 | + return "secondary"; |
| 153 | +}); |
| 154 | +</script> |
| 155 | +<template> |
| 156 | + <span v-if="canExpire" class="expiration-indicator"> |
| 157 | + <BBadge v-b-tooltip.noninteractive.hover.left :variant="variant" :title="expirationTooltip"> |
| 158 | + <FontAwesomeIcon :icon="faHourglass" /> {{ expirationMessage }} |
| 159 | + </BBadge> |
| 160 | + </span> |
| 161 | +</template> |
0 commit comments