Skip to content

Commit

Permalink
Fix accountState query when only has one transaction (#48)
Browse files Browse the repository at this point in the history
* fix accountState query

* lint fix
  • Loading branch information
ricardocr987 authored Mar 23, 2024
1 parent 4fc084a commit 1539172
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 20 deletions.
8 changes: 4 additions & 4 deletions packages/core/src/utils/concurrence/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { sleep } from '../time.js'
*/
export async function concurrentPromises<T>(
it: Iterator<Promise<unknown>, T>,
concurrency: number = 20,
concurrency = 20,
): Promise<T> {
let done
let lastValue!: T
Expand Down Expand Up @@ -118,18 +118,18 @@ export class Mutex {
/**
* An util for retaining a unique snapshot of data while
* the previous snapshot is being processed.
*
*
* Example:
* ```ts
* const job = new DebouncedJob<string>(async (data) => {
* // Do something with the data
* console.log(data)
* return data
* }, 1000)
*
*
* job.run('foo')
* ```
*
*
* @param callback - The callback function that will be called with the data
* @param throttle - The minimum time between calls
*/
Expand Down
12 changes: 7 additions & 5 deletions packages/core/src/utils/time.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,13 @@ export function splitDurationIntoIntervals(
return []
}

const length = startDate.equals(endDate) ? 1 : Math.ceil(
endDate
.diff(startDate, intervalUnit as DurationUnit)
.get(intervalUnit as DurationUnit) / intervalSize,
)
const length = startDate.equals(endDate)
? 1
: Math.ceil(
endDate
.diff(startDate, intervalUnit as DurationUnit)
.get(intervalUnit as DurationUnit) / intervalSize,
)

// Divide range (A, D) in chunks of intervalSize * intervalUnit time intervals
// [(A, B), (B, C), (C, D)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,12 @@ export class BaseAccountEntityIndexer<T extends ParsedEntity<unknown>> {
)

const pendingMilis = pendingRanges.reduce(
(acc, curr) => acc + Math.abs(curr.endDate - curr.startDate),
(acc, curr) => acc + Math.max(curr.endDate - curr.startDate, 1),
0,
)

const processedMilis = processedRanges.reduce(
(acc, curr) => acc + Math.abs(curr.endDate - curr.startDate),
(acc, curr) => acc + Math.max(curr.endDate - curr.startDate, 1),
0,
)

Expand Down
14 changes: 11 additions & 3 deletions packages/framework/src/utils/stats/timeSeries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,16 @@ export class TimeSeriesStats<I extends object, O> {
trueDivideTimeFrameIndex--
}
if (timeFrame !== TimeFrame.All && trueDivideTimeFrameIndex > 0) {
const timeFrameSize = Duration.fromObject({[timeFrameName.toLowerCase()]: 1}).toMillis()
const timeFrameSize = Duration.fromObject({
[timeFrameName.toLowerCase()]: 1,
}).toMillis()
while (true) {
if (trueDivideTimeFrameIndex <= 0) break
const smallerSize = Duration.fromObject({[TimeFrame[sortedTimeFrames[trueDivideTimeFrameIndex]].toLowerCase()]: 1}).toMillis()
const smallerSize = Duration.fromObject({
[TimeFrame[
sortedTimeFrames[trueDivideTimeFrameIndex]
].toLowerCase()]: 1,
}).toMillis()
if (timeFrameSize % smallerSize === 0) break
trueDivideTimeFrameIndex--
}
Expand Down Expand Up @@ -259,7 +265,9 @@ export class TimeSeriesStats<I extends object, O> {
let data: O | undefined
for await (const value of inputs) {
const input =
'data' in value && trueDivideTimeFrameIndex !== 0 ? value.data : value
'data' in value && trueDivideTimeFrameIndex !== 0
? value.data
: value
data = await aggregator({
input,
interval,
Expand Down
13 changes: 7 additions & 6 deletions packages/indexer-example/src/domain/_mainWithStats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ import {
AccountStatsFilters,
AccountTimeSeriesStats,
AccountStats,
} from '@aleph-indexer/framework';
} from '@aleph-indexer/framework'

export default class MainDomain
extends IndexerMainDomain
implements IndexerMainDomainWithStats {
implements IndexerMainDomainWithStats
{
constructor(protected context: IndexerMainDomainContext) {
super(context);
super(context)
}

// Implement the updateStats method
Expand All @@ -30,7 +31,7 @@ export default class MainDomain
filters: AccountStatsFilters,
): Promise<AccountTimeSeriesStats[]> {
// Logic for retrieving and transforming time-series stats for specified accounts
return [];
return []
}

// Implement the getAccountStats method
Expand All @@ -39,6 +40,6 @@ export default class MainDomain
accounts: string[],
): Promise<AccountStats[]> {
// Logic for retrieving global stats for specified accounts
return [];
return []
}
}
}

0 comments on commit 1539172

Please sign in to comment.