Skip to content

Commit

Permalink
Logs (#510)
Browse files Browse the repository at this point in the history
* Billboard widget enhancements (#509)

* add viewerCount polling

* widget demo showcases siwe

* widgetDemo supports new siwe message

* use dark dm3 logo

* add messages to state

* increase version to 0.2.6

* format vite config

* feat: better logs

---------

Co-authored-by: Alex <[email protected]>
  • Loading branch information
hai-ko and AlexNi245 authored Jun 2, 2023
1 parent 3f0bed7 commit 60c34a5
Show file tree
Hide file tree
Showing 42 changed files with 270 additions and 193 deletions.
2 changes: 1 addition & 1 deletion Procfile
Original file line number Diff line number Diff line change
@@ -1 +1 @@
web: yarn workspace dm3-offchain-resolver-backend start
web: yarn start
6 changes: 3 additions & 3 deletions packages/backend/src/cleanup/cleanUpPendingMessages.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { IDatabase } from '../persistance/getDatabase';
import { log } from 'dm3-lib-shared/dist.backend';
import { logInfo } from 'dm3-lib-shared/dist.backend';

//1 day
const DEFAULT_CLEANUP_INTERVAL = 86400000;

async function onCleanUpPendingMessages(db: IDatabase, ttl: number) {
const now = new Date().getTime();
const expiryDate = now - ttl;
log('[Clean up] Delete expired messages', 'info');
logInfo('[Clean up] Delete expired messages');
await db.deleteExpiredMessages(expiryDate);
}

Expand All @@ -21,7 +21,7 @@ export function startCleanUpPendingMessagesJob(
if (ttl <= 0) {
return;
}
log('[Clean up] Start Clean up job', 'info');
logInfo('[Clean up] Start Clean up job');
return setInterval(() => {
onCleanUpPendingMessages(db, ttl);
}, cleaningInterval);
Expand Down
4 changes: 2 additions & 2 deletions packages/backend/src/config/getDeliveryServiceProperties.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { parse } from 'yaml';
import { existsSync, readFileSync } from 'fs';
import { resolve } from 'path';
import { log, validateSchema } from 'dm3-lib-shared/dist.backend';
import { logInfo, validateSchema } from 'dm3-lib-shared/dist.backend';
import {
schema,
DeliveryServiceProperties,
Expand All @@ -19,7 +19,7 @@ export function getDeliveryServiceProperties(
defaultDeliveryServiceProperties: DeliveryServiceProperties = DEFAULT_DELIVERY_SERVICE_PROPERTIES,
): DeliveryServiceProperties {
if (!existsSync(path)) {
log('Config file not found. Default Config is used', 'info');
logInfo('Config file not found. Default Config is used');
return defaultDeliveryServiceProperties;
}
const yamlString = readFileSync(path, { encoding: 'utf-8' });
Expand Down
8 changes: 6 additions & 2 deletions packages/backend/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { getDatabase } from './persistance/getDatabase';
import Profile from './profile';
import RpcProxy from './rpc/rpc-proxy';
import Storage from './storage';
import { log } from 'dm3-lib-shared/dist.backend';
import { logInfo } from 'dm3-lib-shared/dist.backend';

import {
errorHandler,
Expand Down Expand Up @@ -83,5 +83,9 @@ app.use(express.static(path.join(__dirname, '../../web/build')));
const port = process.env.PORT || '8080';

server.listen(port, () => {
log('[Server] listening at port ' + port + ' and dir ' + __dirname, 'info');
logInfo({
text: '[Server] listening',
port,
dir: __dirname,
});
});
2 changes: 1 addition & 1 deletion packages/backend/src/messaging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
checkToken,
incomingMessage,
} from 'dm3-lib-delivery/dist.backend';
import { log } from 'dm3-lib-shared';

const pendingMessageSchema = {
type: 'object',
properties: {
Expand Down
11 changes: 7 additions & 4 deletions packages/backend/src/rpc/methods/handleSubmitMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { validateSchema } from 'dm3-lib-shared/dist.backend';
import 'dotenv/config';
import express from 'express';
import { WithLocals } from '../../types';
import { log } from 'dm3-lib-shared';
import { logError } from 'dm3-lib-shared';

export async function handleSubmitMessage(
req: express.Request & { app: WithLocals },
Expand All @@ -17,12 +17,12 @@ export async function handleSubmitMessage(
const envelop = JSON.parse(stringifiedEnvelop);

if (!token) {
log('Auth token missing', 'error');
logError('Auth token missing');
return res.status(400).send('Auth token missing');
}

if (!envelop) {
log('Envelop missing', 'error');
logError('Envelop missing');

return res.status(400).send('Envelop missing');
}
Expand Down Expand Up @@ -62,7 +62,10 @@ export async function handleSubmitMessage(
method: 'RPC SUBMIT MESSAGE',
error,
});
log('[handleSubmitMessage]' + error, 'error');
logError({
text: '[handleSubmitMessage]',
error,
});

return res.status(400).send();
}
Expand Down
4 changes: 4 additions & 0 deletions packages/billboard-client/src/api/external/getExternalApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { getViewerCountHandler } from './handler/getViewerCount';
import { getMessagesHandler } from './handler/getMessages';
import { IDatabase } from '../../persitance/getDatabase';
import { IViewerService } from '../../service/viewerService/IViewerService';
import { logDebug, logInfo } from 'dm3-lib-shared';

export function getExternalApi(db: IDatabase, viewerService: IViewerService) {
const app = express();
Expand All @@ -13,6 +14,9 @@ export function getExternalApi(db: IDatabase, viewerService: IViewerService) {
app.post('/rpc', async (req: express.Request, res: express.Response) => {
const { method, params } = req.body;

logInfo({ msg: '/rpc', method });
logDebug({ msg: '/rpc', method, params });

//Create Json Rpc request Id
const id = v4();

Expand Down
10 changes: 7 additions & 3 deletions packages/billboard-client/src/api/internal/rest/getChallenge.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import axios from 'axios';
import { normalizeEnsName } from 'dm3-lib-profile';
import { log } from 'dm3-lib-shared';
import { logError } from 'dm3-lib-shared';

export async function getChallenge(
dsUrl: string,
Expand All @@ -11,8 +11,12 @@ export async function getChallenge(
try {
const { data } = await axios.get(url);
return data.challenge;
} catch (e) {
log("can't get challenge from ds " + dsUrl, 'error');
} catch (error) {
logError({
text: "can't get challenge from ds",
error,
dsUrl,
});
return null;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import axios, { AxiosResponse } from 'axios';
import { EncryptionEnvelop } from 'dm3-lib-messaging';
import { log } from 'dm3-lib-shared';
import { logError } from 'dm3-lib-shared';

/**
* Retrieves incoming messages for a specific ENS name.
Expand All @@ -24,13 +24,12 @@ export async function getIncomingMessages(
);

return response.data;
} catch (e) {
log(
`Failed to retrieve incoming messages for ds ${dsUrl}` +
JSON.stringify(e),

'error',
);
} catch (error) {
logError({
text: `Failed to retrieve incoming messages for ds`,
dsUrl,
error,
});
return null;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import axios from 'axios';
import { normalizeEnsName } from 'dm3-lib-profile';
import { log } from 'dm3-lib-shared';
import { logError } from 'dm3-lib-shared';

export async function getNewToken(
dsUrl: string,
Expand All @@ -11,8 +11,11 @@ export async function getNewToken(
try {
const { data } = await axios.post(url, { signature });
return data.token;
} catch (e) {
log("can't get new token from ds: " + JSON.stringify(e), 'error');
} catch (error) {
logError({
text: `can't get new token from ds`,
error,
});
return null;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import axios from 'axios';
import { SignedUserProfile, normalizeEnsName } from 'dm3-lib-profile';
import { log } from 'dm3-lib-shared';
import { logError } from 'dm3-lib-shared';

export async function submitUserProfile(
dsUrl: string,
Expand All @@ -12,11 +12,8 @@ export async function submitUserProfile(
try {
const { data } = await axios.post(url, signedUserProfile);
return data;
} catch (e) {
log(
"can't submit userProfile to ds " + dsUrl + ' ' + JSON.stringify(e),
'error',
);
} catch (error) {
logError({ text: "can't submit userProfile to ds ", dsUrl, error });
return null;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { EncryptionEnvelop } from 'dm3-lib-messaging';
import { DeliveryServiceProfile } from 'dm3-lib-profile';
import { log } from 'dm3-lib-shared';
import { logError, logInfo } from 'dm3-lib-shared';

import { io, Socket } from 'socket.io-client';

Expand All @@ -21,23 +21,29 @@ export const getDeliveryServiceWSClient = (
},
});
c.on('connect', () => {
log(`Connected to Delivery Service ${ds.url}`, 'info');
logInfo({
text: `Connected to Delivery Service`,
dsUrl: ds.url,
});
res(c);
});
c.on('connect_error', (err: any) => {
log(
`Connection error to Delivery Service ${ds.url} ` +
JSON.stringify(err),
'error',
);
c.on('connect_error', (error: any) => {
logError({
text: `Connection error to Delivery Service`,
dsUrl: ds.url,
error,
});

rej(err);
rej(error);
});
});
log(`Register listener`, 'info');
logInfo(`Register listener`);
//register listners
client.on('disconnect', () => {
log(`disconnected from deliveryService ${ds.url}`, 'info');
logInfo({
text: `disconnected from deliveryService `,
dsUrl: ds.url,
});
});
client.on('message', onMessage);

Expand Down
20 changes: 13 additions & 7 deletions packages/billboard-client/src/getBillboardClientApp.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import bodyParser from 'body-parser';
import { log } from 'dm3-lib-shared';
import { logDebug, logInfo } from 'dm3-lib-shared';
import { ethers } from 'ethers';
import express from 'express';
import http from 'http';
Expand Down Expand Up @@ -29,7 +29,10 @@ export const getBillboardClientApp = async (

//Readng the ENV config file
const config = ConfigService().readConfigFromEnv();
log('[getBillboardClientApp] config ' + JSON.stringify(config), 'debug');
logDebug({
text: '[getBillboardClientApp] config ',
config,
});

//Each Ens name provided in the config file is an billboard instance
//Right now we're using one profile and hence one private key for all billboards
Expand All @@ -38,10 +41,10 @@ export const getBillboardClientApp = async (
ensName,
privateKey: config.privateKey,
}));
log(
'[getBillboardClientApp] billboards ' + JSON.stringify(billboards),
'debug',
);
logDebug({
text: '[getBillboardClientApp] billboards',
billboards,
});

//Register services
const viewerService = await ViewerService(httpServer);
Expand All @@ -58,7 +61,10 @@ export const getBillboardClientApp = async (
app.use(getExternalApi(db, viewerService));

httpServer.listen(port, () => {
log('billboard client listening at port ' + port, 'info');
logInfo({
text: 'billboard client listening at port ',
port,
});
});

//In oder to finish the test everything has to be cleaned up
Expand Down
25 changes: 16 additions & 9 deletions packages/billboard-client/src/service/dsManager/DsManagerImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
ProfileKeys,
SignedUserProfile,
} from 'dm3-lib-profile';
import { log } from 'dm3-lib-shared';
import { logDebug, logError, logInfo } from 'dm3-lib-shared';
import { ethers } from 'ethers';
import { Socket } from 'socket.io-client';
import { IDatabase } from '../../persitance/getDatabase';
Expand Down Expand Up @@ -56,7 +56,7 @@ Initializes the connection to delivery services.
@returns A promise that resolves when the connection initialization is complete.
*/
async function connect() {
log('Start to initialize connection to delivery services', 'info');
logInfo('Start to initialize connection to delivery services');
//Get all delivery service profiles
const billboardsWithProfile = await getBillboardProfile(
provider,
Expand All @@ -83,7 +83,7 @@ Initializes the connection to delivery services.
authenticatedBillboards,
encryptAndStoreMessage(onMessage),
);
log('Finished delivery service initialization', 'info');
logInfo('Finished delivery service initialization');
}

/**
Expand Down Expand Up @@ -129,10 +129,14 @@ Encrypts and stores a message to redis using the provided billboard's keypairs a
JSON.parse(encryptionEnvelop.message),
),
) as Message;
log(
'decryptedMessage' + JSON.stringify(decryptedMessage),
'debug',
);
logInfo({
msg: 'encryptAndStoreMessage',
metadata: decryptedMessage.metadata,
});
logDebug({
msg: 'encryptAndStoreMessage',
decryptedMessage,
});
broadcastMessage(
billboardWithDsProfile.ensName,
decryptedMessage,
Expand All @@ -141,8 +145,11 @@ Encrypts and stores a message to redis using the provided billboard's keypairs a
billboardWithDsProfile.ensName,
decryptedMessage,
);
} catch (err: any) {
log("Can't decrypt message " + JSON.stringify(err), 'error');
} catch (error: any) {
logError({
text: `Can't decrypt message`,
error,
});
}
};
}
Expand Down
Loading

0 comments on commit 60c34a5

Please sign in to comment.