All files / components/statusChart statusMetricLineChart.tsx

0% Statements 0/170
100% Branches 1/1
100% Functions 1/1
0% Lines 0/170

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201                                                                                                                                                                                                                                                                                                                                                                                                                 
'use client';
 
import {
  useCallback, useEffect, useState
} from 'react';
import Button from 'react-bootstrap/Button';
import Col from 'react-bootstrap/Col';
import Row from 'react-bootstrap/Row';
import { Line } from 'react-chartjs-2';
import { BsArrowClockwise } from 'react-icons/bs';
 
import {
  useChartData, usePageSize
} from './common';
 
import LoadingSpinner from '@/components/loadingSpinner/loadingSpinner';
import {
  ChartComponentParams, MetricChart
} from '@/types/frontend/chart';
 
const msToSFormat = (places: number = 1) => (v: string | number) => {
  if (typeof v === 'string') {
    v = Number(v);
  }
 
  return (Math.round(v) / 1000).toFixed(places);
};
const secondFormat = (places: number = 1) => (v: string | number) => {
  if (typeof v === 'string') {
    v = Number(v);
  }
 
  return (Math.round(v * 1000) / 1000).toFixed(places);
};
 
export default function StatusMetricLineChart({
  title,
  body,
  shouldFetchData,
  lazyLoad,
  setChartLoaded,
  unit,
  mode,
}: Readonly<ChartComponentParams<MetricChart>>) {
  const [
    shouldLoad,
    setShouldLoad,
  ] = useState(false);
 
  if (!lazyLoad && shouldFetchData && !shouldLoad) {
    setShouldLoad(true);
  }
 
  const [
    data,
    resetDataRaw,
  ] = useChartData(
    body,
    shouldLoad,
    setChartLoaded,
    false
  );
 
  const resetData = useCallback(() => {
    resetDataRaw();
    setShouldLoad(true);
  }, [ resetDataRaw, ]);
 
  useEffect(() => {
    resetDataRaw();
  }, [ mode, ]); // eslint-disable-line react-hooks/exhaustive-deps
 
  const [
    pageWidth,
    pageHeight,
  ] = usePageSize();
  const options: Parameters<typeof Line>[0]['options'] = {
    maintainAspectRatio: false,
    interaction: {
      mode: 'index',
    },
    scales: {
      y: {
        min: 0,
      },
    },
    plugins: {
      legend: {
        position: pageWidth && pageHeight && pageWidth > pageHeight ? 'right' : 'bottom',
      },
    },
  };
  switch (unit) {
    case 'Milliseconds':
      // Set the scale value to S
      options.scales = options.scales || {};
      options.scales.y = {
        ...options.scales.y,
        title: {
          text: 'Duration (s)',
          display: true,
        },
        ticks: {
          callback: msToSFormat(),
        },
      };
 
      // Set the tooltip to S
      options.plugins = options.plugins || {};
      options.plugins.tooltip = options.plugins.tooltip || {};
      options.plugins.tooltip.callbacks = {
        ...options.plugins.tooltip.callbacks,
        label: function (context) {
          let label = context.dataset.label || '';
 
          if (label) {
            label += ': ';
          }
          if (context.parsed.y !== null) {
            label += msToSFormat(3)(context.parsed.y) + 's';
          }
          return label;
        },
      };
      break;
    case 'Seconds':
      // Set the scale value to S
      options.scales = options.scales || {};
      options.scales.y = {
        ...options.scales.y,
        title: {
          text: 'Duration (s)',
          display: true,
        },
        ticks: {
          callback: secondFormat(0),
        },
      };
 
      // Set the tooltip to S
      options.plugins = options.plugins || {};
      options.plugins.tooltip = options.plugins.tooltip || {};
      options.plugins.tooltip.callbacks = {
        ...options.plugins.tooltip.callbacks,
        label: function (context) {
          let label = context.dataset.label || '';
 
          if (label) {
            label += ': ';
          }
          if (context.parsed.y !== null) {
            label += secondFormat(1)(context.parsed.y) + 's';
          }
          return label;
        },
      };
      break;
  }
 
  return <>
    <h3 className='text-center mt-5'>{title} <BsArrowClockwise
      style={{ cursor: 'pointer', }}
      onClick={resetData}
    /></h3>
    <Row><Col style={{ height: 'calc(80vh - 60px)', }}>
      {typeof data === 'undefined' && !lazyLoad && <div style={{ height: '100%', }}><LoadingSpinner fullHeight={true} /></div>}
      {typeof data === 'undefined' && lazyLoad && shouldLoad && <LoadingSpinner fullHeight={true} />}
      {typeof data === 'undefined' && lazyLoad && !shouldLoad && <Col
        className='d-grid'
        xs={{
          span: 6,
          offset: 3,
        }}
        style={{ height: '100%', }}
      >
        <Button
          className='align-self-center'
          variant='success'
          onClick={() => setShouldLoad(true)}
        >Load Chart</Button>
      </Col>}
      {data === null && <h2 className='text-center'>Error loading data</h2>}
      {data && data.datasets.length === 0 && <Col
        className='d-grid'
        xs={{
          span: 6,
          offset: 3,
        }}
        style={{ height: '75%', }}
      ><h2 className='text-center align-self-center'>No Data Found</h2></Col>}
      {data && data.datasets.length > 0 && <Line
        data={{
          labels: data.labels,
          datasets: data.datasets,
        }}
        options={options}
      />}
    </Col></Row>
  </>;
}