@crawlee/core 3.0.0-alpha.2 → 3.0.0-alpha.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/LICENSE.md +201 -0
- package/{README.md → dist/README.md} +0 -0
- package/dist/autoscaling/autoscaled_pool.d.ts +286 -0
- package/dist/autoscaling/autoscaled_pool.d.ts.map +1 -0
- package/dist/autoscaling/autoscaled_pool.js +624 -0
- package/dist/autoscaling/autoscaled_pool.js.map +1 -0
- package/dist/autoscaling/index.d.ts +4 -0
- package/dist/autoscaling/index.d.ts.map +1 -0
- package/dist/autoscaling/index.js +7 -0
- package/dist/autoscaling/index.js.map +1 -0
- package/dist/autoscaling/snapshotter.d.ts +193 -0
- package/dist/autoscaling/snapshotter.d.ts.map +1 -0
- package/dist/autoscaling/snapshotter.js +377 -0
- package/dist/autoscaling/snapshotter.js.map +1 -0
- package/dist/autoscaling/system_status.d.ts +181 -0
- package/dist/autoscaling/system_status.d.ts.map +1 -0
- package/dist/autoscaling/system_status.js +194 -0
- package/dist/autoscaling/system_status.js.map +1 -0
- package/dist/cache_container.d.ts +18 -0
- package/dist/cache_container.d.ts.map +1 -0
- package/dist/cache_container.js +42 -0
- package/dist/cache_container.js.map +1 -0
- package/dist/configuration.d.ts +150 -0
- package/dist/configuration.d.ts.map +1 -0
- package/dist/configuration.js +284 -0
- package/dist/configuration.js.map +1 -0
- package/dist/constants.d.ts +3 -0
- package/dist/constants.d.ts.map +1 -0
- package/dist/constants.js +6 -0
- package/dist/constants.js.map +1 -0
- package/dist/crawlers/crawler_commons.d.ts +23 -0
- package/dist/crawlers/crawler_commons.d.ts.map +1 -0
- package/dist/crawlers/crawler_commons.js +3 -0
- package/dist/crawlers/crawler_commons.js.map +1 -0
- package/dist/crawlers/crawler_extension.d.ts +11 -0
- package/dist/crawlers/crawler_extension.d.ts.map +1 -0
- package/dist/crawlers/crawler_extension.js +30 -0
- package/dist/crawlers/crawler_extension.js.map +1 -0
- package/dist/crawlers/crawler_utils.d.ts +25 -0
- package/dist/crawlers/crawler_utils.d.ts.map +1 -0
- package/dist/crawlers/crawler_utils.js +72 -0
- package/dist/crawlers/crawler_utils.js.map +1 -0
- package/dist/crawlers/index.d.ts +5 -0
- package/dist/crawlers/index.d.ts.map +1 -0
- package/dist/crawlers/index.js +8 -0
- package/dist/crawlers/index.js.map +1 -0
- package/dist/crawlers/statistics.d.ts +147 -0
- package/dist/crawlers/statistics.d.ts.map +1 -0
- package/dist/crawlers/statistics.js +360 -0
- package/dist/crawlers/statistics.js.map +1 -0
- package/dist/enqueue_links/enqueue_links.d.ts +138 -0
- package/dist/enqueue_links/enqueue_links.d.ts.map +1 -0
- package/dist/enqueue_links/enqueue_links.js +123 -0
- package/dist/enqueue_links/enqueue_links.js.map +1 -0
- package/dist/enqueue_links/index.d.ts +3 -0
- package/dist/enqueue_links/index.d.ts.map +1 -0
- package/dist/enqueue_links/index.js +6 -0
- package/dist/enqueue_links/index.js.map +1 -0
- package/dist/enqueue_links/shared.d.ts +60 -0
- package/dist/enqueue_links/shared.d.ts.map +1 -0
- package/dist/enqueue_links/shared.js +148 -0
- package/dist/enqueue_links/shared.js.map +1 -0
- package/dist/errors.d.ts +8 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +12 -0
- package/dist/errors.js.map +1 -0
- package/dist/events/event_manager.d.ts +41 -0
- package/dist/events/event_manager.d.ts.map +1 -0
- package/dist/events/event_manager.js +103 -0
- package/dist/events/event_manager.js.map +1 -0
- package/dist/events/index.d.ts +3 -0
- package/dist/events/index.d.ts.map +1 -0
- package/dist/events/index.js +6 -0
- package/dist/events/index.js.map +1 -0
- package/dist/events/local_event_manager.d.ts +32 -0
- package/dist/events/local_event_manager.d.ts.map +1 -0
- package/dist/events/local_event_manager.js +114 -0
- package/dist/events/local_event_manager.js.map +1 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +17 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +63 -0
- package/dist/log.d.ts +3 -0
- package/dist/log.d.ts.map +1 -0
- package/dist/log.js +12 -0
- package/dist/log.js.map +1 -0
- package/dist/package.json +82 -0
- package/dist/proxy_configuration.d.ts +181 -0
- package/dist/proxy_configuration.d.ts.map +1 -0
- package/dist/proxy_configuration.js +209 -0
- package/dist/proxy_configuration.js.map +1 -0
- package/dist/request.d.ts +174 -0
- package/dist/request.d.ts.map +1 -0
- package/dist/request.js +270 -0
- package/dist/request.js.map +1 -0
- package/dist/serialization.d.ts +33 -0
- package/dist/serialization.d.ts.map +1 -0
- package/dist/serialization.js +146 -0
- package/dist/serialization.js.map +1 -0
- package/dist/session_pool/errors.d.ts +8 -0
- package/dist/session_pool/errors.d.ts.map +1 -0
- package/dist/session_pool/errors.js +20 -0
- package/dist/session_pool/errors.js.map +1 -0
- package/dist/session_pool/events.d.ts +3 -0
- package/dist/session_pool/events.d.ts.map +1 -0
- package/dist/session_pool/events.js +6 -0
- package/dist/session_pool/events.js.map +1 -0
- package/dist/session_pool/index.d.ts +6 -0
- package/dist/session_pool/index.d.ts.map +1 -0
- package/dist/session_pool/index.js +9 -0
- package/dist/session_pool/index.js.map +1 -0
- package/dist/session_pool/session.d.ts +212 -0
- package/dist/session_pool/session.d.ts.map +1 -0
- package/dist/session_pool/session.js +360 -0
- package/dist/session_pool/session.js.map +1 -0
- package/dist/session_pool/session_pool.d.ts +222 -0
- package/dist/session_pool/session_pool.d.ts.map +1 -0
- package/dist/session_pool/session_pool.js +389 -0
- package/dist/session_pool/session_pool.js.map +1 -0
- package/dist/session_pool/session_utils.d.ts +9 -0
- package/dist/session_pool/session_utils.d.ts.map +1 -0
- package/dist/session_pool/session_utils.js +19 -0
- package/dist/session_pool/session_utils.js.map +1 -0
- package/dist/storages/dataset.d.ts +358 -0
- package/dist/storages/dataset.d.ts.map +1 -0
- package/dist/storages/dataset.js +415 -0
- package/dist/storages/dataset.js.map +1 -0
- package/dist/storages/index.d.ts +7 -0
- package/dist/storages/index.d.ts.map +1 -0
- package/dist/storages/index.js +11 -0
- package/dist/storages/index.js.map +1 -0
- package/dist/storages/key_value_store.d.ts +242 -0
- package/dist/storages/key_value_store.d.ts.map +1 -0
- package/dist/storages/key_value_store.js +319 -0
- package/dist/storages/key_value_store.js.map +1 -0
- package/dist/storages/request_list.d.ts +461 -0
- package/dist/storages/request_list.d.ts.map +1 -0
- package/dist/storages/request_list.js +715 -0
- package/dist/storages/request_list.js.map +1 -0
- package/dist/storages/request_queue.d.ts +312 -0
- package/dist/storages/request_queue.d.ts.map +1 -0
- package/dist/storages/request_queue.js +708 -0
- package/dist/storages/request_queue.js.map +1 -0
- package/dist/storages/storage.d.ts +260 -0
- package/dist/storages/storage.d.ts.map +1 -0
- package/dist/storages/storage.js +3 -0
- package/dist/storages/storage.js.map +1 -0
- package/dist/storages/storage_manager.d.ts +40 -0
- package/dist/storages/storage_manager.d.ts.map +1 -0
- package/dist/storages/storage_manager.js +119 -0
- package/dist/storages/storage_manager.js.map +1 -0
- package/dist/tsconfig.build.tsbuildinfo +1 -0
- package/dist/typedefs.d.ts +12 -0
- package/dist/typedefs.d.ts.map +1 -0
- package/dist/typedefs.js +15 -0
- package/dist/typedefs.js.map +1 -0
- package/dist/validators.d.ts +21 -0
- package/dist/validators.d.ts.map +1 -0
- package/dist/validators.js +26 -0
- package/dist/validators.js.map +1 -0
- package/package.json +11 -10
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import { Configuration } from '../configuration';
|
|
2
|
+
/**
|
|
3
|
+
* @ignore
|
|
4
|
+
*/
|
|
5
|
+
declare class Job {
|
|
6
|
+
private lastRunAt;
|
|
7
|
+
private runs;
|
|
8
|
+
private durationMillis?;
|
|
9
|
+
run(): number;
|
|
10
|
+
finish(): number;
|
|
11
|
+
retryCount(): number;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* The statistics class provides an interface to collecting and logging run
|
|
15
|
+
* statistics for requests.
|
|
16
|
+
*
|
|
17
|
+
* All statistic information is saved on key value store
|
|
18
|
+
* under the key `SDK_CRAWLER_STATISTICS_*`, persists between
|
|
19
|
+
* migrations and abort/resurrect
|
|
20
|
+
*
|
|
21
|
+
* @category Crawlers
|
|
22
|
+
*/
|
|
23
|
+
export declare class Statistics {
|
|
24
|
+
private static id;
|
|
25
|
+
/**
|
|
26
|
+
* Statistic instance id.
|
|
27
|
+
*/
|
|
28
|
+
readonly id: number;
|
|
29
|
+
/**
|
|
30
|
+
* Current statistic state used for doing calculations on {@link Statistics.calculate} calls
|
|
31
|
+
*/
|
|
32
|
+
state: StatisticState;
|
|
33
|
+
/**
|
|
34
|
+
* Contains the current retries histogram. Index 0 means 0 retries, index 2, 2 retries, and so on
|
|
35
|
+
*/
|
|
36
|
+
readonly requestRetryHistogram: number[];
|
|
37
|
+
private keyValueStore;
|
|
38
|
+
private persistStateKey;
|
|
39
|
+
private logIntervalMillis;
|
|
40
|
+
private logMessage;
|
|
41
|
+
private listener;
|
|
42
|
+
private requestsInProgress;
|
|
43
|
+
private readonly log;
|
|
44
|
+
private instanceStart;
|
|
45
|
+
private logInterval;
|
|
46
|
+
private events;
|
|
47
|
+
/**
|
|
48
|
+
* @internal
|
|
49
|
+
*/
|
|
50
|
+
constructor(options?: StatisticsOptions);
|
|
51
|
+
/**
|
|
52
|
+
* Set the current statistic instance to pristine values
|
|
53
|
+
*/
|
|
54
|
+
reset(): void;
|
|
55
|
+
/**
|
|
56
|
+
* Starts a job
|
|
57
|
+
* @ignore
|
|
58
|
+
*/
|
|
59
|
+
startJob(id: number | string): void;
|
|
60
|
+
/**
|
|
61
|
+
* Mark job as finished and sets the state
|
|
62
|
+
* @ignore
|
|
63
|
+
*/
|
|
64
|
+
finishJob(id: number | string): void;
|
|
65
|
+
/**
|
|
66
|
+
* Mark job as failed and sets the state
|
|
67
|
+
* @ignore
|
|
68
|
+
*/
|
|
69
|
+
failJob(id: number | string): void;
|
|
70
|
+
/**
|
|
71
|
+
* Calculate the current statistics
|
|
72
|
+
*/
|
|
73
|
+
calculate(): {
|
|
74
|
+
requestAvgFailedDurationMillis: number;
|
|
75
|
+
requestAvgFinishedDurationMillis: number;
|
|
76
|
+
requestsFinishedPerMinute: number;
|
|
77
|
+
requestsFailedPerMinute: number;
|
|
78
|
+
requestTotalDurationMillis: number;
|
|
79
|
+
requestsTotal: number;
|
|
80
|
+
crawlerRuntimeMillis: number;
|
|
81
|
+
};
|
|
82
|
+
/**
|
|
83
|
+
* Initializes the key value store for persisting the statistics,
|
|
84
|
+
* displaying the current state in predefined intervals
|
|
85
|
+
*/
|
|
86
|
+
startCapturing(): Promise<void>;
|
|
87
|
+
/**
|
|
88
|
+
* Stops logging and remove event listeners, then persist
|
|
89
|
+
*/
|
|
90
|
+
stopCapturing(): Promise<void>;
|
|
91
|
+
protected _saveRetryCountForJob(job: Job): void;
|
|
92
|
+
/**
|
|
93
|
+
* Persist internal state to the key value store
|
|
94
|
+
*/
|
|
95
|
+
persistState(): Promise<void>;
|
|
96
|
+
/**
|
|
97
|
+
* Loads the current statistic from the key value store if any
|
|
98
|
+
*/
|
|
99
|
+
protected _maybeLoadStatistics(): Promise<void>;
|
|
100
|
+
protected _teardown(): void;
|
|
101
|
+
/**
|
|
102
|
+
* Make this class serializable when called with `JSON.stringify(statsInstance)` directly
|
|
103
|
+
* or through `keyValueStore.setValue('KEY', statsInstance)`
|
|
104
|
+
*/
|
|
105
|
+
toJSON(): StatisticPersistedState;
|
|
106
|
+
}
|
|
107
|
+
interface StatisticsOptions {
|
|
108
|
+
logIntervalSecs?: number;
|
|
109
|
+
logMessage?: string;
|
|
110
|
+
config?: Configuration;
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Format of the persisted stats
|
|
114
|
+
*/
|
|
115
|
+
export interface StatisticPersistedState extends Omit<StatisticState, 'statsPersistedAt'> {
|
|
116
|
+
requestRetryHistogram: number[];
|
|
117
|
+
statsId: number;
|
|
118
|
+
requestAvgFailedDurationMillis: number;
|
|
119
|
+
requestAvgFinishedDurationMillis: number;
|
|
120
|
+
requestsFinishedPerMinute: number;
|
|
121
|
+
requestsFailedPerMinute: number;
|
|
122
|
+
requestTotalDurationMillis: number;
|
|
123
|
+
requestsTotal: number;
|
|
124
|
+
crawlerRuntimeMillis: number;
|
|
125
|
+
crawlerLastStartTimestamp: number;
|
|
126
|
+
statsPersistedAt: string;
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Contains the statistics state
|
|
130
|
+
*/
|
|
131
|
+
export interface StatisticState {
|
|
132
|
+
requestsFinished: number;
|
|
133
|
+
requestsFailed: number;
|
|
134
|
+
requestsRetries: number;
|
|
135
|
+
requestsFailedPerMinute: number;
|
|
136
|
+
requestsFinishedPerMinute: number;
|
|
137
|
+
requestMinDurationMillis: number;
|
|
138
|
+
requestMaxDurationMillis: number;
|
|
139
|
+
requestTotalFailedDurationMillis: number;
|
|
140
|
+
requestTotalFinishedDurationMillis: number;
|
|
141
|
+
crawlerStartedAt: Date | string | null;
|
|
142
|
+
crawlerFinishedAt: Date | string | null;
|
|
143
|
+
crawlerRuntimeMillis: number;
|
|
144
|
+
statsPersistedAt: Date | string | null;
|
|
145
|
+
}
|
|
146
|
+
export {};
|
|
147
|
+
//# sourceMappingURL=statistics.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"statistics.d.ts","sourceRoot":"","sources":["../../src/crawlers/statistics.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AAEjD;;GAEG;AACH,cAAM,GAAG;IACL,OAAO,CAAC,SAAS,CAAuB;IACxC,OAAO,CAAC,IAAI,CAAK;IACjB,OAAO,CAAC,cAAc,CAAC,CAAS;IAEhC,GAAG;IAKH,MAAM;IAKN,UAAU;CAGb;AAED;;;;;;;;;GASG;AACH,qBAAa,UAAU;IACnB,OAAO,CAAC,MAAM,CAAC,EAAE,CAAK;IAEtB;;OAEG;IACH,QAAQ,CAAC,EAA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AE,CAAC;IAChC,OAAO,EAAE,MAAM,CAAC;IAChB,8BAA8B,EAAE,MAAM,CAAC;IACvC,gCAAgC,EAAE,MAAM,CAAC;IACzC,yBAAyB,EAAE,MAAM,CAAC;IAClC,uBAAuB,EAAE,MAAM,CAAC;IAChC,0BAA0B,EAAE,MAAM,CAAC;IACnC,aAAa,EAAE,MAAM,CAAC;IACtB,oBAAoB,EAAE,MAAM,CAAC;IAC7B,yBAAyB,EAAE,MAAM,CAAC;IAClC,gBAAgB,EAAE,MAAM,CAAC;CAC5B;AAED;;GAEG;AACH,MAAM,WAAW,cAAc;IAC3B,gBAAgB,EAAE,MAAM,CAAC;IACzB,cAAc,EAAE,MAAM,CAAC;IACvB,eAAe,EAAE,MAAM,CAAC;IACxB,uBAAuB,EAAE,MAAM,CAAC;IAChC,yBAAyB,EAAE,MAAM,CAAC;IAClC,wBAAwB,EAAE,MAAM,CAAC;IACjC,wBAAwB,EAAE,MAAM,CAAC;IACjC,gCAAgC,EAAE,MAAM,CAAC;IACzC,kCAAkC,EAAE,MAAM,CAAC;IAC3C,gBAAgB,EAAE,IAAI,GAAG,MAAM,GAAG,IAAI,CAAC;IACvC,iBAAiB,EAAE,IAAI,GAAG,MAAM,GAAG,IAAI,CAAC;IACxC,oBAAoB,EAAE,MAAM,CAAC;IAC7B,gBAAgB,EAAE,IAAI,GAAG,MAAM,GAAG,IAAI,CAAC;CAC1C"}
|
|
@@ -0,0 +1,360 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Statistics = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
/* eslint-disable max-classes-per-file */
|
|
6
|
+
const ow_1 = tslib_1.__importDefault(require("ow"));
|
|
7
|
+
const log_1 = require("../log");
|
|
8
|
+
const key_value_store_1 = require("../storages/key_value_store");
|
|
9
|
+
const configuration_1 = require("../configuration");
|
|
10
|
+
/**
|
|
11
|
+
* @ignore
|
|
12
|
+
*/
|
|
13
|
+
class Job {
|
|
14
|
+
constructor() {
|
|
15
|
+
Object.defineProperty(this, "lastRunAt", {
|
|
16
|
+
enumerable: true,
|
|
17
|
+
configurable: true,
|
|
18
|
+
writable: true,
|
|
19
|
+
value: null
|
|
20
|
+
});
|
|
21
|
+
Object.defineProperty(this, "runs", {
|
|
22
|
+
enumerable: true,
|
|
23
|
+
configurable: true,
|
|
24
|
+
writable: true,
|
|
25
|
+
value: 0
|
|
26
|
+
});
|
|
27
|
+
Object.defineProperty(this, "durationMillis", {
|
|
28
|
+
enumerable: true,
|
|
29
|
+
configurable: true,
|
|
30
|
+
writable: true,
|
|
31
|
+
value: void 0
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
run() {
|
|
35
|
+
this.lastRunAt = Date.now();
|
|
36
|
+
return ++this.runs;
|
|
37
|
+
}
|
|
38
|
+
finish() {
|
|
39
|
+
this.durationMillis = Date.now() - this.lastRunAt;
|
|
40
|
+
return this.durationMillis;
|
|
41
|
+
}
|
|
42
|
+
retryCount() {
|
|
43
|
+
return Math.max(0, this.runs - 1);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* The statistics class provides an interface to collecting and logging run
|
|
48
|
+
* statistics for requests.
|
|
49
|
+
*
|
|
50
|
+
* All statistic information is saved on key value store
|
|
51
|
+
* under the key `SDK_CRAWLER_STATISTICS_*`, persists between
|
|
52
|
+
* migrations and abort/resurrect
|
|
53
|
+
*
|
|
54
|
+
* @category Crawlers
|
|
55
|
+
*/
|
|
56
|
+
class Statistics {
|
|
57
|
+
/**
|
|
58
|
+
* @internal
|
|
59
|
+
*/
|
|
60
|
+
constructor(options = {}) {
|
|
61
|
+
/**
|
|
62
|
+
* Statistic instance id.
|
|
63
|
+
*/
|
|
64
|
+
Object.defineProperty(this, "id", {
|
|
65
|
+
enumerable: true,
|
|
66
|
+
configurable: true,
|
|
67
|
+
writable: true,
|
|
68
|
+
value: Statistics.id++
|
|
69
|
+
}); // assign an id while incrementing so it can be saved/restored from KV
|
|
70
|
+
/**
|
|
71
|
+
* Current statistic state used for doing calculations on {@link Statistics.calculate} calls
|
|
72
|
+
*/
|
|
73
|
+
Object.defineProperty(this, "state", {
|
|
74
|
+
enumerable: true,
|
|
75
|
+
configurable: true,
|
|
76
|
+
writable: true,
|
|
77
|
+
value: void 0
|
|
78
|
+
});
|
|
79
|
+
/**
|
|
80
|
+
* Contains the current retries histogram. Index 0 means 0 retries, index 2, 2 retries, and so on
|
|
81
|
+
*/
|
|
82
|
+
Object.defineProperty(this, "requestRetryHistogram", {
|
|
83
|
+
enumerable: true,
|
|
84
|
+
configurable: true,
|
|
85
|
+
writable: true,
|
|
86
|
+
value: []
|
|
87
|
+
});
|
|
88
|
+
Object.defineProperty(this, "keyValueStore", {
|
|
89
|
+
enumerable: true,
|
|
90
|
+
configurable: true,
|
|
91
|
+
writable: true,
|
|
92
|
+
value: null
|
|
93
|
+
});
|
|
94
|
+
Object.defineProperty(this, "persistStateKey", {
|
|
95
|
+
enumerable: true,
|
|
96
|
+
configurable: true,
|
|
97
|
+
writable: true,
|
|
98
|
+
value: `SDK_CRAWLER_STATISTICS_${this.id}`
|
|
99
|
+
});
|
|
100
|
+
Object.defineProperty(this, "logIntervalMillis", {
|
|
101
|
+
enumerable: true,
|
|
102
|
+
configurable: true,
|
|
103
|
+
writable: true,
|
|
104
|
+
value: void 0
|
|
105
|
+
});
|
|
106
|
+
Object.defineProperty(this, "logMessage", {
|
|
107
|
+
enumerable: true,
|
|
108
|
+
configurable: true,
|
|
109
|
+
writable: true,
|
|
110
|
+
value: void 0
|
|
111
|
+
});
|
|
112
|
+
Object.defineProperty(this, "listener", {
|
|
113
|
+
enumerable: true,
|
|
114
|
+
configurable: true,
|
|
115
|
+
writable: true,
|
|
116
|
+
value: void 0
|
|
117
|
+
});
|
|
118
|
+
Object.defineProperty(this, "requestsInProgress", {
|
|
119
|
+
enumerable: true,
|
|
120
|
+
configurable: true,
|
|
121
|
+
writable: true,
|
|
122
|
+
value: new Map()
|
|
123
|
+
});
|
|
124
|
+
Object.defineProperty(this, "log", {
|
|
125
|
+
enumerable: true,
|
|
126
|
+
configurable: true,
|
|
127
|
+
writable: true,
|
|
128
|
+
value: log_1.log.child({ prefix: 'Statistics' })
|
|
129
|
+
});
|
|
130
|
+
Object.defineProperty(this, "instanceStart", {
|
|
131
|
+
enumerable: true,
|
|
132
|
+
configurable: true,
|
|
133
|
+
writable: true,
|
|
134
|
+
value: void 0
|
|
135
|
+
});
|
|
136
|
+
Object.defineProperty(this, "logInterval", {
|
|
137
|
+
enumerable: true,
|
|
138
|
+
configurable: true,
|
|
139
|
+
writable: true,
|
|
140
|
+
value: void 0
|
|
141
|
+
});
|
|
142
|
+
Object.defineProperty(this, "events", {
|
|
143
|
+
enumerable: true,
|
|
144
|
+
configurable: true,
|
|
145
|
+
writable: true,
|
|
146
|
+
value: void 0
|
|
147
|
+
});
|
|
148
|
+
(0, ow_1.default)(options, ow_1.default.object.exactShape({
|
|
149
|
+
logIntervalSecs: ow_1.default.optional.number,
|
|
150
|
+
logMessage: ow_1.default.optional.string,
|
|
151
|
+
config: ow_1.default.optional.object,
|
|
152
|
+
}));
|
|
153
|
+
const { logIntervalSecs = 60, logMessage = 'Statistics', config = configuration_1.Configuration.getGlobalConfig(), } = options;
|
|
154
|
+
this.logIntervalMillis = logIntervalSecs * 1000;
|
|
155
|
+
this.logMessage = logMessage;
|
|
156
|
+
this.listener = this.persistState.bind(this);
|
|
157
|
+
this.events = config.getEventManager();
|
|
158
|
+
// initialize by "resetting"
|
|
159
|
+
this.reset();
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* Set the current statistic instance to pristine values
|
|
163
|
+
*/
|
|
164
|
+
reset() {
|
|
165
|
+
this.state = {
|
|
166
|
+
requestsFinished: 0,
|
|
167
|
+
requestsFailed: 0,
|
|
168
|
+
requestsRetries: 0,
|
|
169
|
+
requestsFailedPerMinute: 0,
|
|
170
|
+
requestsFinishedPerMinute: 0,
|
|
171
|
+
requestMinDurationMillis: Infinity,
|
|
172
|
+
requestMaxDurationMillis: 0,
|
|
173
|
+
requestTotalFailedDurationMillis: 0,
|
|
174
|
+
requestTotalFinishedDurationMillis: 0,
|
|
175
|
+
crawlerStartedAt: null,
|
|
176
|
+
crawlerFinishedAt: null,
|
|
177
|
+
statsPersistedAt: null,
|
|
178
|
+
crawlerRuntimeMillis: 0,
|
|
179
|
+
};
|
|
180
|
+
this.requestRetryHistogram.length = 0;
|
|
181
|
+
this.requestsInProgress.clear();
|
|
182
|
+
this.instanceStart = Date.now();
|
|
183
|
+
this._teardown();
|
|
184
|
+
}
|
|
185
|
+
/**
|
|
186
|
+
* Starts a job
|
|
187
|
+
* @ignore
|
|
188
|
+
*/
|
|
189
|
+
startJob(id) {
|
|
190
|
+
let job = this.requestsInProgress.get(id);
|
|
191
|
+
if (!job)
|
|
192
|
+
job = new Job();
|
|
193
|
+
job.run();
|
|
194
|
+
this.requestsInProgress.set(id, job);
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* Mark job as finished and sets the state
|
|
198
|
+
* @ignore
|
|
199
|
+
*/
|
|
200
|
+
finishJob(id) {
|
|
201
|
+
const job = this.requestsInProgress.get(id);
|
|
202
|
+
if (!job)
|
|
203
|
+
return;
|
|
204
|
+
const jobDurationMillis = job.finish();
|
|
205
|
+
this.state.requestsFinished++;
|
|
206
|
+
this.state.requestTotalFinishedDurationMillis += jobDurationMillis;
|
|
207
|
+
this._saveRetryCountForJob(job);
|
|
208
|
+
if (jobDurationMillis < this.state.requestMinDurationMillis)
|
|
209
|
+
this.state.requestMinDurationMillis = jobDurationMillis;
|
|
210
|
+
if (jobDurationMillis > this.state.requestMaxDurationMillis)
|
|
211
|
+
this.state.requestMaxDurationMillis = jobDurationMillis;
|
|
212
|
+
this.requestsInProgress.delete(id);
|
|
213
|
+
}
|
|
214
|
+
/**
|
|
215
|
+
* Mark job as failed and sets the state
|
|
216
|
+
* @ignore
|
|
217
|
+
*/
|
|
218
|
+
failJob(id) {
|
|
219
|
+
const job = this.requestsInProgress.get(id);
|
|
220
|
+
if (!job)
|
|
221
|
+
return;
|
|
222
|
+
this.state.requestTotalFailedDurationMillis += job.finish();
|
|
223
|
+
this.state.requestsFailed++;
|
|
224
|
+
this._saveRetryCountForJob(job);
|
|
225
|
+
this.requestsInProgress.delete(id);
|
|
226
|
+
}
|
|
227
|
+
/**
|
|
228
|
+
* Calculate the current statistics
|
|
229
|
+
*/
|
|
230
|
+
calculate() {
|
|
231
|
+
const { requestsFailed, requestsFinished, requestTotalFailedDurationMillis, requestTotalFinishedDurationMillis, } = this.state;
|
|
232
|
+
const totalMillis = Date.now() - this.instanceStart;
|
|
233
|
+
const totalMinutes = totalMillis / 1000 / 60;
|
|
234
|
+
return {
|
|
235
|
+
requestAvgFailedDurationMillis: Math.round(requestTotalFailedDurationMillis / requestsFailed) || Infinity,
|
|
236
|
+
requestAvgFinishedDurationMillis: Math.round(requestTotalFinishedDurationMillis / requestsFinished) || Infinity,
|
|
237
|
+
requestsFinishedPerMinute: Math.round(requestsFinished / totalMinutes) || 0,
|
|
238
|
+
requestsFailedPerMinute: Math.floor(requestsFailed / totalMinutes) || 0,
|
|
239
|
+
requestTotalDurationMillis: requestTotalFinishedDurationMillis + requestTotalFailedDurationMillis,
|
|
240
|
+
requestsTotal: requestsFailed + requestsFinished,
|
|
241
|
+
crawlerRuntimeMillis: totalMillis,
|
|
242
|
+
};
|
|
243
|
+
}
|
|
244
|
+
/**
|
|
245
|
+
* Initializes the key value store for persisting the statistics,
|
|
246
|
+
* displaying the current state in predefined intervals
|
|
247
|
+
*/
|
|
248
|
+
async startCapturing() {
|
|
249
|
+
this.keyValueStore = await key_value_store_1.KeyValueStore.open();
|
|
250
|
+
await this._maybeLoadStatistics();
|
|
251
|
+
if (this.state.crawlerStartedAt === null) {
|
|
252
|
+
this.state.crawlerStartedAt = new Date();
|
|
253
|
+
}
|
|
254
|
+
this.events.on("persistState" /* PERSIST_STATE */, this.listener);
|
|
255
|
+
this.logInterval = setInterval(() => {
|
|
256
|
+
this.log.info(this.logMessage, {
|
|
257
|
+
...this.calculate(),
|
|
258
|
+
retryHistogram: this.requestRetryHistogram,
|
|
259
|
+
});
|
|
260
|
+
}, this.logIntervalMillis);
|
|
261
|
+
}
|
|
262
|
+
/**
|
|
263
|
+
* Stops logging and remove event listeners, then persist
|
|
264
|
+
*/
|
|
265
|
+
async stopCapturing() {
|
|
266
|
+
this._teardown();
|
|
267
|
+
this.state.crawlerFinishedAt = new Date();
|
|
268
|
+
await this.persistState();
|
|
269
|
+
}
|
|
270
|
+
_saveRetryCountForJob(job) {
|
|
271
|
+
const retryCount = job.retryCount();
|
|
272
|
+
if (retryCount > 0)
|
|
273
|
+
this.state.requestsRetries++;
|
|
274
|
+
this.requestRetryHistogram[retryCount] = this.requestRetryHistogram[retryCount]
|
|
275
|
+
? this.requestRetryHistogram[retryCount] + 1
|
|
276
|
+
: 1;
|
|
277
|
+
}
|
|
278
|
+
/**
|
|
279
|
+
* Persist internal state to the key value store
|
|
280
|
+
*/
|
|
281
|
+
async persistState() {
|
|
282
|
+
// this might be called before startCapturing was called without using await, should not crash
|
|
283
|
+
if (!this.keyValueStore) {
|
|
284
|
+
return;
|
|
285
|
+
}
|
|
286
|
+
this.log.debug('Persisting state', { persistStateKey: this.persistStateKey });
|
|
287
|
+
await this.keyValueStore.setValue(this.persistStateKey, this.toJSON());
|
|
288
|
+
}
|
|
289
|
+
/**
|
|
290
|
+
* Loads the current statistic from the key value store if any
|
|
291
|
+
*/
|
|
292
|
+
async _maybeLoadStatistics() {
|
|
293
|
+
// this might be called before startCapturing was called without using await, should not crash
|
|
294
|
+
if (!this.keyValueStore) {
|
|
295
|
+
return;
|
|
296
|
+
}
|
|
297
|
+
const savedState = await this.keyValueStore.getValue(this.persistStateKey);
|
|
298
|
+
if (!savedState)
|
|
299
|
+
return;
|
|
300
|
+
// We saw a run where the requestRetryHistogram was not iterable and crashed
|
|
301
|
+
// the actor. Adding some logging to monitor this problem in the future.
|
|
302
|
+
if (!Array.isArray(savedState.requestRetryHistogram)) {
|
|
303
|
+
this.log.warning('Received invalid state from Key-value store.', {
|
|
304
|
+
persistStateKey: this.persistStateKey,
|
|
305
|
+
state: savedState,
|
|
306
|
+
});
|
|
307
|
+
}
|
|
308
|
+
this.log.debug('Recreating state from KeyValueStore', { persistStateKey: this.persistStateKey });
|
|
309
|
+
this.requestRetryHistogram.push(...savedState.requestRetryHistogram);
|
|
310
|
+
this.state.requestsFinished = savedState.requestsFinished;
|
|
311
|
+
this.state.requestsFailed = savedState.requestsFailed;
|
|
312
|
+
this.state.requestsRetries = savedState.requestsRetries;
|
|
313
|
+
this.state.requestTotalFailedDurationMillis = savedState.requestTotalFailedDurationMillis;
|
|
314
|
+
this.state.requestTotalFinishedDurationMillis = savedState.requestTotalFinishedDurationMillis;
|
|
315
|
+
this.state.requestMinDurationMillis = savedState.requestMinDurationMillis;
|
|
316
|
+
this.state.requestMaxDurationMillis = savedState.requestMaxDurationMillis;
|
|
317
|
+
// persisted state uses ISO date strings
|
|
318
|
+
this.state.crawlerFinishedAt = savedState.crawlerFinishedAt ? new Date(savedState.crawlerFinishedAt) : null;
|
|
319
|
+
this.state.crawlerStartedAt = savedState.crawlerStartedAt ? new Date(savedState.crawlerStartedAt) : null;
|
|
320
|
+
this.state.statsPersistedAt = savedState.statsPersistedAt ? new Date(savedState.statsPersistedAt) : null;
|
|
321
|
+
this.state.crawlerRuntimeMillis = savedState.crawlerRuntimeMillis;
|
|
322
|
+
this.instanceStart = Date.now() - (+this.state.statsPersistedAt - savedState.crawlerLastStartTimestamp);
|
|
323
|
+
this.log.debug('Loaded from KeyValueStore');
|
|
324
|
+
}
|
|
325
|
+
_teardown() {
|
|
326
|
+
// this can be called before a call to startCapturing happens (or in a 'finally' block)
|
|
327
|
+
this.events.off("persistState" /* PERSIST_STATE */, this.listener);
|
|
328
|
+
if (this.logInterval) {
|
|
329
|
+
clearInterval(this.logInterval);
|
|
330
|
+
this.logInterval = null;
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
/**
|
|
334
|
+
* Make this class serializable when called with `JSON.stringify(statsInstance)` directly
|
|
335
|
+
* or through `keyValueStore.setValue('KEY', statsInstance)`
|
|
336
|
+
*/
|
|
337
|
+
toJSON() {
|
|
338
|
+
// merge all the current state information that can be used from the outside
|
|
339
|
+
// without the need to reconstruct for the sake of stats.calculate()
|
|
340
|
+
// omit duplicated information
|
|
341
|
+
return {
|
|
342
|
+
...this.state,
|
|
343
|
+
crawlerLastStartTimestamp: this.instanceStart,
|
|
344
|
+
crawlerFinishedAt: this.state.crawlerFinishedAt ? new Date(this.state.crawlerFinishedAt).toISOString() : null,
|
|
345
|
+
crawlerStartedAt: this.state.crawlerStartedAt ? new Date(this.state.crawlerStartedAt).toISOString() : null,
|
|
346
|
+
requestRetryHistogram: this.requestRetryHistogram,
|
|
347
|
+
statsId: this.id,
|
|
348
|
+
statsPersistedAt: new Date().toISOString(),
|
|
349
|
+
...this.calculate(),
|
|
350
|
+
};
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
exports.Statistics = Statistics;
|
|
354
|
+
Object.defineProperty(Statistics, "id", {
|
|
355
|
+
enumerable: true,
|
|
356
|
+
configurable: true,
|
|
357
|
+
writable: true,
|
|
358
|
+
value: 0
|
|
359
|
+
});
|
|
360
|
+
//# sourceMappingURL=statistics.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"statistics.js","sourceRoot":"","sources":["../../src/crawlers/statistics.ts"],"names":[],"mappings":";;;;AAAA,yCAAyC;AACzC,oDAAoB;AACpB,gCAA2C;AAC3C,iEAA4D;AAE5D,oDAAiD;AAEjD;;GAEG;AACH,MAAM,GAAG;IAAT;QACI;;;;mBAAmC,IAAI;WAAC;QACxC;;;;mBAAe,CAAC;WAAC;QACjB;;;;;WAAgC;IAepC,CAAC;IAbG,GAAG;QACC,IAAI,CAA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AE,IAAI,CAAC,qBAAqB;YACjD,OAAO,EAAE,IAAI,CAAC,EAAE;YAChB,gBAAgB,EAAE,IAAI,IAAI,EAAE,CAAC,WAAW,EAAE;YAC1C,GAAG,IAAI,CAAC,SAAS,EAAE;SACtB,CAAC;IACN,CAAC;;AAhRL,gCAiRC;AAhRG;;;;WAAoB,CAAC;GAAC"}
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import { GlobInput, PseudoUrlInput, RegExpInput, RequestTransform } from './shared';
|
|
2
|
+
import { RequestQueue } from '../storages/request_queue';
|
|
3
|
+
import { BatchAddRequestsResult } from '../storages/storage';
|
|
4
|
+
export interface EnqueueLinksOptions {
|
|
5
|
+
/** Limit the count of actually enqueued URLs to this number. Useful for testing across the entire crawling scope. */
|
|
6
|
+
limit?: number;
|
|
7
|
+
/** An array of URLs to enqueue. */
|
|
8
|
+
urls: string[];
|
|
9
|
+
/** A request queue to which the URLs will be enqueued. */
|
|
10
|
+
requestQueue: RequestQueue;
|
|
11
|
+
/** A CSS selector matching links to be enqueued. */
|
|
12
|
+
selector?: string;
|
|
13
|
+
/**
|
|
14
|
+
* A base URL that will be used to resolve relative URLs when using Cheerio. Ignored when using Puppeteer,
|
|
15
|
+
* since the relative URL resolution is done inside the browser automatically.
|
|
16
|
+
*/
|
|
17
|
+
baseUrl?: string;
|
|
18
|
+
/**
|
|
19
|
+
* An array of glob pattern strings or plain objects
|
|
20
|
+
* containing glob pattern strings matching the URLs to be enqueued.
|
|
21
|
+
*
|
|
22
|
+
* The plain objects must include at least the `glob` property, which holds the glob pattern string.
|
|
23
|
+
* All remaining keys will be used as request options for the corresponding enqueued {@link Request} objects.
|
|
24
|
+
*
|
|
25
|
+
* The matching is always case-insensitive.
|
|
26
|
+
* If you need case-sensitive matching, use `regexps` property directly.
|
|
27
|
+
*
|
|
28
|
+
* If `globs` is an empty array or `undefined`, then the function
|
|
29
|
+
* enqueues the links with the same subdomain.
|
|
30
|
+
*/
|
|
31
|
+
globs?: GlobInput[];
|
|
32
|
+
/**
|
|
33
|
+
* An array of regular expressions or plain objects
|
|
34
|
+
* containing regular expressions matching the URLs to be enqueued.
|
|
35
|
+
*
|
|
36
|
+
* The plain objects must include at least the `regexp` property, which holds the regular expression.
|
|
37
|
+
* All remaining keys will be used as request options for the corresponding enqueued {@link Request} objects.
|
|
38
|
+
*
|
|
39
|
+
* If `regexps` is an empty array or `undefined`, then the function
|
|
40
|
+
* enqueues the links with the same subdomain.
|
|
41
|
+
*/
|
|
42
|
+
regexps?: RegExpInput[];
|
|
43
|
+
/**
|
|
44
|
+
* *NOTE:* In future versions of SDK the options will be removed.
|
|
45
|
+
* Please use `globs` or `regexps` instead.
|
|
46
|
+
*
|
|
47
|
+
* An array of {@link PseudoUrl} strings or plain objects
|
|
48
|
+
* containing {@link PseudoUrl} strings matching the URLs to be enqueued.
|
|
49
|
+
*
|
|
50
|
+
* The plain objects must include at least the `purl` property, which holds the pseudo-URL string.
|
|
51
|
+
* All remaining keys will be used as request options for the corresponding enqueued {@link Request} objects.
|
|
52
|
+
*
|
|
53
|
+
* With a pseudo-URL string, the matching is always case-insensitive.
|
|
54
|
+
* If you need case-sensitive matching, use `regexps` property directly.
|
|
55
|
+
*
|
|
56
|
+
* If `pseudoUrls` is an empty array or `undefined`, then the function
|
|
57
|
+
* enqueues the links with the same subdomain.
|
|
58
|
+
*
|
|
59
|
+
* @deprecated prefer using `globs` or `regexps` instead
|
|
60
|
+
*/
|
|
61
|
+
pseudoUrls?: PseudoUrlInput[];
|
|
62
|
+
/**
|
|
63
|
+
* Just before a new {@link Request} is constructed and enqueued to the {@link RequestQueue}, this function can be used
|
|
64
|
+
* to remove it or modify its contents such as `userData`, `payload` or, most importantly `uniqueKey`. This is useful
|
|
65
|
+
* when you need to enqueue multiple `Requests` to the queue that share the same URL, but differ in methods or payloads,
|
|
66
|
+
* or to dynamically update or create `userData`.
|
|
67
|
+
*
|
|
68
|
+
* For example: by adding `keepUrlFragment: true` to the `request` object, URL fragments will not be removed
|
|
69
|
+
* when `uniqueKey` is computed.
|
|
70
|
+
*
|
|
71
|
+
* **Example:**
|
|
72
|
+
* ```javascript
|
|
73
|
+
* {
|
|
74
|
+
* transformRequestFunction: (request) => {
|
|
75
|
+
* request.userData.foo = 'bar';
|
|
76
|
+
* request.keepUrlFragment = true;
|
|
77
|
+
* return request;
|
|
78
|
+
* }
|
|
79
|
+
* }
|
|
80
|
+
* ```
|
|
81
|
+
*
|
|
82
|
+
* Note that `transformRequestFunction` has a priority over request options
|
|
83
|
+
* specified in `globs`, `regexps`, or `pseudoUrls` objects,
|
|
84
|
+
* and thus some options could be over-written by `transformRequestFunction`.
|
|
85
|
+
*/
|
|
86
|
+
transformRequestFunction?: RequestTransform;
|
|
87
|
+
/**
|
|
88
|
+
* The strategy to use when enqueueing the urls.
|
|
89
|
+
* @default EnqueueStrategy.SameSubdomain
|
|
90
|
+
*/
|
|
91
|
+
strategy?: EnqueueStrategy | 'all' | 'same-subdomain' | 'same-hostname';
|
|
92
|
+
}
|
|
93
|
+
export declare enum EnqueueStrategy {
|
|
94
|
+
/**
|
|
95
|
+
* Matches any URLs found
|
|
96
|
+
*/
|
|
97
|
+
All = "all",
|
|
98
|
+
/**
|
|
99
|
+
* Matches any URLs that have the same subdomain as the base URL
|
|
100
|
+
*/
|
|
101
|
+
SameSubdomain = "same-subdomain",
|
|
102
|
+
/**
|
|
103
|
+
* Matches any URLs that have the same hostname.
|
|
104
|
+
* For example, `https://wow.an.example.com` and `https://example.com` will both be matched for a base url of
|
|
105
|
+
* `https://example.com`.
|
|
106
|
+
*/
|
|
107
|
+
SameHostname = "same-hostname"
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* The function finds elements matching a specific CSS selector (HTML anchor (`<a>`) by default)
|
|
111
|
+
* either in a Puppeteer/Playwright page, or in a Cheerio object (parsed HTML),
|
|
112
|
+
* and enqueues the URLs in their `href` attributes to the provided {@link RequestQueue}.
|
|
113
|
+
* If you're looking to find URLs in JavaScript heavy pages where links are not available
|
|
114
|
+
* in `href` elements, but rather navigations are triggered in click handlers
|
|
115
|
+
* see {@link puppeteerUtils.enqueueLinksByClickingElements}.
|
|
116
|
+
*
|
|
117
|
+
* Optionally, the function allows you to filter the target links' URLs using an array of {@link PseudoUrl} objects
|
|
118
|
+
* and override settings of the enqueued {@link Request} objects.
|
|
119
|
+
*
|
|
120
|
+
* **Example usage**
|
|
121
|
+
*
|
|
122
|
+
* ```javascript
|
|
123
|
+
* await enqueueLinks({
|
|
124
|
+
* page,
|
|
125
|
+
* requestQueue,
|
|
126
|
+
* selector: 'a.product-detail',
|
|
127
|
+
* pseudoUrls: [
|
|
128
|
+
* 'https://www.example.com/handbags/[.*]',
|
|
129
|
+
* 'https://www.example.com/purses/[.*]'
|
|
130
|
+
* ],
|
|
131
|
+
* });
|
|
132
|
+
* ```
|
|
133
|
+
*
|
|
134
|
+
* @param options All `enqueueLinks()` parameters are passed via an options object.
|
|
135
|
+
* @returns Promise that resolves to {@link BatchAddRequestsResult} object.
|
|
136
|
+
*/
|
|
137
|
+
export declare function enqueueLinks(options: EnqueueLinksOptions): Promise<BatchAddRequestsResult>;
|
|
138
|
+
//# sourceMappingURL=enqueue_links.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"enqueue_links.d.ts","sourceRoot":"","sources":["../../src/enqueue_links/enqueue_links.ts"],"names":[],"mappings":"AAGA,OAAO,EAMH,SAAS,EACT,cAAc,EACd,WAAW,EACX,gBAAgB,EAEnB,MAAM,UAAU,CAAC;AAClB,OAAO,EAAE,YAAY,EAAE,MAAM,2BAA2B,CAAC;AACzD,OAAO,EAAE,sBAAsB,EAAE,MAAM,qBAAqB,CAAC;AAE7D,MAAM,WAAW,mBAAmB;IAChC,qHAAqH;IACrH,KAAK,CAAC,EAAE,MAAM,CAAC;IAEf,mCAAmC;IACnC,IAAI,EAAE,MAAM,EAAE,CAAC;IAEf,0DAA0D;IAC1D,YAAY,EAAE,YAAY,CA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