Skip to content

Commit

Permalink
Merge pull request #736 from molgenis/feat/disk-warning
Browse files Browse the repository at this point in the history
feat: disk warning
  • Loading branch information
timcadman committed May 13, 2024
2 parents c974da4 + 214ed0e commit 5f6add6
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 19 deletions.
25 changes: 23 additions & 2 deletions ui/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
<div class="container">
<div class="row mt-2">
<div class="col" v-if="username">
<Alert v-if="diskNearFull" type="warning" :dismissible="false">
{{ diskSpaceMessage }}
</Alert>
<Tabs v-if="username" :menu="tabs" :icons="tabIcons" />
</div>
<Login @loginEvent="reloadUser" v-else />
Expand All @@ -23,27 +26,32 @@
import Navbar from "@/components/Navbar.vue";
import Tabs from "@/components/Tabs.vue";
import Login from "@/views/Login.vue";
import Alert from "@/components/Alert.vue";
import { defineComponent, onMounted, ref, Ref } from "vue";
import { getPrincipal, getVersion, logout } from "@/api/api";
import { getPrincipal, getVersion, logout, getFreeDiskSpace } from "@/api/api";
import { useRouter } from "vue-router";
import { ApiError } from "@/helpers/errors";
import { diskSpaceBelowThreshold, convertBytes } from "@/helpers/utils";
export default defineComponent({
name: "ArmadilloPortal",
components: {
Navbar,
Tabs,
Login,
Alert,
},
setup() {
const isAuthenticated: Ref<boolean> = ref(false);
const username: Ref<string> = ref("");
const version: Ref<string> = ref("");
const router = useRouter();
const diskSpace: Ref<string> = ref("");
onMounted(() => {
loadUser();
loadVersion();
loadFreeDiskSpace();
});
const loadUser = async () => {
Expand All @@ -67,13 +75,16 @@ export default defineComponent({
const loadVersion = async () => {
version.value = await getVersion();
};
const loadFreeDiskSpace = async () => {
diskSpace.value = await getFreeDiskSpace();
};
return {
username,
isAuthenticated,
version,
loadUser,
loadVersion,
diskSpace,
};
},
data() {
Expand All @@ -88,6 +99,16 @@ export default defineComponent({
],
};
},
computed: {
diskNearFull() {
return diskSpaceBelowThreshold(this.diskSpace);
},
diskSpaceMessage() {
return `Disk space low (${
this.diskSpace === "" ? "" : convertBytes(this.diskSpace)
} remaining). Saving workspaces may not be possible and users risk losing workspace data. Either allocate more space or remove saved workspaces.`;
},
},
methods: {
logoutUser() {
logout().then(() => {
Expand Down
6 changes: 6 additions & 0 deletions ui/src/api/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -318,3 +318,9 @@ export async function createLinkFile(
};
return postJson(`/storage/projects/${viewProject}/objects/link`, data);
}

export async function getFreeDiskSpace() {
return get("/actuator/metrics/disk.free").then((data) => {
return data.measurements[0].value;
});
}
20 changes: 4 additions & 16 deletions ui/src/components/ActuatorItem.vue
Original file line number Diff line number Diff line change
Expand Up @@ -27,30 +27,18 @@
</template>

<script setup lang="ts">
import { convertBytes } from "@/helpers/utils";
const props = defineProps({
name: {
type: String,
required: true,
},
methods: {
convertBytes,
},
data: {
type: Object,
required: true,
},
});
/**
* Convert given bytes to 2 digits precision round exponent version string.
* @param bytes number
*/
function convertBytes(bytes: number): string {
const units = ["bytes", "KB", "MB", "GB", "TB", "EB"];
let unitIndex = 0;
while (bytes >= 1024 && unitIndex < units.length - 1) {
bytes /= 1024;
unitIndex++;
}
return `${bytes.toFixed(2)} ${units[unitIndex]}`;
}
</script>
5 changes: 5 additions & 0 deletions ui/src/components/Alert.vue
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<slot></slot>
<button
v-on:click="$emit('clear')"
v-if="dismissible"
type="button"
class="btn-close"
aria-label="Close"
Expand All @@ -25,6 +26,10 @@ export default defineComponent({
name: "Alert",
props: {
type: String as PropType<BootstrapType>,
dismissible: {
type: Boolean,
default: true,
},
},
emits: ["clear"],
});
Expand Down
19 changes: 19 additions & 0 deletions ui/src/helpers/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,3 +214,22 @@ export function getTablesFromListOfFiles(
export function encodeUriComponent(component: string) {
return component.replaceAll("/", "%2F").replaceAll("-", "%2D");
}

export function diskSpaceBelowThreshold(diskSpace: number): boolean {
return diskSpace < 2147483648;
}

/**
* Convert given bytes to 2 digits precision round exponent version string.
* @param bytes number
*/
export function convertBytes(bytes: number): string {
const units = ["bytes", "KB", "MB", "GB", "TB", "EB"];
let unitIndex = 0;
while (bytes >= 1024 && unitIndex < units.length - 1) {
bytes /= 1024;
unitIndex++;
}

return `${bytes.toFixed(2)} ${units[unitIndex]}`;
}
46 changes: 45 additions & 1 deletion ui/tests/unit/helpers/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ import {
getRestructuredProject,
getTablesFromListOfFiles,
isLinkFileType,
encodeUriComponent
encodeUriComponent,
convertBytes,
diskSpaceBelowThreshold
} from "@/helpers/utils";
import { StringObject } from "@/types/types";

Expand Down Expand Up @@ -288,3 +290,45 @@ describe("utils", () => {
});
});
});

describe("convertBytes", () => {
it("bytes", () => {
const actual = convertBytes(999)
expect(actual).toEqual("999.00 bytes")
})
it("KB", () => {
const actual = convertBytes(99999)
expect(actual).toEqual("97.66 KB")
})
it("MB", () => {
const actual = convertBytes(9999999)
expect(actual).toEqual("9.54 MB")
})
it("GB", () => {
const actual = convertBytes(9999999999)
expect(actual).toEqual("9.31 GB")
})
it("TB", () => {
const actual = convertBytes(9999999999999)
expect(actual).toEqual("9.09 TB")
})
it("EB", () => {
const actual = convertBytes(9999999999999999)
expect(actual).toEqual("8.88 EB")
})
});


describe("diskSpaceBelowThreshold", () => {
it("Return true", () => {
const actual = diskSpaceBelowThreshold(214748364);
expect(actual).toEqual(true);
});
it("Return false", () => {
const actual = diskSpaceBelowThreshold(9214748364);
expect(actual).toEqual(false);
});
});



0 comments on commit 5f6add6

Please sign in to comment.