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

✨ spotlight sort & order #18

Merged
merged 3 commits into from
Jan 17, 2022
Merged
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
40 changes: 28 additions & 12 deletions src/resolvers/spotlight.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,47 @@ import type { EntityManager } from 'typeorm'
import { NFTEntity } from '../generated/model'
import { SpotlightEntity } from './model/spotlight.model'

enum OrderBy {
sold = 'sold',
total = 'total',
volume = 'volume',
unique = 'unique',
average = 'average',
collections = 'collections',
unique_collectors = 'unique_collectors',
}

enum OrderDirection {
DESC = 'DESC',
ASC = 'ASC',
}

@Resolver()
export class SpotlightResolver {
constructor(private tx: () => Promise<EntityManager>) {}

// TODO: calculate score sold * (unique / total)
@Query(() => [SpotlightEntity])
async spotlightTable(
@Arg('limit', { nullable: true }) limit: number,
@Arg('offset', { nullable: true }) offset: string,
@Arg('limit', { nullable: true, defaultValue: null }) limit: number,
@Arg('offset', { nullable: true, defaultValue: null }) offset: string,
@Arg('orderBy', { nullable: true, defaultValue: 'total' }) orderBy: OrderBy,
@Arg('orderDirection', { nullable: true, defaultValue: 'DESC' }) orderDirection: OrderDirection
): Promise<SpotlightEntity[]> {
const manager = await this.tx()
const result: SpotlightEntity[] = await manager.getRepository(NFTEntity)
.query(`
SELECT
const query = `SELECT
issuer as id, COUNT(distinct collection_id) as collections,
COUNT(distinct meta_id) as unique, AVG(price) as average,
COUNT(*) as total, COUNT(distinct current_owner) as unique_collectors,
SUM(CASE WHEN ne.issuer <> ne.current_owner THEN 1 ELSE 0 END) as sold,
COALESCE(SUM(e.meta::bigint), 0) as volume
FROM nft_entity ne
JOIN event e on e.nft_id = ne.id WHERE e.interaction = 'BUY'
GROUP BY issuer
ORDER BY total DESC
LIMIT $1 OFFSET $2;
`, [limit, offset])
FROM nft_entity ne
JOIN event e on e.nft_id = ne.id WHERE e.interaction = 'BUY'
GROUP BY issuer
ORDER BY ${orderBy} ${orderDirection}
LIMIT $1 OFFSET $2`
const manager = await this.tx()
const result: SpotlightEntity[] = await manager.getRepository(NFTEntity)
.query(query, [limit, offset])

return result
}
Expand Down