-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler.js
More file actions
76 lines (59 loc) · 2.53 KB
/
handler.js
File metadata and controls
76 lines (59 loc) · 2.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
"use strict";
// todo -- clean up dependencies
const request = require("request");
const AWS = require("aws-sdk");
const getObservations = require("./helper/getObservations");
const locationHandler = require("./helper/locationHandler");
const getChecklistCache = require("./helper/getChecklistCache");
const writeToChecklistCache = require("./helper/writeToChecklistCache");
const SANTA_CLARA_COUNTY = "US-CA-085"; // Santa Clara County, California, US
module.exports.scan = async event => {
AWS.config.update({region: "us-west-2"});
const s3 = new AWS.S3({ apiVersion: "2006-03-01" });
const locations=[SANTA_CLARA_COUNTY];
console.log("making call to eBird");
// Make call to eBird for notable observations
const response = await getObservations();
const observations = response.data;
console.log("found " + observations.length + " observations");
// Create Cache of all checklists previously handled
const checklistCache = await getChecklistCache(s3);
const unhandledObservations = observations.filter(obs => !checklistCache[obs.subId]);
console.log("found " + unhandledObservations.length + " unhandledObservations");
unhandledObservations.forEach(obs => {
console.log("unhandled observation: " + obs.obsId);
console.log(obs.comName + " " + obs.subnational2Name);
});
// handle observations ==> Tweet
for (let i = 0; i < locations.length; i++) {
let location = locations[i];
await locationHandler(unhandledObservations,location,s3);
}
// TODO(kevingin) ensure tweet doesn't fail before adding to cache
// (Bug) if a checklist w/ 1 rare bird is submitted, then a new rare bird is added to same checklist, will not tweet new bird
// Living with the tradeoff for now. TODO(kevingin) fix this
// find unique checklists to limit calls to S3 (single checklist can have multiple birds)
const uniqueUnhandledChecklists = {};
for (let i = 0; i < unhandledObservations.length; i++) {
let observation = unhandledObservations[i];
uniqueUnhandledChecklists[observation.subId] = true;
}
// TODO: can we batch this call?
for (let subId in uniqueUnhandledChecklists) {
writeToChecklistCache(subId, s3);
}
// TODO: Don't return 200 if caught exception
return {
statusCode: 200,
body: JSON.stringify(
{
message: "execution successful",
input: event,
},
null,
2
),
};
// Use this code if you don't use the http event with the LAMBDA-PROXY integration
// return { message: 'Go Serverless v1.0! Your function executed successfully!', event };
};