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

simpler stats #186

Merged
merged 1 commit into from
Jul 26, 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
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