mirror of
https://github.com/appwrite/console.git
synced 2026-11-18 20:47:21 +00:00
Merge pull request #2459 from appwrite/fix-DAT-790-fix-negative-usage-values-display
This commit is contained in:
6 files changed
+37
-13
No files matched your search
@@ -18,21 +18,38 @@ export function toDecimals(num: number, decimals: number = 1): number {
|
||||
return parseFloat(num.toFixed(decimals));
|
||||
}
|
||||
|
||||
export function formatNumberWithCommas(number: number): string {
|
||||
export function formatNumberWithCommas(number: number, min: number = 0): string {
|
||||
if (isNaN(number)) return String(number);
|
||||
const formatter = new Intl.NumberFormat('en');
|
||||
return formatter.format(number);
|
||||
return formatter.format(clampMin(number, min));
|
||||
}
|
||||
|
||||
export function formatCurrency(number: number, locale = 'en-US', currency = 'USD'): string {
|
||||
export function formatCurrency(
|
||||
number: number,
|
||||
locale = 'en-US',
|
||||
currency = 'USD',
|
||||
min: number = 0
|
||||
): string {
|
||||
if (isNaN(number)) return String(number);
|
||||
const formatter = new Intl.NumberFormat(locale, {
|
||||
style: 'currency',
|
||||
currency
|
||||
});
|
||||
return formatter.format(number);
|
||||
return formatter.format(clampMin(number, min));
|
||||
}
|
||||
|
||||
export function isWithinSafeRange(val: number) {
|
||||
return Math.abs(val) < Number.MAX_SAFE_INTEGER;
|
||||
}
|
||||
|
||||
/**
|
||||
* Clamps a number to a minimum value
|
||||
*
|
||||
* @export
|
||||
* @param {number} value
|
||||
* @param {number} min
|
||||
* @returns {number}
|
||||
*/
|
||||
export function clampMin(value: number, min: number = 0): number {
|
||||
return Math.max(min, value || 0);
|
||||
}
|
||||
@@ -1,3 +1,5 @@
|
||||
import { clampMin } from './numbers';
|
||||
|
||||
/**
|
||||
* Capitalizes the first letter of a string
|
||||
*
|
||||
@@ -45,8 +47,8 @@ const formatter = Intl.NumberFormat('en', {
|
||||
notation: 'compact'
|
||||
});
|
||||
|
||||
export function formatNum(number: number): string {
|
||||
return formatter.format(number);
|
||||
export function formatNum(number: number, min: number = 0): string {
|
||||
return formatter.format(clampMin(number, min));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
import { Card, SecondaryTabs, SecondaryTabsItem } from '$lib/components';
|
||||
import { page } from '$app/state';
|
||||
import { type Models } from '@appwrite.io/console';
|
||||
import { formatNumberWithCommas } from '$lib/helpers/numbers';
|
||||
import { formatNumberWithCommas, clampMin } from '$lib/helpers/numbers';
|
||||
import { Layout, Typography } from '@appwrite.io/pink-svelte';
|
||||
|
||||
export let title: string;
|
||||
@@ -41,7 +41,7 @@
|
||||
|
||||
<Card>
|
||||
{#if count}
|
||||
{@const totalCount = total.reduce((a, b) => a + b, 0)}
|
||||
{@const totalCount = clampMin(total.reduce((a, b) => a + b, 0))}
|
||||
|
||||
<Layout.Stack gap="xs">
|
||||
<Typography.Title>{formatNumberWithCommas(totalCount)}</Typography.Title>
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
<script lang="ts" context="module">
|
||||
import { total } from '$lib/helpers/array';
|
||||
import { clampMin } from '$lib/helpers/numbers';
|
||||
import type { Metric } from '$lib/sdk/usage';
|
||||
|
||||
export function totalMetrics(set: Array<unknown>): number {
|
||||
if (!set) return 0;
|
||||
return total((set as Metric[]).map((c) => c.value));
|
||||
return clampMin(total((set as Metric[]).map((c) => c.value)));
|
||||
}
|
||||
</script>
|
||||
|
||||
|
||||
+3
-3
@@ -10,7 +10,7 @@
|
||||
import { total } from '$lib/layout/usage.svelte';
|
||||
import { BillingPlan } from '$lib/constants.js';
|
||||
import { base } from '$app/paths';
|
||||
import { formatCurrency, formatNumberWithCommas } from '$lib/helpers/numbers';
|
||||
import { formatCurrency, formatNumberWithCommas, clampMin } from '$lib/helpers/numbers';
|
||||
import { getCountryName } from '$lib/helpers/diallingCodes.js';
|
||||
import { Accordion, Icon, Layout, Link, Table, Typography } from '@appwrite.io/pink-svelte';
|
||||
import { IconChartSquareBar } from '@appwrite.io/pink-icons-svelte';
|
||||
@@ -36,11 +36,11 @@
|
||||
$: legendData = [
|
||||
{
|
||||
name: 'Reads',
|
||||
value: data.usage.databasesReads.reduce((sum, item) => sum + item.value, 0)
|
||||
value: clampMin(data.usage.databasesReads.reduce((sum, item) => sum + item.value, 0))
|
||||
},
|
||||
{
|
||||
name: 'Writes',
|
||||
value: data.usage.databasesWrites.reduce((sum, item) => sum + item.value, 0)
|
||||
value: clampMin(data.usage.databasesWrites.reduce((sum, item) => sum + item.value, 0))
|
||||
}
|
||||
];
|
||||
|
||||
|
||||
+5
-1
@@ -9,6 +9,7 @@
|
||||
import { UsageCard } from '$lib/components';
|
||||
import { humanFileSize } from '$lib/helpers/sizeConvertion';
|
||||
import { formatTimeDetailed } from '$lib/helpers/timeConversion';
|
||||
import { clampMin } from '$lib/helpers/numbers';
|
||||
|
||||
const now = new Date();
|
||||
const rangeOptions = [
|
||||
@@ -84,7 +85,10 @@
|
||||
|
||||
metric.value = time !== '0s' ? time : '-';
|
||||
} else {
|
||||
metric.value = usage[metric.id] ?? '-';
|
||||
metric.value =
|
||||
usage[metric.id] !== undefined
|
||||
? clampMin(usage[metric.id]).toString()
|
||||
: '-';
|
||||
}
|
||||
return metric;
|
||||
});
|
||||
|
||||
Reference in new issue
Block a user