@grafana/data#MutableField TypeScript Examples
The following examples show how to use
@grafana/data#MutableField.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: datasource.ts From autohome-compareQueries-datasource with MIT License | 4 votes |
_compareQuery(options, targets, querys, _this) {
var comparePromises: any[] = [];
//console.log('_compareQuery targets', targets)
_.forEach(targets, function(target) {
var query = target.query;
if (query === null || query === '' || querys[query] === null) {
return;
}
var queryObj = _.cloneDeep(querys[query][0]);
queryObj.hide = false;
if (queryObj) {
var compareDsName = queryObj.datasource;
if (target.timeShifts && target.timeShifts.length > 0) {
_.forEach(target.timeShifts, function(timeShift) {
var timeShiftValue;
var timeShiftAlias;
var aliasType = timeShift.aliasType || 'suffix';
var delimiter = timeShift.delimiter || '_';
var comparePromise = _this.datasourceSrv
.get(compareDsName)
.then(function(compareDs) {
if (compareDs.meta.id === _this.meta.id) {
return { data: [] };
}
timeShiftValue = _this.templateSrv.replace(timeShift.value, options.scopedVars);
timeShiftAlias = _this.templateSrv.replace(timeShift.alias, options.scopedVars) || timeShiftValue;
if (timeShiftValue === null || timeShiftValue === '' || typeof timeShiftValue === 'undefined') {
return { data: [] };
}
let compareOptions = _.cloneDeep(options);
compareOptions.range.from = _this.addTimeShift(compareOptions.range.from, timeShiftValue);
compareOptions.range.to = _this.addTimeShift(compareOptions.range.to, timeShiftValue);
compareOptions.range.raw = {
from: compareOptions.range.from,
to: compareOptions.range.to,
};
compareOptions.rangeRaw = compareOptions.range.raw;
queryObj.refId = queryObj.refId + '_' + timeShiftValue;
compareOptions.targets = [queryObj];
compareOptions.requestId = compareOptions.requestId + '_' + timeShiftValue;
var compareResult = compareDs.query(compareOptions);
return typeof compareResult.toPromise === 'function' ? compareResult.toPromise() : compareResult;
})
.then(function(compareResult) {
var data = compareResult.data;
data.forEach(function(line) {
if (line.target) {
// if old time series format
line.target = _this.generalAlias(line.target, timeShiftAlias, aliasType, delimiter);
typeof line.title !== 'undefined' &&
line.title !== null &&
(line.title = _this.generalAlias(line.title, timeShiftAlias, aliasType, delimiter));
} else if (line.fields) {
//else if new data frames format with multiple series
line.fields.forEach(function(field) {
if (field.name) {
field.name = _this.generalAlias(field.name, timeShiftAlias, aliasType, delimiter);
}
if (field.config && field.config.displayName) {
field.config.displayName = _this.generalAlias(
field.config.displayName,
timeShiftAlias,
aliasType,
delimiter
);
}
if (field.config && field.config.displayNameFromDS) {
field.config.displayNameFromDS = _this.generalAlias(
field.config.displayNameFromDS,
timeShiftAlias,
aliasType,
delimiter
);
}
});
}
if (target.process) {
let timeShift_ms = _this.parseShiftToMs(timeShiftValue);
if (line.type === 'table') {
if (line.rows) {
line.rows.forEach(function(row) {
row[0] = row[0] + timeShift_ms;
});
}
} else {
if (line.datapoints) {
// if old time series format
line.datapoints.forEach(function(datapoint) {
datapoint[1] = datapoint[1] + timeShift_ms;
});
} else if (line.fields && line.fields.length > 0) {
//else if new data frames format
const unshiftedTimeField = line.fields.find(field => field.type === 'time');
if (unshiftedTimeField) {
const timeField: MutableField = {
name: unshiftedTimeField.name,
type: unshiftedTimeField.type,
config: unshiftedTimeField.config || {},
labels: unshiftedTimeField.labels,
values: new ArrayVector(),
};
for (let i = 0; i < line.length; i++) {
timeField.values.set(i, unshiftedTimeField.values.get(i) + timeShift_ms);
}
line.fields[0] = timeField;
}
}
}
}
line.hide = target.hide;
});
return {
data: data,
};
});
comparePromises.push(comparePromise);
});
}
}
});
return this.$q.all(comparePromises).then(function(results) {
return {
data: _.flatten(
_.filter(
_.map(results, function(result) {
var data = result.data;
if (data) {
data = _.filter(result.data, function(datum) {
return datum.hide !== true;
});
}
return data;
}),
function(result) {
return result !== undefined && result !== null;
}
)
),
};
});
}