Skip to content

A self-hosted media server and file management system

fix: reorganise pulse util functions and fix graph max value

aminnausin461284c9c003
diff --git a/resources/js/components/dashboard/DashboardAnalytics.vue b/resources/js/components/dashboard/DashboardAnalytics.vue
index 2b8cf09a..361c6456 100644
--- a/resources/js/components/dashboard/DashboardAnalytics.vue
+++ b/resources/js/components/dashboard/DashboardAnalytics.vue
@@ -4,7 +4,7 @@ import type { PulseResponse } from '@/types/types';
import { useGetPulse, useGetSiteAnalytics } from '@/service/queries';
import { ref, useTemplateRef, watch } from 'vue';
import { handleStartTask } from '@/service/taskService';
-import { periodForHumans } from '@/service/util';
+import { periodForHumans } from '@/service/pulseUtil';
import PulseRequests from '@/components/pulseCards/PulseRequests.vue';
import DashboardCard from '@/components/cards/DashboardCard.vue';
diff --git a/resources/js/components/pulseCards/PulseQueues.vue b/resources/js/components/pulseCards/PulseQueues.vue
index a3dbec81..bbd575b4 100644
--- a/resources/js/components/pulseCards/PulseQueues.vue
+++ b/resources/js/components/pulseCards/PulseQueues.vue
@@ -1,14 +1,14 @@
<script setup lang="ts">
import type { PulseQueueResponse, PulseResponse } from '@/types/types';
-import { format_number, periodForHumans, pulseFormatDate } from '@/service/util';
+import { format_number, periodForHumans, pulseFormatDate, getMaxReading } from '@/service/pulseUtil';
import { ref, watch } from 'vue';
-import IconQueueList from '../icons/IconQueueList.vue';
-import DashboardCard from '../cards/DashboardCard.vue';
-import PulseNoResults from '../pulse/PulseNoResults.vue';
-import PulseScroll from '../pulse/PulseScroll.vue';
-import PulseLineChart from '../charts/PulseLineChart.vue';
+import PulseLineChart from '@/components/charts/PulseLineChart.vue';
+import PulseNoResults from '@/components/pulse/PulseNoResults.vue';
+import DashboardCard from '@/components/cards/DashboardCard.vue';
+import IconQueueList from '@/components/icons/IconQueueList.vue';
+import PulseScroll from '@/components/pulse/PulseScroll.vue';
const validPeriods = ['1_hour', '6_hours', '24_hours', '7_days'];
@@ -33,13 +33,6 @@ function scale(data: { [key: string]: any }) {
return Object.values(data).map((value) => value * (1 / (props.pulseData?.queues?.config.sample_rate ?? 1)));
}
-function getMaxReading(readings: { [key: string]: { [key: string]: string | null } }): number {
- // Flatten the nested object structure and convert values to numbers
- const flattenedValues = Object.values(readings).flatMap((queue) => Object.values(queue));
- const numberValues = flattenedValues.map((value) => (value !== null && !isNaN(Number(value)) ? Number(value) : -Infinity));
- return Math.max(...numberValues);
-}
-
watch(
() => props.pulseData,
() => {
@@ -238,9 +231,7 @@ watch(
<span
v-if="pulseData?.queues?.config.sample_rate && pulseData.queues.config.sample_rate < 1"
:title="`Sample rate: ${pulseData.queues.config.sample_rate}, Raw value: ${format_number(getMaxReading(queues[queue]))}`"
- >~{{
- format_number(getMaxReading(queues[queue]) * (1 / pulseData.queues.config.sample_rate))
- }}</span
+ >~{{ format_number(getMaxReading(queues[queue]) * (1 / pulseData.queues.config.sample_rate)) }}</span
>
<template v-else>
{{ format_number(getMaxReading(queues[queue])) }}
@@ -302,8 +293,7 @@ watch(
tension: 0.2,
spanGaps: false,
segment: {
- borderColor: (ctx: any) =>
- ctx.p0.raw === 0 && ctx.p1.raw === 0 ? 'transparent' : undefined,
+ borderColor: (ctx: any) => (ctx.p0.raw === 0 && ctx.p1.raw === 0 ? 'transparent' : undefined),
},
},
},
diff --git a/resources/js/components/pulseCards/PulseRequests.vue b/resources/js/components/pulseCards/PulseRequests.vue
index 63d59799..4a258726 100644
--- a/resources/js/components/pulseCards/PulseRequests.vue
+++ b/resources/js/components/pulseCards/PulseRequests.vue
@@ -1,7 +1,7 @@
<script setup lang="ts">
import type { PulseResponse, PulseRquestsResponse } from '@/types/types';
-import { format_number, periodForHumans, pulseFormatDate } from '@/service/util';
+import { format_number, periodForHumans, pulseFormatDate, getMaxReading } from '@/service/pulseUtil';
import { ref, watch } from 'vue';
import IconArrowsLeftRight from '@/components/icons/IconArrowsLeftRight.vue';
@@ -33,13 +33,6 @@ function scale(data: { [key: string]: any }) {
return Object.values(data).map((value) => value * (1 / (props.pulseData?.requests?.config.sample_rate ?? 1)));
}
-function getMaxReading(readings: { [key: string]: { [key: string]: string | null } }): number {
- // Flatten the nested object structure and convert values to numbers
- const flattenedValues = Object.values(readings).flatMap((request) => Object.values(request));
- const numberValues = flattenedValues.map((value) => (value !== null && !isNaN(Number(value)) ? Number(value) : -Infinity));
- return Math.max(...numberValues);
-}
-
watch(
() => props.pulseData,
() => {
@@ -62,38 +55,23 @@ watch(
</template>
<template #actions>
<div class="flex flex-wrap gap-4">
- <div
- v-if="pulseData?.requests?.config.record_informational"
- class="flex items-center gap-2 text-xs text-gray-600 dark:text-gray-400 font-medium"
- >
+ <div v-if="pulseData?.requests?.config.record_informational" class="flex items-center gap-2 text-xs text-gray-600 dark:text-gray-400 font-medium">
<div class="h-0.5 w-3 rounded-full" style="background-color: rgba(29, 153, 172, 0.5)"></div>
Informational
</div>
- <div
- v-if="pulseData?.requests?.config.record_successful"
- class="flex items-center gap-2 text-xs text-gray-600 dark:text-gray-400 font-medium"
- >
+ <div v-if="pulseData?.requests?.config.record_successful" class="flex items-center gap-2 text-xs text-gray-600 dark:text-gray-400 font-medium">
<div class="h-0.5 w-3 rounded-full bg-[#9333ea]"></div>
Successful
</div>
- <div
- v-if="pulseData?.requests?.config.record_redirection"
- class="flex items-center gap-2 text-xs text-gray-600 dark:text-gray-400 font-medium"
- >
+ <div v-if="pulseData?.requests?.config.record_redirection" class="flex items-center gap-2 text-xs text-gray-600 dark:text-gray-400 font-medium">
<div class="h-0.5 w-3 rounded-full bg-[rgba(107,114,128,0.5)]"></div>
Redirection
</div>
- <div
- v-if="pulseData?.requests?.config.record_client_error"
- class="flex items-center gap-2 text-xs text-gray-600 dark:text-gray-400 font-medium"
- >
+ <div v-if="pulseData?.requests?.config.record_client_error" class="flex items-center gap-2 text-xs text-gray-600 dark:text-gray-400 font-medium">
<div class="h-0.5 w-3 rounded-full bg-[#eab308]"></div>
Client Error
</div>
- <div
- v-if="pulseData?.requests?.config.record_server_error"
- class="flex items-center gap-2 text-xs text-gray-600 dark:text-gray-400 font-medium"
- >
+ <div v-if="pulseData?.requests?.config.record_server_error" class="flex items-center gap-2 text-xs text-gray-600 dark:text-gray-400 font-medium">
<div class="h-0.5 w-3 rounded-full bg-[#e11d48]"></div>
Server Error
</div>
@@ -113,9 +91,7 @@ watch(
<span
v-if="pulseData?.requests?.config.sample_rate && pulseData.requests.config.sample_rate < 1"
:title="`Sample rate: ${pulseData.requests.config.sample_rate}, Raw value: ${format_number(getMaxReading(requests[request]))}`"
- >~{{
- format_number(getMaxReading(requests[request]) * (1 / pulseData.requests.config.sample_rate))
- }}</span
+ >~{{ format_number(getMaxReading(requests[request]) * (1 / pulseData.requests.config.sample_rate)) }}</span
>
<template v-else>
{{ format_number(getMaxReading(requests[request])) }}
@@ -126,9 +102,7 @@ watch(
<PulseLineChart
:class="' dark:!bg-primary-dark-800 !bg-primary-900 '"
:chart-data="{
- labels: Object.keys(requests[request][Object.keys(requests[request])[0]]).map((v) =>
- pulseFormatDate(v),
- ),
+ labels: Object.keys(requests[request][Object.keys(requests[request])[0]]).map((v) => pulseFormatDate(v)),
datasets: [
{
label: 'Server Error',
@@ -174,8 +148,7 @@ watch(
tension: 0.2,
spanGaps: false,
segment: {
- borderColor: (ctx: any) =>
- ctx.p0.raw === 0 && ctx.p1.raw === 0 ? 'transparent' : undefined,
+ borderColor: (ctx: any) => (ctx.p0.raw === 0 && ctx.p1.raw === 0 ? 'transparent' : undefined),
},
},
},
diff --git a/resources/js/components/pulseCards/PulseServers.vue b/resources/js/components/pulseCards/PulseServers.vue
index 77dc6aab..6027b11a 100644
--- a/resources/js/components/pulseCards/PulseServers.vue
+++ b/resources/js/components/pulseCards/PulseServers.vue
@@ -1,7 +1,8 @@
<script setup lang="ts">
import type { PulseResponse, PulseServerResponse } from '@/types/types.ts';
-import { format_number, toTimeSpan } from '@/service/util';
+import { format_number } from '@/service/pulseUtil';
+import { toTimeSpan } from '@/service/util';
import { ref, watch } from 'vue';
import IconSignalSlash from '../icons/IconSignalSlash.vue';
diff --git a/resources/js/components/pulseCards/PulseUsage.vue b/resources/js/components/pulseCards/PulseUsage.vue
index f01870ce..477f3887 100644
--- a/resources/js/components/pulseCards/PulseUsage.vue
+++ b/resources/js/components/pulseCards/PulseUsage.vue
@@ -1,7 +1,7 @@
<script setup lang="ts">
import type { PulseResponse, PulseUsageResponse } from '@/types/types';
-import { format_number, periodForHumans } from '@/service/util';
+import { format_number, periodForHumans } from '@/service/pulseUtil';
import { ref, watch } from 'vue';
import DashboardCard from '@/components/cards/DashboardCard.vue';
diff --git a/resources/js/service/pulseUtil.ts b/resources/js/service/pulseUtil.ts
new file mode 100644
index 00000000..50a3d33c
--- /dev/null
+++ b/resources/js/service/pulseUtil.ts
@@ -0,0 +1,61 @@
+export function getMaxReading(readings: { [key: string]: { [key: string]: string | null } }, sampleRate: number = 10): number {
+ // Flatten the nested object structure and convert values to numbers
+ // const flattenedValues = Object.values(readings).flatMap((reading) => Object.values(reading));
+ // const numberValues = flattenedValues.map((value) => (value !== null && !isNaN(Number(value)) ? Number(value) : -Infinity));
+
+ const max =
+ Math.max(
+ ...Object.values(readings).map((dataset) => Math.max(...Object.values(dataset).map((value) => (value !== null && !isNaN(Number(value)) ? Number(value) : -Infinity)))),
+ ) *
+ (1 / sampleRate);
+ // console.log(value);
+
+ return max;
+}
+
+/**
+ * LARAVEL PULSE FUNCTION:
+ *
+ * @param value
+ * @returns
+ */
+export function pulseFormatDate(value: string = '') {
+ if (/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/.exec(value) === null) {
+ throw new Error(`Unknown date format [${value}].`);
+ }
+
+ const [date, time] = value.split(' ');
+ const [year, month, day] = date.split('-').map(Number);
+ const [hour, minute, second] = time.split(':').map(Number);
+
+ return new Date(Date.UTC(year, month - 1, day, hour, minute, second, 0)).toLocaleString(undefined, {
+ year: '2-digit',
+ day: '2-digit',
+ month: '2-digit',
+ hourCycle: 'h24',
+ hour: '2-digit',
+ minute: '2-digit',
+ second: '2-digit',
+ timeZoneName: 'short',
+ });
+}
+
+/**
+ * LARAVEL PULSE FUNCTION:
+ *
+ * @param period
+ * @returns
+ */
+export function periodForHumans(period: string) {
+ if (period === '1_hour') return 'hour';
+ return period.replace('_', ' ');
+}
+
+/**
+ * LARAVEL PULSE FUNCTION:
+ * @param value
+ * @returns
+ */
+export function format_number(value: any) {
+ return Intl.NumberFormat().format(value);
+}
diff --git a/resources/js/service/util.ts b/resources/js/service/util.ts
index e1ac17f4..35e76e94 100644
--- a/resources/js/service/util.ts
+++ b/resources/js/service/util.ts
@@ -89,53 +89,6 @@ export function toCalendarFormattedDate(date: string) {
return rawDate.toLocaleDateString('en-CA', { month: 'long', day: '2-digit', year: 'numeric' }).replaceAll('.', '');
}
-/**
- * LARAVEL PULSE FUNCTION:
- *
- * @param value
- * @returns
- */
-export function pulseFormatDate(value: string = '') {
- if (/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/.exec(value) === null) {
- throw new Error(`Unknown date format [${value}].`);
- }
-
- const [date, time] = value.split(' ');
- const [year, month, day] = date.split('-').map(Number);
- const [hour, minute, second] = time.split(':').map(Number);
-
- return new Date(Date.UTC(year, month - 1, day, hour, minute, second, 0)).toLocaleString(undefined, {
- year: '2-digit',
- day: '2-digit',
- month: '2-digit',
- hourCycle: 'h24',
- hour: '2-digit',
- minute: '2-digit',
- second: '2-digit',
- timeZoneName: 'short',
- });
-}
-
-/**
- * LARAVEL PULSE FUNCTION:
- *
- * @param period
- * @returns
- */
-export function periodForHumans(period: string) {
- if (period === '1_hour') return 'hour';
- return period.replace('_', ' ');
-}
-
-/**
- * LARAVEL PULSE FUNCTION:
- * @param value
- * @returns
- */
-export function format_number(value: any) {
- return Intl.NumberFormat().format(value);
-}
-
/**
* Get the current screen size in tailwind notation.
*