-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
160 lines (138 loc) · 5.12 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
const aws = require('aws-sdk');
const sqs = new aws.SQS({apiVersion: '2012-11-05'});
const { v4: uuidv4 } = require('uuid');
const personalizeevents = new aws.PersonalizeEvents();
const userDatasetArn = process.env.userDatasetArn;
const userTableName = process.env.userTableName;
const dynamo = new aws.DynamoDB.DocumentClient();
const tableName = process.env.tableName;
const indexName = process.env.indexName;
const queueUrl = process.env.queueUrl;
exports.handler = async (event, context) => {
//console.log('## ENVIRONMENT VARIABLES: ' + JSON.stringify(process.env));
//console.log('## EVENT: ' + JSON.stringify(event));
const body = JSON.parse(Buffer.from(event["body"], "base64"));
console.log('body: ' + JSON.stringify(body));
//const header = event['multiValueHeaders'];
//console.log('header: ' + JSON.stringify(header));
let response = "";
for(let i in body) {
let userId = body[i]['userId'];
console.log('userId: ' + userId);
let gender = body[i]['gender'];
console.log('gender: ' + gender);
let emotion = body[i]['emotion'];
console.log('emotion: ' + emotion);
// create user dataset
try {
var params = {
datasetArn: userDatasetArn,
users: [{
userId: userId,
properties: {
// "GENERATION": generation,
"GENDER": gender,
"EMOTION": emotion
}
}]
};
console.log('user params: ', JSON.stringify(params));
const result = await personalizeevents.putUsers(params).promise();
//console.log('putUser result: '+JSON.stringify(result));
} catch (error) {
console.log(error);
response = {
statusCode: 500,
body: error
};
}
// DynamodB for personalize users
var personalzeParams = {
TableName: userTableName,
Item: {
USER_ID: userId,
// GENERATION: generation,
GENDER: gender,
EMOTION: emotion,
}
};
// console.log('personalzeParams: ' + JSON.stringify(personalzeParams));
dynamo.put(personalzeParams, function (err, data) {
if (err) {
console.log('Failure: ' + err);
}
else {
// console.log('dynamodb put result: ' + JSON.stringify(data));
}
});
let queryParams;
if(emotion == "any") { // for any
queryParams = {
TableName: tableName,
IndexName: indexName,
KeyConditionExpression: "Emotion = :emotion",
ExpressionAttributeValues: {
":emotion": "surprised"
}
};
}
else {
queryParams = {
TableName: tableName,
IndexName: indexName,
KeyConditionExpression: "Emotion = :emotion",
ExpressionAttributeValues: {
":emotion": emotion
}
};
}
let dynamoQuery;
try {
dynamoQuery = await dynamo.query(queryParams).promise();
// console.log('queryDynamo: '+JSON.stringify(dynamoQuery));
// console.log('queryDynamo: '+dynamoQuery.Count);
} catch (error) {
console.log(error);
return;
}
let date = new Date();
const current = Math.floor(date.getTime()/1000.0);
console.log('current: ', current);
for(let i in dynamoQuery['Items']) {
let itemId = dynamoQuery['Items'][i]['ObjKey'];
let timestamp = current + parseInt(i);
// push the event to SQS
try {
let uuid = uuidv4();
let params = {
MessageDeduplicationId: uuid,
MessageAttributes: {},
MessageBody: JSON.stringify({
itemId: itemId,
userId: userId,
eventType: "click",
eventId: uuid,
timestamp: timestamp
}),
QueueUrl: queueUrl,
MessageGroupId: "generateDataset" // use single lambda for stable diffusion
};
console.log('params: '+JSON.stringify(params));
let result = await sqs.sendMessage(params).promise();
console.log("result="+JSON.stringify(result));
response = {
statusCode: 200,
body: "Success"
};
} catch (error) {
console.log(error);
response = {
statusCode: 500,
body: error
};
}
}
}
console.debug('response: ' + JSON.stringify(response));
return response;
};