eventregistry 8.9.1 → 8.10.1

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.
@@ -0,0 +1,516 @@
1
+ import * as _ from "lodash";
2
+ import { Query, QueryParamsBase } from "./base";
3
+ import { EventRegistry } from "./eventRegistry";
4
+ import { ReturnInfo } from "./returnInfo";
5
+ import { EventRegistryStatic } from "./types";
6
+
7
+ export class QueryMentions extends Query<RequestMentions> {
8
+ public params = {};
9
+ constructor(args: EventRegistryStatic.QueryMentions.Arguments = {}) {
10
+ super();
11
+ const {
12
+ eventTypeUri,
13
+ keywords,
14
+ conceptUri,
15
+ categoryUri,
16
+ sourceUri,
17
+ sourceLocationUri,
18
+ sourceGroupUri,
19
+ industryUri,
20
+ locationUri,
21
+ lang,
22
+ dateStart,
23
+ dateEnd,
24
+ ignoreEventTypeUri,
25
+ ignoreKeywords,
26
+ ignoreConceptUri,
27
+ ignoreCategoryUri,
28
+ ignoreSourceUri,
29
+ ignoreSourceLocationUri,
30
+ ignoreSourceGroupUri,
31
+ ignoreIndustryUri,
32
+ ignoreLocationUri,
33
+ ignoreLang,
34
+ showDuplicates,
35
+ startSourceRankPercentile = 0,
36
+ endSourceRankPercentile = 100,
37
+ minSentiment = -1,
38
+ maxSentiment = 1,
39
+ requestedResult = new RequestMentions(),
40
+ } = args;
41
+
42
+ this.setVal("action", "getMentions");
43
+ this.setQueryArrVal(eventTypeUri, "eventTypeUri", undefined, "or");
44
+ this.setQueryArrVal(keywords, "keyword", "keywordOper", "and");
45
+ this.setQueryArrVal(conceptUri, "conceptUri", "conceptOper", "and");
46
+ this.setQueryArrVal(categoryUri, "categoryUri", "categoryOper", "or");
47
+ this.setQueryArrVal(sourceUri, "sourceUri", undefined, "or");
48
+ this.setQueryArrVal(sourceLocationUri, "sourceLocationUri", undefined, "or");
49
+ this.setQueryArrVal(sourceGroupUri, "sourceGroupUri", undefined, "or");
50
+ this.setQueryArrVal(industryUri, "industryUri", "industryOper", "or");
51
+ this.setQueryArrVal(locationUri, "locationUri", undefined, "or");
52
+ this.setQueryArrVal(lang, "lang", undefined, "or");
53
+ if (!_.isUndefined(dateStart)) {
54
+ this.setDateVal("dateStart", dateStart);
55
+ }
56
+ if (!_.isUndefined(dateEnd)) {
57
+ this.setDateVal("dateEnd", dateEnd);
58
+ }
59
+
60
+ this.setQueryArrVal(ignoreEventTypeUri, "ignoreEventTypeUri", undefined, "or");
61
+ this.setQueryArrVal(ignoreKeywords, "ignoreKeywords", undefined, "or");
62
+ this.setQueryArrVal(ignoreConceptUri, "ignoreConceptUri", undefined, "or");
63
+ this.setQueryArrVal(ignoreCategoryUri, "ignoreCategoryUri", undefined, "or");
64
+ this.setQueryArrVal(ignoreSourceUri, "ignoreSourceUri", undefined, "or");
65
+ this.setQueryArrVal(ignoreSourceLocationUri, "ignoreSourceLocationUri", undefined, "or");
66
+ this.setQueryArrVal(ignoreSourceGroupUri, "ignoreSourceGroupUri", undefined, "or");
67
+ this.setQueryArrVal(ignoreIndustryUri, "ignoreIndustryUri", undefined, "or");
68
+ this.setQueryArrVal(ignoreLocationUri, "ignoreLocationUri", undefined, "or");
69
+
70
+ this.setQueryArrVal(ignoreLang, "ignoreLang", undefined, "or");
71
+ this.setValIfNotDefault("showDuplicates", showDuplicates, false);
72
+ if (startSourceRankPercentile < 0 || startSourceRankPercentile % 10 !== 0 || startSourceRankPercentile > 100) {
73
+ throw new Error("StartSourceRankPercentile: Value should be in range 0-90 and divisible by 10.");
74
+ }
75
+ if (endSourceRankPercentile < 0 || endSourceRankPercentile % 10 !== 0 || endSourceRankPercentile > 100) {
76
+ throw new Error("EndSourceRankPercentile: Value should be in range 0-90 and divisible by 10.");
77
+ }
78
+ if (startSourceRankPercentile > endSourceRankPercentile) {
79
+ throw new Error("SourceRankPercentile: startSourceRankPercentile should be less than endSourceRankPercentile");
80
+ }
81
+
82
+ if (startSourceRankPercentile !== 0 ) {
83
+ this.setVal("startSourceRankPercentile", startSourceRankPercentile);
84
+ }
85
+
86
+ if (endSourceRankPercentile !== 100 ) {
87
+ this.setVal("endSourceRankPercentile", endSourceRankPercentile);
88
+ }
89
+ if (minSentiment > -1 && minSentiment <= 1 ) {
90
+ this.setVal("minSentiment", minSentiment);
91
+ }
92
+ if (maxSentiment >= -1 && maxSentiment < 1 ) {
93
+ this.setVal("maxSentiment", maxSentiment);
94
+ }
95
+ this.setRequestedResult(requestedResult);
96
+ }
97
+
98
+ public get path() {
99
+ return "/api/v1/eventType/mention";
100
+ }
101
+
102
+ /**
103
+ * Set the single result type that you would like to be returned. Any previously set result types will be overwritten.
104
+ * Result types can be the classes that extend RequestMentions base class (see classes below).
105
+ */
106
+ public setRequestedResult(requestMentions: RequestMentions) {
107
+ if (!(requestMentions instanceof RequestMentions)) {
108
+ throw new Error("QueryMentions class can only accept result requests that are of type RequestMentions");
109
+ }
110
+ this.resultTypeList = [
111
+ requestMentions,
112
+ ];
113
+ }
114
+
115
+ public static initWithMentionUriList(...args);
116
+ public static initWithMentionUriList(uriList) {
117
+ const q = new QueryMentions();
118
+ if (!_.isArray(uriList)) {
119
+ throw new Error("uriList has to be a list of strings that represent mention uris");
120
+ }
121
+ q.params = {
122
+ action: "getMentions",
123
+ mentionUri: uriList,
124
+ };
125
+ }
126
+
127
+ public static initWithMentionUriWgtList(...args);
128
+ public static initWithMentionUriWgtList(uriWgtList) {
129
+ const q = new QueryMentions();
130
+ if (!_.isArray(uriWgtList)) {
131
+ throw new Error("uriList has to be a list of strings that represent mention uris");
132
+ }
133
+ q.params = {
134
+ action: "getMentions",
135
+ mentionUriWgtList: _.join(uriWgtList, ","),
136
+ };
137
+ return q;
138
+ }
139
+
140
+ public static initWithComplexQuery(...args);
141
+ public static initWithComplexQuery(complexQuery) {
142
+ const query = new QueryMentions();
143
+ if (_.isString(complexQuery)) {
144
+ query.setVal("query", complexQuery);
145
+ } else if (_.isObject(complexQuery)) {
146
+ query.setVal("query", JSON.stringify(complexQuery));
147
+ } else {
148
+ throw new Error("The instance of query parameter was not a string or an object");
149
+ }
150
+ return query;
151
+ }
152
+ }
153
+
154
+ export class QueryMentionsIter extends QueryMentions {
155
+ private readonly er: EventRegistry;
156
+ private readonly sortBy;
157
+ private readonly sortByAsc;
158
+ private readonly returnInfo;
159
+ private readonly maxItems;
160
+ private page = 0;
161
+ private pages = 1;
162
+ private items = [];
163
+ private returnedSoFar = 0;
164
+ private index = 0;
165
+ private callback: (item) => void = _.noop;
166
+ private doneCallback: (error?) => void = _.noop;
167
+ private errorMessage;
168
+
169
+ constructor(er: EventRegistry, args: EventRegistryStatic.QueryEvents.IteratorArguments = {}) {
170
+ super(args as EventRegistryStatic.QueryMentions.Arguments);
171
+ const {
172
+ sortBy = "rel",
173
+ sortByAsc = false,
174
+ returnInfo = undefined,
175
+ maxItems = -1,
176
+ } = args;
177
+ this.er = er;
178
+ this.sortBy = sortBy;
179
+ this.sortByAsc = sortByAsc;
180
+ this.returnInfo = returnInfo;
181
+ this.maxItems = maxItems;
182
+ }
183
+
184
+ public async count(): Promise<number> {
185
+ this.setRequestedResult(new RequestMentionsInfo());
186
+ const response = await this.er.execQuery(this);
187
+ if (_.has(response, "error")) {
188
+ console.error(_.get(response, "error"));
189
+ }
190
+ return _.get(response, "mentions.totalResults", 0);
191
+ }
192
+
193
+ /**
194
+ * Execute query and fetch batches of articles of the specified size (eventBatchSize)
195
+ * @param callback callback function that'll be called every time we get a new batch of events from the backend
196
+ * @param doneCallback callback function that'll be called when everything is complete
197
+ */
198
+ public execQuery(callback: (item) => void, doneCallback?: (error?) => void) {
199
+ if (callback) { this.callback = callback; }
200
+ if (doneCallback) { this.doneCallback = doneCallback; }
201
+ this.iterate();
202
+ }
203
+
204
+ public static initWithComplexQuery(er, complexQuery, args: EventRegistryStatic.QueryMentions.IteratorArguments = {}) {
205
+ const query = new QueryMentionsIter(er, args);
206
+ if (_.isString(complexQuery)) {
207
+ query.setVal("query", complexQuery);
208
+ } else if (_.isObject(complexQuery)) {
209
+ query.setVal("query", JSON.stringify(complexQuery));
210
+ } else {
211
+ throw new Error("The instance of query parameter was not a string or an object");
212
+ }
213
+ return query;
214
+ }
215
+
216
+ private async iterate() {
217
+ if (this.current) {
218
+ this.callback(this.current);
219
+ this.index += 1;
220
+ } else if (!await this.getNextBatch()) {
221
+ this.doneCallback(this.errorMessage);
222
+ return;
223
+ }
224
+ return this.iterate();
225
+ }
226
+
227
+ /**
228
+ * Extract the results according to maxItems
229
+ * @param response response from the backend
230
+ */
231
+ private extractResults(response): Array<{[name: string]: any}> {
232
+ const results = _.get(response, "mentions.results", []);
233
+ const extractedSize = this.maxItems !== -1 ? this.maxItems - this.returnedSoFar : _.size(results);
234
+ return _.compact(_.pullAt(results, _.range(0, extractedSize)) as Array<{}>);
235
+ }
236
+
237
+ private get current() {
238
+ return this.items[this.index] || undefined;
239
+ }
240
+
241
+ private async getNextBatch() {
242
+ try {
243
+ this.page += 1;
244
+ if (this.page > this.pages || (this.maxItems !== -1 && this.returnedSoFar >= this.maxItems)) {
245
+ return false;
246
+ }
247
+ const requestEventsInfo = new RequestMentionsInfo({
248
+ page: this.page,
249
+ count: 50,
250
+ sortBy: this.sortBy,
251
+ sortByAsc: this.sortByAsc,
252
+ returnInfo: this.returnInfo,
253
+ });
254
+ this.setRequestedResult(requestEventsInfo);
255
+ if (this.er.verboseOutput) {
256
+ console.log(`Downloading event page ${this.page}...`);
257
+ }
258
+ const response = await this.er.execQuery(this);
259
+ const error = _.get(response, "error", "");
260
+ if (error) {
261
+ this.errorMessage = `Error while obtaining a list of events: ${_.get(response, "error")}`;
262
+ } else {
263
+ this.pages = _.get(response, "events.pages", 0);
264
+ }
265
+ const results = this.extractResults(response);
266
+ this.returnedSoFar += _.size(results);
267
+ this.items = [...this.items, ...results];
268
+ return true;
269
+ } catch (error) {
270
+ console.error(error);
271
+ return false;
272
+ }
273
+ }
274
+ }
275
+
276
+ export class RequestMentions {}
277
+
278
+ export class RequestMentionsInfo extends RequestMentions {
279
+ public resultType = "mentions";
280
+ public params;
281
+ constructor(args: EventRegistryStatic.QueryEvents.RequestEventsInfoArguments = {}) {
282
+ super();
283
+ const {
284
+ page = 1,
285
+ count = 100,
286
+ sortBy = "date",
287
+ sortByAsc = false,
288
+ returnInfo = undefined,
289
+ } = args;
290
+ if (page < 1) {
291
+ throw new RangeError("Page has to be >= 1");
292
+ }
293
+ if (count > 100) {
294
+ throw new RangeError("At most 100 mentions can be returned per call");
295
+ }
296
+ this.params = {};
297
+ this.params["mentionsPage"] = page;
298
+ this.params["mentionsCount"] = count;
299
+ this.params["mentionsSortBy"] = sortBy;
300
+ this.params["mentionsSortByAsc"] = sortByAsc;
301
+ if (!!returnInfo) {
302
+ this.params = _.extend({}, this.params, returnInfo.getParams("mentions"));
303
+ }
304
+ }
305
+
306
+ public set page(page) {
307
+ if (page < 1) {
308
+ throw new RangeError("Page has to be >= 1");
309
+ }
310
+ _.set(this.params, "mentionsPage", page);
311
+ }
312
+ }
313
+
314
+ export class RequestMentionsUriWgtList extends RequestMentions {
315
+ public resultType = "uriWgtList";
316
+ public params;
317
+
318
+ constructor(args: EventRegistryStatic.QueryMentions.RequestMentionsUriWgtListArguments = {}) {
319
+ super();
320
+ const {
321
+ page = 1,
322
+ count = 10000,
323
+ sortBy = "fq",
324
+ sortByAsc = false,
325
+ ...unsupported
326
+ } = args;
327
+ if (!_.isEmpty(unsupported)) {
328
+ console.warn(`RequestMentionsUriWgtList: Unsupported parameters detected: ${JSON.stringify(unsupported)}. Please check the documentation.`);
329
+ }
330
+ if (page < 1) {
331
+ throw new RangeError("Page has to be >= 1");
332
+ }
333
+ if (count > 50000) {
334
+ throw new RangeError("At most 50000 results can be returned per call");
335
+ }
336
+ this.params = {};
337
+ this.params["uriWgtListPage"] = page;
338
+ this.params["uriWgtListCount"] = count;
339
+ this.params["uriWgtListSortBy"] = sortBy;
340
+ this.params["uriWgtListSortByAsc"] = sortByAsc;
341
+ }
342
+
343
+ public set page(page) {
344
+ if (page < 1) {
345
+ throw new RangeError("Page has to be >= 1");
346
+ }
347
+ _.set(this.params, "uriWgtListPage", page);
348
+ }
349
+ }
350
+
351
+ export class RequestMentionsTimeAggr extends RequestMentions {
352
+ public resultType = "timeAggr";
353
+ }
354
+
355
+ export class RequestMentionsConceptAggr extends RequestMentions {
356
+ public resultType = "conceptAggr";
357
+ public params;
358
+ constructor(args: EventRegistryStatic.QueryMentions.RequestMentionsConceptAggrArguments = {}) {
359
+ super();
360
+ const {
361
+ conceptCount = 25,
362
+ mentionsSampleSize = 100000,
363
+ conceptScoring = "importance",
364
+ conceptCountPerType = undefined,
365
+ returnInfo = new ReturnInfo(),
366
+ } = args;
367
+ if (conceptCount > 500) {
368
+ throw new RangeError("At most 500 top concepts can be returned");
369
+ }
370
+ if (mentionsSampleSize > 20000) {
371
+ throw new RangeError("At most 20000 results can be used for computing");
372
+ }
373
+ this.params = {};
374
+ this.params["conceptAggrConceptCount"] = conceptCount;
375
+ this.params["conceptAggrSampleSize"] = mentionsSampleSize;
376
+ this.params["conceptAggrScoring"] = conceptScoring;
377
+ if (!!conceptCountPerType) {
378
+ this.params["conceptAggrConceptCountPerType"] = conceptCountPerType;
379
+ }
380
+ this.params = _.extend({}, this.params, returnInfo.getParams("conceptAggr"));
381
+ }
382
+ }
383
+
384
+ export class RequestMentionsCategoryAggr extends RequestMentions {
385
+ public resultType = "categoryAggr";
386
+ public params;
387
+ constructor(args: EventRegistryStatic.QueryMentions.RequestMentionsCategoryAggrArguments = {}) {
388
+ super();
389
+ const {
390
+ mentionsSampleSize = 20000,
391
+ returnInfo = new ReturnInfo(),
392
+ } = args;
393
+ if (mentionsSampleSize > 50000) {
394
+ throw new RangeError("At most 50000 results can be used for aggregation");
395
+ }
396
+ this.params["categoryAggrSampleSize"] = mentionsSampleSize;
397
+ this.params = returnInfo.getParams("categoryAggr");
398
+ }
399
+ }
400
+
401
+ export class RequestMentionsSourceAggr extends RequestMentions {
402
+ public resultType = "sourceAggr";
403
+ public params;
404
+ constructor(args: EventRegistryStatic.QueryMentions.RequestMentionsSourceAggrArguments = {}) {
405
+ super();
406
+ const {
407
+ sourceCount = 50,
408
+ normalizeBySourceArts = false,
409
+ returnInfo = new ReturnInfo(),
410
+ } = args;
411
+ this.params["sourceAggrSourceCount"] = sourceCount;
412
+ this.params = returnInfo.getParams("sourceAggr");
413
+ }
414
+ }
415
+
416
+ export class RequestMentionsKeywordAggr extends RequestMentions {
417
+ public resultType = "keywordAggr";
418
+ public params;
419
+ constructor(args: EventRegistryStatic.QueryMentions.RequestMentionsKeywordAggrArguments = {}) {
420
+ super();
421
+ const {
422
+ mentionsSampleSize = 2000,
423
+ } = args;
424
+ if (mentionsSampleSize > 20000) {
425
+ throw new RangeError("At most 20000 results can be used for aggregation");
426
+ }
427
+ this.params["keywordAggrSampleSize"] = mentionsSampleSize;
428
+ }
429
+ }
430
+
431
+ export class RequestMentionsConceptGraph extends RequestMentions {
432
+ public resultType = "conceptGraph";
433
+ public params;
434
+
435
+ constructor(args: EventRegistryStatic.QueryMentions.RequestMentionsConceptGraphArguments = {}) {
436
+ super();
437
+ const {
438
+ conceptCount = 25,
439
+ linkCount = 50,
440
+ mentionsSampleSize = 10000,
441
+ skipQueryConcepts = true,
442
+ returnInfo = new ReturnInfo(),
443
+ } = args;
444
+ if (conceptCount > 1000) {
445
+ throw new RangeError("At most 1000 top concepts can be returned");
446
+ }
447
+ if (linkCount > 2000) {
448
+ throw new RangeError("At most 2000 links between concepts can be returned");
449
+ }
450
+ if (mentionsSampleSize > 50000) {
451
+ throw new RangeError("At most 50000 results can be used for computing");
452
+ }
453
+ this.params = {};
454
+ this.params["conceptGraphConceptCount"] = conceptCount;
455
+ this.params["conceptGraphLinkCount"] = linkCount;
456
+ this.params["conceptGraphSampleSize"] = mentionsSampleSize;
457
+ this.params["conceptGraphSkipQueryConcepts"] = skipQueryConcepts;
458
+ this.params = _.extend({}, this.params, returnInfo.getParams("conceptGraph"));
459
+ }
460
+ }
461
+
462
+ export class RequestMentionsRecentActivity extends RequestMentions {
463
+ public resultType = "recentActivityMentions";
464
+ public params;
465
+ constructor(args: EventRegistryStatic.QueryMentions.RequestMentionsRecentActivityArguments = {}) {
466
+ super();
467
+ const {
468
+ maxMentionCount = 100,
469
+ updatesAfterUri = undefined,
470
+ updatesAfterTm = undefined,
471
+ updatesAfterMinsAgo = undefined,
472
+ updatesUntilTm = undefined,
473
+ updatesUntilMinsAgo = undefined,
474
+ mandatorySourceLocation = false,
475
+ returnInfo = undefined,
476
+ } = args;
477
+ if (maxMentionCount > 2000) {
478
+ throw new RangeError("At most 2000 mentions can be returned");
479
+ }
480
+ if (!_.isUndefined(updatesAfterTm) && !_.isUndefined(updatesAfterMinsAgo)) {
481
+ throw new Error("You should specify either updatesAfterTm or updatesAfterMinsAgo parameter, but not both");
482
+ }
483
+ if (!_.isUndefined(updatesUntilTm) && !_.isUndefined(updatesUntilMinsAgo)) {
484
+ throw new Error("You should specify either updatesUntilTm or updatesUntilMinsAgo parameter, but not both");
485
+ }
486
+ this.params = {};
487
+ this.params["recentActivityMentionsMaxMentionCount"] = maxMentionCount;
488
+
489
+ if (!_.isUndefined(updatesAfterTm)) {
490
+ this.params["recentActivityMentionsUpdatesAfterTm"] = QueryParamsBase.encodeDateTime(updatesAfterTm);
491
+ }
492
+
493
+ if (!_.isUndefined(updatesAfterMinsAgo)) {
494
+ this.params["recentActivityMentionsUpdatesAfterMinsAgo"] = updatesAfterMinsAgo;
495
+ }
496
+
497
+ if (!_.isUndefined(updatesUntilTm)) {
498
+ this.params["recentActivityMentionsUpdatesUntilTm"] = QueryParamsBase.encodeDateTime(updatesUntilTm);
499
+ }
500
+
501
+ if (!_.isUndefined(updatesUntilMinsAgo)) {
502
+ this.params["recentActivityMentionsUpdatesUntilMinsAgo"] = updatesUntilMinsAgo;
503
+ }
504
+
505
+ if (!_.isUndefined(updatesAfterUri)) {
506
+ this.params["recentActivityMentionsUpdatesAfterUri"] = updatesAfterUri;
507
+ }
508
+
509
+ this.params["recentActivityMentionsMandatorySourceLocation"] = mandatorySourceLocation;
510
+
511
+ if (!_.isUndefined(returnInfo)) {
512
+ this.params = _.extend({}, this.params, returnInfo.getParams("recentActivityMentions"));
513
+ }
514
+ }
515
+ }
516
+
package/src/topicPage.ts CHANGED
@@ -445,7 +445,7 @@ export class TopicPage extends QueryParamsBase {
445
445
  params = {...params, ...otherParameters};
446
446
  }
447
447
  const request = await this.eventRegistry.jsonRequest("/api/v1/article", params);
448
- return _.get(request, "data.articles.results", []);
448
+ return _.get(request, "data", {articles: {results: []}});
449
449
  }
450
450
  /**
451
451
  * Return a list of events that match the topic page
@@ -473,7 +473,7 @@ export class TopicPage extends QueryParamsBase {
473
473
  params = {...params, ...otherParameters};
474
474
  }
475
475
  const request = await this.eventRegistry.jsonRequest("/api/v1/event", params);
476
- return _.get(request, "data.events.results", []);
476
+ return _.get(request, "data", {events: {results: []}});
477
477
  }
478
478
 
479
479
  }