Skip to content

Instantly share code, notes, and snippets.

@EvanBoyle
Created January 9, 2020 22:04
Show Gist options
  • Save EvanBoyle/a9584a5a923d96cb84bca9ae7a7ca7c4 to your computer and use it in GitHub Desktop.
Save EvanBoyle/a9584a5a923d96cb84bca9ae7a7ca7c4 to your computer and use it in GitHub Desktop.
Pulumi serialized lambda
exports.handler = __f0;
var __exports = {};
var __exports_athenaResultsBucket_proto = {};
__f2.prototype = __exports_athenaResultsBucket_proto;
Object.defineProperty(__exports_athenaResultsBucket_proto, "constructor", { configurable: true, writable: true, value: __f2 });
Object.defineProperty(__exports_athenaResultsBucket_proto, "apply", { configurable: true, writable: true, value: __f3 });
Object.defineProperty(__exports_athenaResultsBucket_proto, "get", { configurable: true, writable: true, value: __f4 });
var __exports_athenaResultsBucket = Object.create(__exports_athenaResultsBucket_proto);
__exports_athenaResultsBucket.value = "query-results-bucket-8cbcb4f";
__exports.athenaResultsBucket = __exports_athenaResultsBucket;
var __exports_databaseName = Object.create(__exports_athenaResultsBucket_proto);
__exports_databaseName.value = "analytics_dw";
__exports.databaseName = __exports_databaseName;
var __dwBucket = Object.create(__exports_athenaResultsBucket_proto);
__dwBucket.value = "datawarehouse-bucket-0263aec";
function __f1(__0, __1, __2, __3) {
return (function() {
with({ }) {
return function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
}
}).apply(undefined, undefined).apply(this, arguments);
}
function __f2(__0) {
return (function() {
with({ }) {
return function /*constructor*/(value) {
this.value = value;
};
}
}).apply(undefined, undefined).apply(this, arguments);
}
function __f3(__0) {
return (function() {
with({ }) {
return function /*apply*/(func) {
throw new Error("'apply' is not allowed from inside a cloud-callback. Use 'get' to retrieve the value of this Output directly.");
};
}
}).apply(undefined, undefined).apply(this, arguments);
}
function __f4() {
return (function() {
with({ }) {
return function /*get*/() {
return this.value;
};
}
}).apply(undefined, undefined).apply(this, arguments);
}
function __f0(__0) {
return (function() {
with({ __awaiter: __f1, exports: __exports, region: "us-west-2", moment: require("moment-timezone/index.js"), clicksTableName: "clicks", impressionsTableName: "impressions", aws_sdk_1: require("aws-sdk/lib/aws.js"), dwBucket: __dwBucket, aggregateTableName: "aggregates" }) {
return (event) => __awaiter(void 0, void 0, void 0, function* () {
const athena = require("athena-client");
const bucketUri = `s3://${exports.athenaResultsBucket.get()}`;
const clientConfig = {
bucketUri
};
const awsConfig = {
region
};
const athenaClient = athena.createClient(clientConfig, awsConfig);
let date = moment(event.time);
const partitionKey = date.utc().format("YYYY/MM/DD/HH");
const getAggregateQuery = (table) => `select count(*) from ${exports.databaseName.get()}.${table} where inserted_at='${partitionKey}'`;
const clicksPromise = athenaClient.execute(getAggregateQuery(clicksTableName)).toPromise();
const impressionsPromise = athenaClient.execute(getAggregateQuery(impressionsTableName)).toPromise();
const clickRows = yield clicksPromise;
const impressionRows = yield impressionsPromise;
const clickCount = clickRows.records[0]['_col0'];
const impressionsCount = impressionRows.records[0]['_col0'];
const data = `{ "event_type": "${clicksTableName}", "count": ${clickCount}, "time": "${partitionKey}" }\n{ "event_type": "${impressionsTableName}", "count": ${impressionsCount}, "time": "${partitionKey}"}`;
const s3Client = new aws_sdk_1.S3();
yield s3Client.putObject({
Bucket: dwBucket.get(),
Key: `${aggregateTableName}/${partitionKey}/results.json`,
Body: data
}).promise();
});
}
}).apply(undefined, undefined).apply(this, arguments);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment