Skip to content

Commit

Permalink
change to use npm instead of yarn, clean up layers, use node 20 (#359)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidclaveau authored Jul 18, 2024
1 parent ad2dd8d commit 3d8b129
Show file tree
Hide file tree
Showing 23 changed files with 12,620 additions and 9,001 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/on-pr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ jobs:
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
cache: "yarn"
cache-dependency-path: "arSam/yarn.lock"
- run: yarn install --silent --frozen-lockfile
cache: "npm"
cache-dependency-path: "arSam/package-lock.json"
- run: npm install

# Setup AWS SAM
- name: Setup AWS SAM
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ The `/opt` directory is only available at runtime within the SAM docker containe
"^/opt/baseLayer": "<rootDir>/.aws-sam/build/BaseLayer/baseLayer",
"^/opt/constantsLayer": "<rootDir>/.aws-sam/build/ConstantsLayer/constantsLayer",
...,
"^/opt/varianceLayer": "<rootDir>/.aws-sam/build/VarianceLayer/varianceLayer"
"^/opt/subAreaLayer": "<rootDir>/.aws-sam/build/subAreaLayer/subAreaLayer"
]
}
```
Expand Down
13 changes: 11 additions & 2 deletions arSam/handlers/activity/POST/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
const { marshall, unmarshall } = require('@aws-sdk/util-dynamodb');
const { DateTime } = require('luxon');
const { dynamodb, runQuery, TABLE_NAME, getOne, FISCAL_YEAR_FINAL_MONTH, TIMEZONE, sendResponse, logger } = require('/opt/baseLayer');
const { calculateVariance } = require('/opt/varianceLayer');
const {
dynamodb,
runQuery,
TABLE_NAME,
getOne,
FISCAL_YEAR_FINAL_MONTH,
TIMEZONE,
sendResponse,
logger,
calculateVariance
} = require('/opt/baseLayer');
const { EXPORT_VARIANCE_CONFIG } = require('/opt/constantsLayer');

exports.handlePost = async (event, context) => {
Expand Down
10 changes: 5 additions & 5 deletions arSam/handlers/variance/__tests__/variance.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ describe("Variance Test", () => {
});

test("Variance should trigger", async () => {
const { calculateVariance } = require("/opt/varianceLayer");
const { calculateVariance } = require("/opt/baseLayer");
const res = calculateVariance([8, 8, 8], 10, 0.2);
expect(res).toEqual({
averageHistoricValue: 8,
Expand All @@ -260,7 +260,7 @@ describe("Variance Test", () => {
});

test("Variance should trigger 2", async () => {
const { calculateVariance } = require("/opt/varianceLayer");
const { calculateVariance } = require("/opt/baseLayer");
const res = calculateVariance([8.5, 8.5, 8.5], 10.8, 0.2);
expect(res).toEqual({
averageHistoricValue: 8.5,
Expand All @@ -271,7 +271,7 @@ describe("Variance Test", () => {
});

test("Variance should trigger 3", async () => {
const { calculateVariance } = require("/opt/varianceLayer");
const { calculateVariance } = require("/opt/baseLayer");
const res = calculateVariance([8.5, 8.5, 8.5], 0.8, 0.2);
expect(res).toEqual({
averageHistoricValue: 8.5,
Expand All @@ -282,7 +282,7 @@ describe("Variance Test", () => {
});

test("Variance should not trigger", async () => {
const { calculateVariance } = require("/opt/varianceLayer");
const { calculateVariance } = require("/opt/baseLayer");
const res = calculateVariance([10.2, 10.2, 10.2], 10.2, 0.25);
expect(res).toEqual({
averageHistoricValue: 10.2,
Expand All @@ -293,7 +293,7 @@ describe("Variance Test", () => {
});

test("Variance should calculate variance with two years", async () => {
const { calculateVariance } = require("/opt/varianceLayer");
const { calculateVariance } = require("/opt/baseLayer");
const res = calculateVariance([8, 8, null], 10, 0.2);
expect(res).toEqual({
averageHistoricValue: 8,
Expand Down
2 changes: 1 addition & 1 deletion arSam/layers/baseLayer/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-BaseLayer:
# remove makefile in artifacts directory
rm "$(ARTIFACTS_DIR)/Makefile"
# install dependencies in artifacts directory
cd nodejs && yarn cache clean && yarn --update-checksums && yarn install --ignore-scripts --frozen-lockfile --modules-folder "$(ARTIFACTS_DIR)/nodejs/node_modules"
cd nodejs && npm install --prefix "$(ARTIFACTS_DIR)"/nodejs
# symlink to node_modules folder for testing purposes
cd "$(ARTIFACTS_DIR)" && ln -s "$(ARTIFACTS_DIR)"/nodejs/node_modules node_modules
# remove package.json to avoid rebuilding when changes don't relate to dependencies
Expand Down
55 changes: 52 additions & 3 deletions arSam/layers/baseLayer/baseLayer.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Logger, ResponseUtil, and DynamoUtilLayer are all included in this baseLayer
// Logger, ResponseUtils, VarianceUtils and DynamoUtils are all included in this baseLayer

// Logger
const { createLogger, format, transports } = require('winston');
Expand All @@ -22,7 +22,7 @@ const logger = createLogger({
transports: [new transports.Console()]
});

// ResponseUtil
// ResponseUtils
const sendResponse = function (code, data, context) {
const response = {
statusCode: code,
Expand All @@ -37,7 +37,55 @@ const sendResponse = function (code, data, context) {
return response;
};

// DynamoUtil
// VarianceUtils
function calculateVariance(
historicalValues,
currentValue,
variancePercentage
) {
const filteredInputs = historicalValues.filter((val) => val !== null && !isNaN(val));

logger.info("=== Calculating variance ===");
// We might receive two past years instead of three
const numberOfYearsProvided = filteredInputs.length;
logger.debug("Number of years provided:", numberOfYearsProvided);

// Get the average value across provided years
const averageHistoricValue = filteredInputs.reduce((acc, val) => acc + val, 0) / filteredInputs.length;
logger.debug("Average historic value:", averageHistoricValue);

// Calculate the percentage change only if averageHistoricValue is not zero
let percentageChange;
if (averageHistoricValue !== 0) {
percentageChange = Math.round(((currentValue - averageHistoricValue) / averageHistoricValue) * 100) / 100;
} else {
// Set percentageChange to 0 or some other default value if averageHistoricValue is zero
percentageChange = 0;
}

const percentageChangeAbs = Math.abs(percentageChange);

const varianceMessage = `Variance triggered: ${percentageChangeAbs >= variancePercentage ? "+" : "-"}${Math.round(percentageChangeAbs * 100)}%`;

// Since percentage change is absolute, we can subtract from variance percentage
// If negative, variance is triggered
const varianceTriggered = variancePercentage - percentageChangeAbs <= 0 ? true : false;
logger.info("Variance Triggered:", varianceTriggered);
logger.info("Variance percentageChange:", percentageChange);
logger.info("Variance variancePercentage:", variancePercentage);

const res = {
varianceMessage: varianceMessage,
varianceTriggered: varianceTriggered,
percentageChange: +percentageChange,
averageHistoricValue: averageHistoricValue
};
logger.info("Variance return obj:", res);
logger.info("=== Variance calculation complete ===");
return res;
}

// DynamoUtils
const { DynamoDB } = require('@aws-sdk/client-dynamodb');
const { marshall, unmarshall } = require('@aws-sdk/util-dynamodb');

Expand Down Expand Up @@ -309,6 +357,7 @@ async function incrementAndGetNextSubAreaID() {
module.exports = {
logger,
sendResponse,
calculateVariance,
ACTIVE_STATUS,
RESERVED_STATUS,
EXPIRED_STATUS,
Expand Down
4 changes: 0 additions & 4 deletions arSam/layers/constantsLayer/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,5 @@ build-ConstantsLayer:
cp -r * "$(ARTIFACTS_DIR)"
# remove makefile in artifacts directory
rm "$(ARTIFACTS_DIR)/Makefile"
# install dependencies in artifacts directory
cd nodejs && yarn cache clean && yarn --update-checksums && yarn install --ignore-scripts --frozen-lockfile --modules-folder "$(ARTIFACTS_DIR)/nodejs/node_modules"
# symlink to node_modules folder for testing purposes
cd "$(ARTIFACTS_DIR)" && ln -s "$(ARTIFACTS_DIR)"/nodejs/node_modules node_modules
# remove package.json to avoid rebuilding when changes don't relate to dependencies
rm "$(ARTIFACTS_DIR)/nodejs/package.json"
10 changes: 5 additions & 5 deletions arSam/layers/constantsLayer/nodejs/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ConstantsLayer",
"version": "0.0.1",
"author": "Digitalspace <contact@digitalspace.ca>",
"license": "MIT"
}
"name": "ConstantsLayer",
"version": "0.0.1",
"author": "Digitalspace <contact@digitalspace.ca>",
"license": "MIT"
}
2 changes: 1 addition & 1 deletion arSam/layers/formulaLayer/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-FormulaLayer:
# remove makefile in artifacts directory
rm "$(ARTIFACTS_DIR)/Makefile"
# install dependencies in artifacts directory
cd nodejs && yarn cache clean && yarn --update-checksums && yarn install --ignore-scripts --frozen-lockfile --modules-folder "$(ARTIFACTS_DIR)/nodejs/node_modules"
cd nodejs && npm install --prefix "$(ARTIFACTS_DIR)"/nodejs
# symlink to node_modules folder for testing purposes
cd "$(ARTIFACTS_DIR)" && ln -s "$(ARTIFACTS_DIR)"/nodejs/node_modules node_modules
# remove package.json to avoid rebuilding when changes don't relate to dependencies
Expand Down
2 changes: 1 addition & 1 deletion arSam/layers/functionsLayer/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-FunctionsLayer:
# remove makefile in artifacts directory
rm "$(ARTIFACTS_DIR)/Makefile"
# install dependencies in artifacts directory
cd nodejs && yarn cache clean && yarn --update-checksums && yarn install --ignore-scripts --frozen-lockfile --modules-folder "$(ARTIFACTS_DIR)/nodejs/node_modules"
cd nodejs && npm install --prefix "$(ARTIFACTS_DIR)"/nodejs
# symlink to node_modules folder for testing purposes
cd "$(ARTIFACTS_DIR)" && ln -s "$(ARTIFACTS_DIR)"/nodejs/node_modules node_modules
# remove package.json to avoid rebuilding when changes don't relate to dependencies
Expand Down
2 changes: 1 addition & 1 deletion arSam/layers/keycloakLayer/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-KeycloakLayer:
# remove makefile in artifacts directory
rm "$(ARTIFACTS_DIR)/Makefile"
# install dependencies in artifacts directory
cd nodejs && yarn cache clean && yarn --update-checksums && yarn install --ignore-scripts --frozen-lockfile --modules-folder "$(ARTIFACTS_DIR)/nodejs/node_modules"
cd nodejs && npm install --prefix "$(ARTIFACTS_DIR)"/nodejs
# symlink to node_modules folder for testing purposes
cd "$(ARTIFACTS_DIR)" && ln -s "$(ARTIFACTS_DIR)"/nodejs/node_modules node_modules
# remove package.json to avoid rebuilding when changes don't relate to dependencies
Expand Down
2 changes: 1 addition & 1 deletion arSam/layers/permissionLayer/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-PermissionLayer:
# remove makefile in artifacts directory
rm "$(ARTIFACTS_DIR)/Makefile"
# install dependencies in artifacts directory
cd nodejs && yarn cache clean && yarn --update-checksums && yarn install --ignore-scripts --frozen-lockfile --modules-folder "$(ARTIFACTS_DIR)/nodejs/node_modules"
cd nodejs && npm install --prefix "$(ARTIFACTS_DIR)"/nodejs
# symlink to node_modules folder for testing purposes
cd "$(ARTIFACTS_DIR)" && ln -s "$(ARTIFACTS_DIR)"/nodejs/node_modules node_modules
# remove package.json to avoid rebuilding when changes don't relate to dependencies
Expand Down
2 changes: 1 addition & 1 deletion arSam/layers/subAreaLayer/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-SubAreaLayer:
# remove makefile in artifacts directory
rm "$(ARTIFACTS_DIR)/Makefile"
# install dependencies in artifacts directory
cd nodejs && yarn cache clean && yarn --update-checksums && yarn install --ignore-scripts --frozen-lockfile --modules-folder "$(ARTIFACTS_DIR)/nodejs/node_modules"
cd nodejs && npm install --prefix "$(ARTIFACTS_DIR)"/nodejs
# symlink to node_modules folder for testing purposes
cd "$(ARTIFACTS_DIR)" && ln -s "$(ARTIFACTS_DIR)"/nodejs/node_modules node_modules
# remove package.json to avoid rebuilding when changes don't relate to dependencies
Expand Down
11 changes: 0 additions & 11 deletions arSam/layers/varianceLayer/Makefile

This file was deleted.

6 changes: 0 additions & 6 deletions arSam/layers/varianceLayer/nodejs/package.json

This file was deleted.

52 changes: 0 additions & 52 deletions arSam/layers/varianceLayer/varianceLayer.js

This file was deleted.

Loading

0 comments on commit 3d8b129

Please sign in to comment.