| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192 |
- import _ from 'lodash';
- import TableModel from 'app/core/table_model';
- import { FieldType } from '@grafana/data';
- export default class InfluxSeries {
- series: any;
- alias: any;
- annotation: any;
- constructor(options: { series: any; alias?: any; annotation?: any }) {
- this.series = options.series;
- this.alias = options.alias;
- this.annotation = options.annotation;
- }
- getTimeSeries() {
- const output: any[] = [];
- let i, j;
- if (this.series.length === 0) {
- return output;
- }
- _.each(this.series, series => {
- const columns = series.columns.length;
- const tags = _.map(series.tags, (value, key) => {
- return key + ': ' + value;
- });
- for (j = 1; j < columns; j++) {
- let seriesName = series.name;
- const columnName = series.columns[j];
- if (columnName !== 'value') {
- seriesName = seriesName + '.' + columnName;
- }
- if (this.alias) {
- seriesName = this._getSeriesName(series, j);
- } else if (series.tags) {
- seriesName = seriesName + ' {' + tags.join(', ') + '}';
- }
- const datapoints = [];
- if (series.values) {
- for (i = 0; i < series.values.length; i++) {
- datapoints[i] = [series.values[i][j], series.values[i][0]];
- }
- }
- output.push({ target: seriesName, datapoints: datapoints });
- }
- });
- return output;
- }
- _getSeriesName(series: any, index: number) {
- const regex = /\$(\w+)|\[\[([\s\S]+?)\]\]/g;
- const segments = series.name.split('.');
- return this.alias.replace(regex, (match: any, g1: any, g2: any) => {
- const group = g1 || g2;
- const segIndex = parseInt(group, 10);
- if (group === 'm' || group === 'measurement') {
- return series.name;
- }
- if (group === 'col') {
- return series.columns[index];
- }
- if (!isNaN(segIndex)) {
- return segments[segIndex];
- }
- if (group.indexOf('tag_') !== 0) {
- return match;
- }
- const tag = group.replace('tag_', '');
- if (!series.tags) {
- return match;
- }
- return series.tags[tag];
- });
- }
- getAnnotations() {
- const list: any[] = [];
- _.each(this.series, series => {
- let titleCol: any = null;
- let timeCol: any = null;
- const tagsCol: any = [];
- let textCol: any = null;
- _.each(series.columns, (column, index) => {
- if (column === 'time') {
- timeCol = index;
- return;
- }
- if (column === 'sequence_number') {
- return;
- }
- if (column === this.annotation.titleColumn) {
- titleCol = index;
- return;
- }
- if (_.includes((this.annotation.tagsColumn || '').replace(' ', '').split(','), column)) {
- tagsCol.push(index);
- return;
- }
- if (column === this.annotation.textColumn) {
- textCol = index;
- return;
- }
- // legacy case
- if (!titleCol && textCol !== index) {
- titleCol = index;
- }
- });
- _.each(series.values, value => {
- const data = {
- annotation: this.annotation,
- time: +new Date(value[timeCol]),
- title: value[titleCol],
- // Remove empty values, then split in different tags for comma separated values
- tags: _.flatten(
- tagsCol
- .filter((t: any) => {
- return value[t];
- })
- .map((t: any) => {
- return value[t].split(',');
- })
- ),
- text: value[textCol],
- };
- list.push(data);
- });
- });
- return list;
- }
- getTable() {
- const table = new TableModel();
- let i, j;
- if (this.series.length === 0) {
- return table;
- }
- _.each(this.series, (series: any, seriesIndex: number) => {
- if (seriesIndex === 0) {
- j = 0;
- // Check that the first column is indeed 'time'
- if (series.columns[0] === 'time') {
- // Push this now before the tags and with the right type
- table.columns.push({ text: 'Time', type: FieldType.time });
- j++;
- }
- _.each(_.keys(series.tags), key => {
- table.columns.push({ text: key });
- });
- for (; j < series.columns.length; j++) {
- table.columns.push({ text: series.columns[j] });
- }
- }
- if (series.values) {
- for (i = 0; i < series.values.length; i++) {
- const values = series.values[i];
- const reordered = [values[0]];
- if (series.tags) {
- for (const key in series.tags) {
- if (series.tags.hasOwnProperty(key)) {
- reordered.push(series.tags[key]);
- }
- }
- }
- for (j = 1; j < values.length; j++) {
- reordered.push(values[j]);
- }
- table.rows.push(reordered);
- }
- }
- });
- return table;
- }
- }
|