Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
williamlardier committed Oct 10, 2023
1 parent c24dad7 commit 36fe110
Showing 1 changed file with 55 additions and 1 deletion.
56 changes: 55 additions & 1 deletion CountItemsV2/CountItems.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ class CountItems {
this.watcher = null;
}

/**
* Refreshes the connection state.
* @returns {undefined}
*/
refresh() {
if (this.db?.client) {
this.connected = this.db.client.isConnected();
Expand All @@ -61,6 +65,10 @@ class CountItems {
}
}

/**
* Connects to MongoDB, with retries.
* @returns {Promise} - resolves when the connection is established
*/
async connectWithRetries() {
this.refresh(); // Assuming this refreshes the connection state

Expand Down Expand Up @@ -112,6 +120,10 @@ class CountItems {
});
}

/**
* Main function, starts the worker.
* @returns {undefined}
*/
async work() {
this.log.info('Starting worker...');
await this.connectWithRetries();
Expand Down Expand Up @@ -175,6 +187,7 @@ class CountItems {
/**
* Periodically, the service performs a full refresh of the pool,
* to ensure no deviation with the truth.
* @returns {undefined}
*/
resetPool() {
this.log.info('Resetting pool...');
Expand All @@ -185,7 +198,26 @@ class CountItems {
}
}

/**
* Checks whether the bucket has the SOS CapacityInfo enabled
* @param {object} bucketInfo - bucket metadata
* @returns {boolean} - whether the bucket has the SOS CapacityInfo enabled
*/
isSOSCapacityInfoEnabled(bucketInfo) {
return !!(bucketInfo._capabilities
&& bucketInfo._capabilities.VeeamSOSApi
&& bucketInfo._capabilities.VeeamSOSApi.SystemInfo
&& bucketInfo._capabilities.VeeamSOSApi.SystemInfo.ProtocolCapabilities
&& bucketInfo._capabilities.VeeamSOSApi.SystemInfo.ProtocolCapabilities.CapacityInfo === true
&& bucketInfo._capabilities.VeeamSOSApi.CapacityInfo);
}

/**
* Lists all buckets in the METASTORE collection, and
* updates the pool accordingly.
* @param {boolean} onlySelectSOSAPIEnabledBuckets - whether to only select buckets with SOSAPI enabled
* @returns
*/
async listAllBuckets(onlySelectSOSAPIEnabledBuckets = false) {
this.log.info('Listing all buckets...');
const collection = this.db.getCollection(METASTORE_COLLECTION);
Expand All @@ -209,6 +241,9 @@ class CountItems {
resolve();
return;
}
if (onlySelectSOSAPIEnabledBuckets && doc && doc.value && !this.isSOSCapacityInfoEnabled(doc.value)) {
return;
}
this.log.info('Listing all buckets: cursor processing...', {
bucketNumber: i,
bucketId: doc._id,
Expand All @@ -233,7 +268,8 @@ class CountItems {
/**
* Compares the current bucket list with the previous one, and
* updates the pool accordingly.
* @param {array} currentBucketList
* @param {array} currentBucketList
* @returns {undefined}
*/
syncPoolWithBucketList(currentBucketList) {
// Detect new buckets and remove deleted ones
Expand Down Expand Up @@ -287,6 +323,7 @@ class CountItems {
/**
* Same as getCheckpoint, but here we bulk all the writes to mongodb
* based on the current dictionnary
* @returns {Promise} - resolves to the checkpoint value
*/
setCheckPoints() {
this.log.info('Setting checkpoints...', {
Expand Down Expand Up @@ -333,6 +370,11 @@ class CountItems {
* used to limit the number of scanned entries between two scan runs. In this case, a
* $match is added to the aggregation, on this field, to ensure the objects are
* $gt the provided value;
* @param {string} bucketName - name of the bucket
* @param {string} accountName - name of the account
* @param {string} bucketLocation - location of the bucket
* @param {boolean} isFirstRun - whether this is the first run for this bucket
* @returns {Promise} - resolves to the metrics object
*/
async processBucket(bucketName, accountName, bucketLocation, isFirstRun = false) {
this.log.info('Processing bucket...', {
Expand Down Expand Up @@ -482,11 +524,16 @@ class CountItems {
* Metrics for each location, each account and each bucket.
* This function aggregates all the data and dynamically saves the values
* in the INFOSTORE collection.
* @returns {Promise} - resolves when the aggregation is complete
*/
async aggregateResults() {
this.log.info('Aggregating results...');
}

/**
* Recreates the change stream watcher, in case of error.
* @returns {undefined}
*/
_recreateWatcher() {
this.watcher = this.db.watch([{
$match: {
Expand All @@ -495,6 +542,13 @@ class CountItems {
}]);
}

/**
* Consolidates the results of the aggregation, and computes the
* final metrics for each location, each account and each bucket.
* @param {string} bucketName - name of the bucket
* @param {object} result - result of the aggregation
* @returns {undefined}
*/
consolidateResults(bucketName, result) {
// TODO rework with actual metrics computations
const updateMetrics = (target, source) => {
Expand Down

0 comments on commit 36fe110

Please sign in to comment.