AWS Lambda function to update newly added DynamoDB recordsNode AWS lambda with pg-promiseDeploy a Python...

Is there a familial term for apples and pears?

Showing the closure of a compact subset need not be compact

"which" command doesn't work / path of Safari?

New order #4: World

Can I make popcorn with any corn?

How old can references or sources in a thesis be?

Copenhagen passport control - US citizen

Does the radius of the Spirit Guardians spell depend on the size of the caster?

How do we improve the relationship with a client software team that performs poorly and is becoming less collaborative?

My colleague's body is amazing

Is it legal to have the "// (c) 2019 John Smith" header in all files when there are hundreds of contributors?

Could a US political party gain complete control over the government by removing checks & balances?

XeLaTeX and pdfLaTeX ignore hyphenation

Why is the design of haulage companies so “special”?

Why Is Death Allowed In the Matrix?

cryptic clue: mammal sounds like relative consumer (8)

Why are 150k or 200k jobs considered good when there are 300k+ births a month?

Closed subgroups of abelian groups

Is it possible to make sharp wind that can cut stuff from afar?

least quadratic residue under GRH: an EXPLICIT bound

What makes Graph invariants so useful/important?

Shell script can be run only with sh command

Copycat chess is back

What Brexit solution does the DUP want?



AWS Lambda function to update newly added DynamoDB records


Node AWS lambda with pg-promiseDeploy a Python script as an AWS Lambda micro web serviceTest cases for Ansible lambda module for deployment of AWS LambdasChecking a DB table before doing an insert using AWS LambdaAWS Lambda Python function, to enable / disable access to a VPC environmentOptimizing Node.js http get for AWS LambdaAWS Lambda Function to Register in CognitoLambda@Edge function for handling redirects and rewritesJenkins Powershell script to copy code from Github to AWS Lambda






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}







3












$begingroup$


Approach



All data is submitted into my DynamoDB from another Lambda > API Integration function whereas the lastUpdated row gets inserted as null and then the function below basically polls my database every 1 minute looking for new rows that have a null value & performs actions on them until the lastUpdated can then be updated (once an action is performed elsewhere)



I have the following Node.JS (runtime v8.10) executing on AWS Lambda:



const AWS = require("aws-sdk");
const game = require('game-api');
const uuid = require("uuid");

AWS.config.update({
region: "us-east-1"
});

exports.handler = async (event, context) => {

//set db
var documentClient = new AWS.DynamoDB.DocumentClient();

//find matches
var params = {
TableName: 'matches',
FilterExpression:'updated_at = :updated_at',
ExpressionAttributeValues: {
":updated_at": 0,
}
};
var rows = await documentClient.scan(params).promise();

//game object
let gameAPI = new game(
[
"admin@game.com",
"password"
]
);

await gameAPI.login();

for (let match of rows.Items) {

var player_params = {
TableName: 'players',
Key: { "match_id": match.id }
};

let player_row = await documentClient.get(player_params).promise();

//grab stats and compare
var stats = await gameAPI.getStatsBR(
player_row.Item.player_1_name,
player_row.Item.player_1_network
);
var new_data = compareModiified(
match.match_type,
player_row.Item.player_1_last_updated,
stats
);

if(new_data === true) {

//we have new data
let kills;
let matches;
let kills_completed;
let matches_completed;
let modified;

switch(match.match_type) {
case 'myself':
kills = stats.group.solo.kills;
kills_completed = (kills - player_row.Item.player_1_kills);
matches = stats.group.solo.matches;
matches_completed = (matches - player_row.Item.player_1_matches);
modified = stats.group.solo.lastModified;
break;
case 'solo':
kills = stats.group.duo.kills;
kills_completed = (kills - player_row.Item.player_1_kills);
matches = stats.group.duo.matches;
matches_completed = (matches - player_row.Item.player_1_matches);
modified = stats.group.duo.lastModified;
break;
case 'duo':
kills = stats.group.squad.kills;
kills_completed = (kills - player_row.Item.player_1_kills);
matches = stats.group.squad.matches;
matches_completed = (matches - player_row.Item.player_1_matches);
modified = stats.group.squad.lastModified;
break;
}

var update_params = {
TableName:"matches",
Key: { "id": match.id },
UpdateExpression: "SET #status = :status, updated_at = :modified",
ExpressionAttributeNames:{
"#status":"status"
},
ExpressionAttributeValues:{
":status": 1,
":modified": modified
}
};
await documentClient.update(update_params).promise();

var report_params = {
Item: {
'match_id': match.id,
'kills': kills_completed,
'matches': matches_completed,
'completed_at': new Date().getTime()
},
TableName : 'reports'
};
await documentClient.put(report_params).promise();

} else {

//we don't have new data.
console.log("We don't have new data, let's not do anything..");

}

}

return {
statusCode: 200
};

};

