Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: gather assets models by symbol when querying data #92

Merged
merged 1 commit into from
Jul 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 26 additions & 26 deletions src-tauri/cloudsync/record.txt
Original file line number Diff line number Diff line change
@@ -1,45 +1,45 @@
collection Record {
id: string;

@read
@delegate
@read
@delegate
owner: User;

@read
@delegate
uuid: string;
@read
@delegate
uuid: string;

@read
@delegate
records: string;
@read
@delegate
records: string;

@read
@delegate
createdAt: number;
@read
@delegate
createdAt: number;


constructor (id: string, owner: User, uuid: string, records: string, createdAt: number) {
this.id = id;

this.owner = owner;

this.uuid = uuid;
this.records = records;
this.createdAt = createdAt;
this.uuid = uuid;
this.records = records;
this.createdAt = createdAt;
}

@call
@delegate
del () {
if (!ctx.publicKey) {
error("you must sign the txn");
}
@call
@delegate
del () {
if (!ctx.publicKey) {
error("you must sign the txn");
}

if (this.owner.id != ctx.publicKey.toHex()) {
throw error("you cannot delete others' data");
}

if (this.owner.id != ctx.publicKey.toHex()) {
throw error("you cannot delete others' data");
}

selfdestruct();
}
selfdestruct();
}

}
34 changes: 25 additions & 9 deletions src/middlelayers/charts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,22 @@ async function queryAssets(size = 1): Promise<AssetModel[][]> {
return _(assets).groupBy("createdAt").values().value()
}

function groupAssetModelsListBySymbol(models: AssetModel[][]): AssetModel[][] {
// sum by symbol
const res: AssetModel[][] = []

_(models).forEach(ms => res.push(groupAssetModelsBySymbol(ms)))
return res
}

function groupAssetModelsBySymbol(models: AssetModel[]): AssetModel[] {
return _(models).groupBy("symbol").values().map(assets => ({
..._(assets).first()!,
amount: _(assets).sumBy("amount"),
value: _(assets).sumBy("value"),
})).value()
}

