LogLabelStats.tsx 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import React, { PureComponent } from 'react';
  2. import classnames from 'classnames';
  3. import { LogLabelStatsModel } from 'app/core/logs_model';
  4. function LogLabelStatsRow(logLabelStatsModel: LogLabelStatsModel) {
  5. const { active, count, proportion, value } = logLabelStatsModel;
  6. const percent = `${Math.round(proportion * 100)}%`;
  7. const barStyle = { width: percent };
  8. const className = classnames('logs-stats-row', { 'logs-stats-row--active': active });
  9. return (
  10. <div className={className}>
  11. <div className="logs-stats-row__label">
  12. <div className="logs-stats-row__value">{value}</div>
  13. <div className="logs-stats-row__count">{count}</div>
  14. <div className="logs-stats-row__percent">{percent}</div>
  15. </div>
  16. <div className="logs-stats-row__bar">
  17. <div className="logs-stats-row__innerbar" style={barStyle} />
  18. </div>
  19. </div>
  20. );
  21. }
  22. const STATS_ROW_LIMIT = 5;
  23. interface Props {
  24. stats: LogLabelStatsModel[];
  25. label: string;
  26. value: string;
  27. rowCount: number;
  28. onClickClose: () => void;
  29. }
  30. export class LogLabelStats extends PureComponent<Props> {
  31. render() {
  32. const { label, rowCount, stats, value, onClickClose } = this.props;
  33. const topRows = stats.slice(0, STATS_ROW_LIMIT);
  34. let activeRow = topRows.find(row => row.value === value);
  35. let otherRows = stats.slice(STATS_ROW_LIMIT);
  36. const insertActiveRow = !activeRow;
  37. // Remove active row from other to show extra
  38. if (insertActiveRow) {
  39. activeRow = otherRows.find(row => row.value === value);
  40. otherRows = otherRows.filter(row => row.value !== value);
  41. }
  42. const otherCount = otherRows.reduce((sum, row) => sum + row.count, 0);
  43. const topCount = topRows.reduce((sum, row) => sum + row.count, 0);
  44. const total = topCount + otherCount;
  45. const otherProportion = otherCount / total;
  46. return (
  47. <div className="logs-stats">
  48. <div className="logs-stats__header">
  49. <span className="logs-stats__title">
  50. {label}: {total} of {rowCount} rows have that label
  51. </span>
  52. <span className="logs-stats__close fa fa-remove" onClick={onClickClose} />
  53. </div>
  54. <div className="logs-stats__body">
  55. {topRows.map(stat => <LogLabelStatsRow key={stat.value} {...stat} active={stat.value === value} />)}
  56. {insertActiveRow && activeRow && <LogLabelStatsRow key={activeRow.value} {...activeRow} active />}
  57. {otherCount > 0 && (
  58. <LogLabelStatsRow key="__OTHERS__" count={otherCount} value="Other" proportion={otherProportion} />
  59. )}
  60. </div>
  61. </div>
  62. );
  63. }
  64. }