UNPKG

plywood-proxy

Version:
246 lines (245 loc) 10.1 kB
'use strict'; var Q = require('q'); 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 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'; } var check; var DataSource = (function () { function DataSource(parameters) { var name = parameters.name; this.name = name; this.title = parameters.title || general_1.makeTitle(name); this.engine = parameters.engine; this.source = parameters.source; this.subsetFilter = parameters.subsetFilter; this.options = parameters.options || {}; this.introspection = parameters.introspection || DataSource.DEFAULT_INTROSPECTION; this.timeAttribute = parameters.timeAttribute; this.defaultTimezone = parameters.defaultTimezone; this.defaultDuration = parameters.defaultDuration; this.refreshRule = parameters.refreshRule; this.maxTime = parameters.maxTime; this.executor = parameters.executor; } DataSource.isDataSource = function (candidate) { return immutable_class_1.isInstanceOf(candidate, DataSource); }; DataSource.updateMaxTime = function (dataSource) { if (!dataSource.shouldQueryMaxTime()) return Q(dataSource); 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, executor) { if (executor === void 0) { executor = null; } var engine = parameters.engine; var timeAttributeName = parameters.timeAttribute; if (engine === 'druid' && !timeAttributeName) { timeAttributeName = 'time'; } var timeAttribute = timeAttributeName ? plywood_1.$(timeAttributeName) : null; var introspection = parameters.introspection; // Back compat. var options = parameters.options || {}; if (options['skipIntrospection']) { if (!introspection) introspection = 'none'; delete options['skipIntrospection']; } if (options['disableAutofill']) { if (!introspection) introspection = 'no-autofill'; delete options['disableAutofill']; } introspection = introspection || DataSource.DEFAULT_INTROSPECTION; if (DataSource.INTROSPECTION_VALUES.indexOf(introspection) === -1) { throw new Error("invalid introspection value " + introspection + ", must be one of " + DataSource.INTROSPECTION_VALUES.join(', ')); } var value = { executor: null, name: parameters.name, title: parameters.title, engine: engine, source: parameters.source, subsetFilter: parameters.subsetFilter ? plywood_1.Expression.fromJSLoose(parameters.subsetFilter) : null, options: options, introspection: introspection, timeAttribute: timeAttribute, defaultTimezone: parameters.defaultTimezone ? chronoshift_1.Timezone.fromJS(parameters.defaultTimezone) : DataSource.DEFAULT_TIMEZONE, defaultDuration: parameters.defaultDuration ? chronoshift_1.Duration.fromJS(parameters.defaultDuration) : DataSource.DEFAULT_DURATION, refreshRule: parameters.refreshRule ? refresh_rule_1.RefreshRule.fromJS(parameters.refreshRule) : refresh_rule_1.RefreshRule.query(), maxTime: parameters.maxTime ? max_time_1.MaxTime.fromJS(parameters.maxTime) : null }; if (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, options: this.options, introspection: this.introspection, timeAttribute: this.timeAttribute, defaultTimezone: this.defaultTimezone, defaultDuration: this.defaultDuration, refreshRule: this.refreshRule, maxTime: this.maxTime }; if (this.executor) { value.executor = this.executor; } 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, defaultTimezone: this.defaultTimezone.toJS(), defaultDuration: this.defaultDuration.toJS(), refreshRule: this.refreshRule.toJS() }; if (this.timeAttribute) { js.timeAttribute = this.timeAttribute.name; } if (Object.keys(this.options).length) { js.options = this.options; } if (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 DataSource.isDataSource(other) && this.name === other.name && this.title === other.title && this.engine === other.engine && this.source === other.source && Boolean(this.subsetFilter) === Boolean(other.subsetFilter) && (!this.subsetFilter || this.subsetFilter.equals(other.subsetFilter)) && JSON.stringify(this.options) === JSON.stringify(other.options) && this.introspection === other.introspection && Boolean(this.timeAttribute) === Boolean(other.timeAttribute) && (!this.timeAttribute || this.timeAttribute.equals(other.timeAttribute)) && this.defaultTimezone.equals(other.defaultTimezone) && this.defaultDuration.equals(other.defaultDuration) && this.refreshRule.equals(other.refreshRule); }; DataSource.prototype.attachExecutor = function (executor) { var value = this.valueOf(); value.executor = executor; return new DataSource(value); }; DataSource.prototype.toClientDataSource = function () { var value = this.valueOf(); value.subsetFilter = null; value.introspection = 'none'; return new DataSource(value); }; DataSource.prototype.isQueryable = function () { return Boolean(this.executor); }; DataSource.prototype.getMaxTimeDate = function () { var refreshRule = this.refreshRule; if (refreshRule.rule === 'realtime') { return chronoshift_1.minute.ceil(new Date(), chronoshift_1.Timezone.UTC); } else if (refreshRule.rule === 'fixed') { return refreshRule.time; } else { 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.rule === 'realtime') { return 'Updated: ~1 second ago'; } else if (refreshRule.rule === 'fixed') { return "Fixed to: " + formatTimeDiff(Date.now() - refreshRule.time.valueOf()); } else { var maxTime = this.maxTime; if (maxTime) { return "Updated: " + formatTimeDiff(Date.now() - maxTime.time.valueOf()) + " ago"; } else { return null; } } }; DataSource.prototype.shouldQueryMaxTime = function () { if (!this.executor) return false; return this.refreshRule.shouldQuery(this.maxTime); }; DataSource.prototype.isTimeAttribute = function (ex) { var timeAttribute = this.timeAttribute; return ex.equals(this.timeAttribute); }; DataSource.prototype.addAttributes = function (attributes) { var introspection = this.introspection; if (introspection === 'none' || introspection === 'no-autofill') return this; var value = this.valueOf(); value.introspection = 'no-autofill'; return new DataSource(value); }; DataSource.prototype.changeMaxTime = function (maxTime) { var value = this.valueOf(); value.maxTime = maxTime; return new DataSource(value); }; 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('P3D'); return DataSource; })(); exports.DataSource = DataSource; check = DataSource;