|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License; |
| 4 | + * you may not use this file except in compliance with the Elastic License. |
| 5 | + */ |
| 6 | + |
| 7 | +import React, { useContext, useState } from 'react'; |
| 8 | +import { |
| 9 | + EuiBasicTable, |
| 10 | + EuiFlexItem, |
| 11 | + EuiFlexGroup, |
| 12 | + EuiSpacer, |
| 13 | + EuiTitle, |
| 14 | + EuiStat, |
| 15 | + EuiToolTip, |
| 16 | +} from '@elastic/eui'; |
| 17 | +import numeral from '@elastic/numeral'; |
| 18 | +import { i18n } from '@kbn/i18n'; |
| 19 | +import { useUrlParams } from '../../../../hooks/useUrlParams'; |
| 20 | +import { useFetcher } from '../../../../hooks/useFetcher'; |
| 21 | +import { I18LABELS } from '../translations'; |
| 22 | +import { CsmSharedContext } from '../CsmSharedContext'; |
| 23 | + |
| 24 | +export function JSErrors() { |
| 25 | + const { urlParams, uiFilters } = useUrlParams(); |
| 26 | + |
| 27 | + const { start, end, serviceName } = urlParams; |
| 28 | + |
| 29 | + const [pagination, setPagination] = useState({ pageIndex: 0, pageSize: 5 }); |
| 30 | + |
| 31 | + const { data, status } = useFetcher( |
| 32 | + (callApmApi) => { |
| 33 | + if (start && end && serviceName) { |
| 34 | + return callApmApi({ |
| 35 | + pathname: '/api/apm/rum-client/js-errors', |
| 36 | + params: { |
| 37 | + query: { |
| 38 | + start, |
| 39 | + end, |
| 40 | + uiFilters: JSON.stringify(uiFilters), |
| 41 | + pageSize: String(pagination.pageSize), |
| 42 | + pageIndex: String(pagination.pageIndex), |
| 43 | + }, |
| 44 | + }, |
| 45 | + }); |
| 46 | + } |
| 47 | + return Promise.resolve(null); |
| 48 | + }, |
| 49 | + [start, end, serviceName, uiFilters, pagination] |
| 50 | + ); |
| 51 | + |
| 52 | + const { |
| 53 | + sharedData: { totalPageViews }, |
| 54 | + } = useContext(CsmSharedContext); |
| 55 | + |
| 56 | + const items = (data?.items ?? []).map(({ errorMessage, count }) => ({ |
| 57 | + errorMessage, |
| 58 | + percent: i18n.translate('xpack.apm.rum.jsErrors.percent', { |
| 59 | + defaultMessage: '{pageLoadPercent} %', |
| 60 | + values: { pageLoadPercent: ((count / totalPageViews) * 100).toFixed(1) }, |
| 61 | + }), |
| 62 | + })); |
| 63 | + |
| 64 | + const cols = [ |
| 65 | + { |
| 66 | + field: 'errorMessage', |
| 67 | + name: I18LABELS.errorMessage, |
| 68 | + }, |
| 69 | + { |
| 70 | + name: I18LABELS.impactedPageLoads, |
| 71 | + field: 'percent', |
| 72 | + align: 'right' as const, |
| 73 | + }, |
| 74 | + ]; |
| 75 | + |
| 76 | + const onTableChange = ({ |
| 77 | + page, |
| 78 | + }: { |
| 79 | + page: { size: number; index: number }; |
| 80 | + }) => { |
| 81 | + setPagination({ |
| 82 | + pageIndex: page.index, |
| 83 | + pageSize: page.size, |
| 84 | + }); |
| 85 | + }; |
| 86 | + |
| 87 | + return ( |
| 88 | + <> |
| 89 | + <EuiTitle size="xs"> |
| 90 | + <h3>{I18LABELS.jsErrors}</h3> |
| 91 | + </EuiTitle> |
| 92 | + <EuiSpacer size="s" /> |
| 93 | + <EuiFlexGroup> |
| 94 | + <EuiFlexItem grow={false}> |
| 95 | + <EuiStat |
| 96 | + titleSize="s" |
| 97 | + title={ |
| 98 | + <EuiToolTip content={data?.totalErrors ?? 0}> |
| 99 | + <>{numeral(data?.totalErrors ?? 0).format('0 a')}</> |
| 100 | + </EuiToolTip> |
| 101 | + } |
| 102 | + description={I18LABELS.totalErrors} |
| 103 | + isLoading={status !== 'success'} |
| 104 | + /> |
| 105 | + </EuiFlexItem> |
| 106 | + <EuiFlexItem grow={false}> |
| 107 | + <EuiStat |
| 108 | + titleSize="s" |
| 109 | + title={i18n.translate('xpack.apm.rum.jsErrors.errorRateValue', { |
| 110 | + defaultMessage: '{errorRate} %', |
| 111 | + values: { |
| 112 | + errorRate: ( |
| 113 | + ((data?.totalErrorPages ?? 0) / totalPageViews) * |
| 114 | + 100 |
| 115 | + ).toFixed(0), |
| 116 | + }, |
| 117 | + })} |
| 118 | + description={I18LABELS.errorRate} |
| 119 | + isLoading={status !== 'success'} |
| 120 | + /> |
| 121 | + </EuiFlexItem>{' '} |
| 122 | + </EuiFlexGroup> |
| 123 | + <EuiSpacer size="s" /> |
| 124 | + <EuiBasicTable |
| 125 | + loading={status !== 'success'} |
| 126 | + responsive={false} |
| 127 | + compressed={true} |
| 128 | + columns={cols} |
| 129 | + items={items} |
| 130 | + onChange={onTableChange} |
| 131 | + pagination={{ |
| 132 | + ...pagination, |
| 133 | + totalItemCount: data?.totalErrorGroups ?? 0, |
| 134 | + }} |
| 135 | + /> |
| 136 | + </> |
| 137 | + ); |
| 138 | +} |
0 commit comments