Skip to content

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Boldizsar Mezei committed Apr 21, 2024
1 parent 1326528 commit 821ffd0
Show file tree
Hide file tree
Showing 19 changed files with 150 additions and 114 deletions.
3 changes: 2 additions & 1 deletion packages/database/src/pg/impl/pubsub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ const topic = pubSub.topic('onupsert');
export const subscriptions: { [key: string]: Promise<Subscription> } = {};

export const getSubscription = (table: string) => {
const subsName = Math.random().toString().replace('0.', 'subs');
if (!subscriptions[table]) {
const subsName = Math.random().toString().replace('0.', 'subs-onupsert-');
subscriptions[table] = new Promise<Subscription>(async (res) => {
const [subscription] = await topic.createSubscription(subsName, {
filter: `attributes.table="${table}"`,
ackDeadlineSeconds: 600,
expirationPolicy: { ttl: { seconds: 86400 } },
});
subscription.setMaxListeners(0);
res(subscription);
});
}
Expand Down
4 changes: 2 additions & 2 deletions packages/database/src/pg/impl/tables/milestone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ export class MilestoneConverter implements Converter<Milestone, PgMilestone> {
uid: get(milestone, 'uid', ''),
createdOn: milestone.createdOn?.toDate(),
completed: milestone.completed,
completedOn: milestone.completedOn.toDate(),
completedOn: milestone.completedOn?.toDate(),
listenerNodeId: milestone.listenerNodeId,
milestone: milestone.milestone,
milestoneTimestamp: milestone.milestoneTimestamp.toDate(),
milestoneTimestamp: milestone.milestoneTimestamp?.toDate(),
trxConflictCount: milestone.trxConflictCount,
trxFailedCount: milestone.trxFailedCount,
trxValidCount: milestone.trxValidCount,
Expand Down
2 changes: 1 addition & 1 deletion packages/database/src/pg/impl/tables/space.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export class SpaceConverter implements Converter<Space, PgSpace> {
alias_address: space.alias?.address,
alias_aliasId: space.alias?.aliasId,
alias_blockId: space.alias?.blockId,
alias_mintedOn: space.alias?.mintedOn.toDate(),
alias_mintedOn: space.alias?.mintedOn?.toDate(),
alias_mintedBy: space.alias?.mintedBy,
});

Expand Down
6 changes: 3 additions & 3 deletions packages/database/src/pg/impl/tables/stake_reward.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ export class StakeRewardConverter implements Converter<StakeReward, PgStakeRewar
updatedOn: sr.updatedOn?.toDate(),
createdBy: sr.createdBy,

startDate: sr.startDate.toDate(),
endDate: sr.endDate.toDate(),
tokenVestingDate: sr.tokenVestingDate.toDate(),
startDate: sr.startDate?.toDate(),
endDate: sr.endDate?.toDate(),
tokenVestingDate: sr.tokenVestingDate?.toDate(),
tokensToDistribute: sr.tokensToDistribute,
token: sr.token,
totalStaked: sr.totalStaked,
Expand Down
22 changes: 13 additions & 9 deletions packages/database/src/pg/interfaces/document.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { COL, SUB_COL } from '@build-5/interfaces';
import { Message, Subscription } from '@google-cloud/pubsub';
import { Message } from '@google-cloud/pubsub';
import { Knex } from 'knex';
import { get, isArray, isDate, isObject, isUndefined, unset } from 'lodash';
import { getPgData, getSubscription } from '../impl/pubsub';
Expand Down Expand Up @@ -42,7 +42,7 @@ export class IDocument<C, B extends BaseRecord, U extends Update> {

onSnapshot = (
callback: (data: C | undefined) => Promise<void> | void,
_error?: (err: any) => void,
onError?: (err: any) => void,
) => {
const onMemssage = (message: Message) => {
message.ack();
Expand All @@ -51,16 +51,20 @@ export class IDocument<C, B extends BaseRecord, U extends Update> {
callback(this.converter.fromPg(update));
}
};
let subscription: Subscription | undefined = undefined;

this.getQuery().then(async (raw) => {
callback(raw ? this.converter.fromPg(raw) : undefined);
subscription = await getSubscription(this.table);
subscription.on('message', onMemssage);
});
const subsPromise = getSubscription(this.table);

this.getQuery()
.then(async (raw) => {
callback(raw ? this.converter.fromPg(raw) : undefined);
(await subsPromise).on('message', onMemssage);
})
.catch(onError);

return () => {
subscription?.removeListener('message', onMemssage);
subsPromise.then((subs) => {
subs.removeListener('message', onMemssage);
});
};
};

Expand Down
36 changes: 19 additions & 17 deletions packages/database/src/pg/interfaces/query.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { COL, SUB_COL } from '@build-5/interfaces';
import { Message, Subscription } from '@google-cloud/pubsub';
import { Message } from '@google-cloud/pubsub';
import { Knex } from 'knex';
import { get, gt, gte, isEqual, lt, lte, orderBy, take } from 'lodash';
import { getPgData, getSubscription } from '../impl/pubsub';
Expand All @@ -24,7 +24,7 @@ export class IQuery<C, Q extends BaseRecord> {
this.table = getTableName(col);
}

private getQuery = () => {
private getQuery = async () => {
let query = this.con(this.table).select('*');

for (const ins of this.whereIns) {
Expand Down Expand Up @@ -78,7 +78,14 @@ export class IQuery<C, Q extends BaseRecord> {
query = query.orderBy(orderBy.key, orderBy.dir);
}

return query;
const sql = query.toQuery();
const queryPlan = await this.con.raw(`EXPLAIN ${sql}`);
const usesIndex = queryPlan.rows.some((row: any) => row['QUERY PLAN'].includes('Index'));
if (!usesIndex) {
throw new Error('Query requires and index.\n' + sql);

Check failure on line 85 in packages/database/src/pg/interfaces/query.ts

View workflow job for this annotation

GitHub Actions / chunk_5

Expired nft stake cron › Should process expired nft stake

Query requires and index. select * from "nft_stake" where "expiresAt" <= '2024-04-21 12:08:40.058' and "expirationProcessed" = false order by "uid" asc at IQuery.getQuery (../database/src/pg/interfaces/query.ts:85:13) at IQuery.get (../database/src/pg/interfaces/query.ts:92:18) at processExpiredNftStakes (src/cron/nftStake.cron.ts:6:16) at async Promise.all (index 0) at Object.<anonymous> (test/cron/nft-stake.cron.spec.ts:40:5)

Check failure on line 85 in packages/database/src/pg/interfaces/query.ts

View workflow job for this annotation

GitHub Actions / chunk_57

Stamp tangle test › Should create and mint 2 stamps

Query requires and index. select * from "stamp" where "createdBy" = 'rms1qrskn306syamz005q6y3v0622yan40qg7r6sd0ammw3sqgj0u29mj38fxae' order by "uid" asc at IQuery.getQuery (../database/src/pg/interfaces/query.ts:85:13) at IQuery.get (../database/src/pg/interfaces/query.ts:92:18) at test-tangle/stamp-tangle/stamp-tangle_2.spec.ts:26:20 at wait (test/controls/common.ts:90:9) at Object.<anonymous> (test-tangle/stamp-tangle/stamp-tangle_2.spec.ts:25:5)

Check failure on line 85 in packages/database/src/pg/interfaces/query.ts

View workflow job for this annotation

GitHub Actions / chunk_72

Web3 cron test › Should upload collection&nft media on mint

Query requires and index. select * from "token" where "mediaStatus" = 'pending_upload' order by "uid" asc at IQuery.getQuery (../database/src/pg/interfaces/query.ts:85:13) at IQuery.get (../database/src/pg/interfaces/query.ts:92:18) at cleanupPendingUploads (test-tangle/web3/web3_1.spec.ts:117:18) at Object.<anonymous> (test-tangle/web3/web3_1.spec.ts:54:5)

Check failure on line 85 in packages/database/src/pg/interfaces/query.ts

View workflow job for this annotation

GitHub Actions / chunk_72

Web3 cron test › Should upload collection&nft media on mint

Query requires and index. select * from "token" where "mediaStatus" = 'pending_upload' order by "uid" asc at IQuery.getQuery (../database/src/pg/interfaces/query.ts:85:13) at IQuery.get (../database/src/pg/interfaces/query.ts:92:18) at cleanupPendingUploads (test-tangle/web3/web3_1.spec.ts:117:18) at Object.<anonymous> (test-tangle/web3/web3_1.spec.ts:111:5)

Check failure on line 85 in packages/database/src/pg/interfaces/query.ts

View workflow job for this annotation

GitHub Actions / chunk_72

Web3 cron test › Should upload token media on mint

Query requires and index. select * from "token" where "mediaStatus" = 'pending_upload' order by "uid" asc at IQuery.getQuery (../database/src/pg/interfaces/query.ts:85:13) at IQuery.get (../database/src/pg/interfaces/query.ts:92:18) at cleanupPendingUploads (test-tangle/web3/web3_1.spec.ts:117:18) at Object.<anonymous> (test-tangle/web3/web3_1.spec.ts:54:5)

Check failure on line 85 in packages/database/src/pg/interfaces/query.ts

View workflow job for this annotation

GitHub Actions / chunk_72

Web3 cron test › Should upload token media on mint

Query requires and index. select * from "token" where "mediaStatus" = 'pending_upload' order by "uid" asc at IQuery.getQuery (../database/src/pg/interfaces/query.ts:85:13) at IQuery.get (../database/src/pg/interfaces/query.ts:92:18) at cleanupPendingUploads (test-tangle/web3/web3_1.spec.ts:117:18) at Object.<anonymous> (test-tangle/web3/web3_1.spec.ts:111:5)

Check failure on line 85 in packages/database/src/pg/interfaces/query.ts

View workflow job for this annotation

GitHub Actions / chunk_21

Metadata nft › Should mint metada nft then update metadata

Query requires and index. select * from "transaction" where "member" = '0x86e160edba000aee759b0ad9a5d51cec320b8ce6' and "type" = 'METADATA_NFT' order by "uid" asc at IQuery.getQuery (../database/src/pg/interfaces/query.ts:85:13) at IQuery.get (../database/src/pg/interfaces/query.ts:92:18) at test-tangle/metadata-nft/mint-metadata-nft_3.spec.ts:45:20 at wait (test/controls/common.ts:90:9) at Object.<anonymous> (test-tangle/metadata-nft/mint-metadata-nft_3.spec.ts:44:5)

Check failure on line 85 in packages/database/src/pg/interfaces/query.ts

View workflow job for this annotation

GitHub Actions / chunk_2

Award tangle request › Should create with tangle request

Query requires and index. select * from "transaction" where "type" = 'CREDIT_TANGLE_REQUEST' and "member" = '0x9ba88dc246b41dc9649f1e9a4c741bdf9c31b0ad' order by "uid" asc at IQuery.getQuery (../database/src/pg/interfaces/query.ts:85:13) at IQuery.get (../database/src/pg/interfaces/query.ts:92:18) at test-tangle/award-tangle/award-tangle_5.spec.ts:67:20 at wait (test/controls/common.ts:90:9) at Object.<anonymous> (test-tangle/award-tangle/award-tangle_5.spec.ts:66:5)

Check failure on line 85 in packages/database/src/pg/interfaces/query.ts

View workflow job for this annotation

GitHub Actions / chunk_58

Stamp tangle test › Should create and mint stamp with zip file

Query requires and index. select * from "stamp" where "createdBy" = 'rms1qqx60ufh332atagy00p3kf8acwwc2t0cmd5rxrqpwnvll0j4gs2duemlaph' order by "uid" asc at IQuery.getQuery (../database/src/pg/interfaces/query.ts:85:13) at IQuery.get (../database/src/pg/interfaces/query.ts:92:18) at test-tangle/stamp-tangle/stamp-tangle_5.spec.ts:32:20 at wait (test/controls/common.ts:90:9) at Object.<anonymous> (test-tangle/stamp-tangle/stamp-tangle_5.spec.ts:31:5)

Check failure on line 85 in packages/database/src/pg/interfaces/query.ts

View workflow job for this annotation

GitHub Actions / chunk_77

Collection minting › Should return credits when nft deposit order does not receive nft

Query requires and index. select * from "transaction" where "type" = 'CREDIT' and "member" = '0xb198dac83430d335adc2ea17f2ef8f2639784aaf' order by "uid" asc at IQuery.getQuery (../database/src/pg/interfaces/query.ts:85:13) at IQuery.get (../database/src/pg/interfaces/query.ts:92:18) at test-tangle/withdraw-deposit-nft/deposit-withraw-nft_16.spec.ts:37:20 at wait (test/controls/common.ts:90:9) at Object.<anonymous> (test-tangle/withdraw-deposit-nft/deposit-withraw-nft_16.spec.ts:36:5)

Check failure on line 85 in packages/database/src/pg/interfaces/query.ts

View workflow job for this annotation

GitHub Actions / chunk_51

Create proposal via tangle request › Should create proposal with tangle request

Query requires and index. select * from "transaction" where "type" = 'CREDIT_TANGLE_REQUEST' and "member" = '0x8f0a17f1d5951efac94cdd2c6025349c15a258a9' order by "uid" asc at IQuery.getQuery (../database/src/pg/interfaces/query.ts:85:13) at IQuery.get (../database/src/pg/interfaces/query.ts:92:18) at test-tangle/proposal-tangle/Helper.ts:104:20 at wait (test/controls/common.ts:90:9) at Helper.sendCreateProposalRequest (test-tangle/proposal-tangle/Helper.ts:103:5) at Object.<anonymous> (test-tangle/proposal-tangle/proposal.create.spec.ts:15:5)

Check failure on line 85 in packages/database/src/pg/interfaces/query.ts

View workflow job for this annotation

GitHub Actions / chunk_53

Create space › Should create space via tangle request

Query requires and index. select * from "transaction" where "type" = 'CREDIT_TANGLE_REQUEST' and "member" = '0x4e95da9269ad03274c937259f22606bfb0c38768' order by "uid" asc at IQuery.getQuery (../database/src/pg/interfaces/query.ts:85:13) at IQuery.get (../database/src/pg/interfaces/query.ts:92:18) at test-tangle/space-tangle/space.create.spec.ts:44:20 at wait (test/controls/common.ts:90:9) at Object.<anonymous> (test-tangle/space-tangle/space.create.spec.ts:43:5)

Check failure on line 85 in packages/database/src/pg/interfaces/query.ts

View workflow job for this annotation

GitHub Actions / chunk_19

Metadata nft › Should mint metada nft

Query requires and index. select * from "transaction" where "member" = '0x26f1a3510064e393becff466041d18b290a1dad5' and "type" = 'METADATA_NFT' order by "uid" asc at IQuery.getQuery (../database/src/pg/interfaces/query.ts:85:13) at IQuery.get (../database/src/pg/interfaces/query.ts:92:18) at test-tangle/metadata-nft/mint-metadata-nft_1_b.spec.ts:45:20 at wait (test/controls/common.ts:90:9) at Object.<anonymous> (test-tangle/metadata-nft/mint-metadata-nft_1_b.spec.ts:44:5)

Check failure on line 85 in packages/database/src/pg/interfaces/query.ts

View workflow job for this annotation

GitHub Actions / chunk_43

Nft bulk order › Should order 2 nfts

Query requires and index. select * from "transaction" where "member" = '0x0fb044a34360af8edbc4818df4f0cc540a8b097d' and "type" = 'CREDIT_TANGLE_REQUEST' order by "uid" asc at IQuery.getQuery (../database/src/pg/interfaces/query.ts:85:13) at IQuery.get (../database/src/pg/interfaces/query.ts:92:18) at test-tangle/nft-bulk/order.bulk_6.spec.ts:57:20 at wait (test/controls/common.ts:90:9) at Object.<anonymous> (test-tangle/nft-bulk/order.bulk_6.spec.ts:56:5)

Check failure on line 85 in packages/database/src/pg/interfaces/query.ts

View workflow job for this annotation

GitHub Actions / chunk_69

Transaction trigger spec › Should not credit expired output

Query requires and index. select * from "transaction" where "member" = 'rms1qrgnatn84jqp9ja0330pc47fm3hskrg5zydy7hsrad9xq2m3gcf7uz4jklc' order by "uid" asc at IQuery.getQuery (../database/src/pg/interfaces/query.ts:85:13) at IQuery.get (../database/src/pg/interfaces/query.ts:92:18) at test-tangle/transaction-trigger/transaction-trigger_15.spec.ts:37:20 at wait (test/controls/common.ts:90:9) at Object.<anonymous> (test-tangle/transaction-trigger/transaction-trigger_15.spec.ts:36:5)

Check failure on line 85 in packages/database/src/pg/interfaces/query.ts

View workflow job for this annotation

GitHub Actions / chunk_22

Metadata nft › Should mint metada nft

Query requires and index. select * from "transaction" where "member" = '0x81d3e220a046a60bf90757920fca9b8c55e9e5bd' and "type" = 'METADATA_NFT' order by "uid" asc at IQuery.getQuery (../database/src/pg/interfaces/query.ts:85:13) at IQuery.get (../database/src/pg/interfaces/query.ts:92:18) at test-tangle/metadata-nft/mint-metadata-nft_5.spec.ts:45:20 at wait (test/controls/common.ts:90:9) at Object.<anonymous> (test-tangle/metadata-nft/mint-metadata-nft_5.spec.ts:44:5)

Check failure on line 85 in packages/database/src/pg/interfaces/query.ts

View workflow job for this annotation

GitHub Actions / chunk_54

Join space › Should join space via tangle request

Query requires and index. select * from "transaction" where "type" = 'CREDIT_TANGLE_REQUEST' and "member" = '0x6b80c59b980343e57d544c549df2af2c6ab6ec52' order by "uid" asc at IQuery.getQuery (../database/src/pg/interfaces/query.ts:85:13) at IQuery.get (../database/src/pg/interfaces/query.ts:92:18) at test-tangle/space-tangle/space.join.spec.ts:44:20 at wait (test/controls/common.ts:90:9) at Object.<anonymous> (test-tangle/space-tangle/space.join.spec.ts:43:5)

Check failure on line 85 in packages/database/src/pg/interfaces/query.ts

View workflow job for this annotation

GitHub Actions / chunk_52

Create proposal via tangle request › Should vote full

Query requires and index. select * from "transaction" where "type" = 'CREDIT_TANGLE_REQUEST' and "member" = '0xc927acbc1aef71b22ffc1a1f4eaaec6572877fb4' order by "uid" asc at IQuery.getQuery (../database/src/pg/interfaces/query.ts:85:13) at IQuery.get (../database/src/pg/interfaces/query.ts:92:18) at test-tangle/proposal-tangle/Helper.ts:104:20 at wait (test/controls/common.ts:90:9) at Helper.sendCreateProposalRequest (test-tangle/proposal-tangle/Helper.ts:103:5) at Object.<anonymous> (test-tangle/proposal-tangle/proposal.token.voting.spec.ts:27:19)

Check failure on line 85 in packages/database/src/pg/interfaces/query.ts

View workflow job for this annotation

GitHub Actions / chunk_20

Metadata nft › Mint NFT with https request

Query requires and index. select * from "transaction" where "member" = '0xaaf147b09dde8529ca47b63aefe4020e9e55cc54' and "type" = 'METADATA_NFT' order by "uid" asc at IQuery.getQuery (../database/src/pg/interfaces/query.ts:85:13) at IQuery.get (../database/src/pg/interfaces/query.ts:92:18) at test-tangle/metadata-nft/mint-metadata-nft_10.spec.ts:36:20 at wait (test/controls/common.ts:90:9) at Object.<anonymous> (test-tangle/metadata-nft/mint-metadata-nft_10.spec.ts:35:5)

Check failure on line 85 in packages/database/src/pg/interfaces/query.ts

View workflow job for this annotation

GitHub Actions / chunk_1

Award tangle request › Should create and fund award with tangle request after create

Query requires and index. select * from "transaction" where "type" = 'CREDIT_TANGLE_REQUEST' and "member" = '0x5b037b03bec3943a9b48fb6d0341be98190560a5' order by "uid" asc at IQuery.getQuery (../database/src/pg/interfaces/query.ts:85:13) at IQuery.get (../database/src/pg/interfaces/query.ts:92:18) at test-tangle/award-tangle/award-tangle_2.spec.ts:73:22 at wait (test/controls/common.ts:90:9) at test-tangle/award-tangle/award-tangle_2.spec.ts:72:7

Check failure on line 85 in packages/database/src/pg/interfaces/query.ts

View workflow job for this annotation

GitHub Actions / chunk_1

Award tangle request › Should create and fund award with tangle request after create

Query requires and index. select * from "transaction" where "type" = 'CREDIT_TANGLE_REQUEST' and "member" = '0x4ec245b35a57e05efc22b17c8d4759212a186c43' order by "uid" asc at IQuery.getQuery (../database/src/pg/interfaces/query.ts:85:13) at IQuery.get (../database/src/pg/interfaces/query.ts:92:18) at test-tangle/award-tangle/award-tangle_2.spec.ts:73:22 at wait (test/controls/common.ts:90:9) at test-tangle/award-tangle/award-tangle_2.spec.ts:72:7

Check failure on line 85 in packages/database/src/pg/interfaces/query.ts

View workflow job for this annotation

GitHub Actions / chunk_1

Award tangle request › Should create and fund award with new tangle request

Query requires and index. select * from "transaction" where "type" = 'CREDIT_TANGLE_REQUEST' and "member" = '0x9f26fd306b24ea1dbb1d03effdcda6251129328a' order by "uid" asc at IQuery.getQuery (../database/src/pg/interfaces/query.ts:85:13) at IQuery.get (../database/src/pg/interfaces/query.ts:92:18) at test-tangle/award-tangle/award-tangle_2.spec.ts:113:20 at wait (test/controls/common.ts:90:9) at Object.<anonymous> (test-tangle/award-tangle/award-tangle_2.spec.ts:112:5)

Check failure on line 85 in packages/database/src/pg/interfaces/query.ts

View workflow job for this annotation

GitHub Actions / chunk_60

Swap control test › Should create swap with OTR

Query requires and index. select * from "swap" where "createdBy" = 'rms1qzfsjrreq3fag5vxa6j647mqa3xxpefm60kqdyhzhwlwu4jzduzvznhgas2' order by "uid" asc at IQuery.getQuery (../database/src/pg/interfaces/query.ts:85:13) at IQuery.get (../database/src/pg/interfaces/query.ts:92:18) at test-tangle/swap/swap_4.spec.ts:47:20 at wait (test/controls/common.ts:90:9) at Object.<anonymous> (test-tangle/swap/swap_4.spec.ts:46:5)

Check failure on line 85 in packages/database/src/pg/interfaces/query.ts

View workflow job for this annotation

GitHub Actions / chunk_23

Metadata nft › Mint NFT with https request

Query requires and index. select * from "transaction" where "member" = '0xfde1c1db906dff061d7fa5d38ecdd6cc2be2ce5d' and "type" = 'METADATA_NFT' order by "uid" asc at IQuery.getQuery (../database/src/pg/interfaces/query.ts:85:13) at IQuery.get (../database/src/pg/interfaces/query.ts:92:18) at test-tangle/metadata-nft/mint-metadata-nft_8.spec.ts:29:20 at wait (test/controls/common.ts:90:9) at Object.<anonymous> (test-tangle/metadata-nft/mint-metadata-nft_8.spec.ts:28:5)

Check failure on line 85 in packages/database/src/pg/interfaces/query.ts

View workflow job for this annotation

GitHub Actions / chunk_68

Transaction trigger spec › Should retry with same output ids

Query requires and index. select * from "transaction" where "payload_walletReference_confirmed" = false and "payload_walletReference_inProgress" = true and "payload_walletReference_count" <= 5 order by "uid" asc at IQuery.getQuery (../database/src/pg/interfaces/query.ts:85:13) at IQuery.get (../database/src/pg/interfaces/query.ts:92:18) at retryWallet (src/cron/wallet.cron.ts:11:16) at test-tangle/transaction-trigger/transaction-trigger_12.spec.ts:71:22

Check failure on line 85 in packages/database/src/pg/interfaces/query.ts

View workflow job for this annotation

GitHub Actions / chunk_68

Transaction trigger spec › Should retry with same output ids

Query requires and index. select * from "transaction" where "payload_walletReference_confirmed" = false and "payload_walletReference_inProgress" = true and "payload_walletReference_count" <= 5 order by "uid" asc at IQuery.getQuery (../database/src/pg/interfaces/query.ts:85:13) at IQuery.get (../database/src/pg/interfaces/query.ts:92:18) at retryWallet (src/cron/wallet.cron.ts:11:16) at test-tangle/transaction-trigger/transaction-trigger_12.spec.ts:71:22

Check failure on line 85 in packages/database/src/pg/interfaces/query.ts

View workflow job for this annotation

GitHub Actions / chunk_66

Token minting › Should credit

Query requires and index. select * from "transaction" where "type" = 'CREDIT' and "member" = '0x9f07d866fdb81bb012d16497ad8236ddd3973575' order by "uid" asc at IQuery.getQuery (../database/src/pg/interfaces/query.ts:85:13) at IQuery.get (../database/src/pg/interfaces/query.ts:92:18) at test-tangle/token.mint/token.mint_4.spec.ts:52:23 at wait (test/controls/common.ts:90:9) at Object.<anonymous> (test-tangle/token.mint/token.mint_4.spec.ts:51:5)

Check failure on line 85 in packages/database/src/pg/interfaces/query.ts

View workflow job for this annotation

GitHub Actions / chunk_66

Token minting › Should credit

Query requires and index. select * from "transaction" where "type" = 'CREDIT' and "member" = '0xb42089c5c6593420aa6e316f01ea8fc8b199fcb2' order by "uid" asc at IQuery.getQuery (../database/src/pg/interfaces/query.ts:85:13) at IQuery.get (../database/src/pg/interfaces/query.ts:92:18) at test-tangle/token.mint/token.mint_4.spec.ts:89:23 at wait (test/controls/common.ts:90:9) at Object.<anonymous> (test-tangle/token.mint/token.mint_4.spec.ts:88:5)

Check failure on line 85 in packages/database/src/pg/interfaces/query.ts

View workflow job for this annotation

GitHub Actions / chunk_71

Transaction trigger spec › Should run all simple transaction one by one

Query requires and index. select * from "transaction" where "payload_sourceAddress" = 'rms1qpwgmnygvzc3780h8afzsmugzh7le5q9k9wh7knleyst4aelfmkw7c3jun5' order by "uid" asc at IQuery.getQuery (../database/src/pg/interfaces/query.ts:85:13) at IQuery.get (../database/src/pg/interfaces/query.ts:92:18) at test-tangle/transaction-trigger/transaction-trigger_7.spec.ts:65:22 at wait (test/controls/common.ts:90:9) at test-tangle/transaction-trigger/transaction-trigger_7.spec.ts:64:7

Check failure on line 85 in packages/database/src/pg/interfaces/query.ts

View workflow job for this annotation

GitHub Actions / chunk_71

Transaction trigger spec › Should run all simple transaction one by one

Query requires and index. select * from "transaction" where "payload_sourceAddress" = 'rms1qpdujynlewuf0hf3gemumn6gh34nhmpg2tj4egytkvw0f9v7t90tkfhl5uf' order by "uid" asc at IQuery.getQuery (../database/src/pg/interfaces/query.ts:85:13) at IQuery.get (../database/src/pg/interfaces/query.ts:92:18) at test-tangle/transaction-trigger/transaction-trigger_7.spec.ts:65:22 at wait (test/controls/common.ts:90:9) at test-tangle/transaction-trigger/transaction-trigger_7.spec.ts:64:7

Check failure on line 85 in packages/database/src/pg/interfaces/query.ts

View workflow job for this annotation

GitHub Actions / chunk_70

Transaction trigger spec › Should rerun transaction only after RETRY_UNCOFIRMED_PAYMENT_DELAY

Query requires and index. select * from "transaction" where "payload_walletReference_confirmed" = false and "payload_walletReference_inProgress" = true and "payload_walletReference_count" <= 5 order by "uid" asc at IQuery.getQuery (../database/src/pg/interfaces/query.ts:85:13) at IQuery.get (../database/src/pg/interfaces/query.ts:92:18) at retryWallet (src/cron/wallet.cron.ts:11:16) at Object.<anonymous> (test-tangle/transaction-trigger/transaction-trigger_5.spec.ts:61:29)

Check failure on line 85 in packages/database/src/pg/interfaces/query.ts

View workflow job for this annotation

GitHub Actions / chunk_62

Tangle request spec › Should return amount

Query requires and index. select * from "transaction" where "member" = 'rms1qr72epg376e2de6m3qf2vmqywq27uyd7kr9pnkz7v72m2g7fuyayvwjwm2w' and "type" = 'CREDIT_TANGLE_REQUEST' order by "uid" asc at IQuery.getQuery (../database/src/pg/interfaces/query.ts:85:13) at IQuery.get (../database/src/pg/interfaces/query.ts:92:18) at test-tangle/tangleRequest/tangle-request.spec.ts:69:20 at wait (test/controls/common.ts:90:9) at Object.<anonymous> (test-tangle/tangleRequest/tangle-request.spec.ts:68:5)

Check failure on line 85 in packages/database/src/pg/interfaces/query.ts

View workflow job for this annotation

GitHub Actions / chunk_62

Tangle request spec › Should use sender as owner when multiple users

Query requires and index. select * from "token_market" where "owner" = 'rms1qzpk0kxe2t7xtv2wc2443z355nr64ac695lqxap9c6rezlkpjd4tjvatkv2' and "type" = 'buy' order by "uid" asc at IQuery.getQuery (../database/src/pg/interfaces/query.ts:85:13) at IQuery.get (../database/src/pg/interfaces/query.ts:92:18) at test-tangle/tangleRequest/tangle-request.spec.ts:103:20 at wait (test/controls/common.ts:90:9) at Object.<anonymous> (test-tangle/tangleRequest/tangle-request.spec.ts:102:5)

Check failure on line 85 in packages/database/src/pg/interfaces/query.ts

View workflow job for this annotation

GitHub Actions / chunk_62

Tangle request spec › Should process multiple request at the same time

Query requires and index. select * from "token_market" where "owner" = '0xd6ef1afa860ebe17f9683b3bd8f754b4cdee71b0' order by "uid" asc at IQuery.getQuery (../database/src/pg/interfaces/query.ts:85:13) at IQuery.get (../database/src/pg/interfaces/query.ts:92:18) at test-tangle/tangleRequest/tangle-request.spec.ts:145:20 at wait (test/controls/common.ts:90:9) at Object.<anonymous> (test-tangle/tangleRequest/tangle-request.spec.ts:144:5)

Check failure on line 85 in packages/database/src/pg/interfaces/query.ts

View workflow job for this annotation

GitHub Actions / chunk_62

Tangle request spec › Should throw

Query requires and index. select * from "transaction" where "member" = '0x495cf8882d00ff226c79f0debf0ae7ed4cab9065' and "type" = 'CREDIT_TANGLE_REQUEST' order by "uid" asc at IQuery.getQuery (../database/src/pg/interfaces/query.ts:85:13) at IQuery.get (../database/src/pg/interfaces/query.ts:92:18) at test-tangle/tangleRequest/tangle-request.spec.ts:160:20 at wait (test/controls/common.ts:90:9) at Object.<anonymous> (test-tangle/tangleRequest/tangle-request.spec.ts:159:5)

Check failure on line 85 in packages/database/src/pg/interfaces/query.ts

View workflow job for this annotation

GitHub Actions / chunk_76

Collection minting › Should migrate

Query requires and index. select * from "nft" where "owner" = '0x48dc67f5acd6b0d6ee7d367211149007c8290f8c' order by "uid" asc at IQuery.getQuery (../database/src/pg/interfaces/query.ts:85:13) at IQuery.get (../database/src/pg/interfaces/query.ts:92:18) at test-tangle/withdraw-deposit-nft/deposit-withraw-nft_13.spec.ts:53:20 at wait (test/controls/common.ts:90:9) at Object.<anonymous> (test-tangle/withdraw-deposit-nft/deposit-withraw-nft_13.spec.ts:52:5)

Check failure on line 85 in packages/database/src/pg/interfaces/query.ts

View workflow job for this annotation

GitHub Actions / chunk_76

Collection minting › Should migrated collection not minted with alias and claim space

Query requires and index. select * from "nft" where "owner" = '0x38159511f1cda216de05f6958a369185914df4a4' order by "uid" asc at IQuery.getQuery (../database/src/pg/interfaces/query.ts:85:13) at IQuery.get (../database/src/pg/interfaces/query.ts:92:18) at test-tangle/withdraw-deposit-nft/deposit-withraw-nft_13.spec.ts:74:20 at wait (test/controls/common.ts:90:9) at Object.<anonymous> (test-tangle/withdraw-deposit-nft/deposit-withraw-nft_13.spec.ts:73:5)

Check failure on line 85 in packages/database/src/pg/interfaces/query.ts

View workflow job for this annotation

GitHub Actions / chunk_35

Minted toke trading tangle request › Should create purchase

Query requires and index. select * from "token_market" where "owner" = '0x0b6980da466db5d397da21baa246c07b001c6d61' order by "uid" asc at IQuery.getQuery (../database/src/pg/interfaces/query.ts:85:13) at IQuery.get (../database/src/pg/interfaces/query.ts:92:18) at test-tangle/minted-token-trade/minted-token-trade_19.spec.ts:75:20 at wait (test/controls/common.ts:90:9) at Object.<anonymous> (test-tangle/minted-token-trade/minted-token-trade_19.spec.ts:74:5)
}

return await query;
};

get = async (): Promise<C[]> => {
Expand Down Expand Up @@ -147,7 +154,7 @@ export class IQuery<C, Q extends BaseRecord> {
return act;
};

onSnapshot = (callback: (data: C[]) => Promise<void> | void, error?: (err: any) => void) => {
onSnapshot = (callback: (data: C[]) => Promise<void> | void, onError?: (err: any) => void) => {
let pgData: Q[] = [];

const onMemssage = (message: Message) => {
Expand All @@ -161,29 +168,24 @@ export class IQuery<C, Q extends BaseRecord> {
callback(callbackData);
}
} catch (err) {
console.error('onSnapshot-onMemssage', this.table, this.getQuery().toSQL(), err);
error?.({ code: 500, message: 'Internal server error' });
onError?.(err);
}
};

let subscription: Subscription | undefined = undefined;
const subsPromise = getSubscription(this.table);

this.getQuery()
.then(async (raw: Q[]) => {
pgData = raw;

callback(raw.map(this.converter.fromPg));

subscription = await getSubscription(this.table);
subscription.on('message', onMemssage);
(await subsPromise).on('message', onMemssage);
})
.catch((err) => {
console.error('onSnapshot-getQuery', this.table, this.getQuery().toSQL(), err);
error?.({ code: 500, message: 'Internal server error' });
});
.catch(onError);

return () => {
subscription?.removeListener('message', onMemssage);
subsPromise.then((subs) => {
subs.removeListener('message', onMemssage);
});
};
};

Expand All @@ -206,7 +208,7 @@ export class IQuery<C, Q extends BaseRecord> {
return this;
};

startAfter = (data: C) => {
startAfter = (data: C | undefined) => {
this.startAfterData = data;
return this;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
CollectionStatus,
CollectionType,
Network,
Nft,
TRANSACTION_AUTO_EXPIRY_MS,
Transaction,
TransactionPayloadType,
Expand Down Expand Up @@ -137,17 +138,16 @@ const getNftsTotalStorageDeposit = async (
) => {
let storageDeposit = 0;
let nftsToMint = 0;
let lastUid = '';
let lastDoc: Nft | undefined = undefined;
do {
const nfts = await build5Db()
const nfts: Nft[] = await build5Db()
.collection(COL.NFT)
.where('collection', '==', collection.uid)
.where('placeholderNft', '==', false)
.where('uid', '>', lastUid)
.orderBy('uid')
.startAfter(lastDoc)
.limit(500)
.get();
lastUid = last(nfts)?.uid || '';
lastDoc = last(nfts);

const promises = nfts.map(async (nft) => {
if (unsoldMintingOptions === UnsoldMintingOptions.BURN_UNSOLD && !nft.sold) {
Expand All @@ -162,7 +162,7 @@ const getNftsTotalStorageDeposit = async (
const amounts = await Promise.all(promises);
storageDeposit += amounts.reduce((acc, act) => acc + act, 0);
nftsToMint += amounts.filter((a) => a !== 0).length;
} while (lastUid);
} while (lastDoc);

return { storageDeposit, nftsToMint };
};
Expand Down
77 changes: 49 additions & 28 deletions packages/search/src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,40 +28,61 @@ const queryStrToParams = (url: string) => {
export const isHiddenNft = (dataset: Dataset, data?: any) =>
dataset === Dataset.NFT && data?.hidden === true;

export const queryToObservable = <C>(query: IQuery<C, BaseRecord>) =>
new Observable<C[]>((observer) => {
const unsubscribe = query.onSnapshot(
(data) => {
observer.next(data);
},
(error) => {
observer.error(error);
},
);
return () => {
unsubscribe();
};
export const queryToObservable = <C>(query: IQuery<C, BaseRecord>, isLive: boolean) => {
if (isLive) {
return new Observable<C[]>((obs) => {
const unsubscribe = query.onSnapshot(
(data) => {
obs.next(data);
},
(error) => {
obs.error(error);
},
);
return () => {
unsubscribe();
};
});
}

return new Observable<C[]>((obs) => {
query
.get()
.then((r) => obs.next(r))
.catch((err) => obs.error(err));
});
};

export const documentToObservable = <C, B extends BaseRecord, U extends Update>(
doc: IDocument<C, B, U>,
) =>
new Observable<C>((observer) => {
const unsubscribe = doc.onSnapshot(
(data) => {
observer.next(data);
},
(error) => {
observer.error(error);
},
);
return () => {
unsubscribe();
};
isLive: boolean,
): Observable<C> => {
if (isLive) {
return new Observable<C>((observer) => {
const unsubscribe = doc.onSnapshot(
(data) => {
observer.next(data);
},
(error) => {
observer.error(error);
},
);
return () => {
unsubscribe();
};
});
}

return new Observable<C>((obs) => {
doc
.get()
.then((r) => obs.next(r))
.catch((err) => obs.error(err));
});
};

export const getHeadPriceObs = (query: IQuery<TokenPurchase, PgTokenPurchase>) =>
queryToObservable(query).pipe(map((r) => head(r)?.price || 0));
export const getHeadPriceObs = (query: IQuery<TokenPurchase, PgTokenPurchase>, isLive: boolean) =>
queryToObservable(query, isLive).pipe(map((r) => head(r)?.price || 0));

// Used to be 42, changed to 5 to support milestone get and transactions subset
export const minAddressLength = 5;
Expand Down
4 changes: 2 additions & 2 deletions packages/search/src/getAddresses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const getAddressesSchema = Joi.object({
createdAfter: Joi.number().min(0).max(MAX_MILLISECONDS).integer().required(),
});

export const getAddresses = async (url: string) => {
export const getAddresses = async (url: string, isLive: boolean) => {
const body = getQueryParams<GetAddressesRequest>(url, getAddressesSchema);

const query = build5Db()
Expand All @@ -23,7 +23,7 @@ export const getAddresses = async (url: string) => {
.orderBy('createdOn')
.limit(1000);

return queryToObservable(query).pipe(map((mnemonics) => mnemonics.map(sanitizeMnemonic)));
return queryToObservable(query, isLive).pipe(map((mnemonics) => mnemonics.map(sanitizeMnemonic)));
};

const sanitizeMnemonic = (mnemonic: Mnemonic) => ({
Expand Down
12 changes: 6 additions & 6 deletions packages/search/src/getAvgPrice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,20 @@ const getAvgPriceSchema = Joi.object({
.required(),
});

export const getAvgPrice = async (url: string) => {
export const getAvgPrice = async (url: string, isLive: boolean) => {
const body = getQueryParams<GetAvgPriceRequest>(url, getAvgPriceSchema);

const tokens = Array.isArray(body.token) ? body.token : [body.token];
const changes = tokens.map(getAvgLive);
const changes = tokens.map((t) => getAvgLive(t, isLive));
const observable = combineLatest(changes).pipe(map((r) => (r.length === 1 ? r[0] : r)));

return observable;
};

const getAvgLive = (token: string) => {
const lowestPurchaseObs = getHeadPriceObs(purchaseQuery(token, true));
const highestPurchaseObs = getHeadPriceObs(purchaseQuery(token, false));
const lastPurchaseObs = getHeadPriceObs(purchaseQuery(token));
const getAvgLive = (token: string, isLive: boolean) => {
const lowestPurchaseObs = getHeadPriceObs(purchaseQuery(token, true), isLive);
const highestPurchaseObs = getHeadPriceObs(purchaseQuery(token, false), isLive);
const lastPurchaseObs = getHeadPriceObs(purchaseQuery(token), isLive);
return combineLatest([lowestPurchaseObs, highestPurchaseObs, lastPurchaseObs]).pipe(
map(([lowest, highest, last]) => (highest + lowest + last) / 3),
map((avg) => ({ id: token, avg })),
Expand Down
4 changes: 2 additions & 2 deletions packages/search/src/getById.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const getByIdSchema = Joi.object({
subsetId: CommonJoi.uid(false, 7),
});

export const getById = async (url: string) => {
export const getById = async (url: string, isLive: boolean) => {
const body = getQueryParams<GetByIdRequest>(url, getByIdSchema);

const docRef = build5Db().doc(
Expand All @@ -31,7 +31,7 @@ export const getById = async (url: string) => {
body.subsetId,
)! as unknown as IDocument<any, BaseRecord, Update>;

const observable = documentToObservable(docRef).pipe(
const observable = documentToObservable(docRef, isLive).pipe(
map((data) => {
if (!data || isHiddenNft(body.dataset, data)) {
return {};
Expand Down
4 changes: 2 additions & 2 deletions packages/search/src/getMany.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ const getManySchema = Joi.object({
startAfter: CommonJoi.uid(false),
});

export const getMany = async (project: string, url: string) => {
export const getMany = async (project: string, url: string, isLive: boolean) => {
const body = getQueryParams<GetManyRequest>(url, getManySchema);

let query = build5Db()
Expand Down Expand Up @@ -96,7 +96,7 @@ export const getMany = async (project: string, url: string) => {
}
}

const observable = queryToObservable(query).pipe(
const observable = queryToObservable(query, isLive).pipe(
map((snap) => snap.filter((d) => !isEmpty(d)).map((d) => ({ id: d.uid, ...d }))),
);
return observable;
Expand Down
Loading

0 comments on commit 821ffd0

Please sign in to comment.