| 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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724 |
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
19×
19×
35×
35×
35×
35×
1×
19×
19×
25×
25×
25×
25×
1×
1×
1×
1×
1×
29×
29×
28×
28×
28×
28×
28×
28×
28×
28×
28×
28×
28×
28×
28×
28×
28×
28×
28×
28×
28×
28×
28×
28×
28×
28×
1×
45×
1×
1×
19×
19×
19×
19×
19×
19×
19×
19×
19×
19×
19×
19×
19×
19×
19×
19×
19×
6×
19×
19×
19×
19×
19×
35×
25×
19×
5×
19×
19×
19×
19×
19×
19×
19×
1×
10×
10×
10×
4×
10×
1×
17×
36×
29×
17×
17×
17×
17×
17×
11×
17×
17×
17×
17×
1×
2×
1×
2×
1×
40×
1×
40×
1×
28×
28×
25×
1×
1×
1×
1×
1×
1×
3×
3×
1×
1×
1×
1×
1×
1×
1×
3×
3×
2×
1×
1×
5×
5×
4×
4×
2×
4×
1×
1×
1×
1×
1×
1×
1×
1×
1×
2×
2×
2×
2×
2×
2×
2×
2×
2×
2×
2×
2×
2×
2×
1×
1×
1×
1×
16×
16×
16×
1×
1×
1×
24×
1×
10×
1×
1×
1×
1×
89×
1×
15×
1×
1×
5×
1×
1×
1×
1×
1×
1×
5×
5×
1×
4×
4×
4×
4×
1×
3×
1×
3×
3×
3×
3×
4×
4×
1×
3×
1×
2×
1×
1×
1×
5×
5×
5×
5×
5×
5×
5×
27×
27×
27×
4×
23×
23×
4×
4×
4×
4×
10×
4×
4×
4×
4×
4×
6×
6×
6×
6×
10×
9×
9×
9×
11×
11×
9×
5×
5×
5×
5×
5×
5×
4×
5×
5×
1×
1×
1×
1×
1×
1×
1×
1×
1×
| "use strict";
var Q = require('q');
var immutable_1 = require('immutable');
var immutable_class_1 = require('immutable-class');
var chronoshift_1 = require('chronoshift');
var plywood_1 = require('plywood');
var general_1 = require('../../utils/general/general');
var dimension_1 = require('../dimension/dimension');
var measure_1 = require('../measure/measure');
var filter_1 = require('../filter/filter');
var max_time_1 = require('../max-time/max-time');
var refresh_rule_1 = require('../refresh-rule/refresh-rule');
function formatTimeDiff(diff) {
diff = Math.round(Math.abs(diff) / 1000); // turn to seconds
if (diff < 60)
return 'less than 1 minute';
diff = Math.floor(diff / 60); // turn to minutes
if (diff === 1)
return '1 minute';
if (diff < 60)
return diff + ' minutes';
diff = Math.floor(diff / 60); // turn to hours
if (diff === 1)
return '1 hour';
if (diff <= 24)
return diff + ' hours';
diff = Math.floor(diff / 24); // turn to days
return diff + ' days';
}
function makeUniqueDimensionList(dimensions) {
var seen = {};
return immutable_1.List(dimensions.filter(function (dimension) {
var dimensionName = dimension.name.toLowerCase();
Iif (seen[dimensionName])
return false;
seen[dimensionName] = 1;
return true;
}));
}
function makeUniqueMeasureList(measures) {
var seen = {};
return immutable_1.List(measures.filter(function (measure) {
var measureName = measure.name.toLowerCase();
Iif (seen[measureName])
return false;
seen[measureName] = 1;
return true;
}));
}
function measuresFromLongForm(longForm) {
var metricColumn = longForm.metricColumn, values = longForm.values, possibleAggregates = longForm.possibleAggregates, titleNameTrim = longForm.titleNameTrim;
var myPossibleAggregates = {};
for (var agg in possibleAggregates) {
if (!general_1.hasOwnProperty(possibleAggregates, agg))
continue;
myPossibleAggregates[agg] = plywood_1.Expression.fromJSLoose(possibleAggregates[agg]);
}
var measures = [];
for (var _i = 0, values_1 = values; _i < values_1.length; _i++) {
var value = values_1[_i];
var aggregates = value.aggregates;
if (!Array.isArray(aggregates)) {
throw new Error('must have aggregates in longForm value');
}
for (var _a = 0, aggregates_1 = aggregates; _a < aggregates_1.length; _a++) {
var aggregate = aggregates_1[_a];
var myExpression = myPossibleAggregates[aggregate];
if (!myExpression)
throw new Error("can not find aggregate " + aggregate + " for value " + value.value);
var name = general_1.makeUrlSafeName(aggregate + "_" + value.value);
measures.push(new measure_1.Measure({
name: name,
title: general_1.makeTitle(titleNameTrim ? name.replace(titleNameTrim, '') : name),
expression: myExpression.substitute(function (ex) {
if (ex instanceof plywood_1.RefExpression && ex.name === 'filtered') {
return plywood_1.$('main').filter(plywood_1.$(metricColumn).is(plywood_1.r(value.value)));
}
return null;
})
}));
}
}
return measures;
}
function filterFromLongFrom(longForm) {
var metricColumn = longForm.metricColumn, values = longForm.values;
return plywood_1.$(metricColumn).in(values.map(function (v) { return v.value; }));
}
var check;
var DataSource = (function () {
function DataSource(parameters) {
var name = parameters.name;
general_1.verifyUrlSafeName(name);
this.name = name;
this.title = parameters.title || general_1.makeTitle(name);
this.engine = parameters.engine || 'druid';
this.source = parameters.source || name;
this.subsetFilter = parameters.subsetFilter;
this.rollup = Boolean(parameters.rollup);
this.options = parameters.options || {};
this.introspection = parameters.introspection || DataSource.DEFAULT_INTROSPECTION;
this.attributes = parameters.attributes || [];
this.attributeOverrides = parameters.attributeOverrides || [];
this.derivedAttributes = parameters.derivedAttributes;
this.dimensions = parameters.dimensions || immutable_1.List([]);
this.measures = parameters.measures || immutable_1.List([]);
this.timeAttribute = parameters.timeAttribute;
this.defaultTimezone = parameters.defaultTimezone;
this.defaultFilter = parameters.defaultFilter;
this.defaultDuration = parameters.defaultDuration;
this.defaultSortMeasure = parameters.defaultSortMeasure;
this.defaultPinnedDimensions = parameters.defaultPinnedDimensions;
this.refreshRule = parameters.refreshRule;
this.maxTime = parameters.maxTime;
this.executor = parameters.executor;
this.external = parameters.external;
this._validateDefaults();
}
DataSource.isDataSource = function (candidate) {
return immutable_class_1.isInstanceOf(candidate, DataSource);
};
DataSource.updateMaxTime = function (dataSource) {
if (dataSource.refreshRule.isRealtime()) {
return Q(dataSource.changeMaxTime(max_time_1.MaxTime.fromNow()));
}
var ex = plywood_1.ply().apply('maxTime', plywood_1.$('main').max(dataSource.timeAttribute));
return dataSource.executor(ex).then(function (dataset) {
var maxTimeDate = dataset.data[0]['maxTime'];
if (!isNaN(maxTimeDate)) {
return dataSource.changeMaxTime(max_time_1.MaxTime.fromDate(maxTimeDate));
}
return dataSource;
});
};
DataSource.fromJS = function (parameters, context) {
Eif (context === void 0) { context = {}; }
var executor = context.executor, external = context.external;
var engine = parameters.engine;
var introspection = parameters.introspection;
var attributeOverrideJSs = parameters.attributeOverrides;
// Back compat.
var options = parameters.options || {};
Iif (options.skipIntrospection) {
if (!introspection)
introspection = 'none';
delete options.skipIntrospection;
}
Iif (options.disableAutofill) {
if (!introspection)
introspection = 'no-autofill';
delete options.disableAutofill;
}
Iif (options.attributeOverrides) {
if (!attributeOverrideJSs)
attributeOverrideJSs = options.attributeOverrides;
delete options.attributeOverrides;
}
Iif (options.defaultSplitDimension) {
options.defaultSplits = options.defaultSplitDimension;
delete options.defaultSplitDimension;
}
// End Back compat.
introspection = introspection || DataSource.DEFAULT_INTROSPECTION;
Iif (DataSource.INTROSPECTION_VALUES.indexOf(introspection) === -1) {
throw new Error("invalid introspection value " + introspection + ", must be one of " + DataSource.INTROSPECTION_VALUES.join(', '));
}
var refreshRule = parameters.refreshRule ? refresh_rule_1.RefreshRule.fromJS(parameters.refreshRule) : refresh_rule_1.RefreshRule.query();
var maxTime = parameters.maxTime ? max_time_1.MaxTime.fromJS(parameters.maxTime) : null;
Iif (!maxTime && refreshRule.isRealtime()) {
maxTime = max_time_1.MaxTime.fromNow();
}
var timeAttributeName = parameters.timeAttribute;
if (engine === 'druid' && !timeAttributeName) {
timeAttributeName = '__time';
}
var timeAttribute = timeAttributeName ? plywood_1.$(timeAttributeName) : null;
var attributeOverrides = plywood_1.AttributeInfo.fromJSs(attributeOverrideJSs || []);
var attributes = plywood_1.AttributeInfo.fromJSs(parameters.attributes || []);
var derivedAttributes = null;
Iif (parameters.derivedAttributes) {
derivedAttributes = plywood_1.helper.expressionLookupFromJS(parameters.derivedAttributes);
}
var dimensions = makeUniqueDimensionList((parameters.dimensions || []).map(function (d) { return dimension_1.Dimension.fromJS(d); }));
var measures = makeUniqueMeasureList((parameters.measures || []).map(function (m) { return measure_1.Measure.fromJS(m); }));
if (timeAttribute && !dimension_1.Dimension.getDimensionByExpression(dimensions, timeAttribute)) {
dimensions = dimensions.unshift(new dimension_1.Dimension({
name: timeAttributeName,
expression: timeAttribute,
kind: 'time'
}));
}
var subsetFilter = parameters.subsetFilter ? plywood_1.Expression.fromJSLoose(parameters.subsetFilter) : null;
var longForm = parameters.longForm;
Iif (longForm) {
measures = measures.concat(measuresFromLongForm(longForm));
if (longForm.addSubsetFilter) {
if (!subsetFilter)
subsetFilter = plywood_1.Expression.TRUE;
subsetFilter = subsetFilter.and(filterFromLongFrom(longForm)).simplify();
}
}
var value = {
executor: null,
name: parameters.name,
title: parameters.title,
engine: engine,
source: parameters.source,
subsetFilter: subsetFilter,
rollup: parameters.rollup,
options: options,
introspection: introspection,
attributeOverrides: attributeOverrides,
attributes: attributes,
derivedAttributes: derivedAttributes,
dimensions: dimensions,
measures: measures,
timeAttribute: timeAttribute,
defaultTimezone: parameters.defaultTimezone ? chronoshift_1.Timezone.fromJS(parameters.defaultTimezone) : DataSource.DEFAULT_TIMEZONE,
defaultFilter: parameters.defaultFilter ? filter_1.Filter.fromJS(parameters.defaultFilter) : filter_1.Filter.EMPTY,
defaultDuration: parameters.defaultDuration ? chronoshift_1.Duration.fromJS(parameters.defaultDuration) : DataSource.DEFAULT_DURATION,
defaultSortMeasure: parameters.defaultSortMeasure || (measures.size ? measures.first().name : null),
defaultPinnedDimensions: immutable_1.OrderedSet(parameters.defaultPinnedDimensions || []),
refreshRule: refreshRule,
maxTime: maxTime
};
Iif (external)
value.external = external;
Iif (executor)
value.executor = executor;
return new DataSource(value);
};
DataSource.prototype.valueOf = function () {
var value = {
name: this.name,
title: this.title,
engine: this.engine,
source: this.source,
subsetFilter: this.subsetFilter,
rollup: this.rollup,
options: this.options,
introspection: this.introspection,
attributeOverrides: this.attributeOverrides,
attributes: this.attributes,
derivedAttributes: this.derivedAttributes,
dimensions: this.dimensions,
measures: this.measures,
timeAttribute: this.timeAttribute,
defaultTimezone: this.defaultTimezone,
defaultFilter: this.defaultFilter,
defaultDuration: this.defaultDuration,
defaultSortMeasure: this.defaultSortMeasure,
defaultPinnedDimensions: this.defaultPinnedDimensions,
refreshRule: this.refreshRule,
maxTime: this.maxTime
};
Iif (this.executor)
value.executor = this.executor;
if (this.external)
value.external = this.external;
return value;
};
DataSource.prototype.toJS = function () {
var js = {
name: this.name,
title: this.title,
engine: this.engine,
source: this.source,
subsetFilter: this.subsetFilter ? this.subsetFilter.toJS() : null,
introspection: this.introspection,
dimensions: this.dimensions.toArray().map(function (dimension) { return dimension.toJS(); }),
measures: this.measures.toArray().map(function (measure) { return measure.toJS(); }),
defaultTimezone: this.defaultTimezone.toJS(),
defaultFilter: this.defaultFilter.toJS(),
defaultDuration: this.defaultDuration.toJS(),
defaultSortMeasure: this.defaultSortMeasure,
defaultPinnedDimensions: this.defaultPinnedDimensions.toArray(),
refreshRule: this.refreshRule.toJS()
};
Iif (this.rollup)
js.rollup = true;
Eif (this.timeAttribute)
js.timeAttribute = this.timeAttribute.name;
Iif (this.attributeOverrides.length)
js.attributeOverrides = plywood_1.AttributeInfo.toJSs(this.attributeOverrides);
if (this.attributes.length)
js.attributes = plywood_1.AttributeInfo.toJSs(this.attributes);
Iif (this.derivedAttributes)
js.derivedAttributes = plywood_1.helper.expressionLookupToJS(this.derivedAttributes);
Iif (Object.keys(this.options).length)
js.options = this.options;
Iif (this.maxTime)
js.maxTime = this.maxTime.toJS();
return js;
};
DataSource.prototype.toJSON = function () {
return this.toJS();
};
DataSource.prototype.toString = function () {
return "[DataSource: " + this.name + "]";
};
DataSource.prototype.equals = function (other) {
return this.equalsWithoutMaxTime(other) &&
Boolean(this.maxTime) === Boolean(other.maxTime) &&
(!this.maxTime || this.maxTime.equals(other.maxTime));
};
DataSource.prototype.equalsWithoutMaxTime = function (other) {
return DataSource.isDataSource(other) &&
this.name === other.name &&
this.title === other.title &&
this.engine === other.engine &&
this.source === other.source &&
immutable_class_1.immutableEqual(this.subsetFilter, other.subsetFilter) &&
this.rollup === other.rollup &&
JSON.stringify(this.options) === JSON.stringify(other.options) &&
this.introspection === other.introspection &&
immutable_class_1.immutableArraysEqual(this.attributeOverrides, other.attributeOverrides) &&
immutable_class_1.immutableArraysEqual(this.attributes, other.attributes) &&
immutable_class_1.immutableLookupsEqual(this.derivedAttributes, other.derivedAttributes) &&
general_1.immutableListsEqual(this.dimensions, other.dimensions) &&
general_1.immutableListsEqual(this.measures, other.measures) &&
immutable_class_1.immutableEqual(this.timeAttribute, other.timeAttribute) &&
this.defaultTimezone.equals(other.defaultTimezone) &&
this.defaultFilter.equals(other.defaultFilter) &&
this.defaultDuration.equals(other.defaultDuration) &&
this.defaultSortMeasure === other.defaultSortMeasure &&
this.defaultPinnedDimensions.equals(other.defaultPinnedDimensions) &&
this.refreshRule.equals(other.refreshRule);
};
DataSource.prototype._validateDefaults = function () {
var _a = this, measures = _a.measures, defaultSortMeasure = _a.defaultSortMeasure;
if (defaultSortMeasure) {
if (!measures.find(function (measure) { return measure.name === defaultSortMeasure; })) {
throw new Error("can not find defaultSortMeasure '" + defaultSortMeasure + "' in data source '" + this.name + "'");
}
}
};
DataSource.prototype.getMainTypeContext = function () {
var _a = this, attributes = _a.attributes, derivedAttributes = _a.derivedAttributes;
Iif (!attributes)
return null;
var datasetType = {};
for (var _i = 0, attributes_1 = attributes; _i < attributes_1.length; _i++) {
var attribute = attributes_1[_i];
datasetType[attribute.name] = attribute;
}
for (var name in derivedAttributes) {
datasetType[name] = {
type: derivedAttributes[name].type
};
}
return {
type: 'DATASET',
datasetType: datasetType
};
};
DataSource.prototype.getIssues = function () {
var _a = this, dimensions = _a.dimensions, measures = _a.measures;
var mainTypeContext = this.getMainTypeContext();
var issues = [];
dimensions.forEach(function (dimension) {
try {
dimension.expression.referenceCheckInTypeContext(mainTypeContext);
}
catch (e) {
issues.push("failed to validate dimension '" + dimension.name + "': " + e.message);
}
});
var measureTypeContext = {
type: 'DATASET',
datasetType: {
main: mainTypeContext
}
};
measures.forEach(function (measure) {
try {
measure.expression.referenceCheckInTypeContext(measureTypeContext);
}
catch (e) {
var message = e.message;
// If we get here it is possible that the user has misunderstood what the meaning of a measure is and have tried
// to do something like $volume / $volume. We detect this here by checking for a reference to $main
// If there is no main reference raise a more informative issue.
if (measure.expression.getFreeReferences().indexOf('main') === -1) {
message = 'measure must contain a $main reference';
}
issues.push("failed to validate measure '" + measure.name + "': " + message);
}
});
return issues;
};
DataSource.prototype.createExternal = function (requester, introspectionStrategy, timeout) {
Iif (this.engine !== 'druid')
return; // Only Druid supported for now.
var value = this.valueOf();
var context = {
timeout: timeout
};
Iif (this.introspection === 'none') {
value.external = new plywood_1.DruidExternal({
suppress: true,
dataSource: this.source,
rollup: this.rollup,
timeAttribute: this.timeAttribute.name,
customAggregations: this.options.customAggregations,
attributes: plywood_1.AttributeInfo.override(this.deduceAttributes(), this.attributeOverrides),
derivedAttributes: this.derivedAttributes,
introspectionStrategy: introspectionStrategy,
filter: this.subsetFilter,
allowSelectQueries: true,
context: context,
requester: requester
});
}
else {
value.external = new plywood_1.DruidExternal({
suppress: true,
dataSource: this.source,
rollup: this.rollup,
timeAttribute: this.timeAttribute.name,
attributeOverrides: this.attributeOverrides,
derivedAttributes: this.derivedAttributes,
customAggregations: this.options.customAggregations,
introspectionStrategy: introspectionStrategy,
filter: this.subsetFilter,
allowSelectQueries: true,
context: context,
requester: requester
});
}
return new DataSource(value);
};
DataSource.prototype.introspect = function () {
var _this = this;
var external = this.external;
Iif (this.engine === 'native')
return Q(this);
Iif (!external)
throw new Error("must have external to introspect in " + this.name);
var countDistinctReferences = [];
Eif (this.measures) {
countDistinctReferences = [].concat.apply([], this.measures.toArray().map(function (measure) {
return measure_1.Measure.getCountDistinctReferences(measure.expression);
}));
}
return external.introspect()
.then(function (introspectedExternal) {
Iif (immutable_class_1.immutableArraysEqual(external.attributes, introspectedExternal.attributes))
return _this;
Iif (!countDistinctReferences) {
var attributes = introspectedExternal.attributes;
for (var _i = 0, attributes_2 = attributes; _i < attributes_2.length; _i++) {
var attribute = attributes_2[_i];
// This is a metric that should really be a HLL
if (attribute.type === 'NUMBER' && countDistinctReferences.indexOf(attribute.name) !== -1) {
introspectedExternal = introspectedExternal.updateAttribute(plywood_1.AttributeInfo.fromJS({
name: attribute.name,
special: 'unique'
}));
}
}
}
var value = _this.addAttributes(introspectedExternal.attributes).valueOf();
value.external = introspectedExternal;
value.executor = plywood_1.basicExecutorFactory({
datasets: { main: introspectedExternal }
});
return new DataSource(value);
});
};
DataSource.prototype.attachExecutor = function (executor) {
var value = this.valueOf();
value.executor = executor;
return new DataSource(value);
};
DataSource.prototype.toClientDataSource = function () {
var value = this.valueOf();
// Do not reveal the subset filter to the client
value.subsetFilter = null;
// No need for any introspection on the client
value.introspection = 'none';
// No point sending over the maxTime
if (this.refreshRule.isRealtime()) {
value.maxTime = null;
}
// No need for the overrides
value.attributeOverrides = null;
return new DataSource(value);
};
DataSource.prototype.isQueryable = function () {
return Boolean(this.executor);
};
DataSource.prototype.getMaxTimeDate = function () {
var refreshRule = this.refreshRule;
Eif (refreshRule.isFixed())
return refreshRule.time;
// refreshRule is query or realtime
var maxTime = this.maxTime;
if (!maxTime)
return null;
return chronoshift_1.second.ceil(maxTime.time, chronoshift_1.Timezone.UTC);
};
DataSource.prototype.updatedText = function () {
var refreshRule = this.refreshRule;
if (refreshRule.isRealtime()) {
return 'Updated ~1 second ago';
}
else if (refreshRule.isFixed()) {
return "Fixed to " + refreshRule.time.toISOString();
}
else {
var maxTime = this.maxTime;
if (maxTime) {
return "Updated " + formatTimeDiff(Date.now() - maxTime.time.valueOf()) + " ago";
}
else {
return null;
}
}
};
DataSource.prototype.shouldUpdateMaxTime = function () {
if (!this.refreshRule.shouldUpdate(this.maxTime))
return false;
return Boolean(this.executor) || this.refreshRule.isRealtime();
};
DataSource.prototype.getDimension = function (dimensionName) {
return dimension_1.Dimension.getDimension(this.dimensions, dimensionName);
};
DataSource.prototype.getDimensionByExpression = function (expression) {
return dimension_1.Dimension.getDimensionByExpression(this.dimensions, expression);
};
DataSource.prototype.getDimensionByKind = function (kind) {
return this.dimensions.filter(function (d) { return d.kind === kind; });
};
DataSource.prototype.getTimeDimension = function () {
return this.getDimensionByExpression(this.timeAttribute);
};
DataSource.prototype.isTimeAttribute = function (ex) {
return ex.equals(this.timeAttribute);
};
DataSource.prototype.getMeasure = function (measureName) {
return measure_1.Measure.getMeasure(this.measures, measureName);
};
DataSource.prototype.getMeasureByExpression = function (expression) {
return this.measures.find(function (measure) { return measure.expression.equals(expression); });
};
DataSource.prototype.changeDimensions = function (dimensions) {
var value = this.valueOf();
value.dimensions = dimensions;
return new DataSource(value);
};
DataSource.prototype.rolledUp = function () {
return this.engine === 'druid';
};
/**
* This function tries to deduce the structure of the dataSource based on the dimensions and measures defined within.
* It should only be used when, for some reason, introspection if not available.
*/
DataSource.prototype.deduceAttributes = function () {
var _a = this, dimensions = _a.dimensions, measures = _a.measures, timeAttribute = _a.timeAttribute, attributeOverrides = _a.attributeOverrides;
var attributes = [];
Eif (timeAttribute) {
attributes.push(plywood_1.AttributeInfo.fromJS({ name: timeAttribute.name, type: 'TIME' }));
}
dimensions.forEach(function (dimension) {
var expression = dimension.expression;
if (expression.equals(timeAttribute))
return;
var references = expression.getFreeReferences();
for (var _i = 0, references_1 = references; _i < references_1.length; _i++) {
var reference = references_1[_i];
if (plywood_1.helper.findByName(attributes, reference))
continue;
attributes.push(plywood_1.AttributeInfo.fromJS({ name: reference, type: 'STRING' }));
}
});
measures.forEach(function (measure) {
var expression = measure.expression;
var references = measure_1.Measure.getAggregateReferences(expression);
var countDistinctReferences = measure_1.Measure.getCountDistinctReferences(expression);
for (var _i = 0, references_2 = references; _i < references_2.length; _i++) {
var reference = references_2[_i];
if (plywood_1.helper.findByName(attributes, reference))
continue;
if (countDistinctReferences.indexOf(reference) !== -1) {
attributes.push(plywood_1.AttributeInfo.fromJS({ name: reference, special: 'unique' }));
}
else {
attributes.push(plywood_1.AttributeInfo.fromJS({ name: reference, type: 'NUMBER' }));
}
}
});
Iif (attributeOverrides.length) {
attributes = plywood_1.AttributeInfo.override(attributes, attributeOverrides);
}
return attributes;
};
DataSource.prototype.addAttributes = function (newAttributes) {
var _this = this;
var _a = this, introspection = _a.introspection, dimensions = _a.dimensions, measures = _a.measures, attributes = _a.attributes;
Iif (introspection === 'none')
return this;
var autofillDimensions = introspection === 'autofill-dimensions-only' || introspection === 'autofill-all';
var autofillMeasures = introspection === 'autofill-measures-only' || introspection === 'autofill-all';
var $main = plywood_1.$('main');
for (var _i = 0, newAttributes_1 = newAttributes; _i < newAttributes_1.length; _i++) {
var newAttribute = newAttributes_1[_i];
var name = newAttribute.name, type = newAttribute.type, special = newAttribute.special;
// Already exists
if (attributes && plywood_1.helper.findByName(attributes, name))
continue;
var expression;
switch (type) {
case 'TIME':
Iif (!autofillDimensions)
continue;
expression = plywood_1.$(name);
Eif (this.getDimensionByExpression(expression))
continue;
// Add to the start
dimensions = dimensions.unshift(new dimension_1.Dimension({
name: general_1.makeUrlSafeName(name),
kind: 'time',
expression: expression
}));
break;
case 'STRING':
if (special === 'unique') {
Iif (!autofillMeasures)
continue;
var newMeasures = measure_1.Measure.measuresFromAttributeInfo(newAttribute);
newMeasures.forEach(function (newMeasure) {
Iif (_this.getMeasureByExpression(newMeasure.expression))
return;
measures = measures.push(newMeasure);
});
}
else {
Iif (!autofillDimensions)
continue;
expression = plywood_1.$(name);
Iif (this.getDimensionByExpression(expression))
continue;
dimensions = dimensions.push(new dimension_1.Dimension({
name: general_1.makeUrlSafeName(name),
expression: expression
}));
}
break;
case 'SET/STRING':
if (!autofillDimensions)
continue;
expression = plywood_1.$(name);
if (this.getDimensionByExpression(expression))
continue;
dimensions = dimensions.push(new dimension_1.Dimension({
name: general_1.makeUrlSafeName(name),
expression: expression
}));
break;
case 'BOOLEAN':
if (!autofillDimensions)
continue;
expression = plywood_1.$(name);
if (this.getDimensionByExpression(expression))
continue;
dimensions = dimensions.push(new dimension_1.Dimension({
name: general_1.makeUrlSafeName(name),
kind: 'boolean',
expression: expression
}));
break;
case 'NUMBER':
Iif (!autofillMeasures)
continue;
var newMeasures = measure_1.Measure.measuresFromAttributeInfo(newAttribute);
newMeasures.forEach(function (newMeasure) {
Iif (_this.getMeasureByExpression(newMeasure.expression))
return;
measures = (name === 'count') ? measures.unshift(newMeasure) : measures.push(newMeasure);
});
break;
default:
throw new Error("unsupported type " + type);
}
}
Iif (!this.rolledUp() && !measures.find(function (m) { return m.name === 'count'; })) {
measures = measures.unshift(new measure_1.Measure({
name: 'count',
expression: $main.count()
}));
}
var value = this.valueOf();
value.attributes = attributes ? plywood_1.AttributeInfo.override(attributes, newAttributes) : newAttributes;
value.dimensions = dimensions;
value.measures = measures;
if (!value.defaultSortMeasure) {
value.defaultSortMeasure = measures.size ? measures.first().name : null;
}
// ToDo: remove this when Pivot can handle it
Iif (!value.timeAttribute && dimensions.first().kind === 'time') {
value.timeAttribute = dimensions.first().expression;
}
return new DataSource(value);
};
DataSource.prototype.changeMaxTime = function (maxTime) {
var value = this.valueOf();
value.maxTime = maxTime;
return new DataSource(value);
};
DataSource.prototype.getDefaultSortAction = function () {
return new plywood_1.SortAction({
expression: plywood_1.$(this.defaultSortMeasure),
direction: plywood_1.SortAction.DESCENDING
});
};
DataSource.DEFAULT_INTROSPECTION = 'autofill-all';
DataSource.INTROSPECTION_VALUES = ['none', 'no-autofill', 'autofill-dimensions-only', 'autofill-measures-only', 'autofill-all'];
DataSource.DEFAULT_TIMEZONE = chronoshift_1.Timezone.UTC;
DataSource.DEFAULT_DURATION = chronoshift_1.Duration.fromJS('P1D');
return DataSource;
}());
exports.DataSource = DataSource;
check = DataSource;
|