Skip to content

Commit

Permalink
fix: dashboard optimize (#46)
Browse files Browse the repository at this point in the history
Co-authored-by: 唐琮霖 <[email protected]>
Co-authored-by: 一个不知名の睡觉高手 <[email protected]>
  • Loading branch information
3 people authored Jun 13, 2023
1 parent 288c942 commit 9360330
Show file tree
Hide file tree
Showing 23 changed files with 419 additions and 136 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ npm-debug.log*
.nyc_output
.test-reports
.coverage
.history

# Dependency directories
node_modules/
Expand All @@ -30,6 +31,7 @@ dist

# IDE - VSCode
.vscode/*
!.vscode/settings.json

# Env files
.env
Expand Down
10 changes: 10 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"deno.enable": true,
"deno.lint": true,
"deno.unstable": true,
"editor.formatOnSave": true,
"editor.defaultFormatter": "denoland.vscode-deno",
"[typescriptreact]": {
"editor.defaultFormatter": "denoland.vscode-deno"
}
}
1 change: 1 addition & 0 deletions CONFIGURATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ The following environment variables can be set:
| SECRET | Long random secret. | abcdefghijklmnopqrstuvwxyz1234567890 |
| RELAY_PORT | Relay's server port | 8008 |
| RELAY_PRIVATE_KEY | Relay's private key in hex | (auto-generated) |
| API_KEY | With access to doshboard (dashborad)| |
| MONGO_URI | MongoDB URI | |
| MONGO_MIN_POOL_SIZE | Min. connections per worker | 0 |
| MONGO_MAX_POOL_SIZE | Max. connections per worker | 3 |
Expand Down
6 changes: 5 additions & 1 deletion deno.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,14 @@
"@isaacs/ttlcache": "npm:@isaacs/[email protected]",
"mongodb": "npm:[email protected]",
"mongoose": "npm:[email protected]",
"mongoose-paginate": "npm:[email protected]",
"mongoose-aggregate-paginate": "npm:[email protected]",
"axios": "npm:[email protected]",
"tor-control-ts": "npm:[email protected]",
"js-yaml": "npm:[email protected]",
"bech32": "npm:[email protected]"
"bech32": "npm:[email protected]",
"underscore": "npm:[email protected]",
"dayjs": "npm:[email protected]"
},
"fmt": {
"files": {
Expand Down
22 changes: 21 additions & 1 deletion deno.lock

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

4 changes: 4 additions & 0 deletions src/@types/api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export interface AmountRow {
_id: string
total: number
}
4 changes: 1 addition & 3 deletions src/@types/invoice.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Buffer } from 'Buffer'
import mongoose from 'mongoose'
import { ObjectId } from 'mongodb'

import { Pubkey } from './base.ts'

Expand Down Expand Up @@ -37,8 +36,7 @@ export interface LnurlInvoice extends Invoice {
}

export interface DBInvoice extends mongoose.Document {
_id: ObjectId
id: string
_id: string
pubkey: Buffer
bolt11: string
amount_requested: bigint
Expand Down
2 changes: 1 addition & 1 deletion src/@types/repositories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export interface IEventRepository {
}

export interface IInvoiceRepository {
findById(id: string): Promise<Invoice | undefined>
findById(invoiceId: string): Promise<Invoice | undefined>
upsert(invoice: Partial<Invoice>): Promise<number>
updateStatus(
invoice: Pick<Invoice, 'id' | 'status'>,
Expand Down
26 changes: 26 additions & 0 deletions src/controllers/api/events-controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { helpers, IController, Request, Response, RouterContext } from '@/@types/controllers.ts'
import { readReplicaEventsModel } from '@/database/models/Events.ts'
import { Sort } from '@/constants/base.ts'
import { toNostrEvent } from '@/utils/event.ts'

export class EventsController implements IController {
public async handleRequest(_: Request, response: Response, ctx: RouterContext) {
const query = helpers.getQuery(ctx)
const { sortField = 'event_created_at', sortValue = 'desc' } = query

const limit = query?.limit ? parseInt(query.limit) : 10
const page = query?.page ? parseInt(query.page) : 1
const sort = { [sortField]: Sort.DESC }
if (['asc', 'desc'].includes(sortValue)) {
if (sortValue === 'asc') {
sort[sortField] = Sort.ASC
}
}

response.body = await readReplicaEventsModel.paginate({ event_kind: 1 }, { sort, limit, page })
.then((result) => ({
...result,
docs: result.docs.map(toNostrEvent),
}))
}
}
2 changes: 1 addition & 1 deletion src/database/DatabaseWatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export class DatabaseWatcher extends EventEmitter {

private metrics?: any

private changeStream: ChangeStream
private changeStream!: ChangeStream

/**
* Last doc timestamp received from a real time event
Expand Down
63 changes: 34 additions & 29 deletions src/database/models/Events.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import mongoose, { FilterQuery } from 'mongoose'
import paginate from 'mongoose-paginate'
import aggregatePaginate from 'mongoose-aggregate-paginate'

import { getMasterDbClient, getReadReplicaDbClient } from '@/database/client.ts'
import { Buffer } from 'Buffer'
Expand All @@ -8,7 +10,7 @@ import { isGenericTagQuery } from '@/utils/filter.ts'
import { Sort } from '@/constants/base.ts'
import { toBuffer } from '@/utils/transform.ts'

const EventSchema = new mongoose.Schema({
const eventSchema = new mongoose.Schema({
event_id: {
type: Buffer,
require: true,
Expand Down Expand Up @@ -49,82 +51,85 @@ const EventSchema = new mongoose.Schema({
expires_at: { type: Number },
})

EventSchema.index({ 'event_id': 1 }, {
eventSchema.index({ 'event_id': 1 }, {
background: true,
unique: true,
})
EventSchema.index({ 'event_pubkey': 1 }, {
eventSchema.index({ 'event_pubkey': 1 }, {
background: true,
})
EventSchema.index({ 'event_kind': 1 }, {
eventSchema.index({ 'event_kind': 1 }, {
background: true,
})
EventSchema.index({ 'event_signature': 1 }, {
eventSchema.index({ 'event_signature': 1 }, {
background: true,
})
EventSchema.index({ 'event_created_at': 1 }, {
eventSchema.index({ 'event_created_at': 1 }, {
background: true,
})
EventSchema.index({ 'event_tags.0.0': 1 }, {
eventSchema.index({ 'event_tags.0.0': 1 }, {
background: true,
sparse: true,
})
EventSchema.index({ 'event_tags.0.1': 1 }, {
eventSchema.index({ 'event_tags.0.1': 1 }, {
background: true,
sparse: true,
})
EventSchema.index({ 'event_tags.0.2': 1 }, {
eventSchema.index({ 'event_tags.0.2': 1 }, {
background: true,
sparse: true,
})
EventSchema.index({ 'event_tags.0.3': 1 }, {
eventSchema.index({ 'event_tags.0.3': 1 }, {
background: true,
sparse: true,
})
EventSchema.index({ 'event_tags.1.0': 1 }, {
eventSchema.index({ 'event_tags.1.0': 1 }, {
background: true,
sparse: true,
})
EventSchema.index({ 'event_tags.1.1': 1 }, {
eventSchema.index({ 'event_tags.1.1': 1 }, {
background: true,
sparse: true,
})
EventSchema.index({ 'event_tags.1.2': 1 }, {
eventSchema.index({ 'event_tags.1.2': 1 }, {
background: true,
sparse: true,
})
EventSchema.index({ 'event_tags.1.3': 1 }, {
eventSchema.index({ 'event_tags.1.3': 1 }, {
background: true,
sparse: true,
})
EventSchema.index({ 'event_tags.2.0': 1 }, {
eventSchema.index({ 'event_tags.2.0': 1 }, {
background: true,
sparse: true,
})
EventSchema.index({ 'event_tags.2.1': 1 }, {
eventSchema.index({ 'event_tags.2.1': 1 }, {
background: true,
sparse: true,
})
EventSchema.index({ 'event_tags.2.2': 1 }, {
eventSchema.index({ 'event_tags.2.2': 1 }, {
background: true,
sparse: true,
})
EventSchema.index({ 'event_tags.2.3': 1 }, {
eventSchema.index({ 'event_tags.2.3': 1 }, {
background: true,
sparse: true,
})
EventSchema.index({ 'remote_address': 1 }, {
eventSchema.index({ 'remote_address': 1 }, {
background: true,
})
EventSchema.index({ 'expires_at': 1 }, {
eventSchema.index({ 'expires_at': 1 }, {
background: true,
sparse: true,
})
EventSchema.index({ 'deleted_at': 1 }, {
eventSchema.index({ 'deleted_at': 1 }, {
background: true,
sparse: true,
})

eventSchema.plugin(paginate)
eventSchema.plugin(aggregatePaginate)

export const buildMongoFilter = (
filters: SubscriptionFilter[],
) => {
Expand Down Expand Up @@ -215,7 +220,7 @@ export const buildMongoFilter = (
}
}

EventSchema.static('findBySubscriptionFilter', function (filters: SubscriptionFilter[], maxLimit: number) {
eventSchema.static('findBySubscriptionFilter', function (filters: SubscriptionFilter[], maxLimit: number) {
const query = buildMongoFilter(filters)
const defaultLimit = 500
let sort = Sort.ASC
Expand All @@ -233,19 +238,19 @@ EventSchema.static('findBySubscriptionFilter', function (filters: SubscriptionFi
return this.find(query).limit(limit).sort({ event_created_at: sort })
})

EventSchema.static('countBySubscriptionFilter', function (filters: SubscriptionFilter[]) {
eventSchema.static('countBySubscriptionFilter', function (filters: SubscriptionFilter[]) {
const query = buildMongoFilter(filters)
return this.countDocuments(query)
})

export const EventsModelName = 'Events'
export const EventsCollectionName = 'events'
export const modelName = 'Events'
export const collectionName = 'events'

export const EventsModel = (dbClient: mongoose.Connection) =>
dbClient.model<DBEvent>(
EventsModelName,
EventSchema,
EventsCollectionName,
dbClient.model<DBEvent, mongoose.PaginateModel<DBEvent>>(
modelName,
eventSchema,
collectionName,
)

export const masterEventsModel = EventsModel(getMasterDbClient())
Expand Down
Loading

0 comments on commit 9360330

Please sign in to comment.