Initial commit
This commit is contained in:
commit
827c6b3134
8 changed files with 1899 additions and 0 deletions
8
.gitignore
vendored
Normal file
8
.gitignore
vendored
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
# package directories
|
||||||
|
node_modules
|
||||||
|
jspm_packages
|
||||||
|
|
||||||
|
# Serverless directories
|
||||||
|
.serverless
|
||||||
|
|
||||||
|
*.env*
|
22
Makefile
Normal file
22
Makefile
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
yarnbin = $(shell yarn bin)
|
||||||
|
|
||||||
|
test:
|
||||||
|
sh -c "source .env; $(yarnbin)/serverless invoke local --function check"
|
||||||
|
|
||||||
|
deploy:
|
||||||
|
sh -c "source .env.production; $(yarnbin)/serverless deploy"
|
||||||
|
|
||||||
|
setup:
|
||||||
|
docker-compose up -d
|
||||||
|
aws dynamodb create-table \
|
||||||
|
--endpoint-url http://localhost:8898 \
|
||||||
|
--table-name lightspeed-checks-dev \
|
||||||
|
--attribute-definitions '[{"AttributeName":"area","AttributeType":"S"},{"AttributeName":"time","AttributeType":"S"}]' \
|
||||||
|
--key-schema '[{"AttributeName":"area","KeyType":"HASH"},{"AttributeName":"time","KeyType":"RANGE"}]' \
|
||||||
|
--provisioned-throughput '{"ReadCapacityUnits":1,"WriteCapacityUnits":1}' \
|
||||||
|
--region local
|
||||||
|
|
||||||
|
cleanup:
|
||||||
|
docker-compose stop && docker-compose rm -f
|
||||||
|
|
||||||
|
reboot: cleanup setup
|
6
docker-compose.yml
Normal file
6
docker-compose.yml
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
version: '2'
|
||||||
|
services:
|
||||||
|
dynamodb:
|
||||||
|
image: bhuisgen/alpine-dynamodb
|
||||||
|
ports:
|
||||||
|
- "8898:8000"
|
17
dynamodb-connect.js
Normal file
17
dynamodb-connect.js
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
const ddb = exports
|
||||||
|
const aws = require('aws-sdk')
|
||||||
|
|
||||||
|
ddb.connect = () => {
|
||||||
|
return new aws.DynamoDB.DocumentClient(options())
|
||||||
|
}
|
||||||
|
|
||||||
|
function options () {
|
||||||
|
if (process.env.DYNAMODB_ENDPOINT) {
|
||||||
|
return {
|
||||||
|
region: 'localhost',
|
||||||
|
endpoint: process.env.DYNAMODB_ENDPOINT
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return {}
|
||||||
|
}
|
||||||
|
}
|
72
handler.js
Normal file
72
handler.js
Normal file
|
@ -0,0 +1,72 @@
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
const http = require('http')
|
||||||
|
const ddb = require('./dynamodb-connect').connect()
|
||||||
|
|
||||||
|
const TIMESTAMP = new Date().toISOString()
|
||||||
|
|
||||||
|
function zones () {
|
||||||
|
return process.env.ZONES
|
||||||
|
.split(',')
|
||||||
|
.map(zone => zone.trim())
|
||||||
|
}
|
||||||
|
|
||||||
|
function promisedGet (url) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
http.get(url, response => {
|
||||||
|
let data = ''
|
||||||
|
|
||||||
|
response.on('data', chunk => data += chunk)
|
||||||
|
response.on('end', () => resolve(data))
|
||||||
|
})
|
||||||
|
.on('error', error => reject(error))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
function formattedDetails (details) {
|
||||||
|
return {
|
||||||
|
time: TIMESTAMP,
|
||||||
|
area: details.area,
|
||||||
|
count: parseInt(details.count),
|
||||||
|
target: parseInt(details.target),
|
||||||
|
orders: parseInt(details.orders),
|
||||||
|
needed: parseInt(details.needed),
|
||||||
|
status: details.status,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports.check = (event, context, callback) => {
|
||||||
|
promisedGet('http://www.golightspeed.com/signup/crowdsource.php')
|
||||||
|
.then(data => {
|
||||||
|
const zonePromises = zones().map(zone => {
|
||||||
|
const statusRegexp = new RegExp(
|
||||||
|
'http://www.golightspeed.com/signup/json.php\\?sa=' +
|
||||||
|
zone +
|
||||||
|
'&orders=\\d+&status=[^\']*', "gm"
|
||||||
|
)
|
||||||
|
const url = data.match(statusRegexp)[0]
|
||||||
|
|
||||||
|
return promisedGet(url)
|
||||||
|
})
|
||||||
|
|
||||||
|
return Promise.all(zonePromises)
|
||||||
|
})
|
||||||
|
.then(results => {
|
||||||
|
const storePromises = results.map(result => {
|
||||||
|
const data = JSON.parse(result).features[0].properties
|
||||||
|
|
||||||
|
const params = {
|
||||||
|
TableName: process.env.LIGHTSPEED_TABLE,
|
||||||
|
Item: formattedDetails(data),
|
||||||
|
}
|
||||||
|
|
||||||
|
return ddb.put(params).promise()
|
||||||
|
})
|
||||||
|
|
||||||
|
return Promise.all(storePromises)
|
||||||
|
})
|
||||||
|
.then(data => {
|
||||||
|
callback(null, { success: true })
|
||||||
|
})
|
||||||
|
.catch(error => callback(error))
|
||||||
|
}
|
5
package.json
Normal file
5
package.json
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
{
|
||||||
|
"devDependencies": {
|
||||||
|
"serverless": "^1.26.1"
|
||||||
|
}
|
||||||
|
}
|
54
serverless.yml
Normal file
54
serverless.yml
Normal file
|
@ -0,0 +1,54 @@
|
||||||
|
service: lightspeed-watcher
|
||||||
|
|
||||||
|
provider:
|
||||||
|
name: aws
|
||||||
|
runtime: nodejs6.10
|
||||||
|
environment:
|
||||||
|
ZONES: ${env:ZONES}
|
||||||
|
LIGHTSPEED_TABLE: lightspeed-checks-${opt:stage, self:provider.stage}
|
||||||
|
DYNAMODB_ENDPOINT: ${env:DYNAMODB_ENDPOINT}
|
||||||
|
|
||||||
|
resources:
|
||||||
|
Resources:
|
||||||
|
DynamoDbTable:
|
||||||
|
Type: AWS::DynamoDB::Table
|
||||||
|
Properties:
|
||||||
|
TableName: ${self:provider.environment.LIGHTSPEED_TABLE}
|
||||||
|
AttributeDefinitions:
|
||||||
|
- AttributeName: area
|
||||||
|
AttributeType: S
|
||||||
|
- AttributeName: time
|
||||||
|
AttributeType: S
|
||||||
|
KeySchema:
|
||||||
|
- AttributeName: area
|
||||||
|
KeyType: HASH
|
||||||
|
- AttributeName: time
|
||||||
|
KeyType: RANGE
|
||||||
|
ProvisionedThroughput:
|
||||||
|
ReadCapacityUnits: 1
|
||||||
|
WriteCapacityUnits: 1
|
||||||
|
DynamoDBIamPolicy:
|
||||||
|
Type: AWS::IAM::Policy
|
||||||
|
DependsOn: DynamoDbTable
|
||||||
|
Properties:
|
||||||
|
PolicyName: lambda-dynamodb
|
||||||
|
PolicyDocument:
|
||||||
|
Version: '2012-10-17'
|
||||||
|
Statement:
|
||||||
|
- Effect: Allow
|
||||||
|
Action:
|
||||||
|
- dynamodb:GetItem
|
||||||
|
- dynamodb:PutItem
|
||||||
|
- dynamodb:Query
|
||||||
|
Resource: arn:aws:dynamodb:*:*:table/${self:provider.environment.LIGHTSPEED_TABLE}
|
||||||
|
Roles:
|
||||||
|
- Ref: IamRoleLambdaExecution
|
||||||
|
|
||||||
|
functions:
|
||||||
|
check:
|
||||||
|
handler: handler.check
|
||||||
|
timeout: 15
|
||||||
|
events:
|
||||||
|
- schedule:
|
||||||
|
rate: rate(60 minutes)
|
||||||
|
enabled: true
|
Loading…
Reference in a new issue