add logging view class for community and use it in dht-node

This commit is contained in:
einhorn_b 2024-01-17 13:30:31 +01:00
parent f4169db9b9
commit 073edb57b5
4 changed files with 64 additions and 3 deletions

View File

@ -0,0 +1,33 @@
import util from 'util'
import { Decimal } from 'decimal.js-light'
export abstract class AbstractLoggingView {
protected bufferStringFormat: BufferEncoding = 'hex'
// This function gets called automatically when JSON.stringify() is called on this class instance
// eslint-disable-next-line @typescript-eslint/no-explicit-any
public abstract toJSON(): any
public toString(): string {
return JSON.stringify(this.toJSON(), null, 2)
}
// called form console.log or log4js logging functions
[util.inspect.custom](): string {
return this.toString()
}
public dateToString(date: Date | undefined | null): string | undefined {
if (date) {
return date.toISOString()
}
return undefined
}
public decimalToString(number: Decimal | undefined | null): string | undefined {
if (number) {
return number.toString()
}
return undefined
}
}

View File

@ -0,0 +1,26 @@
import { Community } from '../entity/Community'
import { AbstractLoggingView } from './AbstractLogging.view'
export class CommunityLoggingView extends AbstractLoggingView {
public constructor(private self: Community) {
super()
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
public toJSON(): any {
return {
id: this.self.id,
foreign: this.self.foreign,
url: this.self.url,
publicKey: this.self.publicKey.toString(this.bufferStringFormat),
communityUuid: this.self.communityUuid,
authenticatedAt: this.dateToString(this.self.authenticatedAt),
name: this.self.name,
description: this.self.description?.substring(0, 24),
creationDate: this.dateToString(this.self.creationDate),
createdAt: this.dateToString(this.self.createdAt),
updatedAt: this.dateToString(this.self.updatedAt),
}
}
}

View File

@ -3,6 +3,7 @@
import { Community as DbCommunity } from '@entity/Community'
import { FederatedCommunity as DbFederatedCommunity } from '@entity/FederatedCommunity'
import DHT from '@hyperswarm/dht'
import { CommunityLoggingView } from '@logging/CommunityLogging.view'
import { v4 as uuidv4 } from 'uuid'
import { CONFIG } from '@/config'
@ -227,7 +228,7 @@ async function writeHomeCommunityEntry(keyPair: KeyPair): Promise<void> {
homeCom.name = CONFIG.COMMUNITY_NAME
homeCom.description = CONFIG.COMMUNITY_DESCRIPTION
await DbCommunity.save(homeCom)
logger.info(`home-community updated successfully:`, homeCom)
logger.info(`home-community updated successfully:`, new CommunityLoggingView(homeCom))
} else {
// insert a new homecommunity entry including a new ID and a new but ensured unique UUID
homeCom = new DbCommunity()
@ -240,7 +241,7 @@ async function writeHomeCommunityEntry(keyPair: KeyPair): Promise<void> {
homeCom.description = CONFIG.COMMUNITY_DESCRIPTION
homeCom.creationDate = new Date()
await DbCommunity.insert(homeCom)
logger.info(`home-community inserted successfully:`, homeCom)
logger.info(`home-community inserted successfully:`, new CommunityLoggingView(homeCom))
}
} catch (err) {
throw new Error(`Federation: Error writing HomeCommunity-Entry: ${err}`)

View File

@ -52,7 +52,8 @@
/* external */
"@typeorm/*": ["../backend/src/typeorm/*", "../../backend/src/typeorm/*"],
"@dbTools/*": ["../database/src/*", "../../database/build/src/*"],
"@entity/*": ["../database/entity/*", "../../database/build/entity/*"]
"@entity/*": ["../database/entity/*", "../../database/build/entity/*"],
"@logging/*": ["../database/logging/*", "../../database/build/logging/*"]
},
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
"typeRoots": ["src/dht_node/@types", "node_modules/@types"], /* List of folders to include type definitions from. */