Skip to content

Commit

Permalink
Merge pull request #10 from soonaverse/exports
Browse files Browse the repository at this point in the history
Fix exports
  • Loading branch information
adamunchained authored Jul 7, 2023
2 parents 6c49dc5 + d1ea541 commit bca7172
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 93 deletions.
11 changes: 11 additions & 0 deletions src/app/@api/space.api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,17 @@ export class SpaceApi extends BaseApi<Space> {
public getMembersWithoutData = (spaceId: string, lastValue?: string) =>
this.spaceMemberRepo.getAll(spaceId, lastValue);

public getAllMembersWithoutData = async (spaceId: string) => {
const members: SpaceMember[] = [];
let actMembers: SpaceMember[] = [];
do {
const last = members[members.length - 1]?.uid;
actMembers = await this.getMembersWithoutData(spaceId, last);
members.push(...actMembers);
} while (members.length === QUERY_MAX_LENGTH);
return members;
};

public listenMembers = (spaceId: string, lastValue?: string) =>
this.spaceMemberRepo.getAllLive(spaceId, lastValue).pipe(switchMap(this.getMembers));

Expand Down
19 changes: 15 additions & 4 deletions src/app/@api/token.api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import {
PublicCollections,
QUERY_MAX_LENGTH,
Token,
TokenDistribution,
Transaction,
Expand Down Expand Up @@ -73,12 +74,22 @@ export class TokenApi extends BaseApi<Token> {
return this.tokenDistributionRepo.getByIdLive(tokenId.toLowerCase(), memberId.toLowerCase());
}

public getDistributions(tokenId?: string): Observable<TokenDistribution[] | undefined> {
public getDistributionsLive = (tokenId?: string, lastValue?: string) =>
tokenId ? this.tokenDistributionRepo.getAllLive(tokenId.toLowerCase(), lastValue) : of([]);

public getAllDistributions = async (tokenId?: string) => {
if (!tokenId) {
return of(undefined);
return [];
}
return this.tokenDistributionRepo.getAllLive(tokenId.toLowerCase());
}
const distributions: TokenDistribution[] = [];
let actDistributions: TokenDistribution[] = [];
do {
const last = distributions[distributions.length - 1]?.uid;
actDistributions = await this.tokenDistributionRepo.getAll(tokenId.toLowerCase(), last);
distributions.push(...actDistributions);
} while (actDistributions.length === QUERY_MAX_LENGTH);
return distributions;
};

public stats(tokenId: string) {
if (!tokenId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { download } from '@core/utils/tools.utils';
import { UntilDestroy, untilDestroyed } from '@ngneat/until-destroy';
import { DataService } from '@pages/token/services/data.service';
import { HelperService } from '@pages/token/services/helper.service';
import { Token } from '@build-5/interfaces';
import { QUERY_MAX_LENGTH, Token, TokenDistribution } from '@build-5/interfaces';
import Papa from 'papaparse';
import { debounceTime } from 'rxjs';

Expand Down Expand Up @@ -46,46 +46,43 @@ export class TokenInfoDescriptionComponent {
return DescriptionItemType;
}

public downloadCurrentDistribution(): void {
this.tokenApi
.getDistributions(this.token?.uid)
.pipe(debounceTime(2500), untilDestroyed(this))
.subscribe((distributions) => {
const fields = [
'',
'ethAddress',
'tokenOwned',
'unclaimedTokens',
'tokenClaimed',
'lockedForSale',
'sold',
'totalBought',
'refundedAmount',
'totalPaid',
'totalDeposit',
];
const csv = Papa.unparse({
fields,
data:
distributions?.map((d) => [
d.uid,
d.tokenOwned,
<any>d.totalUnclaimedAirdrop || 0,
d.tokenClaimed,
d.lockedForSale,
d.sold,
d.totalBought,
d.refundedAmount,
d.totalPaid,
d.totalDeposit,
]) || [],
});
public async downloadCurrentDistribution(): Promise<void> {
const distributions = await this.tokenApi.getAllDistributions(this.token?.uid);
const fields = [
'',
'ethAddress',
'tokenOwned',
'unclaimedTokens',
'tokenClaimed',
'lockedForSale',
'sold',
'totalBought',
'refundedAmount',
'totalPaid',
'totalDeposit',
];

download(
`data:text/csv;charset=utf-8${csv}`,
`soonaverse_${this.token?.symbol}_distribution.csv`,
);
this.cd.markForCheck();
});
const csv = Papa.unparse({
fields,
data:
distributions?.map((d) => [
d.uid,
d.tokenOwned,
d.totalUnclaimedAirdrop || 0,
d.tokenClaimed,
d.lockedForSale,
d.sold,
d.totalBought,
d.refundedAmount,
d.totalPaid,
d.totalDeposit,
]) || [],
});

download(
`data:text/csv;charset=utf-8${csv}`,
`soonaverse_${this.token?.symbol}_distribution.csv`,
);
this.cd.markForCheck();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -135,24 +135,16 @@ export class SpaceAboutComponent implements OnInit, OnDestroy {
}
this.exportingMembers = true;

const data: string[][] = [];
let members: SpaceMember[] = [];
do {
const last = data[data.length - 1]?.[0];
members = await this.spaceApi.getMembersWithoutData(space.uid, last);
data.push(...members.map((m) => [m.uid]));
if (members.length < QUERY_MAX_LENGTH) {
break;
}
} while (members.length);
const members = await this.spaceApi.getAllMembersWithoutData(space.uid);

this.exportingMembers = false;
const fields = ['', 'address'];
const csv = Papa.unparse({ fields, data });
const csv = Papa.unparse({ fields, data: members.map((m) => [m.uid]) });

const filteredSpaceName = space?.name?.toLowerCase().replace(/[^a-zA-Z0-9-_]/g, '');
download(`data:text/csv;charset=utf-8${csv}`, `soonaverse_${filteredSpaceName}_members.csv`);
this.cd.markForCheck();

this.exportingMembers = false;
}

public isSoonSpace(): Observable<boolean> {
Expand Down
62 changes: 27 additions & 35 deletions src/app/pages/space/pages/space/space.page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,9 @@ import {
SOON_SPACE_TEST,
Space,
StakeType,
TokenDistribution,
} from '@build-5/interfaces';
import Papa from 'papaparse';
import { BehaviorSubject, combineLatest, debounceTime, map, Observable, skip } from 'rxjs';
import { BehaviorSubject, combineLatest, map, Observable, skip } from 'rxjs';
import { SpaceApi } from './../../../../@api/space.api';
import { NavigationService } from './../../../../@core/services/navigation/navigation.service';
import { NotificationService } from './../../../../@core/services/notification/notification.service';
Expand Down Expand Up @@ -147,46 +146,39 @@ export class SpacePage implements OnInit, OnDestroy {
);
}

public exportCurrentStakers(token: string): void {
// In progress.
public async exportCurrentStakers(token: string): Promise<void> {
if (this.exportingCurrentStakers) {
return;
}

this.exportingCurrentStakers = true;
this.tokenApi
.getDistributions(token)
.pipe(debounceTime(2500), untilDestroyed(this))
.subscribe((transactions: TokenDistribution[] | undefined) => {
if (!transactions) {
return;
}
const distributions = await this.tokenApi.getAllDistributions(token);

this.exportingCurrentStakers = false;
const fields = [
'',
'memberId',
'tokenStakedDynamic',
'tokenStakedStatic',
'stakedValueDynamic',
'stakedValueStatic',
'totalStakeRewards',
];
const csv = Papa.unparse({
fields,
data: transactions.map((t) => [
t.uid,
t.stakes?.[StakeType.DYNAMIC]?.amount || 0,
t.stakes?.[StakeType.STATIC]?.amount || 0,
t.stakes?.[StakeType.DYNAMIC]?.value || 0,
t.stakes?.[StakeType.STATIC]?.value || 0,
t.stakeRewards || 0,
]),
});
const fields = [
'',
'memberId',
'tokenStakedDynamic',
'tokenStakedStatic',
'stakedValueDynamic',
'stakedValueStatic',
'totalStakeRewards',
];
const csv = Papa.unparse({
fields,
data: distributions.map((t) => [
t.uid,
t.stakes?.[StakeType.DYNAMIC]?.amount || 0,
t.stakes?.[StakeType.STATIC]?.amount || 0,
t.stakes?.[StakeType.DYNAMIC]?.value || 0,
t.stakes?.[StakeType.STATIC]?.value || 0,
t.stakeRewards || 0,
]),
});

download(`data:text/csv;charset=utf-8${csv}`, `soonaverse_${token}_stakers.csv`);
this.cd.markForCheck();
});
download(`data:text/csv;charset=utf-8${csv}`, `soonaverse_${token}_stakers.csv`);
this.cd.markForCheck();

this.exportingCurrentStakers = false;
}

public get bannerUrl$(): Observable<string | undefined> {
Expand Down
2 changes: 1 addition & 1 deletion src/app/pages/token/pages/token/token.page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ export class TokenPage implements OnInit, OnDestroy {
);
this.subscriptions$.push(
this.tokenApi
.getDistributions(t.uid)
.getDistributionsLive(t.uid)
.pipe(debounceTime(2500), untilDestroyed(this))
.subscribe(this.data.distributions$),
);
Expand Down

0 comments on commit bca7172

Please sign in to comment.