Skip to content

Commit

Permalink
feat: display only if collected
Browse files Browse the repository at this point in the history
Signed-off-by: David Dal Busco <[email protected]>
  • Loading branch information
peterpeterparker committed Aug 18, 2024
1 parent e655331 commit dec5243
Show file tree
Hide file tree
Showing 10 changed files with 266 additions and 169 deletions.
14 changes: 9 additions & 5 deletions src/declarations/orbiter/orbiter.did.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,16 @@ export interface AnalyticsTop10PageViews {
export interface AnalyticsTrackEvents {
total: Array<[string, number]>;
}
export interface AnalyticsWebVitalsPageMetrics {
cls: [] | [number];
fcp: [] | [number];
inp: [] | [number];
lcp: [] | [number];
ttfb: [] | [number];
}
export interface AnalyticsWebVitalsPerformanceMetrics {
cls: number;
fcp: number;
inp: number;
lcp: number;
ttfb: number;
overall: AnalyticsWebVitalsPageMetrics;
pages: Array<[string, AnalyticsWebVitalsPageMetrics]>;
}
export interface CalendarDate {
day: number;
Expand Down
14 changes: 9 additions & 5 deletions src/declarations/orbiter/orbiter.factory.did.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,16 @@ export const idlFactory = ({ IDL }) => {
satellite_id: IDL.Principal,
version: IDL.Opt(IDL.Nat64)
});
const AnalyticsWebVitalsPageMetrics = IDL.Record({
cls: IDL.Opt(IDL.Float64),
fcp: IDL.Opt(IDL.Float64),
inp: IDL.Opt(IDL.Float64),
lcp: IDL.Opt(IDL.Float64),
ttfb: IDL.Opt(IDL.Float64)
});
const AnalyticsWebVitalsPerformanceMetrics = IDL.Record({
cls: IDL.Float64,
fcp: IDL.Float64,
inp: IDL.Float64,
lcp: IDL.Float64,
ttfb: IDL.Float64
overall: AnalyticsWebVitalsPageMetrics,
pages: IDL.Vec(IDL.Tuple(IDL.Text, AnalyticsWebVitalsPageMetrics))
});
const TrackEvent = IDL.Record({
updated_at: IDL.Nat64,
Expand Down
14 changes: 9 additions & 5 deletions src/declarations/orbiter/orbiter.factory.did.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,16 @@ export const idlFactory = ({ IDL }) => {
satellite_id: IDL.Principal,
version: IDL.Opt(IDL.Nat64)
});
const AnalyticsWebVitalsPageMetrics = IDL.Record({
cls: IDL.Opt(IDL.Float64),
fcp: IDL.Opt(IDL.Float64),
inp: IDL.Opt(IDL.Float64),
lcp: IDL.Opt(IDL.Float64),
ttfb: IDL.Opt(IDL.Float64)
});
const AnalyticsWebVitalsPerformanceMetrics = IDL.Record({
cls: IDL.Float64,
fcp: IDL.Float64,
inp: IDL.Float64,
lcp: IDL.Float64,
ttfb: IDL.Float64
overall: AnalyticsWebVitalsPageMetrics,
pages: IDL.Vec(IDL.Tuple(IDL.Text, AnalyticsWebVitalsPageMetrics))
});
const TrackEvent = IDL.Record({
updated_at: IDL.Nat64,
Expand Down
5 changes: 4 additions & 1 deletion src/frontend/src/lib/api/orbiter.api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,10 @@ export const getPerformanceMetricsAnalyticsWebVitals = async ({
to,
identity
}: PageViewsParams): Promise<AnalyticsWebVitalsPerformanceMetrics> => {
const { get_performance_metrics_analytics_web_vitals } = await getOrbiterActor({ orbiterId, identity });
const { get_performance_metrics_analytics_web_vitals } = await getOrbiterActor({
orbiterId,
identity
});
return getAnalytics({
satelliteId,
from,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,122 +1,12 @@
<script lang="ts">
import { i18n } from '$lib/stores/i18n.store';
import type { AnalyticsWebVitalsPerformanceMetrics } from '$declarations/orbiter/orbiter.did';
import { formatNumber } from '$lib/utils/number.utils';
import AnalyticsPerformancePageMetrics from '$lib/components/analytics/AnalyticsPerformancePageMetrics.svelte';
export let performanceMetrics: AnalyticsWebVitalsPerformanceMetrics;
let cls: number;
let fcp: number;
let inp: number;
let lcp: number;
let ttfb: number;
let metrics: AnalyticsPerformancePageMetrics;
$: ({ cls, fcp, inp, lcp, ttfb } = performanceMetrics);
type Rating = 'good' | 'needs_improvement' | 'poor';
// https://github.com/GoogleChrome/web-vitals/blob/9b932519b16f72328c6d8e9814b811f1bc1a0bb5/src/lib/bindReporter.ts#L19
const getRating = ({
value,
thresholds: [improve, poor]
}: {
value: number;
thresholds: [number, number];
}): Rating => {
if (value > poor) {
return 'poor';
}
if (value > improve) {
return 'needs_improvement';
}
return 'good';
};
// https://github.com/GoogleChrome/web-vitals/blob/9b932519b16f72328c6d8e9814b811f1bc1a0bb5/src/onCLS.ts#L28
// https://web.dev/articles/cls#what_is_a_good_cls_score
let clsRating: Rating;
$: clsRating = getRating({ value: cls, thresholds: [0.1, 0.25] });
// https://github.com/GoogleChrome/web-vitals/blob/9b932519b16f72328c6d8e9814b811f1bc1a0bb5/src/onFCP.ts#L28
// https://web.dev/articles/fcp#what_is_a_good_fcp_score
let fcpRating: Rating;
$: fcpRating = getRating({ value: fcp, thresholds: [1800, 3000] });
// https://github.com/GoogleChrome/web-vitals/blob/9b932519b16f72328c6d8e9814b811f1bc1a0bb5/src/onINP.ts#L35C55-L35C63
// https://web.dev/articles/inp#what_is_a_good_inp_score
let inpRating: Rating;
$: inpRating = getRating({ value: inp, thresholds: [200, 500] });
// https://github.com/GoogleChrome/web-vitals/blob/9b932519b16f72328c6d8e9814b811f1bc1a0bb5/src/onLCP.ts#L31
// https://web.dev/articles/lcp#what_is_a_good_lcp_score
let lcpRating: Rating;
$: lcpRating = getRating({ value: lcp, thresholds: [2500, 4000] });
// https://web.dev/articles/ttfb#what_is_a_good_ttfb_score
// https://github.com/GoogleChrome/web-vitals/blob/9b932519b16f72328c6d8e9814b811f1bc1a0bb5/src/onTTFB.ts#L26C56-L26C65
let ttfbRating: Rating;
$: ttfbRating = getRating({ value: ttfb, thresholds: [800, 1800] });
$: metrics = performanceMetrics.overall;
</script>

<div class="table-container">
<table>
<thead>
<tr>
<th class="description"> {$i18n.analytics.web_vitals} </th>
<th class="score"> {$i18n.analytics.score} </th>
<th> {$i18n.analytics.rating} </th>
</tr>
</thead>

<tbody>
<tr>
<td>{@html $i18n.analytics.ttfb}</td>
<td class="score">{formatNumber(ttfb)}</td>
<td>{$i18n.analytics[ttfbRating]}</td>
</tr>

<tr>
<td>{@html $i18n.analytics.fcp}</td>
<td class="score">{formatNumber(fcp)}</td>
<td>{$i18n.analytics[fcpRating]}</td>
</tr>

<tr>
<td>{@html $i18n.analytics.lcp}</td>
<td class="score">{formatNumber(lcp)}</td>
<td>{$i18n.analytics[lcpRating]}</td>
</tr>

<tr>
<td>{@html $i18n.analytics.cls}</td>
<td class="score">{formatNumber(cls)}</td>
<td>{$i18n.analytics[clsRating]}</td>
</tr>

<tr>
<td>{@html $i18n.analytics.inp}</td>
<td class="score">{formatNumber(inp)}</td>
<td>{$i18n.analytics[inpRating]}</td>
</tr>
</tbody>
</table>
</div>

<style lang="scss">
@use '../../styles/mixins/media';
.description {
width: 50%;
}
.score {
display: none;
width: 20%;
@include media.min-width(medium) {
display: table-cell;
vertical-align: middle;
}
}
</style>
<AnalyticsPerformancePageMetrics {metrics} />
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
<script lang="ts">
import { i18n } from '$lib/stores/i18n.store';
import type { AnalyticsWebVitalsPageMetrics } from '$declarations/orbiter/orbiter.did';
import { formatNumber } from '$lib/utils/number.utils';
import { fromNullable, isNullish, nonNullish } from '@dfinity/utils';
export let metrics: AnalyticsWebVitalsPageMetrics;
let cls: [] | [number];
let fcp: [] | [number];
let inp: [] | [number];
let lcp: [] | [number];
let ttfb: [] | [number];
$: ({ cls, fcp, inp, lcp, ttfb } = metrics);
type Rating = 'good' | 'needs_improvement' | 'poor';
// https://github.com/GoogleChrome/web-vitals/blob/9b932519b16f72328c6d8e9814b811f1bc1a0bb5/src/lib/bindReporter.ts#L19
const getRating = ({
metric,
thresholds: [improve, poor]
}: {
metric: [] | [number];
thresholds: [number, number];
}): Rating | undefined => {
const value = fromNullable(metric);
if (isNullish(value)) {
return undefined;
}
if (value > poor) {
return 'poor';
}
if (value > improve) {
return 'needs_improvement';
}
return 'good';
};
// https://github.com/GoogleChrome/web-vitals/blob/9b932519b16f72328c6d8e9814b811f1bc1a0bb5/src/onCLS.ts#L28
// https://web.dev/articles/cls#what_is_a_good_cls_score
let clsRating: Rating | undefined;
$: clsRating = getRating({ metric: cls, thresholds: [0.1, 0.25] });
// https://github.com/GoogleChrome/web-vitals/blob/9b932519b16f72328c6d8e9814b811f1bc1a0bb5/src/onFCP.ts#L28
// https://web.dev/articles/fcp#what_is_a_good_fcp_score
let fcpRating: Rating | undefined;
$: fcpRating = getRating({ metric: fcp, thresholds: [1800, 3000] });
// https://github.com/GoogleChrome/web-vitals/blob/9b932519b16f72328c6d8e9814b811f1bc1a0bb5/src/onINP.ts#L35C55-L35C63
// https://web.dev/articles/inp#what_is_a_good_inp_score
let inpRating: Rating | undefined;
$: inpRating = getRating({ metric: inp, thresholds: [200, 500] });
// https://github.com/GoogleChrome/web-vitals/blob/9b932519b16f72328c6d8e9814b811f1bc1a0bb5/src/onLCP.ts#L31
// https://web.dev/articles/lcp#what_is_a_good_lcp_score
let lcpRating: Rating | undefined;
$: lcpRating = getRating({ metric: lcp, thresholds: [2500, 4000] });
// https://web.dev/articles/ttfb#what_is_a_good_ttfb_score
// https://github.com/GoogleChrome/web-vitals/blob/9b932519b16f72328c6d8e9814b811f1bc1a0bb5/src/onTTFB.ts#L26C56-L26C65
let ttfbRating: Rating | undefined;
$: ttfbRating = getRating({ metric: ttfb, thresholds: [800, 1800] });
</script>

<div class="table-container">
<table>
<thead>
<tr>
<th class="description"> {$i18n.analytics.web_vitals} </th>
<th class="score"> {$i18n.analytics.score} </th>
<th> {$i18n.analytics.rating} </th>
</tr>
</thead>

<tbody>
{#if nonNullish(ttfbRating)}
<tr>
<td>{@html $i18n.analytics.ttfb}</td>
<td class="score">{formatNumber(fromNullable(ttfb) ?? 0)}</td>
<td>{$i18n.analytics[ttfbRating]}</td>
</tr>
{/if}

{#if nonNullish(fcpRating)}
<tr>
<td>{@html $i18n.analytics.fcp}</td>
<td class="score">{formatNumber(fromNullable(fcp) ?? 0)}</td>
<td>{$i18n.analytics[fcpRating]}</td>
</tr>
{/if}

{#if nonNullish(lcpRating)}
<tr>
<td>{@html $i18n.analytics.lcp}</td>
<td class="score">{formatNumber(fromNullable(lcp) ?? 0)}</td>
<td>{$i18n.analytics[lcpRating]}</td>
</tr>
{/if}

{#if nonNullish(clsRating)}
<tr>
<td>{@html $i18n.analytics.cls}</td>
<td class="score">{formatNumber(fromNullable(cls) ?? 0)}</td>
<td>{$i18n.analytics[clsRating]}</td>
</tr>
{/if}

{#if nonNullish(inpRating)}
<tr>
<td>{@html $i18n.analytics.inp}</td>
<td class="score">{formatNumber(fromNullable(inp) ?? 0)}</td>
<td>{$i18n.analytics[inpRating]}</td>
</tr>
{/if}
</tbody>
</table>
</div>

<style lang="scss">
@use '../../styles/mixins/media';
.description {
width: 50%;
}
.score {
display: none;
width: 20%;
@include media.min-width(medium) {
display: table-cell;
vertical-align: middle;
}
}
</style>
14 changes: 9 additions & 5 deletions src/orbiter/orbiter.did
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,16 @@ type AnalyticsTop10PageViews = record {
pages : vec record { text; nat32 };
};
type AnalyticsTrackEvents = record { total : vec record { text; nat32 } };
type AnalyticsWebVitalsPageMetrics = record {
cls : opt float64;
fcp : opt float64;
inp : opt float64;
lcp : opt float64;
ttfb : opt float64;
};
type AnalyticsWebVitalsPerformanceMetrics = record {
cls : float64;
fcp : float64;
inp : float64;
lcp : float64;
ttfb : float64;
overall : AnalyticsWebVitalsPageMetrics;
pages : vec record { text; AnalyticsWebVitalsPageMetrics };
};
type CalendarDate = record { day : nat8; month : nat8; year : int32 };
type Controller = record {
Expand Down
Loading

0 comments on commit dec5243

Please sign in to comment.