|
| 1 | +import React, { useState } from 'react'; |
| 2 | +import { all, solo as soloStyle } from './sizeSwitch'; |
| 3 | + |
| 4 | +interface EventChartProps { |
| 5 | + metricName: string; |
| 6 | + token: string; |
| 7 | +} |
| 8 | + |
| 9 | +// interface SoloStyles { |
| 10 | +// height: number; |
| 11 | +// width: number; |
| 12 | +// } |
| 13 | + |
| 14 | + |
| 15 | + |
| 16 | +/** |
| 17 | + * @params {EventChartProps} props - the props object containing relevant data. |
| 18 | + * @desc Handles k8s metrics. Memoized component to generate event chart with formatted data |
| 19 | + * @returns {JSX.Element} The JSX element with the event chart. |
| 20 | + */ |
| 21 | +const GrafanaEventChart: React.FC<EventChartProps> = React.memo(props => { |
| 22 | + const { metricName, token } = props; |
| 23 | + const [graphType, setGraphType] = useState<string>("timeseries"); |
| 24 | + // const [solo, setSolo] = useState<SoloStyles | null>(null); |
| 25 | + console.log("inside GrafanaEventChart") |
| 26 | + |
| 27 | + // setInterval(() => { |
| 28 | + // if (solo !== soloStyle) { |
| 29 | + // setSolo(soloStyle); |
| 30 | + // } |
| 31 | + // }, 20); |
| 32 | + console.log("metricName: ", metricName) |
| 33 | + let uid = metricName.replace(/.*\/.*\//g, '') |
| 34 | + if (uid.length >= 40) { |
| 35 | + uid = metricName.slice(metricName.length - 39); |
| 36 | + } |
| 37 | + |
| 38 | + let parsedName = metricName.replace(/.*\/.*\//g, '') |
| 39 | + console.log("uid: ", uid) |
| 40 | + console.log("parsedName: ", parsedName) |
| 41 | + |
| 42 | + const handleSelectionChange = async (event) => { |
| 43 | + setGraphType(event.target.value); |
| 44 | + await fetch('http://localhost:1111/api/updateDashboard', { |
| 45 | + method: 'POST', |
| 46 | + headers: { |
| 47 | + 'Content-Type': 'application/json', |
| 48 | + }, |
| 49 | + body: JSON.stringify({ graphType: event.target.value, metric: metricName, token: token }), |
| 50 | + }) |
| 51 | + |
| 52 | + } |
| 53 | + |
| 54 | + return ( |
| 55 | + <div className="chart" data-testid="Grafana Event Chart"> |
| 56 | + <h2>{parsedName}</h2> |
| 57 | + {/* create chart using grafana iframe tag*/} |
| 58 | + <iframe src={`http://localhost:32000/d-solo/${uid}/${parsedName}?orgId=1&refresh=10s&from=now-5m&to=now&panelId=1`} width="650" height="400" ></iframe> |
| 59 | + <select name="graphType" id="graphType" value={graphType} onChange={handleSelectionChange}> |
| 60 | + <option value="timeseries">Time Series</option> |
| 61 | + <option value="barchart">Bar Chart</option> |
| 62 | + <option value="stat">Stat</option> |
| 63 | + <option value="gauge">Gauge</option> |
| 64 | + <option value="table">Table</option> |
| 65 | + <option value="histogram">Histogram</option> |
| 66 | + </select> |
| 67 | + </div> |
| 68 | + ); |
| 69 | +}); |
| 70 | + |
| 71 | +export default GrafanaEventChart; |
0 commit comments