Skip to content

Commit

Permalink
all track active proposal count fix
Browse files Browse the repository at this point in the history
  • Loading branch information
KanishkaRajputd committed Apr 26, 2024
2 parents e1330c1 + 8433fcb commit ded76c5
Show file tree
Hide file tree
Showing 47 changed files with 1,412 additions and 179 deletions.
53 changes: 49 additions & 4 deletions cloud-functions/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions cloud-functions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"dependencies": {
"@polkadot/api": "^10.12.4",
"algoliasearch": "^4.17.1",
"axios": "^1.6.8",
"bn.js": "^5.2.1",
"cors": "^2.8.5",
"dayjs": "^1.11.8",
Expand Down
36 changes: 26 additions & 10 deletions cloud-functions/src/trackLevelAnalytics/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import { networkTrackInfo } from './utils/trackInfo';
import { ApiPromise, WsProvider } from '@polkadot/api';
import BN from 'bn.js';
import formatBnBalance from '../utils/formateBnBalance';
import { firestoreDB } from '..';
import { GET_ALL_TRACK_PROPOSALS, GET_TOTAL_VOTES_FOR_PROPOSAL } from '../queries';
import { firestoreDB } from '..';

const ZERO_BN = new BN(0);

Expand All @@ -18,6 +18,7 @@ ACCOUNTS='accounts',
CONVICTIONVOTES = 'convictionVotes',
VOTEAMOUNT = 'voteAmount',
}

interface IVoteType {
lockPeriod: number;
balance: string;
Expand Down Expand Up @@ -193,25 +194,26 @@ const getSupportData = async (data: IDataType, network: string, api: any) => {
};

const trackLevelAnalytics = async () => {
const analyticsData = [];
for (const network of AllNetworks) {
let analyticsData: any = [];
const analyticsDataPromise = AllNetworks.map(async (network) => {
const wsProvider = new WsProvider(getWSProvider(network) as string);
const api = await ApiPromise.create({ provider: wsProvider });
const trackNumbers = Object.entries(networkTrackInfo[network]).map(([, value]) => {
return value.trackId;
});

for (const trackNumber of trackNumbers) {
const trackNumbersPromise = trackNumbers.map(async (trackNumber) => {
const subsquidRes = await fetchSubsquid({
network,
query: GET_ALL_TRACK_PROPOSALS,
variables: {
track_eq: trackNumber
}
});

const proposals = subsquidRes?.['data']?.proposals || [];

for (const proposal of proposals) {
const proposalsPromise = proposals.map(async (proposal: any) => {
const query = GET_TOTAL_VOTES_FOR_PROPOSAL;

const variables: any = {
Expand Down Expand Up @@ -251,7 +253,7 @@ const trackLevelAnalytics = async () => {
referendaIndex: proposal.index,
votes: {
convictionVotes: {
delegationSplitData: getDelegationSplit(referenda, EVoteType.CONVICTIONVOTES ),
delegationSplitData: getDelegationSplit(referenda, EVoteType.CONVICTIONVOTES),
supportData: supportData,
votesSplitData: getVotesSplit(referenda, EVoteType.CONVICTIONVOTES)
},
Expand All @@ -269,9 +271,21 @@ const trackLevelAnalytics = async () => {
}
};
analyticsData.push(payload);
}
});
Promise.allSettled(proposalsPromise);
});
Promise.allSettled(trackNumbersPromise);
});
await Promise.allSettled(analyticsDataPromise);

analyticsData = await Promise.allSettled(analyticsData);

analyticsData = analyticsData.map((item: any) => {
if (item.status =='fulfilled') {
return item?.value;
}
}
});

logger.log(analyticsData, 'analyticsData');
function chunkArray(array: IResponse[], chunkSize: number) {
const chunks = [];
Expand All @@ -286,8 +300,10 @@ const trackLevelAnalytics = async () => {
for (const chunk of chunkedArray) {
const batch = firestoreDB.batch();
for (const item of chunk) {
const activityRef = firestoreDB.collection('networks').doc(item?.network).collection('track_level_analytics').doc(String(item.trackNumber)).collection('votes').doc(String(item?.referendaIndex));
batch.set(activityRef, item?.votes, { merge: true });
if (item?.network && typeof item?.trackNumber=='number' && typeof item?.referendaIndex === 'number' && item?.votes) {
const activityRef = firestoreDB.collection('networks').doc(item?.network).collection('track_level_analytics').doc(String(item.trackNumber)).collection('votes').doc(String(item?.referendaIndex));
batch.set(activityRef, item?.votes, { merge: true });
}
}
try {
await batch.commit();
Expand Down
Loading

0 comments on commit ded76c5

Please sign in to comment.