-
-
Notifications
You must be signed in to change notification settings - Fork 23
Fixes #85 Improvements to session management (Breaking Change) #124
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
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
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 |
---|---|---|
|
@@ -3,7 +3,7 @@ | |
"compilerOptions": { | ||
"lib": [ | ||
"DOM", | ||
"ES2020" | ||
"ES2021" | ||
], | ||
"outDir": "dist", | ||
"rootDir": "src", | ||
|
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 |
---|---|---|
|
@@ -5,6 +5,7 @@ import { ConsoleLog } from "../logging/ConsoleLog.js"; | |
import { NullLog } from "../logging/NullLog.js"; | ||
import { UserInfo } from "../models/data/UserInfo.js"; | ||
import { HeartbeatPlugin } from "../plugins/default/HeartbeatPlugin.js"; | ||
import { SessionIdManagementPlugin } from "../plugins/default/SessionIdManagementPlugin.js"; | ||
import { EventPluginContext } from "../plugins/EventPluginContext.js"; | ||
import { EventPluginManager } from "../plugins/EventPluginManager.js"; | ||
import { IEventPlugin } from "../plugins/IEventPlugin.js"; | ||
|
@@ -428,32 +429,24 @@ export class Configuration { | |
} | ||
|
||
/** | ||
* Set the default user identity for all events. If the heartbeat interval is | ||
* greater than 0 (default: 30000ms), heartbeats will be sent after the first | ||
* event submission. | ||
* Set the default user identity for all events. | ||
*/ | ||
public setUserIdentity(userInfo: UserInfo, heartbeatInterval?: number): void; | ||
public setUserIdentity(identity: string, heartbeatInterval?: number): void; | ||
public setUserIdentity(identity: string, name: string, heartbeatInterval?: number): void; | ||
public setUserIdentity(userInfoOrIdentity: UserInfo | string, nameOrHeartbeatInterval?: string | number, heartbeatInterval: number = 30000): void { | ||
const name: string | undefined = typeof nameOrHeartbeatInterval === "string" ? nameOrHeartbeatInterval : undefined; | ||
public setUserIdentity(userInfo: UserInfo): void; | ||
public setUserIdentity(identity: string): void; | ||
public setUserIdentity(identity: string, name: string): void; | ||
public setUserIdentity(userInfoOrIdentity: UserInfo | string, name?: string): void { | ||
const userInfo: UserInfo = typeof userInfoOrIdentity !== "string" | ||
? userInfoOrIdentity | ||
: <UserInfo>{ identity: userInfoOrIdentity, name }; | ||
|
||
const interval: number = typeof nameOrHeartbeatInterval === "number" ? nameOrHeartbeatInterval : heartbeatInterval; | ||
const plugin = new HeartbeatPlugin(interval); | ||
|
||
const shouldRemove: boolean = !userInfo || (!userInfo.identity && !userInfo.name); | ||
if (shouldRemove) { | ||
this.removePlugin(plugin) | ||
delete this.defaultData[KnownEventDataKeys.UserInfo]; | ||
} else { | ||
this.addPlugin(plugin) | ||
this.defaultData[KnownEventDataKeys.UserInfo] = userInfo; | ||
} | ||
|
||
this.services.log.info(`user identity: ${shouldRemove ? "null" : <string>userInfo.identity} (heartbeat interval: ${interval}ms)`); | ||
this.services.log.info(`user identity: ${shouldRemove ? "null" : <string>userInfo.identity}`); | ||
} | ||
|
||
/** | ||
|
@@ -477,7 +470,39 @@ export class Configuration { | |
* This setting only works in environments that supports persisted storage. | ||
* There is also a performance penalty of extra IO/serialization. | ||
*/ | ||
public usePersistedQueueStorage = false; | ||
public usePersistedQueueStorage: boolean = false; | ||
|
||
/** | ||
* Gets or sets a value indicating whether to automatically send session start, | ||
* session heartbeats and session end events. | ||
*/ | ||
public sessionsEnabled = false; | ||
|
||
/** | ||
* Internal property used to track the current session identifier. | ||
*/ | ||
public currentSessionIdentifier: string | null = null; | ||
niemyjski marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/** | ||
* | ||
* @param sendHeartbeats Controls whether heartbeat events are sent on an interval. | ||
* @param heartbeatInterval The interval at which heartbeats are sent after the last sent event. The default is 1 minutes. | ||
* @param useSessionIdManagement Allows you to manually control the session id. This is only recommended for single user desktop environments. | ||
*/ | ||
public useSessions(sendHeartbeats: boolean = true, heartbeatInterval: number = 60000, useSessionIdManagement: boolean = false) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @ejsmith I'm not sure about useSessionIdManagement, I guess this is the safe default since this can be used on node. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It seems fine. |
||
this.sessionsEnabled = true; | ||
|
||
if (useSessionIdManagement) { | ||
this.addPlugin(new SessionIdManagementPlugin()); | ||
} | ||
|
||
const plugin = new HeartbeatPlugin(heartbeatInterval); | ||
if (sendHeartbeats) { | ||
this.addPlugin(plugin); | ||
} else { | ||
this.removePlugin(plugin); | ||
} | ||
} | ||
|
||
private originalSettings?: Record<string, string>; | ||
|
||
|
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
29 changes: 29 additions & 0 deletions
29
packages/core/src/plugins/default/SessionIdManagementPlugin.ts
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,29 @@ | ||
import { guid } from "../../Utils.js"; | ||
import { EventPluginContext } from "../EventPluginContext.js"; | ||
import { IEventPlugin } from "../IEventPlugin.js"; | ||
|
||
export class SessionIdManagementPlugin implements IEventPlugin { | ||
public priority = 25; | ||
public name = "SessionIdManagementPlugin"; | ||
|
||
public run(context: EventPluginContext): Promise<void> { | ||
const ev = context.event; | ||
const isSessionStart: boolean = ev.type === "session"; | ||
const { config } = context.client; | ||
if (isSessionStart || !config.currentSessionIdentifier) { | ||
config.currentSessionIdentifier = guid().replaceAll("-", ""); | ||
} | ||
|
||
if (isSessionStart) { | ||
ev.reference_id = config.currentSessionIdentifier; | ||
} else { | ||
if (!ev.data) { | ||
ev.data = {}; | ||
} | ||
|
||
ev.data["@ref:session"] = config.currentSessionIdentifier; | ||
} | ||
|
||
return Promise.resolve(); | ||
} | ||
} |
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
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why did you downgrade this one from es2019 to es2017?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because all the other ones were es2017. I'm going to upgrade all of these.