Skip to content

Commit

Permalink
fix: make function way more dynamic
Browse files Browse the repository at this point in the history
  • Loading branch information
kilted-andres committed Aug 6, 2024
1 parent 1f88c60 commit ef6f286
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 50 deletions.
74 changes: 46 additions & 28 deletions src/utilities/indexer/queryCTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { logger } from '../logger';

import { matchesGenerator } from './queryFromIndexer';
import { DidNames, wholeBlock } from './fragments';
import { writeQuery } from './writeQuery';

export async function queryCTypes() {
const latestCType = await CTypeModel.findOne({
Expand All @@ -22,34 +23,51 @@ export async function queryCTypes() {
const fromBlock = latestCType ? Number(latestCType.dataValues.block) : 0;

// When modifying this query, first try it out on the https://indexer.kilt.io/ (or dev-indexer) and click on "Prettify"
const writtenQuery = `
query {
cTypes(
filter: { registrationBlock: { id: { greaterThan: "${fromBlock}" } } }
orderBy: REGISTRATION_BLOCK_ID_ASC
# BlockIDs are strings, this means that "42" > "1000"
# Sadly, time_stamps can only be use to order queries of blocks.
) {
totalCount
nodes {
id
author {
...DidNames
}
registrationBlock {
...wholeBlock
}
attestationsCreated
attestationsRevoked
attestationsRemoved
validAttestations
definition
}
}
}
${wholeBlock}
${DidNames}
`;
// const writtenQuery = `
// query {
// cTypes(
// filter: { registrationBlock: { id: { greaterThan: "${fromBlock}" } } }
// orderBy: REGISTRATION_BLOCK_ID_ASC
// # BlockIDs are strings, this means that "42" > "1000"
// # Sadly, time_stamps can only be use to order queries of blocks.
// ) {
// totalCount
// nodes {
// id
// author {
// ...DidNames
// }
// registrationBlock {
// ...wholeBlock
// }
// attestationsCreated
// attestationsRevoked
// attestationsRemoved
// validAttestations
// definition
// }
// }
// }
// ${wholeBlock}
// ${DidNames}
// `;

const fieldsToQuery = [
'id',
'author {...DidNames}',
`registrationBlock {...wholeBlock}`,
'attestationsCreated',
'attestationsRevoked',
'attestationsRemoved',
'validAttestations',
'definition',
];
const writtenQuery = writeQuery({
entity: 'cTypes',
fields: fieldsToQuery,
filter: `{ registrationBlock: { id: { greaterThan: "${fromBlock}" } } }`,
fragments: [wholeBlock, DidNames],
});

type ISO8601DateString = string; // like 2022-02-09T13:09:18.217

Expand Down
5 changes: 3 additions & 2 deletions src/utilities/indexer/queryFromIndexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const { indexer } = configuration;
// import { sleep } from '../sleep';

// const QUERY_INTERVAL_MS = 1000;
export const QUERY_SIZE = 50;

const queryBlocks = `
query {
Expand Down Expand Up @@ -81,7 +82,7 @@ export async function* matchesGenerator(query: string = queryBlocks) {
if (indexer.graphqlEndpoint === 'NONE') {
return;
}
const { totalCount: count, matches } = await queryFromIndexer(query);
const { totalCount, matches } = await queryFromIndexer(query);

// TODO: handle queries that have more than 100 matches

Expand All @@ -91,7 +92,7 @@ export async function* matchesGenerator(query: string = queryBlocks) {
);
}

if (count === 0) {
if (totalCount === 0) {
logger.debug(
`The Indexed Data under "${indexer.graphqlEndpoint}" has no matches for query: ${query}.`,
);
Expand Down
16 changes: 5 additions & 11 deletions src/utilities/indexer/updateAttestationCount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import { logger } from '../logger';
import { matchesGenerator } from './queryFromIndexer';
import { writeQuery } from './writeQuery';

const QUERY_SIZE = 50;

export async function updateAttestationsCount() {
// When modifying this query, first try it out on the https://indexer.kilt.io/ (or dev-indexer) and click on "Prettify"
// const writtenQuery = `
Expand All @@ -26,16 +24,12 @@ export async function updateAttestationsCount() {
// }
// }
// `;
const fields = `cTypeId: id
attestationsCreated
registrationBlockId`;
const writtenQuery = writeQuery(
'cTypes',
const fields = ['cTypeId: id', 'attestationsCreated', 'registrationBlockId'];
const writtenQuery = writeQuery({
entity: 'cTypes',
alias: 'attestationsCount',
fields,
QUERY_SIZE,
0,
'attestationsCount',
);
});

interface QueriedAttestationCount {
cTypeId: ICType['$id'];
Expand Down
36 changes: 27 additions & 9 deletions src/utilities/indexer/writeQuery.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,36 @@
export function writeQuery(
entity: string,
fields: string,
querySize: number,
offset: number,
alias?: string,
) {
import { QUERY_SIZE } from './queryFromIndexer';

export function writeQuery({
entity,
alias,
fields,
querySize = QUERY_SIZE,
offset = 0,
filter,
fragments,
}: {
entity: string;
alias?: string;
fields: string[];
querySize?: number;
offset?: number;
filter?: string;
fragments?: string[];
}) {
// BlockIDs are strings, this means that "42" > "1000"
// Sadly, time_stamps can only be use to order queries of blocks.
// Specially for queries with many matches, it is important to request an order of the results, to avoid duplicates and assure totality.
// It does not matter what the order criterion is as long as it is consistent.
// ID_ASC exists for all entities, so it should work as a general approach.
return `
query {
${alias ? alias + ':' : ''} ${entity}(orderBy: ID_ASC, first: ${querySize}, offset: ${offset}) {
${alias ? alias + ':' : ''} ${entity}(orderBy: ID_ASC, first: ${querySize}, offset: ${offset}, ${filter ? 'filter: ' + filter : ''}) {
totalCount
nodes {
${fields}
${fields.join('\n ')}
}
}
}
${fragments ? fragments.join('\n') : ''}
`;
}

0 comments on commit ef6f286

Please sign in to comment.