This repository has been archived by the owner on Feb 4, 2020. It is now read-only.
/
EventsUploader.js
239 lines (220 loc) · 6.77 KB
/
EventsUploader.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/**
* Copyright (c) 2017-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @flow
*/
import {
datasetEndpoint,
createCAEndpoint,
} from './APISettings';
import type {FeedUploaderConfigs} from './ConfigTypes';
import {getBatchSigStr, logBatchUploadStart, logBatchUploadEnd} from './UploadSession';
import {
MODE_ROW_NAMES,
MODE_CA,
MODE_OC,
} from './FeedUploaderConstants';
import {getLogger, getLoggerAWS} from './Logger';
import {getValidEvents} from './RequestDataBuilder';
import {
UNSUPPORTED_MODE,
ERROR_NO_CA_ID_OR_ACT_ID,
} from './ErrorTypes';
const querystring = require('querystring');
const request = require('requestretry');
const APIErrorTypes = require('./APIErrorTypes');
const MAX_RETRIES = 10; // Retry 10 times.
const RETRY_DELAY = 5000; // Wait 5s before trying again.
const RETRY_STRATEGY = request.RetryStrategies.HTTPOrNetworkError; // retry on 5xx or network errors.
export const uploadEventsBatch = (
events: Array<Object>,
postData: string,
uploadSessionTag: string,
fileOffset: number,
configs: FeedUploaderConfigs,
callback: batchUploadCallbackType,
): void => {
// Do the real uploading (calling into graph API) if not silent.
if (configs.silent !== true) {
getLogger().info(
`Posting rows ${getBatchSigStr({offset: fileOffset, size: events.length})} to \
${datasetEndpoint(configs)}`
);
_postEvents(events, postData, fileOffset, configs, uploadSessionTag, callback);
} else {
getLogger().info('Silent Mode');
callback(null, fileOffset, events, configs);
}
};
export const batchUploadCallback = (
err: Error,
fileOffset: number,
events: Array<?Object>,
configs: FeedUploaderConfigs,
): void => {
const rowName = MODE_ROW_NAMES[configs.mode];
if (err === null) {
getLogger().info(
`Rows ${getBatchSigStr({offset: fileOffset, size: events.length})} - ` +
`Successfully uploaded ${getValidEvents(events).length} ${rowName}.`
);
} else {
const error_subcode = APIErrorTypes.getErrorSubcode(JSON.parse(err.message));
if(error_subcode === APIErrorTypes.API_ERROR_SUBCODE_OVERLAPPED_PROGRESS) {
getLogger().info(
`Rows ${getBatchSigStr({offset: fileOffset, size: events.length})} ` +
`were previously uploaded.`
);
} else if (configs.aws) {
getLoggerAWS().error(JSON.stringify({
Rows: `${getBatchSigStr({offset: fileOffset, size: events.length})}`,
rowName: `${rowName}`,
err: err.message
}));
} else {
getLogger().error(
`Rows ${getBatchSigStr({offset: fileOffset, size: events.length})} ` +
`- Error uploading ${getValidEvents(events).length} ${rowName}: ` +
err.message
);
}
}
};
export type batchUploadCallbackType = (
err: ?Error,
fileOffset: number,
events: Array<Object>,
configs: FeedUploaderConfigs,
) => void;
const _postEvents = (
events: Array<Object>,
postData: string,
fileOffset: number,
configs: FeedUploaderConfigs,
uploadSessionTag: string,
callback: batchUploadCallbackType,
): void => {
request({
url: datasetEndpoint(configs),
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': Buffer.byteLength(postData),
},
body: postData,
fullResponse: true, // full response object not just the body.
maxAttempts: MAX_RETRIES,
retryDelay: RETRY_DELAY,
retryStrategy: RETRY_STRATEGY
})
.then(function (res) {
getLogger().verbose(`statusCode: ${res.statusCode}`);
getLogger().debug(`headers: ${JSON.stringify(res.headers)}`);
logBatchUploadEnd(
uploadSessionTag,
{offset: fileOffset, size: events.length},
);
callback(
res.statusCode === 200 ? null : new Error(res.body),
fileOffset,
events,
configs,
);
})
.catch(function(err) {
callback(err, fileOffset, events, configs);
});
logBatchUploadStart(
uploadSessionTag,
{offset: fileOffset, size: events.length}
);
};
export type uploadCallback = (
configs: FeedUploaderConfigs,
) => void;
export const createCustomAudience = (
configs: FeedUploaderConfigs,
callback: uploadCallback,
): void => {
// sanity check
if (configs.mode !== MODE_CA) {
callback(configs);
return;
}
if (configs.customAudienceId) {
callback(configs); // already has the id... just proceed
return;
}
if (!configs.adAccountId) {
if (configs.aws) {
getLoggerAWS().error(ERROR_NO_CA_ID_OR_ACT_ID);
} else {
getLogger().error(ERROR_NO_CA_ID_OR_ACT_ID.description);
}
return;
}
let postData = {
name: getCaNameFromFilePath(configs.inputFilePath),
subtype: 'CUSTOM',
access_token: configs.accessToken,
};
getLogger().info(`Creating a new custom audience (name: ${postData.name}) ...`);
postData = querystring.stringify(postData);
getLogger().silly(`postData: ${postData}`);
request({
url: createCAEndpoint(configs),
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': Buffer.byteLength(postData),
},
body: postData,
fullResponse: true, // full response object not just the body.
maxAttempts: MAX_RETRIES,
retryDelay: RETRY_DELAY,
retryStrategy: RETRY_STRATEGY
})
.then(function (res) {
getLogger().verbose(`statusCode: ${res.statusCode}`);
getLogger().debug(`headers: ${JSON.stringify(res.headers)}`);
const d = JSON.parse(res.body);
if (d.error) {
if (configs.aws) {
getLoggerAWS().error(JSON.stringify({failedAPIRes: `${JSON.stringify(d.error)}`}));
} else {
getLogger().error(`Custom audience creation failed. API responded:\n${JSON.stringify(d.error)}`);
}
} else if (d.id) {
getLogger().info(`Created a new custom audience (id: ${d.id})`);
configs.customAudienceId = d.id;
callback(configs);
} else {
if (configs.aws) {
getLoggerAWS().error(JSON.stringify({unknownErrRes: `${JSON.stringify(d)}`}))
} else {
getLogger().error(`Unknown error when creating custom audience. Response: ${JSON.stringify(d)}`);
}
}
})
.catch(function(err) {
if (configs.aws) {
getLoggerAWS().error(err.message);
} else {
getLogger().error(err.message);
}
});
};
export const getCaNameFromFilePath = (
path: string,
): string => {
// append today's date at the end
const date = new Date();
const dateString = '_' + date.toISOString().slice(0, 10);
// extract file name and remove extension (the last .something)
return path.replace(/^.*[\\\/]/, '').replace(/\.[^\.]+$/, '') + dateString;
};