export async function queryAssetsAfterCreatedAt(createdAt?: number): Promise<AssetModel[]> {
const db = await getDatabase()
const ts = createdAt ? new Date(createdAt).toISOString() : new Date(0).toISOString()
Expand All @@ -86,7 +102,7 @@ async function deleteAssetByUUID(uuid: string): Promise<void> {
}

export async function queryTotalValue(): Promise<TotalValueData> {
const results = await queryAssets(2)
const results = groupAssetModelsListBySymbol(await queryAssets(2))

if (results.length === 0) {
return {
Expand Down Expand Up @@ -117,7 +133,7 @@ export async function queryTotalValue(): Promise<TotalValueData> {

export async function queryTopCoinsRank(size = 10): Promise<TopCoinsRankData> {

const assets = await queryAssets(size) || []
const assets = groupAssetModelsListBySymbol(await queryAssets(size) || [])

const reservedAssets = _(assets).reverse().value()

Expand Down Expand Up @@ -155,7 +171,7 @@ export async function queryTopCoinsRank(size = 10): Promise<TopCoinsRankData> {
}

export async function queryTopCoinsPercentageChangeData(size = 10): Promise<TopCoinsPercentageChangeData> {
const assets = await queryAssets(size) || []
const assets = groupAssetModelsListBySymbol(await queryAssets(size) || [])

const reservedAssets = _(assets).reverse().value()

Expand Down Expand Up @@ -206,7 +222,7 @@ function getCoins(assets: AssetModel[][]): string[] {

export async function queryAssetChange(size = 10): Promise<AssetChangeData> {

const assets = await queryAssets(size) || []
const assets = groupAssetModelsListBySymbol(await queryAssets(size) || [])

const reservedAssets = _(assets).reverse().value()

Expand All @@ -220,7 +236,7 @@ export async function queryLatestAssetsPercentage(): Promise<LatestAssetsPercent
const size = 1
const backgroundColors = generateRandomColors(11) // top 10 and others

const assets = await queryAssets(size) || []
const assets = groupAssetModelsListBySymbol(await queryAssets(size) || [])
if (assets.length === 0) {
return []
}
Expand Down Expand Up @@ -257,7 +273,7 @@ export async function queryLatestAssetsPercentage(): Promise<LatestAssetsPercent
export async function queryCoinsAmountChange(size = 10): Promise<CoinsAmountAndValueChangeData> {
const querySize = size * 2

const assets = await queryAssets(querySize) || []
const assets = groupAssetModelsListBySymbol(await queryAssets(querySize) || [])
if (!assets) {
return []
}
Expand Down Expand Up @@ -297,7 +313,7 @@ export async function queryCoinsAmountChange(size = 10): Promise<CoinsAmountAndV
}

export async function queryHistoricalData(size = 30): Promise<HistoricalData[]> {
const models = await queryAssets(size)
const models = groupAssetModelsListBySymbol(await queryAssets(size))

const assetsModelsToHistoricalData = (ams: AssetModel[]): HistoricalData => {
return {
Expand All @@ -316,7 +332,7 @@ export async function deleteHistoricalDataByUUID(uuid: string): Promise<void> {
}

export async function queryCoinDataById(id: string): Promise<CoinData[]> {
const models = await queryAssetByUUID(id)
const models = groupAssetModelsBySymbol(await queryAssetByUUID(id))

const res: CoinData[] = _(models)
.map(m => ({
Expand All @@ -332,7 +348,7 @@ export async function queryAllDataDates(): Promise<{
id: string
date: string
}[]> {
const assets = await queryAssets(-1)
const assets = groupAssetModelsListBySymbol(await queryAssets(-1))

return _(assets)
.map(ass => _(ass).first())
Expand Down
4 changes: 3 additions & 1 deletion src/middlelayers/cloudsync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -318,13 +318,15 @@ async function removeAssetsInCloud(assets: AssetModel[]): Promise<number> {
async function writeAssetsToDB(d: Database, assets: AssetModel[]): Promise<number> {
const insertValuesStr = assets.map(() => `(?, ?, ?, ?, ?, ?)`).join(", ")

await d.execute(`INSERT INTO ${ASSETS_TABLE_NAME} (uuid, createdAt, symbol, amount, value, price) VALUES ${insertValuesStr}`, _(assets).map((asset) => [
await d.execute(`INSERT INTO ${ASSETS_TABLE_NAME} (uuid, createdAt, symbol, amount, value, price, wallet, walletAlias) VALUES ${insertValuesStr}`, _(assets).map((asset) => [
asset.uuid,
asset.createdAt,
asset.symbol,
asset.amount,
asset.value,
asset.price,
asset.wallet,
asset.walletAlias,
]).flatten().value())

return assets.length
Expand Down
16 changes: 8 additions & 8 deletions src/utils/color.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import _, { random } from 'lodash'
import _ from 'lodash'
import ColorDiff from 'color-diff'

const niceColors = [
Expand All @@ -16,20 +16,20 @@ const niceColors = [
]

function generateRandomColor(): { R: number; G: number; B: number } {
let r = random(0, 255)
let g = random(0, 255)
let b = random(0, 255)
let r = _.random(0, 255)
let g = _.random(0, 255)
let b = _.random(0, 255)
// Check the color's brightness
let brightness = Math.sqrt(0.299 * r ** 2 + 0.587 * g ** 2 + 0.114 * b ** 2)
// Limit the brightness range
if (brightness < 130) {
brightness = random(130, 255)
brightness = _.random(130, 255)
}
// Check for clashing colors
if (r > 200 && g > 200 && b > 200) {
r = random(0, 200)
g = random(0, 200)
b = random(0, 200)
r = _.random(0, 200)
g = _.random(0, 200)
b = _.random(0, 200)
}
return {
R: r,
Expand Down
Loading