All files / components/statusChart statusTowerLineChart.tsx

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

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 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224                                                                                                                                                                                                                                                                                                                                                                                                                                                               
'use client';
 
import {
  useCallback,
  useContext, 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 } from './common';
 
import LoadingSpinner from '@/components/loadingSpinner/loadingSpinner';
import {
  ChartComponentParams, TowerChart
} from '@/types/frontend/chart';
import { DarkModeContext } from '@/utils/frontend/clientContexts';
 
interface ColorConfig {
  backgroundColor: string;
  borderColor: string;
}
 
const color1: ColorConfig = {
  backgroundColor: 'rgba(54, 162, 235, 0.5)',
  borderColor: 'rgba(54, 162, 235, 0.5)',
};
const color2: ColorConfig = {
  backgroundColor: 'rgba(255, 99, 132, 0.5)',
  borderColor: 'rgba(255, 99, 132, 0.5)',
};
 
export default function StatusTowerLineChart({
  title,
  body,
  lazyLoad,
  shouldFetchData,
  setChartLoaded,
  mode,
}: Readonly<ChartComponentParams<TowerChart>>) {
  const darkMode = useContext(DarkModeContext);
  const [
    shouldLoad,
    setShouldLoad,
  ] = useState(false);
 
  if (!lazyLoad && shouldFetchData && !shouldLoad) {
    setShouldLoad(true);
  }
 
  const [
    data,
    resetDataRaw,
  ] = useChartData(
    body,
    shouldLoad,
    setChartLoaded,
    true
  );
 
  const resetData = useCallback(() => {
    resetDataRaw();
    setShouldLoad(true);
  }, [ resetDataRaw, ]);
 
  useEffect(() => {
    resetDataRaw();
  }, [ mode, ]); // eslint-disable-line react-hooks/exhaustive-deps
 
  if (data && data.datasets.length === 3) {
    data.datasets.push({
      backgroundColor: 'rgba(0,0,0,0)',
      fill: false,
      pointStyle: false,
      label: 'none',
      data: data.labels.map((label, i) => {
        const dataA = data.datasets[0].data[i];
        const dataB = data.datasets[1].data[i] as number;
 
        if (dataA < 30 && dataB < 30) {
          return dataA;
        }
        if (dataA > 30 && dataB > 30) {
          return dataB;
        }
        return 30;
      }),
    });
  }
 
  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 && <Line
        data={{
          labels: data.labels,
          datasets: data.datasets
            .map((dataset, idx) => {
              dataset.label = dataset.label || '';
 
              if (idx === 0) {
                dataset.borderColor = color1.borderColor;
                dataset.backgroundColor = color1.backgroundColor;
                dataset.showLine = false;
                dataset.fill = {
                  target: 3,
                  above: color1.backgroundColor,
                  below: 'rgba(0, 0, 0, 0)',
                };
              } else if (idx === 1) {
                dataset.borderColor = color2.borderColor;
                dataset.backgroundColor = color2.backgroundColor;
                dataset.showLine = false;
                dataset.fill = {
                  target: 3,
                  above: 'rgba(0, 0, 0, 0)',
                  below: color2.backgroundColor,
                };
              } else if (idx === 2) {
                dataset.yAxisID = 'y2';
                if (darkMode === 'dark') {
                  dataset.borderColor = 'gray';
                }
              }
 
              return dataset;
            }),
        }}
        options={{
          maintainAspectRatio: false,
          interaction: {
            mode: 'index',
          },
          scales: {
            y: {
              min: 0,
              max: 45,
              title: {
                text: 'Messages per Second',
                display: true,
              },
            },
            y2: {
              type: 'linear',
              display: true,
              position: 'right',
              grid: {
                drawOnChartArea: false,
              },
              title: {
                text: 'Recordings Uploaded',
                display: true,
              },
            },
          },
          plugins: {
            annotation: {
              annotations: {
                line1: {
                  type: 'line',
                  label: {
                    content: 'Service Degraded',
                    display: true,
                    color: darkMode === 'dark' ? '#fff' : '#000',
                    backgroundColor: 'transparent',
                    yAdjust: 10,
                  },
                  yMin: 30,
                  yMax: 30,
                  borderColor: color2.borderColor,
                  borderWidth: 2,
                },
                line2: {
                  type: 'line',
                  label: {
                    content: 'Optimal Service',
                    display: true,
                    color: darkMode === 'dark' ? '#fff' : '#000',
                    backgroundColor: 'transparent',
                    yAdjust: -10,
                  },
                  yMin: 40,
                  yMax: 40,
                  borderColor: color1.borderColor,
                  borderWidth: 2,
                },
              },
            },
            legend: {
              display: false,
            },
            tooltip: {
              filter: (a, b, c) => a.dataset.label !== 'none' &&
                c.map(v => v.dataset.label).indexOf(a.dataset.label) === b,
            },
          },
        }}
      />}
    </Col></Row>
  </>;
}