From 50fb2bbc3ea72f5233c523aeca460cadd6c55910 Mon Sep 17 00:00:00 2001 From: Damian Zehnder Date: Wed, 11 Oct 2023 17:04:14 +0200 Subject: [PATCH] feat: dynamodb put working for site and audit --- package-lock.json | 49 +++++++++++++++++++++++++++++++++++++++++++++++ package.json | 1 + src/db.js | 42 +++++++++++++++++++++++++--------------- src/index.js | 11 ++++++----- 4 files changed, 83 insertions(+), 20 deletions(-) diff --git a/package-lock.json b/package-lock.json index 05f95ac7..68601687 100644 --- a/package-lock.json +++ b/package-lock.json @@ -16,6 +16,7 @@ "@adobe/helix-universal-logger": "3.0.11", "@aws-sdk/client-dynamodb": "^3.427.0", "@aws-sdk/client-s3": "^3.427.0", + "@aws-sdk/lib-dynamodb": "^3.427.0", "axios": "^1.5.1" }, "devDependencies": { @@ -1965,6 +1966,22 @@ "node": ">=14.0.0" } }, + "node_modules/@aws-sdk/lib-dynamodb": { + "version": "3.427.0", + "resolved": "https://artifactory.corp.adobe.com/artifactory/api/npm/npm-adobe-release/@aws-sdk/lib-dynamodb/-/lib-dynamodb-3.427.0.tgz", + "integrity": "sha512-a08zd+rUyXXRXI1a/0Kja5yjZbuxWyZZA4/h7PmoW/vfuYFYUoDl0BcTh/hpdeiItlg2LRsJJ2zANf66BnI5IQ==", + "license": "Apache-2.0", + "dependencies": { + "@aws-sdk/util-dynamodb": "3.427.0", + "tslib": "^2.5.0" + }, + "engines": { + "node": ">=14.0.0" + }, + "peerDependencies": { + "@aws-sdk/client-dynamodb": "^3.0.0" + } + }, "node_modules/@aws-sdk/middleware-bucket-endpoint": { "version": "3.425.0", "resolved": "https://artifactory.corp.adobe.com/artifactory/api/npm/npm-adobe-release/@aws-sdk/middleware-bucket-endpoint/-/middleware-bucket-endpoint-3.425.0.tgz", @@ -2378,6 +2395,21 @@ "node": ">=14.0.0" } }, + "node_modules/@aws-sdk/util-dynamodb": { + "version": "3.427.0", + "resolved": "https://artifactory.corp.adobe.com/artifactory/api/npm/npm-adobe-release/@aws-sdk/util-dynamodb/-/util-dynamodb-3.427.0.tgz", + "integrity": "sha512-eXPdPM7ZIS4DfRJrGeWQJjcxnNAaioRrtgNpRoVy1HwMVYPtUvIyTuUWY8xIeJyXnX3/JrBJWCDoQPxkrp9T2w==", + "license": "Apache-2.0", + "dependencies": { + "tslib": "^2.5.0" + }, + "engines": { + "node": ">=14.0.0" + }, + "peerDependencies": { + "@aws-sdk/client-dynamodb": "^3.0.0" + } + }, "node_modules/@aws-sdk/util-endpoints": { "version": "3.418.0", "resolved": "https://registry.npmjs.org/@aws-sdk/util-endpoints/-/util-endpoints-3.418.0.tgz", @@ -22461,6 +22493,15 @@ "tslib": "^2.5.0" } }, + "@aws-sdk/lib-dynamodb": { + "version": "3.427.0", + "resolved": "https://artifactory.corp.adobe.com/artifactory/api/npm/npm-adobe-release/@aws-sdk/lib-dynamodb/-/lib-dynamodb-3.427.0.tgz", + "integrity": "sha512-a08zd+rUyXXRXI1a/0Kja5yjZbuxWyZZA4/h7PmoW/vfuYFYUoDl0BcTh/hpdeiItlg2LRsJJ2zANf66BnI5IQ==", + "requires": { + "@aws-sdk/util-dynamodb": "3.427.0", + "tslib": "^2.5.0" + } + }, "@aws-sdk/middleware-bucket-endpoint": { "version": "3.425.0", "resolved": "https://artifactory.corp.adobe.com/artifactory/api/npm/npm-adobe-release/@aws-sdk/middleware-bucket-endpoint/-/middleware-bucket-endpoint-3.425.0.tgz", @@ -22795,6 +22836,14 @@ "tslib": "^2.5.0" } }, + "@aws-sdk/util-dynamodb": { + "version": "3.427.0", + "resolved": "https://artifactory.corp.adobe.com/artifactory/api/npm/npm-adobe-release/@aws-sdk/util-dynamodb/-/util-dynamodb-3.427.0.tgz", + "integrity": "sha512-eXPdPM7ZIS4DfRJrGeWQJjcxnNAaioRrtgNpRoVy1HwMVYPtUvIyTuUWY8xIeJyXnX3/JrBJWCDoQPxkrp9T2w==", + "requires": { + "tslib": "^2.5.0" + } + }, "@aws-sdk/util-endpoints": { "version": "3.418.0", "resolved": "https://registry.npmjs.org/@aws-sdk/util-endpoints/-/util-endpoints-3.418.0.tgz", diff --git a/package.json b/package.json index a2d94bb7..906b3864 100644 --- a/package.json +++ b/package.json @@ -60,6 +60,7 @@ "@adobe/helix-universal-logger": "3.0.11", "@aws-sdk/client-dynamodb": "^3.427.0", "@aws-sdk/client-s3": "^3.427.0", + "@aws-sdk/lib-dynamodb": "^3.427.0", "axios": "^1.5.1" }, "devDependencies": { diff --git a/src/db.js b/src/db.js index 7a8e03dc..7e7c8393 100644 --- a/src/db.js +++ b/src/db.js @@ -9,26 +9,30 @@ * OF ANY KIND, either express or implied. See the License for the specific language * governing permissions and limitations under the License. */ -import { DynamoDB } from "@aws-sdk/client-dynamodb"; +import { DynamoDBClient } from '@aws-sdk/client-dynamodb'; +import { DynamoDBDocumentClient, PutCommand } from '@aws-sdk/lib-dynamodb'; import { log } from './util.js'; +import {json} from 'mocha/lib/reporters/index.js'; -const TABLE_SITES = 'sites'; -const TABLE_AUDITS = 'audits'; +const TABLE_SITES = 'spacecat-site'; +const TABLE_AUDITS = 'spacecat-audit-index'; function DB(config) { - const dynamoDb = new DynamoDB({ region: config.region }); + const client = new DynamoDBClient({ region: config.region }); + const docClient = DynamoDBDocumentClient.from(client); /** - * Save a record to the DynamoDB. - * @param {object} record - The new record to save. - */ + * Save a record to the DynamoDB. + * @param {object} record - The new record to save. + * @param tableName - The name of the table to save the record to. + */ async function saveRecord(record, tableName) { try { - const params = { + const command = new PutCommand({ TableName: tableName, Item: record, - }; - await dynamoDb.put(params); + }); + await docClient.send(command); } catch (error) { log('error', 'Error saving record: ', error); } @@ -41,17 +45,25 @@ function DB(config) { */ async function saveAuditIndex(site, audit) { const now = new Date().toISOString(); + const uuid = Date.now().toString(); + const newAudit = { + id: uuid, siteId: site.id, auditDate: now, - error: '', isLive: site.isLive, scores: { mobile: { - performance: audit.lighthouseResults.categories.performance.score, - seo: audit.lighthouseResults.categories.seo.score, - 'best-practices': audit.lighthouseResults.categories['best-practices'].score, - accessibility: audit.lighthouseResults.categories.accesibility.score, + performance: audit.result.mobile.categories.performance.score, + seo: audit.result.mobile.categories.seo.score, + 'best-practices': audit.result.mobile.categories['best-practices'].score, + accessibility: audit.result.mobile.categories.accessibility.score, + }, + desktop: { + performance: audit.result.desktop.categories.performance.score, + seo: audit.result.desktop.categories.seo.score, + 'best-practices': audit.result.desktop.categories['best-practices'].score, + accessibility: audit.result.desktop.categories.accessibility.score, }, }, }; diff --git a/src/index.js b/src/index.js index be08353e..05c027a0 100644 --- a/src/index.js +++ b/src/index.js @@ -35,16 +35,17 @@ async function run(request, context) { }); const auditResult = await psiClient.runAudit('https://www.bamboohr.com/'); + const uuid = Date.now().toString(); const site = { - id: 'bamboohr.com', - organization_id: 'bamboohr', + id: uuid, + githubURL: 'bamboohr', domain: 'bamboohr.com', - path: '/', - type: 'edge-delivery', + isLive: '/', + updatedAt: 'edge-delivery', }; await db.saveSite(site); await db.saveAuditIndex(site, auditResult); - return new Response('EXPERIENCE SUCCESS!!!'); + return new Response('SUCCESS'); } export const main = wrap(run)