Skip to content

Commit

Permalink
s
Browse files Browse the repository at this point in the history
  • Loading branch information
alinarublea committed Oct 12, 2023
1 parent 54f3913 commit e946a34
Show file tree
Hide file tree
Showing 5 changed files with 160 additions and 15 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ logs
.DS_Store
test-results.xml
.env
.idea
65 changes: 55 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,41 @@
{
"name": "@adobe/spacecat-audit-post-processor",
"version": "0.0.1",
"description": "SpaceCat Audit Post-Processor",
"description": "SpaceCat Audit Post Processor",
"main": "src/index.js",
"type": "module",
"scripts": {
"test": "c8 mocha",
"start": "nodemon",
"test": "c8 mocha -i -g 'Post-Deploy' --spec=test/**/*.test.js",
"test-postdeploy": "mocha -g 'Post-Deploy' --spec=test/**/*.test.js",
"lint": "eslint .",
"docs": "npx jsdoc2md -c .jsdoc.json --files 'src/*.js' > docs/API.md",
"semantic-release": "semantic-release",
"semantic-release-dry": "semantic-release --dry-run --no-ci --branches $CIRCLE_BRANCH",
"build": "hedy -v --test-bundle",
"deploy": "hedy -v --deploy --test --aws-region=us-east-1",
"deploy-routes": "hedy --no-build -no-hints -l major",
"deploy-ci": "hedy -v --deploy --test --pkgVersion=ci$CIRCLE_BUILD_NUM -l ci --cleanup-ci=24h",
"deploy-secrets": "hedy --aws-update-secrets --params-file=secrets/secrets.env",
"docs": "npm run docs:lint && npm run docs:build",
"docs:build": "npx @redocly/cli build-docs -o ./docs/index.html --config docs/openapi/redocly-config.yaml",
"docs:lint": "npx @redocly/cli lint --config docs/openapi/redocly-config.yaml",
"docs:serve": "npx @redocly/cli preview-docs --config docs/openapi/redocly-config.yaml",
"prepare": "husky install"
},
"wsk": {
"target": "aws",
"name": "spacecat-services/audit-post-processor@${version}",
"memory": 4096,
"awsRole!important": "arn:aws:iam::282898975672:role/spacecat-role-services",
"testUrl": "/_status_check/healthcheck.json",
"awsAttachAuthorizer": "",
"dev": {
"params-file": "secrets/dev-secrets.json"
},
"fastlyServiceId!important": "",
"timeout": 900000,
"nodeVersion": 18
},
"repository": {
"type": "git",
"url": "https://github.com/adobe-rnd/spacecat-audit-post-processor"
Expand All @@ -27,23 +52,43 @@
"reporter": "mocha-multi-reporters",
"reporter-options": "configFile=.mocha-multi.json"
},
"dependencies": {
"@adobe/fetch": "^4.1.0",
"@adobe/helix-shared-secrets": "^2.1.2",
"@adobe/helix-shared-wrap": "2.0.0",
"@adobe/helix-status": "10.0.10",
"@adobe/helix-universal-logger": "3.0.11",
"@aws-sdk/client-sns": "^3.427.0",
"axios": "1.5.1",
"esm": "3.2.25"
},
"devDependencies": {
"@adobe/eslint-config-helix": "2.0.2",
"@adobe/eslint-config-helix": "2.0.3",
"@adobe/helix-deploy": "9.3.6",
"@adobe/helix-universal": "4.4.0",
"@adobe/semantic-release-coralogix": "1.1.26",
"@adobe/semantic-release-skms-cmr": "1.0.12",
"@redocly/cli": "1.2.0",
"@semantic-release/changelog": "6.0.3",
"@semantic-release/exec": "6.0.3",
"@semantic-release/git": "10.0.1",
"@semantic-release/npm": "10.0.4",
"c8": "8.0.1",
"eslint": "8.46.0",
"dotenv": "16.3.1",
"eslint": "8.50.0",
"esmock": "2.5.1",
"husky": "8.0.3",
"jsdoc-to-markdown": "8.0.0",
"junit-report-builder": "3.0.1",
"lint-staged": "13.2.3",
"lint-staged": "14.0.1",
"mocha": "10.2.0",
"mocha-multi-reporters": "1.5.1",
"semantic-release": "21.0.7"
"mocha-suppress-logs": "0.3.1",
"nock": "13.3.3",
"nodemon": "3.0.1",
"semantic-release": "22.0.5",
"yaml": "2.3.2"
},
"lint-staged": {
"*.js": "eslint",
"*.cjs": "eslint"
}
}
}
33 changes: 28 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2021 Adobe. All rights reserved.
* Copyright 2023 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
Expand All @@ -9,12 +9,35 @@
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
import secrets from '@adobe/helix-shared-secrets';
import wrap from '@adobe/helix-shared-wrap';
import { logger } from '@adobe/helix-universal-logger';
import { helixStatus } from '@adobe/helix-status';
import { log } from './util.js';
import SNSAuditClient from './sns-client.js';

/**
* This is the main function
* @param {string} name name of the person to greet
* @returns {string} a greeting
* @param {Request} request the request object (see fetch api)
* @param {UniversalContext} context the context of the universal serverless function
* @returns {Response} a response
*/
export function main(name = 'world') {
return `Hello, ${name}.`;
async function run(request, context) {
log('error', context.invocation.event.Records[0].body);
const { message } = context.invocation.event.Records[0].body.message;
const mobile = message.scores.mobile.performance;
const desktop = message.scores.desktop.performance;
const snsAuditClient = SNSAuditClient();

if (mobile < 0.9 || desktop < 0.9) {
await snsAuditClient.sendNotification('PERFORMANCE DEGRADATION DETECTED!!');
}

return new Response('SUCCESS');
}

export const main = wrap(run)
.with(helixStatus)
.with(logger.trace)
.with(logger)
.with(secrets);
43 changes: 43 additions & 0 deletions src/sns-client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright 2023 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
import { SNSClient, PublishCommand } from '@aws-sdk/client-sns';
import { log } from './util.js';

const REGION = 'us-east-1';
const snsClient = new SNSClient({ region: process.env.REGION });
const topicArn = process.env.SNS_TOPIC;

function SNSAuditClient() {
async function sendNotification(message) {
// Set up the parameters for the publish command
const params = {
Message: message,
TopicArn: topicArn,
};

try {
const data = await snsClient.send(new PublishCommand(params));
log('info', `Message sent to the topic, ${data.MessageId}`);
} catch (err) {
log('error', `Error:, ${err}`);
throw err;
}

return {
statusCode: 200,
body: JSON.stringify({ message: 'SNS notification sent!' }),
};
}
return { sendNotification };
}

export default SNSAuditClient;
33 changes: 33 additions & 0 deletions src/util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright 2023 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
const log = (level, message, ...args) => {
const timestamp = new Date().toISOString();

switch (level) {
case 'info':
console.info(`[${timestamp}] INFO: ${message}`, ...args);
break;
case 'error':
console.error(`[${timestamp}] ERROR: ${message}`, ...args);
break;
case 'warn':
console.warn(`[${timestamp}] WARN: ${message}`, ...args);
break;
default:
console.log(`[${timestamp}] ${message}`, ...args);
break;
}
};

export {
log,
};

0 comments on commit e946a34

Please sign in to comment.