2
2
3
3
import { useMemo } from 'react'
4
4
import { useLocation } from 'react-router-dom-v5-compat'
5
- import { OCPAppResource } from '../resources'
6
5
import { getBackendUrl , IRequestResult , postRequest } from '../resources/utils'
7
- import { flatten , uniqBy } from 'lodash'
8
6
9
7
export const apiSearchUrl = '/proxy/search'
10
8
const searchFilterQuery =
11
9
'query searchResult($input: [SearchInput]) {\n searchResult: search(input: $input) {\n items\n }\n}'
12
- const searchFilterQueryCount =
13
- 'query searchResult($input: [SearchInput]) {\n searchResult: search(input: $input) {\n count\n }\n}'
14
- const searchMatchAndFilterQuery =
15
- 'query searchResult($byNameInput: [SearchInput], $byNamespaceInput: [SearchInput]) {\n searchResultByName: search(input: $byNameInput) {\n items\n }\n searchResultByNamespace: search(input: $byNamespaceInput) {\n items\n }\n}'
16
- const searchMatchAndFilterQueryCount =
17
- 'query searchResult($byNameInput: [SearchInput], $byNamespaceInput: [SearchInput]) {\n searchResultByName: search(input: $byNameInput) {\n count\n }\n searchResultByNamespace: search(input: $byNamespaceInput) {\n count\n }\n}'
18
- const searchMatchWithClusterAndFilterQuery =
19
- 'query searchResult($byNameInput: [SearchInput], $byNamespaceInput: [SearchInput], $byClusterInput: [SearchInput]) {\n searchResultByName: search(input: $byNameInput) {\n items\n }\n searchResultByNamespace: search(input: $byNamespaceInput) {\n items\n }\n searchResultByCluster: search(input: $byClusterInput) {\n items\n }\n}'
20
- const searchMatchWithClusterAndFilterQueryCount =
21
- 'query searchResult($byNameInput: [SearchInput], $byNamespaceInput: [SearchInput], $byClusterInput: [SearchInput]) {\n searchResultByName: search(input: $byNameInput) {\n count\n }\n searchResultByNamespace: search(input: $byNamespaceInput) {\n count\n }\n searchResultByCluster: search(input: $byClusterInput) {\n count\n }\n}'
22
10
23
11
export type ISearchResult = {
24
12
data : {
@@ -45,159 +33,6 @@ export type SearchQuery = {
45
33
query : string
46
34
}
47
35
48
- export function queryStatusCount ( cluster : string ) : IRequestResult < ISearchResult > {
49
- return postRequest < SearchQuery , ISearchResult > ( getBackendUrl ( ) + apiSearchUrl , {
50
- operationName : 'searchResult' ,
51
- variables : {
52
- input : [
53
- {
54
- filters : [
55
- { property : 'compliant' , values : [ '!Compliant' ] } ,
56
- { property : 'kind' , values : [ 'Policy' ] } ,
57
- { property : 'namespace' , values : [ cluster ] } ,
58
- { property : 'cluster' , values : [ 'local-cluster' ] } ,
59
- ] ,
60
- } ,
61
- ] ,
62
- } ,
63
- query :
64
- 'query searchResult($input: [SearchInput]) {\n searchResult: search(input: $input) {\n count\n related {\n kind\n count\n __typename\n }\n __typename\n }\n}\n' ,
65
- } )
66
- }
67
-
68
- export type DiscoveredAppsParams = {
69
- clusters ?: string [ ]
70
- types ?: string [ ]
71
- search ?: string
72
- searchLimit ?: number
73
- }
74
-
75
- function getOCPAppResourceLabelValues ( {
76
- types = [ ] ,
77
- name,
78
- } : Pick < DiscoveredAppsParams , 'types' > & { name ?: DiscoveredAppsParams [ 'search' ] } ) {
79
- const allTypes = types . length === 0
80
- const searchString = name ? `*${ name } *` : '*'
81
- const convertToLabelSearch = ( label : string ) => `${ label } =${ searchString } `
82
- return [
83
- ...( allTypes || types . includes ( 'openshift' ) || types . includes ( 'openshift-default' )
84
- ? [ 'app' , 'app.kubernetes.io/part-of' ] . map ( convertToLabelSearch )
85
- : [ ] ) ,
86
- ...( allTypes || types . includes ( 'flux' )
87
- ? [ 'kustomize.toolkit.fluxcd.io/name' , 'helm.toolkit.fluxcd.io/name' ] . map ( convertToLabelSearch )
88
- : [ ] ) ,
89
- ]
90
- }
91
-
92
- function getOCPAppResourceFilters ( {
93
- cluster,
94
- clusters = [ ] ,
95
- name,
96
- namespace,
97
- types = [ ] ,
98
- } : Pick < DiscoveredAppsParams , 'clusters' | 'types' > & {
99
- cluster ?: DiscoveredAppsParams [ 'search' ]
100
- name ?: DiscoveredAppsParams [ 'search' ]
101
- namespace ?: DiscoveredAppsParams [ 'search' ]
102
- } ) {
103
- const filtersArr = [
104
- {
105
- property : 'kind' ,
106
- values : [ 'CronJob' , 'DaemonSet' , 'Deployment' , 'DeploymentConfig' , 'Job' , 'StatefulSet' ] ,
107
- } ,
108
- {
109
- property : 'label' ,
110
- values : getOCPAppResourceLabelValues ( { types, name } ) ,
111
- } ,
112
- ...( namespace ? [ { property : 'namespace' , values : [ `*${ namespace } *` ] } ] : [ ] ) ,
113
- ]
114
-
115
- if ( clusters . length ) {
116
- filtersArr . push ( {
117
- property : 'cluster' ,
118
- values : clusters ,
119
- } )
120
- } else if ( cluster ) {
121
- filtersArr . push ( {
122
- property : 'cluster' ,
123
- values : [ `*${ cluster } *` ] ,
124
- } )
125
- }
126
-
127
- return filtersArr
128
- }
129
-
130
- export async function queryOCPAppResources ( params : DiscoveredAppsParams ) : Promise < OCPAppResource [ ] >
131
- export async function queryOCPAppResources ( params : DiscoveredAppsParams & { countOnly : true } ) : Promise < number >
132
- export async function queryOCPAppResources (
133
- params : DiscoveredAppsParams & { countOnly ?: true }
134
- ) : Promise < OCPAppResource [ ] | number > {
135
- const { clusters = [ ] , types = [ ] , search, searchLimit, countOnly = false } = params
136
-
137
- let variables : SearchQuery [ 'variables' ]
138
- let query : string
139
-
140
- const limitObject = countOnly ? { } : { limit : searchLimit }
141
-
142
- if ( search ) {
143
- variables = {
144
- byNameInput : [
145
- {
146
- filters : getOCPAppResourceFilters ( { clusters, types, name : search } ) ,
147
- ...limitObject ,
148
- } ,
149
- ] ,
150
- byNamespaceInput : [
151
- {
152
- filters : getOCPAppResourceFilters ( { clusters, types, namespace : search } ) ,
153
- ...limitObject ,
154
- } ,
155
- ] ,
156
- ...( clusters . length
157
- ? { }
158
- : {
159
- byClusterInput : [
160
- {
161
- filters : getOCPAppResourceFilters ( { types, cluster : search } ) ,
162
- ...limitObject ,
163
- } ,
164
- ] ,
165
- } ) ,
166
- }
167
- if ( clusters . length ) {
168
- query = countOnly ? searchMatchAndFilterQueryCount : searchMatchAndFilterQuery
169
- } else {
170
- query = countOnly ? searchMatchWithClusterAndFilterQueryCount : searchMatchWithClusterAndFilterQuery
171
- }
172
- } else {
173
- variables = {
174
- input : [
175
- {
176
- filters : getOCPAppResourceFilters ( { clusters, types } ) ,
177
- ...limitObject ,
178
- } ,
179
- ] ,
180
- }
181
- query = countOnly ? searchFilterQueryCount : searchFilterQuery
182
- }
183
-
184
- const { promise } = postRequest < SearchQuery , ISearchResult > ( getBackendUrl ( ) + apiSearchUrl , {
185
- operationName : 'searchResult' ,
186
- variables,
187
- query,
188
- } )
189
- return promise . then ( ( result ) => {
190
- if ( countOnly ) {
191
- return Math . max ( ...Object . values ( result . data ) . map ( ( value ) => value ?. [ 0 ] ?. count || 0 ) )
192
- } else {
193
- return uniqBy (
194
- flatten ( Object . values ( result . data ) . map ( ( value ) => value ?. [ 0 ] ?. items || [ ] ) ) ,
195
- ( item ) => item . _uid
196
- ) as OCPAppResource [ ]
197
- }
198
- } )
199
- }
200
-
201
36
export function querySearchDisabledManagedClusters ( ) : IRequestResult < ISearchResult > {
202
37
return postRequest < SearchQuery , ISearchResult > ( getBackendUrl ( ) + apiSearchUrl , {
203
38
operationName : 'searchResult' ,
0 commit comments