AnalyticsTab.tsx 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import { cx } from '@emotion/css';
  2. import React, { PureComponent } from 'react';
  3. import { ArrayVector, DataFrame, dateTime, FieldType, TimeRange } from '@grafana/data';
  4. import { LegendDisplayMode, TooltipDisplayMode } from '@grafana/schema';
  5. import { Graph, Themeable2, VizTooltip } from '@grafana/ui';
  6. import { SeriesOptions } from '../../types/flotgraph';
  7. import { getGraphSeriesModel } from '../GraphSeriesModel';
  8. import { DAILY_SUMMARY_DATE_FORMAT, DashboardDailySummaryDTO } from '../api';
  9. import { getInsightsStyles } from '../styles';
  10. interface Props extends Themeable2 {
  11. dailySummaries: DashboardDailySummaryDTO[];
  12. }
  13. interface ChartConfig {
  14. title: string;
  15. valueField: keyof DashboardDailySummaryDTO;
  16. fieldType: FieldType;
  17. width: number;
  18. timeRange: TimeRange;
  19. color: string;
  20. showBars: boolean;
  21. showLines: boolean;
  22. }
  23. // AnalyticsTab is a class made to share functions between the different Tabs of the Analytics Drawer
  24. export class AnalyticsTab<T extends Props, V = any> extends PureComponent<T, V> {
  25. convertSummariesToDataFrame(
  26. data: DashboardDailySummaryDTO[],
  27. valueField: keyof DashboardDailySummaryDTO,
  28. valueFieldType: FieldType
  29. ): DataFrame {
  30. const time = new ArrayVector<number>([]);
  31. const values = new ArrayVector<any>([]);
  32. data.forEach((dailySummary) => {
  33. time.buffer.push(dateTime(dailySummary.day, DAILY_SUMMARY_DATE_FORMAT).valueOf());
  34. values.buffer.push(dailySummary[valueField]);
  35. });
  36. return {
  37. name: valueField,
  38. fields: [
  39. { name: 'Time', type: FieldType.time, config: {}, values: time },
  40. { name: valueField, type: valueFieldType, config: {}, values: values },
  41. ],
  42. length: data.length,
  43. };
  44. }
  45. buildTimeRange(): TimeRange {
  46. const { dailySummaries } = this.props;
  47. const from = dateTime(dailySummaries[0].day);
  48. const to = dateTime(dailySummaries[dailySummaries.length - 1].day).add(12, 'hours');
  49. return {
  50. from: from,
  51. to: to,
  52. raw: { from, to },
  53. };
  54. }
  55. renderChart(config: ChartConfig) {
  56. const { dailySummaries, theme } = this.props;
  57. const { color, fieldType, showBars, showLines, timeRange, title, valueField, width } = config;
  58. const styles = getInsightsStyles(theme);
  59. const dataFrame = this.convertSummariesToDataFrame(dailySummaries, valueField, fieldType);
  60. const seriesOptions: SeriesOptions = {};
  61. seriesOptions[valueField] = { color: color };
  62. const series = getGraphSeriesModel(
  63. [dataFrame],
  64. 'browser',
  65. seriesOptions,
  66. { showBars: showBars, showLines: showLines, showPoints: false },
  67. { placement: 'bottom', displayMode: LegendDisplayMode.Hidden }
  68. );
  69. return (
  70. <div className={cx(styles.graphContainer, 'panel-container')} aria-label="Graph container">
  71. <div className="panel-title">{title}</div>
  72. <div className={cx('panel-content', styles.panelContent)}>
  73. <Graph
  74. height={150}
  75. width={width}
  76. timeRange={timeRange}
  77. showBars={showBars}
  78. showLines={showLines}
  79. showPoints={false}
  80. series={series}
  81. timeZone="browser"
  82. >
  83. <VizTooltip mode={TooltipDisplayMode.Multi} />
  84. </Graph>
  85. </div>
  86. </div>
  87. );
  88. }
  89. }