Skip to content
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

feat: allow configuring crawler statistics #2213

Merged
merged 13 commits into from
Dec 20, 2023
Merged
34 changes: 14 additions & 20 deletions packages/core/src/crawlers/statistics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,17 +159,15 @@ export class Statistics {
this._teardown();
}

async resetStore() {
if (!this.enablePersistence) {
async resetStore(opts?: {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please create a new interface for this

/**
* If true, manually reset KV store entry for the statistics, ignoring the {@apilink StatisticsOptions.enablePersistence} flag
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure about the word "manually" here. If you call this method, its always manual usage, regardless of the options.

*/
force?: boolean;
}) {
if (!this.enablePersistence && opts?.force !== true) {
foxt451 marked this conversation as resolved.
Show resolved Hide resolved
return;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i like to keep one blank line after control statements like if/for/while/etc, this applies to other places in the PR too (e.g. the persistState method), take it as a rule of thumb

Suggested change
}
}

return this.resetStoreForce();
}

/**
* Manually reset KV store entry for the statistics, ignoring the {@apilink StatisticsOptions.enablePersistence} flag
*/
async resetStoreForce() {
if (!this.keyValueStore) {
return;
}
Expand Down Expand Up @@ -300,18 +298,15 @@ export class Statistics {
/**
* Persist internal state to the key value store
*/
async persistState() {
// this might be called before startCapturing was called without using await, should not crash
if (!this.enablePersistence || !this.keyValueStore) {
async persistState(opts?: {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here, you can use the same interface (try to find some generic name that would fit both methods)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, added. I'm not really sure though whether it would be better to add the interface locally to each file or create a shared one

/**
* If true, ignore the {@apilink StatisticsOptions.enablePersistence} flag
*/
force?: boolean;
}) {
if (!this.enablePersistence && opts?.force !== true) {
foxt451 marked this conversation as resolved.
Show resolved Hide resolved
return;
}
return this.persistStateForce();
}

/**
* Same as {@apilink Statistics.persistState}, but ignores the {@apilink StatisticsOptions.enablePersistence} flag
*/
async persistStateForce() {
// this might be called before startCapturing was called without using await, should not crash
if (!this.keyValueStore) {
return;
Expand Down Expand Up @@ -438,7 +433,6 @@ export interface StatisticsOptions {

/**
* Use this flag to disable or enable periodic statistics persistence to key value store.
* You can persist to KV store or reset the record there manually by calling {@apilink Statistics.persistStateForce} or {@apilink Statistics.resetStoreForce}
* @default true
*/
enablePersistence?: boolean;
Expand Down
34 changes: 14 additions & 20 deletions packages/core/src/session_pool/session_pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,6 @@ export interface SessionPoolOptions {

/**
* If set to true, `SessionPool` will periodically persist its state to the key-value store.
* Othewise, {@apilink SessionPool.persistState} and {@apilink SessionPool.resetStore} will have no effect,
* but you will still be able to call their "*Force" counterparts for manual resetting and persisting.
* @default true
*/
enablePersistence?: boolean;
Expand Down Expand Up @@ -309,17 +307,15 @@ export class SessionPool extends EventEmitter {
return this._createSession();
}

async resetStore() {
if (!this.enablePersistence) {
async resetStore(opts?: {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here

/**
* If true, manually reset KV store entry, ignoring the {@apilink SessionPoolOptions.enablePersistence} flag
*/
force?: boolean;
}) {
if (!this.enablePersistence && opts?.force !== true) {
foxt451 marked this conversation as resolved.
Show resolved Hide resolved
return;
}
return this.resetStoreForce();
}

/**
* Manually reset KV store entry for the the session pool state, ignoring the {@apilink SessionPoolOptions.enablePersistence} option.
*/
async resetStoreForce() {
await this.keyValueStore?.setValue(this.persistStateKey, null);
}

Expand All @@ -339,17 +335,15 @@ export class SessionPool extends EventEmitter {
* Persists the current state of the `SessionPool` into the default {@apilink KeyValueStore}.
* The state is persisted automatically in regular intervals.
*/
async persistState(): Promise<void> {
if (!this.enablePersistence) {
async persistState(opts?: {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and here

/**
* If true, ignore the {@apilink SessionPoolOptions.enablePersistence} flag
*/
force?: boolean;
}): Promise<void> {
if (!this.enablePersistence && opts?.force !== true) {
foxt451 marked this conversation as resolved.
Show resolved Hide resolved
return;
}
return this.persistStateForce();
}

/**
* Like {@apilink SessionPool.persistState}, but ignores the {@apilink SessionPoolOptions.enablePersistence} option.
*/
async persistStateForce() {
this.log.debug('Persisting state', {
persistStateKeyValueStoreId: this.persistStateKeyValueStoreId,
persistStateKey: this.persistStateKey,
Expand Down
Loading