LogLabel.tsx 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import React, { PureComponent } from 'react';
  2. import { LogLabelStats } from './LogLabelStats';
  3. import { LogRowModel, LogLabelStatsModel } from '@grafana/data';
  4. import { calculateLogsLabelStats } from 'app/core/logs_model';
  5. interface Props {
  6. getRows?: () => LogRowModel[];
  7. label: string;
  8. plain?: boolean;
  9. value: string;
  10. onClickLabel?: (label: string, value: string) => void;
  11. }
  12. interface State {
  13. showStats: boolean;
  14. stats: LogLabelStatsModel[];
  15. }
  16. export class LogLabel extends PureComponent<Props, State> {
  17. state = {
  18. stats: null,
  19. showStats: false,
  20. };
  21. onClickClose = () => {
  22. this.setState({ showStats: false });
  23. };
  24. onClickLabel = () => {
  25. const { onClickLabel, label, value } = this.props;
  26. if (onClickLabel) {
  27. onClickLabel(label, value);
  28. }
  29. };
  30. onClickStats = () => {
  31. this.setState(state => {
  32. if (state.showStats) {
  33. return { showStats: false, stats: null };
  34. }
  35. const allRows = this.props.getRows();
  36. const stats = calculateLogsLabelStats(allRows, this.props.label);
  37. return { showStats: true, stats };
  38. });
  39. };
  40. render() {
  41. const { getRows, label, plain, value } = this.props;
  42. const { showStats, stats } = this.state;
  43. const tooltip = `${label}: ${value}`;
  44. return (
  45. <span className="logs-label">
  46. <span className="logs-label__value" title={tooltip}>
  47. {value}
  48. </span>
  49. {!plain && (
  50. <span title="Filter for label" onClick={this.onClickLabel} className="logs-label__icon fa fa-search-plus" />
  51. )}
  52. {!plain && getRows && <span onClick={this.onClickStats} className="logs-label__icon fa fa-signal" />}
  53. {showStats && (
  54. <span className="logs-label__stats">
  55. <LogLabelStats
  56. stats={stats}
  57. rowCount={getRows().length}
  58. label={label}
  59. value={value}
  60. onClickClose={this.onClickClose}
  61. />
  62. </span>
  63. )}
  64. </span>
  65. );
  66. }
  67. }