|
| 1 | +import { globalServiceRegistry } from "@workglow/util"; |
| 2 | +import { |
| 3 | + ENTITY_REPOSITORY_TOKEN, |
| 4 | +} from "../../storage/entity/EntitySchema"; |
| 5 | +import { |
| 6 | + FILING_REPOSITORY_TOKEN, |
| 7 | +} from "../../storage/filing/FilingSchema"; |
| 8 | +import { |
| 9 | + COMPANY_FACTS_REPOSITORY_TOKEN, |
| 10 | +} from "../../storage/facts/CompanyFactsSchema"; |
| 11 | +import { |
| 12 | + PROCESSED_SUBMISSIONS_REPOSITORY_TOKEN, |
| 13 | +} from "../../storage/processing/ProcessedSubmissionsSchema"; |
| 14 | +import { |
| 15 | + PROCESSED_FACTS_REPOSITORY_TOKEN, |
| 16 | +} from "../../storage/processing/ProcessedFactsSchema"; |
| 17 | +import { |
| 18 | + PROCESSED_FILINGS_REPOSITORY_TOKEN, |
| 19 | +} from "../../storage/processing/ProcessedFilingsSchema"; |
| 20 | +import { |
| 21 | + INVESTMENT_OFFERING_REPOSITORY_TOKEN, |
| 22 | +} from "../../storage/investment-offering/InvestmentOfferingSchema"; |
| 23 | +import { |
| 24 | + CROWDFUNDING_REPOSITORY_TOKEN, |
| 25 | +} from "../../storage/portal/CrowdfundingSchema"; |
| 26 | +import { |
| 27 | + PERSON_REPOSITORY_TOKEN, |
| 28 | +} from "../../storage/person/PersonSchema"; |
| 29 | +import { |
| 30 | + ADDRESS_REPOSITORY_TOKEN, |
| 31 | +} from "../../storage/address/AddressSchema"; |
| 32 | +import { |
| 33 | + PHONE_REPOSITORY_TOKEN, |
| 34 | +} from "../../storage/phone/PhoneSchema"; |
| 35 | +import { |
| 36 | + COMPANY_REPOSITORY_TOKEN, |
| 37 | +} from "../../storage/company/CompanySchema"; |
| 38 | +import { |
| 39 | + PORTAL_REPOSITORY_TOKEN, |
| 40 | +} from "../../storage/portal/PortalSchema"; |
| 41 | +import type { ServiceToken } from "@workglow/util"; |
| 42 | + |
| 43 | +export interface DbStatusResult { |
| 44 | + readonly entityCount: number; |
| 45 | + readonly filingCount: number; |
| 46 | + readonly factsCount: number; |
| 47 | + readonly processedSubmissions: number; |
| 48 | + readonly processedFacts: number; |
| 49 | + readonly processedFilings: number; |
| 50 | +} |
| 51 | + |
| 52 | +export interface TableStat { |
| 53 | + readonly table: string; |
| 54 | + readonly rows: number; |
| 55 | +} |
| 56 | + |
| 57 | +async function countRows(token: ServiceToken<{ getAll(): Promise<unknown[]> }>): Promise<number> { |
| 58 | + const repo = globalServiceRegistry.get(token); |
| 59 | + const all = (await repo.getAll()) ?? []; |
| 60 | + return all.length; |
| 61 | +} |
| 62 | + |
| 63 | +export async function getDbStatus(): Promise<DbStatusResult> { |
| 64 | + const [entityCount, filingCount, factsCount, processedSubmissions, processedFacts, processedFilings] = |
| 65 | + await Promise.all([ |
| 66 | + countRows(ENTITY_REPOSITORY_TOKEN as any), |
| 67 | + countRows(FILING_REPOSITORY_TOKEN as any), |
| 68 | + countRows(COMPANY_FACTS_REPOSITORY_TOKEN as any), |
| 69 | + countRows(PROCESSED_SUBMISSIONS_REPOSITORY_TOKEN as any), |
| 70 | + countRows(PROCESSED_FACTS_REPOSITORY_TOKEN as any), |
| 71 | + countRows(PROCESSED_FILINGS_REPOSITORY_TOKEN as any), |
| 72 | + ]); |
| 73 | + |
| 74 | + return { |
| 75 | + entityCount, |
| 76 | + filingCount, |
| 77 | + factsCount, |
| 78 | + processedSubmissions, |
| 79 | + processedFacts, |
| 80 | + processedFilings, |
| 81 | + }; |
| 82 | +} |
| 83 | + |
| 84 | +const TABLE_TOKENS: ReadonlyArray<{ |
| 85 | + readonly table: string; |
| 86 | + readonly token: ServiceToken<{ getAll(): Promise<unknown[]> }>; |
| 87 | +}> = [ |
| 88 | + { table: "entity", token: ENTITY_REPOSITORY_TOKEN as any }, |
| 89 | + { table: "filing", token: FILING_REPOSITORY_TOKEN as any }, |
| 90 | + { table: "company_facts", token: COMPANY_FACTS_REPOSITORY_TOKEN as any }, |
| 91 | + { table: "investment_offering", token: INVESTMENT_OFFERING_REPOSITORY_TOKEN as any }, |
| 92 | + { table: "crowdfunding", token: CROWDFUNDING_REPOSITORY_TOKEN as any }, |
| 93 | + { table: "person", token: PERSON_REPOSITORY_TOKEN as any }, |
| 94 | + { table: "address", token: ADDRESS_REPOSITORY_TOKEN as any }, |
| 95 | + { table: "phone", token: PHONE_REPOSITORY_TOKEN as any }, |
| 96 | + { table: "company", token: COMPANY_REPOSITORY_TOKEN as any }, |
| 97 | + { table: "portal", token: PORTAL_REPOSITORY_TOKEN as any }, |
| 98 | +]; |
| 99 | + |
| 100 | +export async function getDbStats(): Promise<TableStat[]> { |
| 101 | + const results = await Promise.all( |
| 102 | + TABLE_TOKENS.map(async ({ table, token }) => { |
| 103 | + const rows = await countRows(token); |
| 104 | + return { table, rows }; |
| 105 | + }) |
| 106 | + ); |
| 107 | + return results; |
| 108 | +} |
0 commit comments