-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
184 lines (173 loc) · 8.24 KB
/
index.js
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/**
* Copyright ©2018. The Regents of the University of California (Regents). All Rights Reserved.
*
* Permission to use, copy, modify, and distribute this software and its documentation
* for educational, research, and not-for-profit purposes, without fee and without a
* signed licensing agreement, is hereby granted, provided that the above copyright
* notice, this paragraph and the following two paragraphs appear in all copies,
* modifications, and distributions.
*
* Contact The Office of Technology Licensing, UC Berkeley, 2150 Shattuck Avenue,
* Suite 510, Berkeley, CA 94720-1620, (510) 643-7201, [email protected],
* http://ipira.berkeley.edu/industry-info for commercial licensing opportunities.
*
* IN NO EVENT SHALL REGENTS BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL,
* INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING OUT OF
* THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF REGENTS HAS BEEN ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*
* REGENTS SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
* SOFTWARE AND ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED HEREUNDER IS PROVIDED
* 'AS IS'. REGENTS HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
* ENHANCEMENTS, OR MODIFICATIONS.
*/
var pg = require('pg');
var config = require('config');
var joi = require('joi');
var AuthAPI = require('./lib/auth');
var DB = require('./lib/db');
var StatementXAPI = require('./lib/xapi');
var StatementCaliper = require('./lib/caliper');
/**
* The function invokes appropriate api request handlers to process the incoming statements.
*
* @param {Object} event Message payload that will be used as event while invoking Lambda function
* @param {Object} context Context object contains lambda runtime information such as functionName, CloudWatch log group etc.
* @param {Object} callback.err An error that occurred with custom response payload, if any
* @param {Object} callback.response Response object with the result and log stream information packaged as payload
*/
exports.handler = function(event, context, callback) {
require('./globals/sequelize.js');
var request = null;
var response = {
logGroupName: context.logGroupName,
logStreamName: context.logStreamName,
functionName: context.functionName,
invokeid: context.invokeid,
result: '',
code: null,
msg: ''
};
if (typeof event === 'string') {
try {
request = JSON.parse(event);
} catch (err) {
console.log('Request is not a well formed JSON');
response.result = 'Failed';
response.code = 400;
response.msg = 'Request not a well formed JSON';
context.callbackWaitsForEmptyEventLoop = false;
return callback(JSON.stringify(response));
}
} else if (typeof event === 'object') {
request = event;
} else {
console.log('Request is not a well formed JSON object');
response.result = 'Failed';
response.code = 400;
response.msg = 'Request not a well formed JSON';
context.callbackWaitsForEmptyEventLoop = false;
return callback(JSON.stringify(response));
}
// Sync data model
DB.init(function() {
// Verify write credentials
AuthAPI.verifyAuth(context, function(err) {
if (err) {
console.log('Authentication failed. \n' + JSON.stringify(err));
response.result = 'Failed';
response.code = 401;
response.msg = 'Authentication Failed. Check your credentials';
context.callbackWaitsForEmptyEventLoop = false;
return callback(JSON.stringify(response));
}
console.log('Connection Successful !!');
console.log('Event :' + JSON.stringify(event));
console.log('Context :' + JSON.stringify(context));
getStatementType(context.ctx, request, function(err, statementType) {
if (err) {
console.log('Unknown Statement type.\n' + JSON.stringify(err));
response.result = 'Failed';
response.code = 400;
response.msg = 'Statement not in xAPI or Caliper format';
context.callbackWaitsForEmptyEventLoop = false;
return callback(JSON.stringify(response));
}
// Process and save XAPI and Caliper statements accordingly
switch (statementType) {
case 'XAPI':
// Invoke XAPI handler
StatementXAPI.saveStatement(context.ctx, request, function(err, statement) {
if (err) {
console.log('Error during save: \n' + JSON.stringify(err));
response.result = 'Failed';
response.code = err.code || 400;
response.msg = err.msg || 'Error during save: \n' + JSON.stringify(err);
context.callbackWaitsForEmptyEventLoop = false;
return callback(JSON.stringify(response));
} else {
console.log('201 : XAPI statement processing successful with uuid: ' + statement.uuid);
response.msg = 'XAPI statement processing successful with uuid: ' + statement.uuid;
response.result = 'Success';
response.code = 201;
context.callbackWaitsForEmptyEventLoop = false;
return callback(null, response);
}
});
break;
case 'CALIPER':
// Invoke Caliper handler
StatementCaliper.saveStatement(context.ctx, request, function(err, statement) {
if (err) {
console.log('Error during save: \n' + JSON.stringify(err));
response.result = 'Failed';
response.code = err.code || 400;
response.msg = err.msg || 'Error during save: \n' + JSON.stringify(err);
context.callbackWaitsForEmptyEventLoop = false;
return callback(JSON.stringify(response));
} else {
var result = '201 : Caliper statement processing successful with uuid: ' + statement.uuid;
console.log(result);
response.result = 'Success';
response.code = 201;
response.msg = 'Caliper statement processing successful with uuid: ' + statement.uuid;
context.callbackWaitsForEmptyEventLoop = false;
return callback(null, response);
}
});
break;
default:
console.log('Statement not in XAPI or Caliper format');
response.result = 'Failed';
response.code = 400;
response.msg = 'Statement not in xAPI or Caliper format';
context.callbackWaitsForEmptyEventLoop = false;
return callback(JSON.stringify(response));
break;
}
});
});
});
};
/**
* The function determines which the statement type for the incoming request so that suitable handlers can be invoked subsequently.
*
* @param {Object} context Context object contains lambda runtime information such as functionName, CloudWatch log group etc.
* @param {Object} statement Message payload that will be used as request while invoking Lambda function
* @param {Object} callback.err An error that occurred if statement type is not XAPI or CALIPER type, if any
* @param {Object} callback.statementType Statement type that was determined
*/
var getStatementType = function(context, statement, callback) {
var statementType = '';
if (statement.hasOwnProperty('id') && statement.hasOwnProperty('@context') && statement.hasOwnProperty('eventTime') && statement.hasOwnProperty('actor') && statement.hasOwnProperty('object')) {
statementType = 'CALIPER';
console.log('Incoming statement is of type CALIPER');
} else if (statement.hasOwnProperty('id') && statement.hasOwnProperty('actor') && statement.hasOwnProperty('verb') && statement.hasOwnProperty('object') && statement.hasOwnProperty('timestamp')) {
statementType = 'XAPI';
console.log('Incoming statement is of type XAPI');
} else {
return callback(new Error('Statement not in xAPI or Caliper format'));
}
return callback(null, statementType);
};