function compareModiified(match_type, db_modifiied, stats) {
var stats_modified;
switch(match_type) {
case 'myself':
stats_modified = stats.group.solo.lastModified;
break;
case 'solo':
stats_modified = stats.group.duo.lastModified;
break;
case 'duo':
stats_modified = stats.group.squad.lastModified;
break;
}
return (stats_modified > db_modifiied);
}









share|improve this question









New contributor




Curtis is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$





This question has an open bounty worth +100
reputation from Curtis ending in 6 days.


The question is widely applicable to a large audience. A detailed canonical answer is required to address all the concerns.












  • 1




    $begingroup$
    The calculation for kills_completed and matches_completed could be done after the switch statement since they're all identical. Then you'd have 4 less lines of code and the same functionality.
    $endgroup$
    – Shelby115
    Apr 3 at 14:42


















3












$begingroup$


Approach



All data is submitted into my DynamoDB from another Lambda > API Integration function whereas the lastUpdated row gets inserted as null and then the function below basically polls my database every 1 minute looking for new rows that have a null value & performs actions on them until the lastUpdated can then be updated (once an action is performed elsewhere)



I have the following Node.JS (runtime v8.10) executing on AWS Lambda:



const AWS = require("aws-sdk");
const game = require('game-api');
const uuid = require("uuid");

AWS.config.update({
region: "us-east-1"
});

exports.handler = async (event, context) => {

//set db
var documentClient = new AWS.DynamoDB.DocumentClient();

//find matches
var params = {
TableName: 'matches',
FilterExpression:'updated_at = :updated_at',
ExpressionAttributeValues: {
":updated_at": 0,
}
};
var rows = await documentClient.scan(params).promise();

//game object
let gameAPI = new game(
[
"admin@game.com",
"password"
]
);

await gameAPI.login();

for (let match of rows.Items) {

var player_params = {
TableName: 'players',
Key: { "match_id": match.id }
};

let player_row = await documentClient.get(player_params).promise();

//grab stats and compare
var stats = await gameAPI.getStatsBR(
player_row.Item.player_1_name,
player_row.Item.player_1_network
);
var new_data = compareModiified(
match.match_type,
player_row.Item.player_1_last_updated,
stats
);

if(new_data === true) {

//we have new data
let kills;
let matches;
let kills_completed;
let matches_completed;
let modified;

switch(match.match_type) {
case 'myself':
kills = stats.group.solo.kills;
kills_completed = (kills - player_row.Item.player_1_kills);
matches = stats.group.solo.matches;
matches_completed = (matches - player_row.Item.player_1_matches);
modified = stats.group.solo.lastModified;
break;
case 'solo':
kills = stats.group.duo.kills;
kills_completed = (kills - player_row.Item.player_1_kills);
matches = stats.group.duo.matches;
matches_completed = (matches - player_row.Item.player_1_matches);
modified = stats.group.duo.lastModified;
break;
case 'duo':
kills = stats.group.squad.kills;
kills_completed = (kills - player_row.Item.player_1_kills);
matches = stats.group.squad.matches;
matches_completed = (matches - player_row.Item.player_1_matches);
modified = stats.group.squad.lastModified;
break;
}

var update_params = {
TableName:"matches",
Key: { "id": match.id },
UpdateExpression: "SET #status = :status, updated_at = :modified",
ExpressionAttributeNames:{
"#status":"status"
},
ExpressionAttributeValues:{
":status": 1,
":modified": modified
}
};
await documentClient.update(update_params).promise();

var report_params = {
Item: {
'match_id': match.id,
'kills': kills_completed,
'matches': matches_completed,
'completed_at': new Date().getTime()
},
TableName : 'reports'
};
await documentClient.put(report_params).promise();

} else {

//we don't have new data.
console.log("We don't have new data, let's not do anything..");

}

}

return {
statusCode: 200
};

};

function compareModiified(match_type, db_modifiied, stats) {
var stats_modified;
switch(match_type) {
case 'myself':
stats_modified = stats.group.solo.lastModified;
break;
case 'solo':
stats_modified = stats.group.duo.lastModified;
break;
case 'duo':
stats_modified = stats.group.squad.lastModified;
break;
}
return (stats_modified > db_modifiied);
}









share|improve this question









New contributor




Curtis is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$





This question has an open bounty worth +100
reputation from Curtis ending in 6 days.


The question is widely applicable to a large audience. A detailed canonical answer is required to address all the concerns.












  • 1




    $begingroup$
    The calculation for kills_completed and matches_completed could be done after the switch statement since they're all identical. Then you'd have 4 less lines of code and the same functionality.
    $endgroup$
    – Shelby115
    Apr 3 at 14:42














