Skip to content

Commit

Permalink
update for google cloud function
Browse files Browse the repository at this point in the history
  • Loading branch information
AshCripps authored and AshCripps committed Jul 8, 2020
1 parent b730ec0 commit 869fe04
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 14 deletions.
4 changes: 3 additions & 1 deletion ansible/roles/metrics/files/process-cloudflare/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
"description": "",
"main": "process-cloudflare.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "echo \"Error: no test specified\" && exit 1",
"processLogs": "node -e 'require(\"./process-cloudflare.js\").processLogs()'"
},
"author": "Rod <[email protected]> (http://r.va.gg/)",
"license": "Apache-2.0",
"dependencies": {
"@google-cloud/storage": "^4.7.0",
"split2": "~3.1.1",
"strftime": "~0.10.0"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
const { pipeline, Transform } = require('stream')
const split2 = require('split2')
const strftime = require('strftime').timezone(0)
const {Storage} = require('@google-cloud/storage');

const storage = new Storage({keyFilename: "metrics-processor-service-key.json"});

const jsonStream = new Transform({
readableObjectMode: true,
Expand Down Expand Up @@ -140,16 +143,30 @@ const logTransformStream = new Transform({
}
})

pipeline(
process.stdin,
split2(),
jsonStream,
logTransformStream,
process.stdout,
(err) => {
if (err) {
console.error('ERROR', err)
process.exit(1)
}
}
)

exports.processLogs = (data, context, callback) => {
console.log('Node version is: ' + process.version);
const file = data;
bucketName = file.bucket;
fileName = file.name;
console.log("DATA " + data);
console.log("BUCKET " + bucketName);
console.log("FILENAME " + fileName);
processedFile = fileName.split(".")[0];
processedFile = processedFile.split("_")[0].concat("_", processedFile.split("_")[1]);
console.log("PROCESSEDFILENAME " + processedFile);

storage.bucket(bucketName).file(file.name).createReadStream()
.on('error', function(err) { console.error(err) })
.pipe(split2())
.pipe(jsonStream)
.pipe(logTransformStream)
.pipe(storage.bucket('processed-logs-nodejs').file(processedFile).createWriteStream({resumable: false})
.on("error", err => {
console.log("ERROR: >> ", err)
})
.on("finish", () => {
console.log("FINISHED")
callback()
}));
}

0 comments on commit 869fe04

Please sign in to comment.