Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(apps/whale-api): add dumpdb #2172

Merged
merged 11 commits into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ dist
# debug log
lerna-debug.log

# debug dump
dump

coverage

# vscode
Expand Down
5 changes: 3 additions & 2 deletions apps/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,17 @@
"class-transformer": "0.5.1",
"class-validator": "0.14.0",
"cross-fetch": "3.1.5",
"fast-csv": "^4.3.6",
"graphology": "0.25.1",
"graphology-components": "1.5.4",
"graphology-simple-path": "0.1.2",
"joi": "17.8.4",
"lodash": "4.17.21",
"level": "7.0.0",
"level-js": "6.0.0",
"level-packager": "6.0.0",
"leveldown": "6.0.0",
"lexicographic-integer-encoding": "1.0.1",
"lodash": "4.17.21",
"memdown": "6.1.1",
"pg": "8.9.0",
"reflect-metadata": "0.1.13",
Expand All @@ -67,9 +68,9 @@
"@types/cache-manager": "4.0.2",
"@types/cron": "2.0.1",
"@types/express": "4.17.17",
"@types/lodash": "4.14.194",
"@types/level": "^6.0.0",
"@types/levelup": "4.3.1",
"@types/lodash": "4.14.194",
"@types/lossless-json": "1.0.1",
"@types/memdown": "3.0.1",
"@types/node": "16.11.58",
Expand Down
6 changes: 4 additions & 2 deletions apps/whale-api/src/app.configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ export function AppConfiguration (): any {
level: {
location: process.env.WHALE_DATABASE_LEVEL_LOCATION
}
}
},
debug: process.env.WHALE_DEBUG
}
}

Expand All @@ -34,6 +35,7 @@ export function ENV_VALIDATION_SCHEMA (): any {
WHALE_NETWORK: Joi.string().valid('mainnet', 'testnet', 'regtest', 'devnet', 'changi').default('regtest'),
WHALE_DEFID_URL: Joi.string().optional(),
WHALE_DATABASE_PROVIDER: Joi.string().optional(),
WHALE_DATABASE_LEVEL_LOCATION: Joi.string().optional()
WHALE_DATABASE_LEVEL_LOCATION: Joi.string().optional(),
WHALE_DEBUG: Joi.bool().optional()
})
}
2 changes: 2 additions & 0 deletions apps/whale-api/src/module.api/_module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { CacheModule, Global, Module } from '@nestjs/common'
import { RpcController } from './rpc.controller'
import { ActuatorController } from './actuator.controller'
import { TransactionController } from './transaction.controller'
import { DebugController } from './debug.controller'
import { ApiValidationPipe } from './pipes/api.validation.pipe'
import { AddressController } from './address.controller'
import { PoolPairController } from './poolpair.controller'
Expand Down Expand Up @@ -62,6 +63,7 @@ import { GovernanceService } from './governance.service'
LoanController,
LegacyController,
ConsortiumController,
DebugController,
GovernanceController
],
providers: [
Expand Down
18 changes: 18 additions & 0 deletions apps/whale-api/src/module.api/debug.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Controller, ForbiddenException, Get } from '@nestjs/common'
import { Database } from '../module.database/database'

@Controller('/debug')
export class DebugController {
constructor (
protected readonly database: Database
) {
}

@Get('/dumpdb')
async dumpDb (): Promise<void> {
if (process.env.WHALE_DEBUG === undefined) {
throw new ForbiddenException('Debug mode is not enabled')

Check warning on line 14 in apps/whale-api/src/module.api/debug.controller.ts

View check run for this annotation

Codecov / codecov/patch

apps/whale-api/src/module.api/debug.controller.ts#L14

Added line #L14 was not covered by tests
}
await this.database.dump()

Check warning on line 16 in apps/whale-api/src/module.api/debug.controller.ts

View check run for this annotation

Codecov / codecov/patch

apps/whale-api/src/module.api/debug.controller.ts#L16

Added line #L16 was not covered by tests
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@
}
}

