-
Notifications
You must be signed in to change notification settings - Fork 0
/
calculateUserDailyDigest.js
174 lines (145 loc) · 4.74 KB
/
calculateUserDailyDigest.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
import { Config, EnvironmentCredentials, Lambda } from 'aws-sdk';
import ConnectionClass from 'http-aws-es';
import ESSearch from '../common/search/search';
import buildQueryJson from '../common/query';
// Midnight yesterday
const getDateBegin = () => {
const dateBegin = new Date();
dateBegin.setDate(dateBegin.getDate() - 1);
dateBegin.setHours(0, 0, 0, 0);
return dateBegin;
};
// Midnight today
const getDateEnd = () => {
const dateEnd = new Date();
dateEnd.setHours(0, 0, 0, 0);
return dateEnd;
};
const formatInputQueryBody = (subscriptions, dateBegin, dateEnd) => subscriptions.map(value => ({
type: 'post',
search: [{ value }, { dateRange: [dateBegin, dateEnd] }]
}));
const trimDigest = (results, maxPosts) => {
const digestSections = results.length;
let postPool = digestSections >= maxPosts ? 0 : maxPosts - digestSections;
const newResults = [];
for (const entry of results) {
let postsToCopy = 1;
if (postPool > 0) {
postsToCopy = entry.posts.length - 1 <= postPool ? entry.posts.length : postPool + 1;
postPool -= postsToCopy - 1;
}
newResults.push({
searchTerms: entry.searchTerms,
posts: entry.posts.slice(0, postsToCopy)
});
}
return newResults;
};
const compareSortedArrays = (arr1, arr2) => {
if (arr1.length !== arr2.length) return false;
for (let i = 0; i < arr1.length; i += 1) {
if (arr1[i] !== arr2[i]) return false;
}
return true;
};
const transformMultiSearchResult = (userID, subscriptions, mSearchResult, EMAIL_MAX_POSTS) => {
const searchHits = mSearchResult.responses
.map(response => response.hits.hits.map(hit => ({
PostId: hit._id,
...hit._source
})));
const mapPostIdsToPosts = new Map();
searchHits.forEach(searchHit => searchHit
.forEach(post => mapPostIdsToPosts.set(post.PostId, post)));
const mapPostsToSearches = new Map();
for (let i = 0; i < subscriptions.length; i += 1) {
// eslint-disable-next-line no-loop-func
searchHits[i].forEach((post) => {
if (mapPostsToSearches.has(post.PostId)) {
mapPostsToSearches.get(post.PostId).add(subscriptions[i].toLowerCase());
} else {
mapPostsToSearches.set(post.PostId, new Set([subscriptions[i].toLowerCase()]));
}
});
}
let results = [];
let totalPostsCount = 0;
for (const entry of mapPostsToSearches.entries()) {
const postId = entry[0];
const searchTerms = Array.from(entry[1].values());
searchTerms.sort();
let added = false;
for (const digest of results) {
if (compareSortedArrays(digest.searchTerms, searchTerms)) {
digest.posts.push(mapPostIdsToPosts.get(postId));
added = true;
break;
}
}
if (!added) results.push({ searchTerms, posts: [mapPostIdsToPosts.get(postId)] });
totalPostsCount += 1;
}
results.sort((a, b) => b.searchTerms.length - a.searchTerms.length);
if (totalPostsCount > EMAIL_MAX_POSTS) results = trimDigest(results, EMAIL_MAX_POSTS);
return { userID, results };
};
const sendDailyDigestEmail = async (result, FunctionName, lambda) => lambda
.invoke({
FunctionName,
InvocationType: 'Event',
Payload: JSON.stringify(result)
})
.promise();
const processUserSubscriptions = async (
userID,
subscriptions,
EMAIL_MAX_POSTS,
FunctionName,
esSearchClient,
lambda
) => {
console.log(`Processing ${subscriptions.length} subscriptions of user with ID: ${userID}`);
const buildQueryInput = formatInputQueryBody(subscriptions, getDateBegin(), getDateEnd());
const body = [];
for (const input of buildQueryInput) {
body.push({ index: 'posts', type: 'post' });
body.push(buildQueryJson(input, undefined, 10000));
}
const mSearchInput = { body };
const mSearchResult = await esSearchClient.doMultiSearch(mSearchInput);
const result = transformMultiSearchResult(userID, subscriptions, mSearchResult, EMAIL_MAX_POSTS);
if (result.results.length) {
// eslint-disable-next-line no-use-before-define
await exportedFunctions.sendDailyDigestEmail(result, FunctionName, lambda);
console.log(`Sent ${result.results.length} digest updates to user ${userID}`);
}
};
export const handler = async (event) => {
const {
ES_SEARCH_API,
EMAIL_MAX_POSTS,
SEND_DIGEST_EMAIL_LAMBDA_NAME: FunctionName
} = process.env;
const esSearchClient = new ESSearch({
host: ES_SEARCH_API,
connectionClass: ConnectionClass,
awsConfig: new Config({
credentials: new EnvironmentCredentials('AWS')
})
});
const lambda = new Lambda();
const { userId: userID, subscriptions } = event;
await processUserSubscriptions(
userID,
subscriptions,
EMAIL_MAX_POSTS,
FunctionName,
esSearchClient,
lambda
);
};
export const exportedFunctions = {
handler,
sendDailyDigestEmail
};