-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add stats endpoint for inscription counts (#70)
* feat: add stats endpoint for inscription counts * test: add simple stats unit test * refactor: switch to normalize window function * test: update inscription count tests * refactor: add range filters * refactor: rewrite query without defaults * test: refactor tests to use less build() * refactor: rename migration to match style * refactor: add openapi info * refactor: track minimum value directly * refactor: make filters object mandatory * chore: rename migration * fix: add cache to stats endpoint * refactor: rename total to inscription_count_accum * fix: change schema to string for bigints * refactor: add block hash and timestamp to stats * fix: use min in query to ignore potential conflicts * fix: update etag to allow for partial block ingestion --------- Co-authored-by: janniks <janniks@users.noreply.github.com>
- Loading branch information
Showing
12 changed files
with
570 additions
and
35 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* eslint-disable @typescript-eslint/naming-convention */ | ||
import { MigrationBuilder, ColumnDefinitions } from 'node-pg-migrate'; | ||
|
||
export const shorthands: ColumnDefinitions | undefined = undefined; | ||
|
||
export function up(pgm: MigrationBuilder): void { | ||
pgm.createTable('inscriptions_per_block', { | ||
block_height: { | ||
type: 'bigint', | ||
primaryKey: true, | ||
}, | ||
block_hash: { | ||
type: 'text', | ||
notNull: true, | ||
}, | ||
inscription_count: { | ||
type: 'bigint', | ||
notNull: true, | ||
}, | ||
inscription_count_accum: { | ||
type: 'bigint', | ||
notNull: true, | ||
}, | ||
timestamp: { | ||
type: 'timestamptz', | ||
notNull: true, | ||
}, | ||
}); | ||
} |
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
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,52 @@ | ||
import { TypeBoxTypeProvider } from '@fastify/type-provider-typebox'; | ||
import { Type } from '@sinclair/typebox'; | ||
import { FastifyPluginAsync, FastifyPluginCallback } from 'fastify'; | ||
import { Server } from 'http'; | ||
import { BlockHeightParam, InscriptionsPerBlockResponse, NotFoundResponse } from '../schemas'; | ||
import { handleInscriptionsPerBlockCache } from '../util/cache'; | ||
import { blockParam } from '../util/helpers'; | ||
|
||
const IndexRoutes: FastifyPluginCallback<Record<never, never>, Server, TypeBoxTypeProvider> = ( | ||
fastify, | ||
options, | ||
done | ||
) => { | ||
fastify.addHook('preHandler', handleInscriptionsPerBlockCache); | ||
fastify.get( | ||
'/stats/inscriptions', | ||
{ | ||
schema: { | ||
operationId: 'getStatsInscriptionCount', | ||
summary: 'Inscription Count per Block', | ||
description: 'Retrieves statistics on the number of inscriptions revealed per block', | ||
tags: ['Statistics'], | ||
querystring: Type.Object({ | ||
from_block_height: Type.Optional(BlockHeightParam), | ||
to_block_height: Type.Optional(BlockHeightParam), | ||
}), | ||
response: { | ||
200: InscriptionsPerBlockResponse, | ||
404: NotFoundResponse, | ||
}, | ||
}, | ||
}, | ||
async (request, reply) => { | ||
const inscriptions = await fastify.db.getInscriptionCountPerBlock({ | ||
...blockParam(request.query.from_block_height, 'from_block'), | ||
...blockParam(request.query.to_block_height, 'to_block'), | ||
}); | ||
await reply.send({ | ||
results: inscriptions, | ||
}); | ||
} | ||
); | ||
done(); | ||
}; | ||
|
||
export const StatsRoutes: FastifyPluginAsync< | ||
Record<never, never>, | ||
Server, | ||
TypeBoxTypeProvider | ||
> = async fastify => { | ||
await fastify.register(IndexRoutes); | ||
}; |
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
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
Oops, something went wrong.