3












3








3





$begingroup$


Approach



All data is submitted into my DynamoDB from another Lambda > API Integration function whereas the lastUpdated row gets inserted as null and then the function below basically polls my database every 1 minute looking for new rows that have a null value & performs actions on them until the lastUpdated can then be updated (once an action is performed elsewhere)



I have the following Node.JS (runtime v8.10) executing on AWS Lambda:



const AWS = require("aws-sdk");
const game = require('game-api');
const uuid = require("uuid");

AWS.config.update({
region: "us-east-1"
});

exports.handler = async (event, context) => {

//set db
var documentClient = new AWS.DynamoDB.DocumentClient();

//find matches
var params = {
TableName: 'matches',
FilterExpression:'updated_at = :updated_at',
ExpressionAttributeValues: {
":updated_at": 0,
}
};
var rows = await documentClient.scan(params).promise();

//game object
let gameAPI = new game(
[
"admin@game.com",
"password"
]
);

await gameAPI.login();

for (let match of rows.Items) {

var player_params = {
TableName: 'players',
Key: { "match_id": match.id }
};

let player_row = await documentClient.get(player_params).promise();

//grab stats and compare
var stats = await gameAPI.getStatsBR(
player_row.Item.player_1_name,
player_row.Item.player_1_network
);
var new_data = compareModiified(
match.match_type,
player_row.Item.player_1_last_updated,
stats
);

if(new_data === true) {

//we have new data
let kills;
let matches;
let kills_completed;
let matches_completed;
let modified;

switch(match.match_type) {
case 'myself':
kills = stats.group.solo.kills;
kills_completed = (kills - player_row.Item.player_1_kills);
matches = stats.group.solo.matches;
matches_completed = (matches - player_row.Item.player_1_matches);
modified = stats.group.solo.lastModified;
break;
case 'solo':
kills = stats.group.duo.kills;
kills_completed = (kills - player_row.Item.player_1_kills);
matches = stats.group.duo.matches;
matches_completed = (matches - player_row.Item.player_1_matches);
modified = stats.group.duo.lastModified;
break;
case 'duo':
kills = stats.group.squad.kills;
kills_completed = (kills - player_row.Item.player_1_kills);
matches = stats.group.squad.matches;
matches_completed = (matches - player_row.Item.player_1_matches);
modified = stats.group.squad.lastModified;
break;
}

var update_params = {
TableName:"matches",
Key: { "id": match.id },
UpdateExpression: "SET #status = :status, updated_at = :modified",
ExpressionAttributeNames:{
"#status":"status"
},
ExpressionAttributeValues:{
":status": 1,
":modified": modified
}
};
await documentClient.update(update_params).promise();

var report_params = {
Item: {
'match_id': match.id,
'kills': kills_completed,
'matches': matches_completed,
'completed_at': new Date().getTime()
},
TableName : 'reports'
};
await documentClient.put(report_params).promise();

} else {

//we don't have new data.
console.log("We don't have new data, let's not do anything..");

}

}

return {
statusCode: 200
};

};

function compareModiified(match_type, db_modifiied, stats) {
var stats_modified;
switch(match_type) {
case 'myself':
stats_modified = stats.group.solo.lastModified;
break;
case 'solo':
stats_modified = stats.group.duo.lastModified;
break;
case 'duo':
stats_modified = stats.group.squad.lastModified;
break;
}
return (stats_modified > db_modifiied);
}









share|improve this question









New contributor




Curtis is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$




Approach



All data is submitted into my DynamoDB from another Lambda > API Integration function whereas the lastUpdated row gets inserted as null and then the function below basically polls my database every 1 minute looking for new rows that have a null value & performs actions on them until the lastUpdated can then be updated (once an action is performed elsewhere)



I have the following Node.JS (runtime v8.10) executing on AWS Lambda:



const AWS = require("aws-sdk");
const game = require('game-api');
const uuid = require("uuid");

AWS.config.update({
region: "us-east-1"
});

