Skip to content

Commit

Permalink
simpler stats (#186)
Browse files Browse the repository at this point in the history
  • Loading branch information
arty-name authored Jul 26, 2023
1 parent 939fb2f commit 30a9229
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 25 deletions.
27 changes: 10 additions & 17 deletions src/models/ctype.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
import type { DidUri, ICType } from '@kiltprotocol/sdk-js';

import {
DataTypes,
HasManyGetAssociationsMixin,
Model,
ModelAttributes,
Sequelize,
} from 'sequelize';
import { DataTypes, Model, ModelAttributes, Sequelize } from 'sequelize';

import { Attestation } from './attestation';
import { Tag } from './tag';
Expand All @@ -26,9 +20,7 @@ export interface CTypeData extends CTypeDataInput {
tags?: Pick<Tag, 'dataValues'>[];
}

export class CType extends Model<CTypeData, CTypeDataInput> {
declare getTags: HasManyGetAssociationsMixin<Tag>;
}
export class CType extends Model<CTypeData, CTypeDataInput> {}

export const CTypeModelDefinition: ModelAttributes = {
id: {
Expand Down Expand Up @@ -83,25 +75,26 @@ CTypeModelDefinition.search = {
},
};

// Cannot be provided as a part of the scope, include it in queries manually
export const groupForAttestationsCount = ['CType.id', 'Attestations.cTypeId'];

export function initCType(sequelize: Sequelize) {
CType.init(CTypeModelDefinition, {
sequelize,

scopes: {
stats: {
subQuery: false,
// Attestation’s attributes array must be empty
include: [{ model: Attestation, attributes: [] }],
attributes: [
// Unfortunately all the CType model’s fields have to be listed explicitly
...Object.keys(CTypeModelDefinition).filter(
(key) => key !== 'search',
),
[
Sequelize.fn('count', Sequelize.col('Attestations.claimHash')),
Sequelize.literal(
`coalesce(
(select count(*)
from "Attestations"
where "Attestations"."cTypeId" = "CType"."id"
group by "CType"."id"),
0)`,
),
'attestationsCount',
],
],
Expand Down
8 changes: 2 additions & 6 deletions src/pages/ctype/[id].astro
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,17 @@
import Layout from '../../layouts/Layout.astro';
import Header from '../../components/Header/Header.astro';
import CTypeDetails from '../../components/CTypeDetails/CTypeDetails.astro';
import { CType, groupForAttestationsCount as group } from '../../models/ctype';
import { CType } from '../../models/ctype';
import { NotFoundResponse } from '../../utilities/NotFoundResponse';
const { id } = Astro.params;
const success = Astro.url.searchParams.has('success');
const cType = await CType.scope('stats').findByPk(id, {
group,
});
const cType = await CType.scope('stats').findByPk(id, { include: 'tags' });
if (!cType) {
return new NotFoundResponse();
}
cType.dataValues.tags = await cType.getTags();
---

<Layout>
Expand Down
3 changes: 1 addition & 2 deletions src/utilities/paginate.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Page } from 'astro';
import type { Attributes, WhereOptions } from 'sequelize';

import { CType, groupForAttestationsCount as group } from '../models/ctype';
import { CType } from '../models/ctype';

export async function paginate(
url: URL,
Expand Down Expand Up @@ -31,7 +31,6 @@ export async function paginate(
limit: size,
order: [['createdAt', 'DESC']],
where,
group,
});

const current = url.toString();
Expand Down

0 comments on commit 30a9229

Please sign in to comment.