Skip to content

Commit

Permalink
feat: dynamodb put working for site and audit
Browse files Browse the repository at this point in the history
  • Loading branch information
dzehnder committed Oct 11, 2023
1 parent 618fd71 commit 50fb2bb
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 20 deletions.
49 changes: 49 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
42 changes: 27 additions & 15 deletions src/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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,
},
},
};
Expand Down
11 changes: 6 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 50fb2bb

Please sign in to comment.