All files / apps/developer-portal/layers/telemetry/app/components TelemetryDaily.vue

89.18% Statements 33/37
40.9% Branches 9/22
63.63% Functions 7/11
90.32% Lines 28/31

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105  6x               2x 6x   2x       2x 2x 2x         2x 2x 2x                                             5x   5x         5x       6x   5x   5x 5x   5x                       5x 6x 6x 6x 84x 84x 6x 6x 6x   6x                                
<template>
    <WidgetPanel v-model:filter="view" :title="chart.title" :filters="VIEWS">
        <div v-if="view === 'chart'" class="px-4 pt-5 pb-4">
            <DailyChart
                :days
                :values="chart.values"
                :unit="chart.unit"
                :end-label="$t('telemetryDaily.endLabel')"
            />
            <p class="mt-4 text-sm text-toned">{{ chart.reading }}</p>
        </div>
        <div v-else class="max-h-96 overflow-y-auto">
            <table class="data-table">
                <thead>
                    <tr>
                        <th>{{ $t("telemetryDaily.columns.day") }}</th>
                        <th class="numeric">{{ $t("telemetryDaily.columns.runs") }}</th>
                        <th class="numeric">{{ $t("telemetryDaily.columns.agent") }}</th>
                        <th class="numeric">{{ $t("telemetryDaily.columns.failed") }}</th>
                    </tr>
                </thead>
                <tbody>
                    <tr v-for="row in dayRows" :key="row.day">
                        <td class="font-mono">{{ row.label }}</td>
                        <td class="numeric font-mono">{{ row.runs }}</td>
                        <td class="numeric font-mono">{{ row.agent }}</td>
                        <td class="numeric font-mono" :class="row.failures ? 'tone-bad' : ''">
                            {{ row.failures }}
                        </td>
                    </tr>
                </tbody>
            </table>
        </div>
    </WidgetPanel>
</template>
 
<script setup lang="ts">
import { dailyTotals, scriptRows, shownDays } from "#layers/telemetry/shared/usageView.ts";
import type { UsageArtifact } from "#layers/snapshot/shared/types/UsageArtifact.ts";
 
interface Props {
    usage: UsageArtifact;
    /** Which count the chart draws, picked from the page's facts. */
    series: string;
}
 
const { usage, series } = defineProps<Props>();
 
const { t } = useI18n();
 
const VIEWS = [
    { value: "chart", label: t("telemetryDaily.views.chart") },
    { value: "table", label: t("telemetryDaily.views.table") },
];
 
const SERIES = ["runs", "agent", "failures"] as const;
 
type Series = (typeof SERIES)[number];
 
const isSeries = (value: string): value is Series => (SERIES as readonly string[]).includes(value);
 
const view = ref("chart");
 
const days = computed(() => shownDays(usage));
const daily = computed(() => dailyTotals(scriptRows(usage)));
 
const dayRows = computed(() =>
    days.value
        .map((day, index) => ({
            day,
            label: calendarDay(day),
            runs: daily.value[index]?.runs ?? 0,
            agent: daily.value[index]?.agent ?? 0,
            failures: daily.value[index]?.failures ?? 0,
        }))
        .toReversed(),
);
 
const chart = computed(() => {
    const key: Series = isSeries(series) ? series : "runs";
    const title = t(`telemetryDaily.series.${key}.title`);
    const unit = t(`telemetryDaily.series.${key}.unit`);
    const values = days.value.map((_, index) => daily.value[index]?.[key] ?? 0);
    const total = values.reduce((acc, value) => acc + value, 0);
    const peak = Math.max(0, ...values);
    const peakDay = days.value[values.indexOf(peak)] ?? "";
    const average = values.length ? Math.round((total / values.length) * 10) / 10 : 0;
 
    return {
        title,
        unit,
        values,
        reading:
            total === 0
                ? t("telemetryDaily.readingNone", { unit })
                : t("telemetryDaily.reading", {
                      average,
                      unit,
                      day: calendarDay(peakDay),
                      peak,
                  }),
    };
});
</script>