Skip to content

Commit

Permalink
improve data in coin ana page (#208)
Browse files Browse the repository at this point in the history
  • Loading branch information
domechn committed Dec 8, 2023
1 parent 2d80e35 commit ed2f06f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 15 deletions.
33 changes: 18 additions & 15 deletions src/components/coin-analytics.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
loadAllAssetActionsBySymbol,
queryAssetMaxAmountBySymbol,
queryLastAssetsBySymbol,
updateAssetPrice,
} from "@/middlelayers/charts";
Expand Down Expand Up @@ -80,6 +81,8 @@ const App = ({
const [updatePriceValue, setUpdatePriceValue] = useState(-1);
const [updatePriceDialogOpen, setUpdatePriceDialogOpen] = useState(false);

const [maxPosition, setMaxPosition] = useState<number>(0);

const [walletAliasMap, setWalletAliasMap] = useState<{
[k: string]: string | undefined;
}>({});
Expand Down Expand Up @@ -153,18 +156,14 @@ const App = ({
[currency, latestPrice]
);

const currentValue = useMemo(() => latestAsset?.value || 0, [latestAsset]);
const currentValueStr = useMemo(
() =>
currency.symbol +
prettyNumberToLocaleString(currencyWrapper(currency)(currentValue)),
[currency, currentValue]
);
const rank = useMemo(() => {
const rankNum = _(allowSymbols).indexOf(symbol) + 1;
if (rankNum === 0) {
return "-";
}

const rank = useMemo(
() => latestAsset?.amount.toFixed(10).replace(/0+$/, "") || 0,
[latestAsset]
);
return "#" + rankNum;
}, [symbol, allowSymbols]);

const profitStr = useMemo(
() =>
Expand All @@ -190,6 +189,10 @@ const App = ({
setLatestAsset(res);
});

queryAssetMaxAmountBySymbol(s).then((res) => {
setMaxPosition(res);
});

getLogoPath(s).then((res) => {
setLogo(res);
});
Expand Down Expand Up @@ -344,7 +347,7 @@ const App = ({
</div>
</div>
<p className="text-xs text-muted-foreground overflow-hidden whitespace-nowrap overflow-ellipsis">
amount: {rank}
rank: {rank}
</p>
</CardContent>
</Card>
Expand Down Expand Up @@ -373,7 +376,7 @@ const App = ({
{breakEvenPriceStr}
</div>
<p className="text-xs text-muted-foreground overflow-hidden whitespace-nowrap overflow-ellipsis">
profit rate: {profitRate}%
latest price: {latestPriceStr}
</p>
</CardContent>
</Card>
Expand Down Expand Up @@ -401,7 +404,7 @@ const App = ({
{profitStr}
</div>
<p className="text-xs text-muted-foreground overflow-hidden whitespace-nowrap overflow-ellipsis">
current value: {currentValueStr}
profit rate: {profitRate}%
</p>
</CardContent>
</Card>
Expand All @@ -428,7 +431,7 @@ const App = ({
{costPriceStr}
</div>
<p className="text-xs text-muted-foreground overflow-hidden whitespace-nowrap overflow-ellipsis">
latest price: {latestPriceStr}
max pos: {maxPosition.toFixed(8).replace(/0+$/, "")}
</p>
</CardContent>
</Card>
Expand Down
13 changes: 13 additions & 0 deletions src/middlelayers/charts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,11 @@ async function queryAssets(size = 1, symbol?: string): Promise<AssetModel[][]> {
return _(assets).groupBy("createdAt").values().value()
}

export async function queryAssetMaxAmountBySymbol(symbol: string): Promise<number> {
const res = await queryMaxAmountBySymbol(symbol) || 0
return res
}

// return all asset prices for all symbols
export function queryAllAssetPrices(): Promise<AssetPriceModel[]> {
return queryAssetPrices()
Expand Down Expand Up @@ -225,6 +230,14 @@ export async function queryLastAssetsBySymbol(symbol: string): Promise<Asset | u
} as Asset : undefined
}

export async function queryMaxAmountBySymbol(symbol: string): Promise<number | undefined> {
const db = await getDatabase()
const sql = `SELECT sum(amount) as amount FROM ${ASSETS_TABLE_NAME} WHERE symbol = ? GROUP BY uuid ORDER BY amount DESC LIMIT 1`
const models = await db.select<{ amount: number }[]>(sql, [symbol])

return models[0]?.amount
}

export async function queryAssetsAfterCreatedAt(createdAt?: number): Promise<AssetModel[]> {
const db = await getDatabase()
const ts = createdAt ? new Date(createdAt).toISOString() : new Date(0).toISOString()
Expand Down

0 comments on commit ed2f06f

Please sign in to comment.