exports.handler = async (event, context) => {

//set db
var documentClient = new AWS.DynamoDB.DocumentClient();

//find matches
var params = {
TableName: 'matches',
FilterExpression:'updated_at = :updated_at',
ExpressionAttributeValues: {
":updated_at": 0,
}
};
var rows = await documentClient.scan(params).promise();

//game object
let gameAPI = new game(
[
"admin@game.com",
"password"
]
);

await gameAPI.login();

for (let match of rows.Items) {

var player_params = {
TableName: 'players',
Key: { "match_id": match.id }
};

let player_row = await documentClient.get(player_params).promise();

//grab stats and compare
var stats = await gameAPI.getStatsBR(
player_row.Item.player_1_name,
player_row.Item.player_1_network
);
var new_data = compareModiified(
match.match_type,
player_row.Item.player_1_last_updated,
stats
);

if(new_data === true) {

//we have new data
let kills;
let matches;
let kills_completed;
let matches_completed;
let modified;

switch(match.match_type) {
case 'myself':
kills = stats.group.solo.kills;
kills_completed = (kills - player_row.Item.player_1_kills);
matches = stats.group.solo.matches;
matches_completed = (matches - player_row.Item.player_1_matches);
modified = stats.group.solo.lastModified;
break;
case 'solo':
kills = stats.group.duo.kills;
kills_completed = (kills - player_row.Item.player_1_kills);
matches = stats.group.duo.matches;
matches_completed = (matches - player_row.Item.player_1_matches);
modified = stats.group.duo.lastModified;
break;
case 'duo':
kills = stats.group.squad.kills;
kills_completed = (kills - player_row.Item.player_1_kills);
matches = stats.group.squad.matches;
matches_completed = (matches - player_row.Item.player_1_matches);
modified = stats.group.squad.lastModified;
break;
}

var update_params = {
TableName:"matches",
Key: { "id": match.id },
UpdateExpression: "SET #status = :status, updated_at = :modified",
ExpressionAttributeNames:{
"#status":"status"
},
ExpressionAttributeValues:{
":status": 1,
":modified": modified
}
};
await documentClient.update(update_params).promise();

var report_params = {
Item: {
'match_id': match.id,
'kills': kills_completed,
'matches': matches_completed,
'completed_at': new Date().getTime()
},
TableName : 'reports'
};
await documentClient.put(report_params).promise();

} else {

//we don't have new data.
console.log("We don't have new data, let's not do anything..");

}

}

return {
statusCode: 200
};

};

function compareModiified(match_type, db_modifiied, stats) {
var stats_modified;
switch(match_type) {
case 'myself':
stats_modified = stats.group.solo.lastModified;
break;
case 'solo':
stats_modified = stats.group.duo.lastModified;
break;
case 'duo':
stats_modified = stats.group.squad.lastModified;
break;
}
return (stats_modified > db_modifiied);
}






javascript node.js amazon-web-services nosql






share|improve this question









New contributor




Curtis is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Curtis is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 22 hours ago







Curtis













New contributor




Curtis is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked Apr 3 at 14:05









CurtisCurtis

163




163




New contributor




Curtis is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Curtis is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Curtis is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






This question has an open bounty worth +100
reputation from Curtis ending in 6 days.


The question is widely applicable to a large audience. A detailed canonical answer is required to address all the concerns.








This question has an open bounty worth +100
reputation from Curtis ending in 6 days.


The question is widely applicable to a large audience. A detailed canonical answer is required to address all the concerns.










  • 1




    $begingroup$
    The calculation for kills_completed and matches_completed could be done after the switch statement since they're all identical. Then you'd have 4 less lines of code and the same functionality.
    $endgroup$
    – Shelby115
    Apr 3 at 14:42














  • 1




    $begingroup$
    The calculation for kills_completed and matches_completed could be done after the switch statement since they're all identical. Then you'd have 4 less lines of code and the same functionality.
    $endgroup$
    – Shelby115
    Apr 3 at 14:42








1




1




$begingroup$
The calculation for kills_completed and matches_completed could be done after the switch statement since they're all identical. Then you'd have 4 less lines of code and the same functionality.
$endgroup$
– Shelby115
Apr 3 at 14:42




$begingroup$
The calculation for kills_completed and matches_completed could be done after the switch statement since they're all identical. Then you'd have 4 less lines of code and the same functionality.
$endgroup$
– Shelby115
Apr 3 at 14:42










0






active

oldest

votes












Your Answer





StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");

StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "196"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});






Curtis is a new contributor. Be nice, and check out our Code of Conduct.










draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f216782%2faws-lambda-function-to-update-newly-added-dynamodb-records%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes








Curtis is a new contributor. Be nice, and check out our Code of Conduct.










draft saved

draft discarded


















Curtis is a new contributor. Be nice, and check out our Code of Conduct.













Curtis is a new contributor. Be nice, and check out our Code of Conduct.












Curtis is a new contributor. Be nice, and check out our Code of Conduct.
















Thanks for contributing an answer to Code Review Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


Use MathJax to format equations. MathJax reference.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f216782%2faws-lambda-function-to-update-newly-added-dynamodb-records%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

is 'sed' thread safeWhat should someone know about using Python scripts in the shell?Nexenta bash script uses...

How do i solve the “ No module named 'mlxtend' ” issue on Jupyter?

Pilgersdorf Inhaltsverzeichnis Geografie | Geschichte | Bevölkerungsentwicklung | Politik | Kultur...