Skip to content

Commit

Permalink
MWPW-128690 Unit Tests (#93)
Browse files Browse the repository at this point in the history
* Update README.md

* MWPW-142865: Stage deploy script updated (#91)

* .env template for easier setup and onboarding (#90)

* Update README.md

* .env template for easier setup and onboarding

* remove temp change

* MWPW-142865: Stage deploy script updated (#89)

* Revert temp readme change

* MWPW-128690 Unit Tests

Unit Test Coverage for action helpers and utilities.
Payload handling updated

* Minor fixes

* Sharepoint reference updated

* Rename FgAction

---------

Co-authored-by: Sunil Kamat <[email protected]>
Co-authored-by: Raghu A <[email protected]>
  • Loading branch information
3 people authored Mar 19, 2024
1 parent 69ba144 commit c4f457f
Show file tree
Hide file tree
Showing 40 changed files with 2,994 additions and 1,305 deletions.
116 changes: 78 additions & 38 deletions actions/appConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,22 @@ const { strToArray, strToBool, getAioLogger } = require('./utils');
const UrlInfo = require('./urlInfo');

// Max activation is 1hrs, set to 2hrs
const MAX_ACTIVATION_TIME = 2 * 60 * 60 * 1000;
const ENV_VAR_ACTIVATION_ID = '__OW_ACTIVATION_ID';
const GRAPH_API = 'https://graph.microsoft.com/v1.0';

/**
* This store the Floodate configs.
* Common Configs - Parameters like Batch
*/
class AppConfig {
// set payload per activation
configMap = { payload: {} };
constructor(params) {
this.configMap = { payload: {} };
if (params) {
this.setAppConfig(params);
}
}

setAppConfig(params) {
const payload = this.initPayload();
// Called during action start to cleanup old entries
this.removeOldPayload();
const payload = this.getPayload();

// These are payload parameters
// eslint-disable-next-line no-underscore-dangle
Expand Down Expand Up @@ -85,37 +86,8 @@ class AppConfig {
};
}

// Activation Payload Related
initPayload() {
this.configMap.payload[this.getPayloadKey()] = {
payloadAccessedOn: new Date().getTime()
};
return this.configMap.payload[this.getPayloadKey()];
}

getPayloadKey() {
return process.env[ENV_VAR_ACTIVATION_ID];
}

getPayload() {
this.configMap.payload[this.getPayloadKey()].payloadAccessedOn = new Date().getTime();
return this.configMap.payload[this.getPayloadKey()];
}

removePayload() {
delete this.configMap.payload[this.getPayloadKey()];
}

/**
* Similar to LRU
*/
removeOldPayload() {
const { payload } = this.configMap;
const payloadKeys = Object.keys(payload);
const leastTime = new Date().getTime();
payloadKeys
.filter((key) => payload[key]?.payloadAccessedOn < leastTime - MAX_ACTIVATION_TIME)
.forEach((key) => delete payload[key]);
return this.configMap.payload;
}

// Configs related methods
Expand Down Expand Up @@ -242,6 +214,74 @@ class AppConfig {
getUserToken() {
return this.getPayload().spToken;
}

getSpConfig() {
if (!this.getUrlInfo().isValid()) {
return undefined;
}

const config = this.getConfig();

// get drive id if available
const { driveId, rootFolder, fgRootFolder } = this.getPayload();
const drive = driveId ? `/drives/${driveId}` : '/drive';

const baseURI = `${config.fgSite}${drive}/root:${rootFolder}`;
const fgBaseURI = `${config.fgSite}${drive}/root:${fgRootFolder}`;
const baseItemsURI = `${config.fgSite}${drive}/items`;
return {
api: {
url: GRAPH_API,
file: {
get: { baseURI, fgBaseURI },
download: { baseURI: `${config.fgSite}${drive}/items` },
upload: {
baseURI,
fgBaseURI,
method: 'PUT',
},
delete: {
baseURI,
fgBaseURI,
method: 'DELETE',
},
update: {
baseURI,
fgBaseURI,
method: 'PATCH',
},
createUploadSession: {
baseURI,
fgBaseURI,
method: 'POST',
payload: { '@microsoft.graph.conflictBehavior': 'replace' },
},
copy: {
baseURI,
fgBaseURI,
method: 'POST',
payload: { '@microsoft.graph.conflictBehavior': 'replace' },
},
},
directory: {
create: {
baseURI,
fgBaseURI,
method: 'PATCH',
payload: { folder: {} },
},
},
excel: {
get: { baseItemsURI },
update: {
baseItemsURI,
method: 'POST',
},
},
batch: { uri: `${GRAPH_API}/$batch` },
},
};
}
}

module.exports = new AppConfig();
module.exports = AppConfig;
4 changes: 1 addition & 3 deletions actions/batch.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
************************************************************************* */

const { getAioLogger } = require('./utils');
const appConfig = require('./appConfig');

const logger = getAioLogger();

Expand All @@ -41,7 +40,7 @@ class Batch {
this.filesSdk = params.filesSdk;
this.instancePath = params.instancePath;
this.batchNumber = params?.batchNumber || 1;
this.maxFilesPerBatch = appConfig.getBatchConfig().maxFilesPerBatch;
this.maxFilesPerBatch = params?.maxFilesPerBatch || 200;
this.batchPath = `${this.instancePath}/${FOLDER_PREFIX}_${this.batchNumber}`;
this.batchInfoFile = `${this.batchPath}/${BATCH_INFO_FILE}`;
this.resultsFile = `${this.batchPath}/${RESULTS_FILE}`;
Expand Down Expand Up @@ -85,7 +84,6 @@ class Batch {
if (!this.filesSdk || !this.batchFiles?.length) return;
const dataStr = JSON.stringify(this.batchFiles);
await this.filesSdk.write(this.batchInfoFile, dataStr);
this.batchInfoFile = [];
}

async getFiles() {
Expand Down
15 changes: 10 additions & 5 deletions actions/batchManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
************************************************************************* */
const filesLib = require('@adobe/aio-lib-files');
const Batch = require('./batch');
const appConfig = require('./appConfig');
const { getAioLogger } = require('./utils');

const logger = getAioLogger();
Expand All @@ -37,6 +36,8 @@ const logger = getAioLogger();
* There needs to be enhacement to handle this within this e.g. Batch execution stargergy should be implemented.
*/
class BatchManager {
batchConfig = null;

filesSdk = null;

instanceData = { lastBatch: '', dtls: { batchesInfo: [] } };
Expand All @@ -50,7 +51,8 @@ class BatchManager {
constructor(params) {
this.params = params || {};
this.batches = [];
this.batchFilesPath = appConfig.getBatchConfig()?.batchFilesPath;
this.batchConfig = params.batchConfig;
this.batchFilesPath = this.batchConfig.batchFilesPath;
this.key = params.key;
this.bmPath = `${this.batchFilesPath}/${this.key}`;
this.bmTracker = `${this.bmPath}/tracker.json`;
Expand Down Expand Up @@ -82,7 +84,8 @@ class BatchManager {
...this.params,
filesSdk: this.filesSdk,
instancePath: this.instancePath,
batchNumber: this.currentBatchNumber
batchNumber: this.currentBatchNumber,
maxFilesPerBatch: this.batchConfig.maxFilesPerBatch
});
this.batches.push(this.currentBatch);
}
Expand All @@ -106,7 +109,8 @@ class BatchManager {
* Structure
* {
* instanceKeys: [_milo_pink],
* '_milo_pink': {done: <true>, proceed: <true>}
* '_milo_pink': {done: <true>, proceed: <true>},
* * '_bacom_pink': {done: <true>, proceed: <true>},
* }
*/
async readBmTracker() {
Expand Down Expand Up @@ -275,7 +279,8 @@ class BatchManager {
this.currentBatch = new Batch({
filesSdk: this.filesSdk,
instancePath: this.instancePath,
batchNumber: this.currentBatchNumber
batchNumber: this.currentBatchNumber,
maxFilesPerBatch: this.batchConfig.maxFilesPerBatch
});
this.batches.push(this.currentBatch);
this.instanceData.lastBatch = this.currentBatchNumber;
Expand Down
118 changes: 0 additions & 118 deletions actions/config.js

This file was deleted.

Loading

0 comments on commit c4f457f

Please sign in to comment.