-
Notifications
You must be signed in to change notification settings - Fork 176
/
dashboard.tsx
156 lines (145 loc) · 4.94 KB
/
dashboard.tsx
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
import React, { useState, useEffect } from 'react';
import { SearchResponse } from '../../../../../../../src/core/server';
import { getPlugins } from '../../../../kibana-services';
import { ViewMode } from '../../../../../../../src/plugins/embeddable/public';
import { getDashboardPanels } from './dashboard_panels';
import { I18nProvider } from '@osd/i18n/react';
import useSearchBar from '../../../common/search-bar/use-search-bar';
import { withErrorBoundary } from '../../../common/hocs';
import { IndexPattern } from '../../../../../../../src/plugins/data/common';
import {
ErrorFactory,
ErrorHandler,
HttpError,
} from '../../../../react-services/error-management';
import { compose } from 'redux';
import { SampleDataWarning } from '../../../visualize/components';
import {
AlertsDataSourceRepository,
PatternDataSource,
tParsedIndexPattern,
useDataSource,
} from '../../../common/data-source';
import { AlertsGoogleCloudDataSource } from '../../../common/data-source/pattern/alerts/alerts-google-cloud/alerts-google-cloud-data-source';
import { DiscoverNoResults } from '../../../common/no-results/no-results';
import { LoadingSpinner } from '../../../common/loading-spinner/loading-spinner';
import { useReportingCommunicateSearchContext } from '../../../common/hooks/use-reporting-communicate-search-context';
const plugins = getPlugins();
const SearchBar = getPlugins().data.ui.SearchBar;
const DashboardByRenderer = plugins.dashboard.DashboardContainerByValueRenderer;
const DashboardGoogleCloudComponent: React.FC = () => {
const {
filters,
dataSource,
fetchFilters,
isLoading: isDataSourceLoading,
fetchData,
setFilters,
} = useDataSource<tParsedIndexPattern, PatternDataSource>({
DataSource: AlertsGoogleCloudDataSource,
repository: new AlertsDataSourceRepository(),
});
const [results, setResults] = useState<SearchResponse>({} as SearchResponse);
const { searchBarProps } = useSearchBar({
indexPattern: dataSource?.indexPattern as IndexPattern,
filters,
setFilters,
});
const { query, dateRangeFrom, dateRangeTo } = searchBarProps;
useReportingCommunicateSearchContext({
isSearching: isDataSourceLoading,
totalResults: results?.hits?.total ?? 0,
indexPattern: dataSource?.indexPattern,
filters: fetchFilters,
query: query,
time: {
from: dateRangeFrom,
to: dateRangeTo,
},
});
useEffect(() => {
if (isDataSourceLoading) {
return;
}
fetchData({
query,
dateRange: {
to: dateRangeTo,
from: dateRangeFrom,
},
})
.then(results => setResults(results))
.catch(error => {
const searchError = ErrorFactory.create(HttpError, {
error,
message: 'Error fetching alerts',
});
ErrorHandler.handleError(searchError);
});
}, [
JSON.stringify(fetchFilters),
JSON.stringify(query),
JSON.stringify(dateRangeFrom),
JSON.stringify(dateRangeTo),
]);
return (
<>
<I18nProvider>
<>
{isDataSourceLoading && !dataSource ? (
<LoadingSpinner />
) : (
<div className='wz-search-bar hide-filter-control'>
<SearchBar
appName='google-cloud-searchbar'
{...searchBarProps}
showDatePicker={true}
showQueryInput={true}
showQueryBar={true}
showSaveQuery={true}
/>
</div>
)}
{dataSource && results?.hits?.total === 0 ? (
<DiscoverNoResults />
) : null}
{dataSource && results?.hits?.total > 0 ? (
<>
<SampleDataWarning />
<div className='google-cloud-dashboard-responsive'>
<DashboardByRenderer
input={{
viewMode: ViewMode.VIEW,
panels: getDashboardPanels(
dataSource?.id,
Boolean(dataSource?.getPinnedAgentFilter()?.length),
),
isFullScreenMode: false,
filters: fetchFilters ?? [],
useMargins: true,
id: 'google-cloud-detector-dashboard-tab',
timeRange: {
from: searchBarProps.dateRangeFrom,
to: searchBarProps.dateRangeTo,
},
title: 'Google Cloud detector dashboard',
description: 'Dashboard of the Google Cloud',
query: searchBarProps.query,
refreshConfig: {
pause: false,
value: 15,
},
hidePanelTitles: false,
}}
/>
</div>
</>
) : null}
</>
</I18nProvider>
</>
);
};
export const DashboardGoogleCloud = compose(withErrorBoundary)(
DashboardGoogleCloudComponent,
);