-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: satellite cycles consumption chart
Signed-off-by: David Dal Busco <[email protected]>
- Loading branch information
1 parent
18ee019
commit 6520ed9
Showing
4 changed files
with
129 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
src/frontend/src/lib/components/satellites/SatelliteCycles.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
<script lang="ts"> | ||
import type { Satellite } from '$declarations/mission_control/mission_control.did'; | ||
import { fade } from 'svelte/transition'; | ||
import { listSatelliteStatuses } from '$lib/api/mission-control.api'; | ||
import { missionControlStore } from '$lib/stores/mission-control.store'; | ||
import type { Principal } from '@dfinity/principal'; | ||
import { authStore } from '$lib/stores/auth.store'; | ||
import { fromNullable, isNullish } from '@dfinity/utils'; | ||
import { formatToDay, fromBigIntNanoSeconds } from '$lib/utils/date.utils'; | ||
import { formatTCycles } from '$lib/utils/cycles.utils'; | ||
import AxisY from '$lib/components/charts/AxisY.svelte'; | ||
import AxisX from '$lib/components/charts/AxisX.svelte'; | ||
import Area from '$lib/components/charts/Area.svelte'; | ||
import Line from '$lib/components/charts/Line.svelte'; | ||
import { LayerCake, Svg } from 'layercake'; | ||
export let satellite: Satellite; | ||
let dailyTotalArray: [string, number][]; | ||
const xKey = 'myX'; | ||
const yKey = 'myY'; | ||
type ChartsData = { | ||
[xKey]: string; | ||
[yKey]: number; | ||
}; | ||
let chartsData: ChartsData[] = []; | ||
const load = async ({ missionControlId }: { missionControlId: Principal | undefined | null }) => { | ||
if (isNullish(missionControlId)) { | ||
return; | ||
} | ||
const results = await listSatelliteStatuses({ | ||
identity: $authStore.identity, | ||
missionControlId, | ||
satelliteId: satellite.satellite_id | ||
}); | ||
chartsData = (fromNullable(results) ?? []).map(([timestamp, result]) => { | ||
if ('Err' in result) { | ||
return { | ||
[xKey]: `${fromBigIntNanoSeconds(timestamp).getTime()}`, | ||
[yKey]: 0 | ||
}; | ||
} | ||
const { | ||
Ok: { | ||
status: { cycles } | ||
} | ||
} = result; | ||
return { | ||
[xKey]: `${fromBigIntNanoSeconds(timestamp).getTime()}`, | ||
[yKey]: parseFloat(formatTCycles(cycles)) | ||
}; | ||
}); | ||
}; | ||
$: $missionControlStore, (async () => load({ missionControlId: $missionControlStore }))(); | ||
let ticks: string[]; | ||
$: ticks = Object.values(chartsData).map(({ [xKey]: a }) => a); | ||
const formatTick = (d: string): string => { | ||
const date = new Date(parseInt(d)); | ||
return formatToDay(date); | ||
}; | ||
</script> | ||
|
||
Cycles | ||
|
||
{#if chartsData.length > 0} | ||
<div class="chart-container" in:fade> | ||
<LayerCake | ||
padding={{ top: 32, right: 16, bottom: 32, left: 16 }} | ||
x={xKey} | ||
y={yKey} | ||
yNice={4} | ||
yDomain={[0, null]} | ||
data={chartsData} | ||
> | ||
<Svg> | ||
<AxisX {formatTick} {ticks} /> | ||
<AxisY ticks={4} /> | ||
<Line /> | ||
<Area /> | ||
</Svg> | ||
</LayerCake> | ||
</div> | ||
{/if} | ||
|
||
|
||
<style lang="scss"> | ||
@use '../../styles/mixins/shadow'; | ||
.chart-container { | ||
width: 100%; | ||
height: 300px; | ||
fill: var(--value-color); | ||
margin: 0 0 var(--padding-4x); | ||
padding: var(--padding-2x) var(--padding-6x); | ||
@include shadow.strong-card; | ||
} | ||
</style> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters