Skip to content

Activity Log backend and WebUI #110

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

Merged
merged 21 commits into from
Sep 15, 2022
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
71 changes: 71 additions & 0 deletions main/activity-log.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
'use strict'

/** @typedef {import('./typings').Activity} Activity */
/** @typedef {import('./typings').RecordActivityArgs} RecordActivityArgs */

const Store = require('electron-store')
const crypto = require('node:crypto')

const activityLogStore = new Store({
name: 'activity-log'
})

class ActivityLog {
#entries

constructor () {
this.#entries = loadStoredEntries()
}

/**
* @param {RecordActivityArgs} args
* @returns {Activity}
*/
record ({ source, type, message }) {
/** @type {Activity} */
const activity = {
id: crypto.randomUUID(),
timestamp: Date.now(),
source,
type,
message
}
// Freeze the data to prevent ActivityLog users from accidentally changing our store
Object.freeze(activity)

this.#entries.push(activity)

if (this.#entries.length > 100) {
// Delete the oldest activity to keep ActivityLog at constant size
this.#entries.shift()
}
this.#save()
return activity
}

getAllEntries () {
// Clone the array to prevent the caller from accidentally changing our store
return [...this.#entries]
}

reset () {
this.#entries = []
this.#save()
}

#save () {
activityLogStore.set('activities', this.#entries)
}
}

/**
* @returns {Activity[]}
*/
function loadStoredEntries () {
// A workaround to fix false TypeScript errors
return /** @type {any} */(activityLogStore.get('activities', []))
}

module.exports = {
ActivityLog
}
36 changes: 34 additions & 2 deletions main/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,25 @@ const setupUI = require('./ui')
const setupTray = require('./tray')
const setupUpdater = require('./updater')
const saturnNode = require('./saturn-node')
const { setupIpcMain } = require('./ipc')
const { setupIpcMain, ipcMainEvents } = require('./ipc')
const { setupAppMenu } = require('./app-menu')

const { ActivityLog } = require('./activity-log')
const { ipcMain } = require('electron/main')

/** @typedef {import('./typings').Activity} Activity */
/** @typedef {import('./typings').RecordActivityArgs} RecordActivityOptions */

const inTest = (process.env.NODE_ENV === 'test')
const isDev = !app.isPackaged && !inTest

function handleError (/** @type {any} */ err) {
ctx.recordActivity({
source: 'Station',
type: 'error',
message: `Station failed to start: ${err.message || err}`
})

log.error(err)
dialog.showErrorBox('Error occured', err.stack ?? err.message ?? err)
}
Expand All @@ -35,13 +48,30 @@ if (!app.requestSingleInstanceLock() && !inTest) {
app.quit()
}

const activityLog = new ActivityLog()
if (isDev) {
// Do not preserve old Activity entries in development mode
activityLog.reset()
}

/** @type {import('./typings').Context} */
const ctx = {
getAllActivities: () => activityLog.getAllEntries(),

recordActivity: (args) => {
activityLog.record(args)
ipcMain.emit(ipcMainEvents.ACTIVITY_LOGGED, activityLog.getAllEntries())
},

manualCheckForUpdates: () => { throw new Error('never get here') },
showUI: () => { throw new Error('never get here') },
loadWebUIFromDist: serve({ directory: path.resolve(__dirname, '../renderer/dist') })
}

process.on('exit', () => {
ctx.recordActivity({ source: 'Station', type: 'info', message: 'Station stopped.' })
})

async function run () {
try {
await app.whenReady()
Expand All @@ -56,7 +86,9 @@ async function run () {
await setupAppMenu(ctx)
await setupUI(ctx)
await setupUpdater(ctx)
await setupIpcMain()
await setupIpcMain(ctx)

ctx.recordActivity({ source: 'Station', type: 'info', message: 'Station started.' })

await saturnNode.setup(ctx)
} catch (e) {
Expand Down
10 changes: 8 additions & 2 deletions main/ipc.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,19 @@ const { ipcMain } = require('electron')
const saturnNode = require('./saturn-node')
const stationConfig = require('./station-config')

/** @typedef {import('./typings').Context} Context */

const ipcMainEvents = Object.freeze({
ACTIVITY_LOGGED: 'station:activity-logged',

UPDATE_CHECK_STARTED: 'station:update-check:started',
UPDATE_CHECK_FINISHED: 'station:update-check:finished'
})

function setupIpcMain () {
function setupIpcMain (/** @type {Context} */ ctx) {
ipcMain.handle('saturn:isRunning', saturnNode.isRunning)
ipcMain.handle('saturn:isReady', saturnNode.isReady)
ipcMain.handle('saturn:start', saturnNode.start)
ipcMain.handle('saturn:start', _event => saturnNode.start(ctx))
ipcMain.handle('saturn:stop', saturnNode.stop)
ipcMain.handle('saturn:getLog', saturnNode.getLog)
ipcMain.handle('saturn:getWebUrl', saturnNode.getWebUrl)
Expand All @@ -26,6 +30,8 @@ function setupIpcMain () {
ipcMain.handle('station:setOnboardingCompleted', (_event) => stationConfig.setOnboardingCompleted())
ipcMain.handle('station:getUserConsent', stationConfig.getUserConsent)
ipcMain.handle('station:setUserConsent', (_event, consent) => stationConfig.setUserConsent(consent))

ipcMain.handle('station:getAllActivities', (_event, _args) => ctx.getAllActivities())
}

module.exports = {
Expand Down
16 changes: 16 additions & 0 deletions main/preload.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,22 @@
const { contextBridge, ipcRenderer } = require('electron')

contextBridge.exposeInMainWorld('electron', {
getAllActivities: () => ipcRenderer.invoke('station:getAllActivities'),

/**
* @param {(Activity: import('./typings').Activity) => void} callback
*/
onActivityLogged (callback) {
/** @type {(event: import('electron').IpcRendererEvent, ...args: any[]) => void} */
const listener = (_event, activities) => callback(activities)

ipcRenderer.on('station:activity-logged', listener)

return function unsubscribe () {
ipcRenderer.removeListener('station:activity-logged', listener)
}
},

saturnNode: {
start: () => ipcRenderer.invoke('saturn:start'),
stop: () => ipcRenderer.invoke('saturn:stop'),
Expand Down
38 changes: 33 additions & 5 deletions main/saturn-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ const Store = require('electron-store')
const consts = require('./consts')
const configStore = new Store()

/** @typedef {import('./typings').Context} Context */

const saturnBinaryPath = getSaturnBinaryPath()

/** @type {import('execa').ExecaChildProcess | null} */
Expand All @@ -31,7 +33,7 @@ const ConfigKeys = {

let filAddress = /** @type {string | undefined} */ (configStore.get(ConfigKeys.FilAddress))

async function setup (/** @type {import('./typings').Context} */ _ctx) {
async function setup (/** @type {Context} */ ctx) {
console.log('Using Saturn L2 Node binary: %s', saturnBinaryPath)

const stat = await fs.stat(saturnBinaryPath)
Expand All @@ -41,7 +43,7 @@ async function setup (/** @type {import('./typings').Context} */ _ctx) {
if (!childProcess) return
stop()
})
await start()
await start(ctx)
}

function getSaturnBinaryPath () {
Expand All @@ -55,7 +57,7 @@ function getSaturnBinaryPath () {
: path.resolve(__dirname, '..', 'build', 'saturn', `l2node-${process.platform}-${arch}`, name)
}

async function start () {
async function start (/** @type {Context} */ ctx) {
if (!filAddress) {
console.info('Saturn node requires FIL address. Please configure it in the Station UI.')
return
Expand Down Expand Up @@ -88,6 +90,7 @@ async function start () {
stdout.on('data', (/** @type {string} */ data) => {
forwardChunkFromSaturn(data, console.log)
appendToChildLog(data)
handleActivityLogs(ctx, data)
})

stderr.setEncoding('utf-8')
Expand All @@ -111,6 +114,8 @@ async function start () {
console.log('Saturn node is up and ready (Web URL: %s)', webUrl)
ready = true
stdout.off('data', readyHandler)

ctx.recordActivity({ source: 'Saturn', type: 'info', message: 'Saturn module started.' })
resolve()
}
}
Expand All @@ -131,6 +136,7 @@ async function start () {
const msg = `Saturn node exited ${reason}`
console.log(msg)
appendToChildLog(msg)
ctx.recordActivity({ source: 'Saturn', type: 'info', message: msg })

ready = false
})
Expand All @@ -141,9 +147,11 @@ async function start () {
setTimeout(500)
])
} catch (err) {
const msg = err instanceof Error ? err.message : '' + err
appendToChildLog(`Cannot start Saturn node: ${msg}`)
const errorMsg = err instanceof Error ? err.message : '' + err
const message = `Cannot start Saturn node: ${errorMsg}`
appendToChildLog(message)
console.error('Cannot start Saturn node:', err)
ctx.recordActivity({ source: 'Saturn', type: 'error', message })
}
}

Expand Down Expand Up @@ -211,6 +219,26 @@ function appendToChildLog (text) {
.join('')
}

/**
* @param {Context} ctx
* @param {string} text
*/
function handleActivityLogs (ctx, text) {
text
.trimEnd()
.split(/\n/g)
.forEach(line => {
const m = line.match(/^(INFO|ERROR): (.*)$/)
if (!m) return

ctx.recordActivity({
source: 'Saturn',
type: /** @type {any} */(m[1].toLowerCase()),
message: m[2]
})
})
}

module.exports = {
setup,
start,
Expand Down
80 changes: 80 additions & 0 deletions main/test/activity-log.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
'use strict'

const assert = require('assert').strict
const { ActivityLog } = require('../activity-log')
const { assertTimestampIsCloseToNow, pickProps } = require('./test-helpers')

/** @typedef {import('../typings').RecordActivityArgs} RecordActivityOptions */

describe('ActivityLog', function () {
beforeEach(function () {
return new ActivityLog().reset()
})

it('record activities and assign them timestamp and id ', function () {
const activityLog = new ActivityLog()
const activityCreated = activityLog.record(givenActivity({
source: 'Station',
type: 'info',
message: 'Hello world!'
}))

assert.strictEqual(activityLog.getAllEntries().length, 1)
assert.deepStrictEqual(activityCreated, activityLog.getAllEntries()[0])

const { id, timestamp, ...activity } = activityLog.getAllEntries()[0]
assert.deepStrictEqual(activity, {
source: 'Station',
type: 'info',
message: 'Hello world!'
})

assert.equal(typeof id, 'string')
assertTimestampIsCloseToNow(timestamp, 'activity.timestamp')
})

it('assigns unique ids', function () {
const activityLog = new ActivityLog()
activityLog.record(givenActivity({ message: 'one' }))
activityLog.record(givenActivity({ message: 'two' }))
const [first, second] = activityLog.getAllEntries()
assert(first.id !== second.id, `Expected unique ids. Got the same value: ${first.id}`)
})

it('preserves activities across restarts', function () {
new ActivityLog().record(givenActivity({ message: 'first run' }))
const activityLog = new ActivityLog()
activityLog.record(givenActivity({ message: 'second run' }))
assert.deepStrictEqual(activityLog.getAllEntries().map(it => pickProps(it, 'message')), [
{ message: 'first run' },
{ message: 'second run' }
])
})

it('limits the log to the most recent 50 entries', /** @this {Mocha.Test} */ function () {
this.timeout(10000)

const log = new ActivityLog()
for (let i = 0; i < 110; i++) {
log.record(givenActivity({ message: `activity ${i}` }))
}
const entries = log.getAllEntries()
assert.deepStrictEqual(
[entries.at(0)?.message, entries.at(-1)?.message],
['activity 10', 'activity 109']
)
})
})

/**
* @param {Partial<RecordActivityOptions>} [props]
* @returns {RecordActivityOptions}
*/
function givenActivity (props) {
return {
source: 'Station',
type: 'info',
message: 'some message',
...props
}
}
9 changes: 0 additions & 9 deletions main/test/smoke.test.js

This file was deleted.

Loading