export async function shouldDump (database: Database): Promise<void> {
const dump = await database.dump()
expect(dump).toBeTruthy()

Check warning on line 38 in apps/whale-api/src/module.database/database.spec/specifications.ts

View check run for this annotation

Codecov / codecov/patch

apps/whale-api/src/module.database/database.spec/specifications.ts#L37-L38

Added lines #L37 - L38 were not covered by tests
}

export async function shouldGetByPartitionKey (database: Database): Promise<void> {
const index = PartitionMapping.index
for (const data of PARTITIONS) {
Expand Down
2 changes: 2 additions & 0 deletions apps/whale-api/src/module.database/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ export abstract class Database {
mapping: ModelMapping<M>,
id: string
): Promise<void>

abstract dump (): Promise<boolean>
}

export enum SortOrder {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import fs from 'fs'
import path from 'path'
import { format } from '@fast-csv/format'
import sub from 'subleveldown'
import level from 'level'
import { LevelUp } from 'levelup'
Expand All @@ -24,6 +27,40 @@
await this.root.close()
}

async dump (): Promise<boolean> {
let id = 0
const maxSize = 500_000
const dir = path.join(process.cwd(), 'dump')

Check warning on line 33 in apps/whale-api/src/module.database/provider.level/level.database.ts

View check run for this annotation

Codecov / codecov/patch

apps/whale-api/src/module.database/provider.level/level.database.ts#L30-L33

Added lines #L30 - L33 were not covered by tests
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir)

Check warning on line 35 in apps/whale-api/src/module.database/provider.level/level.database.ts

View check run for this annotation

Codecov / codecov/patch

apps/whale-api/src/module.database/provider.level/level.database.ts#L35

Added line #L35 was not covered by tests
}
let items: string[] = []
return await new Promise((resolve, reject) => {
console.time('dump done in: ')
this.root.createReadStream() // query stream
.on('data', function (data) {
items.push(data)

Check warning on line 42 in apps/whale-api/src/module.database/provider.level/level.database.ts

View check run for this annotation

Codecov / codecov/patch

apps/whale-api/src/module.database/provider.level/level.database.ts#L37-L42

Added lines #L37 - L42 were not covered by tests
if (items.length >= maxSize) {
const writer = fs.createWriteStream(`${dir}/dump-${id}.csv`)
const csv = format()
csv.pipe(writer)
items.forEach(i => csv.write(i))
csv.end()

Check warning on line 48 in apps/whale-api/src/module.database/provider.level/level.database.ts

View check run for this annotation

Codecov / codecov/patch

apps/whale-api/src/module.database/provider.level/level.database.ts#L44-L48

Added lines #L44 - L48 were not covered by tests

items = []
id += 1

Check warning on line 51 in apps/whale-api/src/module.database/provider.level/level.database.ts

View check run for this annotation

Codecov / codecov/patch

apps/whale-api/src/module.database/provider.level/level.database.ts#L50-L51

Added lines #L50 - L51 were not covered by tests
}
}).on('error', function (err) {
reject(err)
}).on('close', function () {
reject(new Error('stream closed'))
}).on('end', function () {
resolve(true)
console.timeEnd('dump done in: ')

Check warning on line 59 in apps/whale-api/src/module.database/provider.level/level.database.ts

View check run for this annotation

Codecov / codecov/patch

apps/whale-api/src/module.database/provider.level/level.database.ts#L53-L59

Added lines #L53 - L59 were not covered by tests
})
})
}

/**
* Sub index space for model indexes.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ it('should delete and be deleted', async () => {
await spec.shouldDelete(database)
})

it('should dump', async () => {
await spec.shouldDump(database)
})

it('should query by partition pagination', async () => {
await spec.shouldQueryPartitionPagination(database)
})
Expand Down
Loading