@meltwater/conversations-api-services 1.0.11 → 1.0.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3,6 +3,7 @@ import logger from '../../lib/logger.js';
3
3
  import { removePrefix } from '../../lib/externalId.helpers.js';
4
4
  import assert from 'assert';
5
5
  import { CredentialsApiClient } from '../http/credentialsApi.client.js';
6
+ import { MeltwaterAttributes } from '../../lib/logger.helpers.js';
6
7
 
7
8
  const INSTAGRAM_URL = 'https://graph.facebook.com';
8
9
  export class InstagramApiClient {
@@ -28,7 +29,7 @@ export class InstagramApiClient {
28
29
  ) {
29
30
  let response = {};
30
31
  const loggerChild = logger.child({
31
- 'meltwater.document.id': documentId,
32
+ [MeltwaterAttributes.DOCUMENTID]: documentId,
32
33
  });
33
34
 
34
35
  try {
@@ -67,7 +68,7 @@ export class InstagramApiClient {
67
68
  if (response.status !== 200) {
68
69
  loggerChild.error(
69
70
  `Failed to call instagram api for documentId ${documentId}`,
70
- response.body
71
+ { response: JSON.stringify(response.body) }
71
72
  );
72
73
  let error = new Error(
73
74
  `Failed to call instagram api for documentId ${documentId}`
@@ -76,7 +77,7 @@ export class InstagramApiClient {
76
77
  throw error;
77
78
  }
78
79
  loggerChild.debug('Instagram Response status', response.status);
79
- loggerChild.debug('Instagram Response body', response.body);
80
+ loggerChild.debug('Instagram Response body', JSON.stringify(response.body));
80
81
  loggerChild.debug('Instagram Response body.id', response.body.id);
81
82
  return response;
82
83
  }
@@ -85,12 +86,12 @@ export class InstagramApiClient {
85
86
  let response;
86
87
  const { externalId, documentId } = payload;
87
88
  const loggerChild = logger.child({
88
- 'meltwater.document.id': documentId,
89
+ [MeltwaterAttributes.DOCUMENTID]: documentId,
89
90
  });
90
91
 
91
92
  loggerChild.debug(
92
93
  `Starting to call instagram getPost api for documentId ${documentId}`,
93
- { payload }
94
+ { payload: JSON.stringify(payload) }
94
95
  );
95
96
  try {
96
97
  response = await this.getPost(
@@ -106,7 +107,7 @@ export class InstagramApiClient {
106
107
  );
107
108
  return response.body;
108
109
  } catch (err) {
109
- loggerChild.error('Error getLikes Instagram', { payload });
110
+ loggerChild.error('Error getLikes Instagram', { payload: JSON.stringify(payload) });
110
111
  return 0;
111
112
  }
112
113
  }
@@ -120,7 +121,7 @@ export class InstagramApiClient {
120
121
  ) {
121
122
  let response = {};
122
123
  const loggerChild = logger.child({
123
- 'meltwater.document.id': documentId,
124
+ [MeltwaterAttributes.DOCUMENTID]: documentId,
124
125
  });
125
126
 
126
127
  try {
@@ -152,7 +153,7 @@ export class InstagramApiClient {
152
153
  if (response.status !== 200) {
153
154
  loggerChild.error(
154
155
  `Failed to call instagram api for documentId ${documentId}`,
155
- response.body
156
+ { response: JSON.stringify(response.body) }
156
157
  );
157
158
  let error = new Error(
158
159
  `Failed to call instagram api for documentId ${documentId}`
@@ -161,7 +162,7 @@ export class InstagramApiClient {
161
162
  throw error;
162
163
  }
163
164
  loggerChild.debug('Instagram Response status', response.status);
164
- loggerChild.debug('Instagram Response body', response.body);
165
+ loggerChild.debug('Instagram Response body', { response: JSON.stringify(response.body) });
165
166
  loggerChild.debug('Instagram Response body.id', response.body.id);
166
167
  return response;
167
168
  }
@@ -169,7 +170,7 @@ export class InstagramApiClient {
169
170
  async hideApi(apiUrl, accessToken, hideStatus, documentId) {
170
171
  let response = {};
171
172
  const loggerChild = logger.child({
172
- 'meltwater.document.id': documentId,
173
+ [MeltwaterAttributes.DOCUMENTID]: documentId,
173
174
  });
174
175
 
175
176
  try {
@@ -195,7 +196,7 @@ export class InstagramApiClient {
195
196
  if (response.status !== 200) {
196
197
  loggerChild.error(
197
198
  `Failed to call instagram api for documentId ${documentId}`,
198
- response.body
199
+ { response: JSON.stringify(response.body) }
199
200
  );
200
201
  let error = new Error(
201
202
  `Failed to call instagram api for documentId ${documentId}`
@@ -204,7 +205,7 @@ export class InstagramApiClient {
204
205
  throw error;
205
206
  }
206
207
  loggerChild.debug('Instagram Response status', response.status);
207
- loggerChild.debug('Instagram Response body', response.body);
208
+ loggerChild.debug('Instagram Response body', { response: JSON.stringify(response.body) });
208
209
  loggerChild.debug('Instagram Response body.id', response.body.id);
209
210
  return response;
210
211
  }
@@ -212,7 +213,7 @@ export class InstagramApiClient {
212
213
  async getPost(apiUrl, accessToken, fields, documentId) {
213
214
  let response = {};
214
215
  const loggerChild = logger.child({
215
- 'meltwater.document.id': documentId,
216
+ [MeltwaterAttributes.DOCUMENTID]: documentId,
216
217
  });
217
218
 
218
219
  try {
@@ -236,9 +237,8 @@ export class InstagramApiClient {
236
237
  }
237
238
 
238
239
  if (response.status !== 200) {
239
- loggerChild.error(
240
- `Failed to call instagram api for documentId ${documentId}`,
241
- response.body
240
+ loggerChild.error(`Failed to call instagram api for documentId ${documentId}`,
241
+ { response: JSON.stringify(response.body) }
242
242
  );
243
243
  let error = new Error(
244
244
  `Failed to call instagram api for documentId ${documentId}`
@@ -247,7 +247,7 @@ export class InstagramApiClient {
247
247
  throw error;
248
248
  }
249
249
  loggerChild.debug('Instagram Response status', response.status);
250
- loggerChild.debug('Instagram Response body', response.body);
250
+ loggerChild.debug('Instagram Response body', { response: JSON.stringify(response.body) });
251
251
  loggerChild.debug('Instagram Response body.id', response.body.id);
252
252
  return response;
253
253
  }
@@ -256,12 +256,12 @@ export class InstagramApiClient {
256
256
  let response;
257
257
  const { inReplyToId, text, documentId, isMention, sourceId } = payload;
258
258
  const loggerChild = logger.child({
259
- 'meltwater.document.id': documentId,
259
+ [MeltwaterAttributes.DOCUMENTID]: documentId,
260
260
  });
261
261
 
262
262
  loggerChild.debug(
263
263
  `Starting to call instagram comment api for documentId ${documentId}`,
264
- payload
264
+ { payload: JSON.stringify(payload) }
265
265
  );
266
266
 
267
267
  this.validate(accessToken, payload);
@@ -304,12 +304,12 @@ export class InstagramApiClient {
304
304
  adCampaign,
305
305
  } = payload;
306
306
  const loggerChild = logger.child({
307
- 'meltwater.document.id': documentId,
307
+ [MeltwaterAttributes.DOCUMENTID]: documentId,
308
308
  });
309
309
 
310
310
  loggerChild.debug(
311
311
  `Starting to call instagram comment api for documentId ${documentId}`,
312
- payload
312
+ { payload: JSON.stringify(payload) }
313
313
  );
314
314
 
315
315
  this.validate(accessToken, payload);
@@ -377,9 +377,9 @@ export class InstagramApiClient {
377
377
  };
378
378
 
379
379
  const loggerChild = logger.child({
380
- 'meltwater.document.id': documentId,
381
- 'meltwater.company.id': companyId,
382
- 'meltwater.social.credential_id': credentialId,
380
+ [MeltwaterAttributes.DOCUMENTID]: documentId,
381
+ [MeltwaterAttributes.COMPANYID]: companyId,
382
+ [MeltwaterAttributes.CREDENTIALID]: credentialId,
383
383
  });
384
384
 
385
385
  let updatedDocument;
@@ -392,7 +392,7 @@ export class InstagramApiClient {
392
392
 
393
393
  loggerChild.debug(
394
394
  `finished fetching token for instagram for ${documentId} on company ${companyId} `,
395
- payload
395
+ { payload: JSON.stringify(payload) }
396
396
  );
397
397
 
398
398
  switch (discussionType) {
@@ -433,7 +433,7 @@ export class InstagramApiClient {
433
433
  throw new Error('Unsupported discussion type');
434
434
  }
435
435
  } catch (err) {
436
- loggerChild.error(documentId + ' - exception details ' + err, err);
436
+ loggerChild.error(`${documentId} - exception details`, err);
437
437
  }
438
438
 
439
439
  return apiResponse;
@@ -443,12 +443,12 @@ export class InstagramApiClient {
443
443
  let response;
444
444
  const { externalId, documentId } = payload;
445
445
  const loggerChild = logger.child({
446
- 'meltwater.document.id': documentId,
446
+ [MeltwaterAttributes.DOCUMENTID]: documentId,
447
447
  });
448
448
 
449
449
  loggerChild.debug(
450
450
  `Starting to call instagram hide api for documentId ${documentId}`,
451
- { payload }
451
+ { payload: JSON.stringify(payload) }
452
452
  );
453
453
 
454
454
  response = await this.hideApi(
@@ -469,12 +469,12 @@ export class InstagramApiClient {
469
469
  let response;
470
470
  const { externalId, documentId } = payload;
471
471
  const loggerChild = logger.child({
472
- 'meltwater.document.id': documentId,
472
+ [MeltwaterAttributes.DOCUMENTID]: documentId,
473
473
  });
474
474
 
475
475
  loggerChild.debug(
476
476
  `Starting to call instagram unhide api for documentId ${documentId}`,
477
- payload
477
+ { payload: JSON.stringify(payload) }
478
478
  );
479
479
 
480
480
  response = await this.hideApi(
@@ -496,12 +496,12 @@ export class InstagramApiClient {
496
496
  const { profileId, text, documentId, isMention, sourceId, threadId } =
497
497
  payload;
498
498
  const loggerChild = logger.child({
499
- 'meltwater.document.id': documentId,
499
+ [MeltwaterAttributes.DOCUMENTID]: documentId,
500
500
  });
501
501
 
502
502
  loggerChild.debug(
503
503
  `Starting to call instagram private message api for documentId ${documentId}`,
504
- payload
504
+ { payload: JSON.stringify(payload) }
505
505
  );
506
506
 
507
507
  this.validate(accessToken, payload);
@@ -3,6 +3,7 @@ import superagent from 'superagent';
3
3
  import logger from '../../lib/logger.js';
4
4
  import configuration from '../../lib/configuration.js';
5
5
  import { metricN } from '../../lib/metrics.helper.js';
6
+ import { MeltwaterAttributes } from '../../lib/logger.helpers.js';
6
7
 
7
8
  export class IRClient {
8
9
  RETRY_COUNT = 50;
@@ -35,7 +36,7 @@ export class IRClient {
35
36
  const wait = 100;
36
37
  const url = `${this.documentRevisionsServiceUrl}/${this.company._id}/`;
37
38
  const loggerChild = logger.child({
38
- 'meltwater.company.id': this.company._id,
39
+ [MeltwaterAttributes.COMPANYID]: this.company._id,
39
40
  });
40
41
 
41
42
  let result;
@@ -51,16 +52,17 @@ export class IRClient {
51
52
  .send(operations)
52
53
  .then((result) => result.body);
53
54
  } catch (error) {
54
- loggerChild.error(
55
- `Failed adding a revisions with operation:${JSON.stringify(
56
- operations
57
- )}`,
58
- { error, url, companyId: this.company._id, operations }
59
- );
55
+ loggerChild.error(`Failed adding a revisions with operations`, error, {
56
+ url,
57
+ [MeltwaterAttributes.COMPANYID]: this.company._id,
58
+ operations: JSON.stringify(operations)
59
+ });
60
60
  return;
61
61
  }
62
62
 
63
- loggerChild.info(`Finished adding revision operations`, { operations });
63
+ loggerChild.info(`Finished adding revision operations`, {
64
+ operations: JSON.stringify(operations)
65
+ });
64
66
 
65
67
  return result;
66
68
  }
@@ -70,7 +72,7 @@ export class IRClient {
70
72
  const wait = 100;
71
73
  const url = `${this.documentServiceUrl}/`;
72
74
  const loggerChild = logger.child({
73
- 'meltwater.company.id': this.company._id,
75
+ [MeltwaterAttributes.COMPANYID]: this.company._id,
74
76
  });
75
77
 
76
78
  let result;
@@ -88,9 +90,9 @@ export class IRClient {
88
90
  retryAttempt++;
89
91
  if (retryAttempt > this.RETRY_COUNT) {
90
92
  loggerChild.error(
91
- `Maximum Retrys hit for method modifyDocuments ${err.status} ${err.message}`,
92
- { err, companyId: this.company._id }
93
- );
93
+ `Maximum Retrys hit for method modifyDocuments ${err.status} ${err.message}`, err, {
94
+ [MeltwaterAttributes.COMPANYID]: this.company._id
95
+ });
94
96
  } else if (
95
97
  await this.retryBecauseRateLimiting(err, 'modifyDocuments')
96
98
  ) {
@@ -99,7 +101,7 @@ export class IRClient {
99
101
  }
100
102
 
101
103
  loggerChild.info(`Finished modifying documents operations`, {
102
- operations,
104
+ operations: JSON.stringify(operations),
103
105
  });
104
106
 
105
107
  return result;
@@ -118,8 +120,8 @@ export class IRClient {
118
120
  retryAttempt++;
119
121
  if (retryAttempt > this.RETRY_COUNT) {
120
122
  logger.error(
121
- `Maximum Retrys hit for method getDocumentById ${err.status} ${err.message}`,
122
- { err, 'meltwater.company.id': this.company._id }
123
+ `Maximum Retrys hit for method getDocumentById ${err.status} ${err.message}`, err,
124
+ { [MeltwaterAttributes.COMPANYID]: this.company._id }
123
125
  );
124
126
  } else if (
125
127
  await this.retryBecauseRateLimiting(err, 'getDocumentById')
@@ -131,15 +133,14 @@ export class IRClient {
131
133
 
132
134
  async retryBecauseRateLimiting(err, name) {
133
135
  const loggerChild = logger.child({
134
- 'meltwater.company.id': this.company._id,
136
+ [MeltwaterAttributes.COMPANYID]: this.company._id,
135
137
  });
136
138
 
137
139
  // rate limit error
138
140
  if (err.status === 429) {
139
141
  this.metrics.count(metricNames.irRatelimited);
140
142
  loggerChild.error(
141
- `Error Accessing ir.client method because rate limit: trying again ${name}`,
142
- { err }
143
+ `Error Accessing ir.client method because rate limit: trying again ${name}`, err
143
144
  );
144
145
  // should not be hitting rate limit in production
145
146
  // this is a patch for staging testing
@@ -148,13 +149,11 @@ export class IRClient {
148
149
  }
149
150
  if (err.status == 404) {
150
151
  loggerChild.info(
151
- `Document Not Found ir.client method ${name} ${err.status} ${err.message}`,
152
- { err }
152
+ `Document Not Found ir.client method ${name} ${err.status} ${err.message}`, err
153
153
  );
154
154
  } else {
155
155
  loggerChild.info(
156
- `Could not access ir.client method ${name} ${err.status} ${err.message}`,
157
- { err }
156
+ `Could not access ir.client method ${name} ${err.status} ${err.message}`, err
158
157
  );
159
158
  }
160
159
  return false;
@@ -189,9 +188,7 @@ export class IRClient {
189
188
 
190
189
  return result.body.exportKey;
191
190
  } catch (err) {
192
- logger.error('Error executing search against Export API', {
193
- err,
194
- });
191
+ logger.error('Error executing search against Export API', err);
195
192
  }
196
193
  }
197
194
 
@@ -227,7 +224,7 @@ export class IRClient {
227
224
  !index ||
228
225
  (acc.length <= limitActual &&
229
226
  acc[acc.length - 1].documentId !==
230
- document.quiddity.id)
227
+ document.quiddity.id)
231
228
  ) {
232
229
  acc.push(document.quiddity);
233
230
  } else if (
@@ -235,23 +232,20 @@ export class IRClient {
235
232
  acc.length <= limitActual &&
236
233
  acc[acc.length - 1].documentId === document.quiddity.id
237
234
  ) {
238
- logger.info(
239
- `Duplicates in IR Search Results for DocumentId ` +
240
- document.quiddity.id,
241
- { document, irTraceId: traceId }
242
- );
235
+ logger.info(`Duplicates in IR Search Results for DocumentId`, {
236
+ irTraceId: traceId,
237
+ document: JSON.stringify(document),
238
+ [MeltwaterAttributes.DOCUMENTID]: document.quiddity.id,
239
+ });
243
240
  }
244
241
  return acc;
245
242
  },
246
243
  []
247
244
  );
248
245
 
249
- logger.info(
250
- `Final Results Calculated, ${
251
- finalResults ? finalResults.length : 'no results'
252
- }`,
253
- { irTraceId: traceId, rune }
254
- );
246
+ logger.info(`Final Results Calculated, ${finalResults ? finalResults.length : 'no results'}`, {
247
+ irTraceId: traceId, rune
248
+ });
255
249
 
256
250
  return finalResults;
257
251
  // original return function
@@ -259,8 +253,7 @@ export class IRClient {
259
253
  // return document.quiddity;
260
254
  // })
261
255
  } catch (err) {
262
- logger.error('Error executing search against Search Service', {
263
- err,
256
+ logger.error('Error executing search against Search Service', err, {
264
257
  irTraceId: traceId,
265
258
  rune,
266
259
  });
@@ -280,8 +273,8 @@ export class IRClient {
280
273
  retryAttempt++;
281
274
  if (retryAttempt > this.RETRY_COUNT) {
282
275
  logger.error(
283
- `Maximum Retrys hit for method latest ${err.status} ${err.message}`,
284
- { err, 'meltwater.company.id': this.company._id }
276
+ `Maximum Retrys hit for method latest ${err.status} ${err.message}`, err,
277
+ { [MeltwaterAttributes.COMPANYID]: this.company._id }
285
278
  );
286
279
  } else if (await this.retryBecauseRateLimiting(err, 'latest')) {
287
280
  return await this.latest(rune, retryAttempt);