-
Notifications
You must be signed in to change notification settings - Fork 14
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
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
45d9eb0
feat: add ActivityLog backend
bajtos 733f4e2
feat: record ActivityLog entries
bajtos ac0abd9
feat: expose ActivityLog to renderer
bajtos aad1f6e
fixup! address Julian comments
bajtos 780b762
fixup! use String(val) instead of '' + val
bajtos 610e8cf
fixup! remove unused `clone()`
bajtos 3e97d6a
fixup! import types via JSDoc
bajtos 4b8ddab
feat: limit ActivityLog size to 100 items
bajtos 0359412
fixup! remove forgotten console log
bajtos 4fd20c4
fixup! add more typedefs
bajtos d6eb214
fixup! extract isDev variable
bajtos 3271350
fixup! fix deregistration of the IPC listener
bajtos 7e2dcad
fixup! rework activity streaming to renderer
bajtos 0aaa890
Merge branch 'main' into feat-activity-log-backend
bajtos d8e010f
Refactor activity log types (#127)
juliangruber cf3e3f1
fix: fix eslint issue in tests
bajtos cde1d46
refactor: with Julian
bajtos cd4157d
feat: renderer integration & component
bajtos 6bff5be
fixup! more tweaks
bajtos 3259737
Merge branch 'main' into feat-activity-log-backend
bajtos 739b4a6
fixup! fix store loading and tests
bajtos File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.