Skip to content
This repository was archived by the owner on Nov 1, 2024. It is now read-only.

Commit e8391ba

Browse files
committed
use biquery fetch endpoint
1 parent 7054377 commit e8391ba

14 files changed

+119
-462
lines changed

components/dataproducts/dataset/useColumnTags.tsx

+10-17
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { useState } from 'react'
22
import {
33
BigQueryType,
4-
useGcpGetColumnsQuery,
54
} from '../../../lib/schema/graphql'
5+
import { useFetchBQcolumns } from '../../../lib/rest/bigquery'
66

77
export type PIITagType =
88
| 'PII_DirekteIdentifiserende'
@@ -52,14 +52,7 @@ export const useColumnTags = (
5252
const [pseudoColumnsMap, setPseudoColumnsMap] = useState<PseudoColumnsMapType>(
5353
new Map<string, Map<string, boolean>>()
5454
)
55-
56-
const columnsQuery = useGcpGetColumnsQuery({
57-
variables: {
58-
projectID: projectID,
59-
datasetID: datasetID,
60-
tableID: tableID,
61-
},
62-
})
55+
const fetchColumns = useFetchBQcolumns(projectID, datasetID, tableID)
6356

6457
var tableKey = buildTableKey(projectID, datasetID, tableID)
6558

@@ -69,9 +62,9 @@ export const useColumnTags = (
6962
datasetID &&
7063
tableID &&
7164
!tagsMap.has(tableKey) &&
72-
!columnsQuery.error &&
73-
!columnsQuery.loading &&
74-
columnsQuery.data
65+
!fetchColumns.error &&
66+
!fetchColumns.loading &&
67+
fetchColumns.bqColumns
7568
) {
7669
var newTagsMap = new Map<string, Map<string, PIITagType> | undefined>(
7770
tagsMap
@@ -81,7 +74,7 @@ export const useColumnTags = (
8174

8275
var newPseudoColumnsMap = new Map<string, Map<string, boolean>>(pseudoColumnsMap)
8376
var pseudoColumns = new Map<string, boolean>()
84-
columnsQuery.data.gcpGetColumns.forEach((it) =>{
77+
fetchColumns.bqColumns.forEach((it) =>{
8578
tags.set(
8679
it.name,
8780
(!!tagsFromQuery[it.name] &&
@@ -126,11 +119,11 @@ export const useColumnTags = (
126119

127120
return {
128121
columns:
129-
!columnsQuery.error && !columnsQuery.loading
130-
? (columnsQuery.data?.gcpGetColumns as ColumnType[])
122+
!fetchColumns.error && !fetchColumns.loading
123+
? (fetchColumns.bqColumns as ColumnType[])
131124
: undefined,
132-
loading: columnsQuery.loading,
133-
error: columnsQuery.error,
125+
loading: fetchColumns.loading,
126+
error: fetchColumns.error,
134127
tags: tagsMap.get(tableKey),
135128
pseudoColumns: pseudoColumnsMap.get(tableKey) || new Map<string, boolean>(),
136129
annotateColumn: annotateColumn,

components/dataproducts/datasource/dataset.tsx

+15-20
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
import {
22
BigQueryType,
3-
GcpGetTablesQuery,
4-
useGcpGetTablesLazyQuery,
53
} from '../../../lib/schema/graphql'
64

75
import { TreeItem } from '@mui/x-tree-view/TreeItem';
86

97
import { Loader } from '@navikt/ds-react'
108
import { ExpandFilled, NextFilled } from '@navikt/ds-icons'
119
import Tabell from '../../lib/icons/tabell'
10+
import { useFetchBQTables } from '../../../lib/rest/bigquery';
1211

13-
const DataproductTableIconMap: Record<BigQueryType, () => JSX.Element> = {
14-
materialized_view: Tabell,
15-
table: Tabell,
16-
view: Tabell,
12+
const DataproductTableIconMap: Record<string, () => JSX.Element> = {
13+
"materialized_view": Tabell,
14+
"table": Tabell,
15+
"view": Tabell,
1716
}
1817

1918
export interface DataproductSourceDatasetProps {
@@ -27,11 +26,7 @@ export const Dataset = ({
2726
datasetID,
2827
active,
2928
}: DataproductSourceDatasetProps) => {
30-
const [getTables, { data, loading, called }] = useGcpGetTablesLazyQuery({
31-
variables: { projectID, datasetID },
32-
})
33-
34-
if (active && !called) getTables()
29+
const fetchBQTables= useFetchBQTables(projectID, datasetID)
3530

3631
const loadingPlaceholder = (
3732
<TreeItem
@@ -48,14 +43,14 @@ export const Dataset = ({
4843
/>
4944
)
5045

51-
const datasetContents = (contents: GcpGetTablesQuery['gcpGetTables']) =>
52-
contents?.map(({ name, type }) => (
46+
const datasetContents = (contents: any) =>
47+
contents?.map((it: any) => (
5348
<TreeItem
5449
className="MuiTreeView-leaf"
55-
slots={{ endIcon: DataproductTableIconMap[type]}}
56-
itemId={`${projectID}/${datasetID}/${name}`}
57-
key={`${projectID}/${datasetID}/${name}`}
58-
label={name}
50+
slots={{ endIcon: DataproductTableIconMap[it.type as string]}}
51+
itemId={`${projectID}/${datasetID}/${it.name}`}
52+
key={`${projectID}/${datasetID}/${it.name}`}
53+
label={it.name}
5954
/>
6055
))
6156

@@ -65,10 +60,10 @@ export const Dataset = ({
6560
itemId={`${projectID}/${datasetID}`}
6661
label={datasetID}
6762
>
68-
{loading
63+
{fetchBQTables.loading
6964
? loadingPlaceholder
70-
: data?.gcpGetTables?.length
71-
? datasetContents(data?.gcpGetTables)
65+
: fetchBQTables.bqTables?.length
66+
? datasetContents(fetchBQTables?.bqTables)
7267
: emptyPlaceholder}
7368
</TreeItem>
7469
)

components/dataproducts/datasource/project.tsx

+5-10
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { TreeItem } from '@mui/x-tree-view/TreeItem';
22
import { Loader } from '@navikt/ds-react'
3-
import { useGcpGetDatasetsLazyQuery } from '../../../lib/schema/graphql'
43
import { ExpandFilled, NextFilled } from '@navikt/ds-icons'
54
import { Dataset } from './dataset'
5+
import { useFetchBQDatasets } from '../../../lib/rest/bigquery';
66

77
export interface DataproductSourceDatasetProps {
88
activePaths: string[]
@@ -13,12 +13,7 @@ export const Project = ({
1313
projectID,
1414
activePaths,
1515
}: DataproductSourceDatasetProps) => {
16-
const [getDatasets, { data, loading, error, called }] =
17-
useGcpGetDatasetsLazyQuery({
18-
variables: { projectID },
19-
})
20-
21-
if (!called && activePaths.includes(projectID)) getDatasets()
16+
const fetchBQDatasets= useFetchBQDatasets(projectID)
2217

2318
const emptyPlaceholder = (
2419
<TreeItem
@@ -41,11 +36,11 @@ export const Project = ({
4136
itemId={projectID}
4237
label={projectID}
4338
>
44-
{loading
39+
{fetchBQDatasets.loading
4540
? loadingPlaceholder
46-
: !data?.gcpGetDatasets?.length
41+
: !fetchBQDatasets.bqDatasets?.length
4742
? emptyPlaceholder
48-
: data?.gcpGetDatasets.map((datasetID) => (
43+
: fetchBQDatasets.bqDatasets?.map((datasetID) => (
4944
<Dataset
5045
key={datasetID}
5146
projectID={projectID}

components/dataproducts/datasource/projectTables.tsx

-23
This file was deleted.

lib/queries/dataproduct/dataproductSummary.ts

-20
This file was deleted.

lib/queries/dataproduct/dataproducts.ts

-17
This file was deleted.

lib/queries/dataproduct/gcpGetAllTablesInProject.ts

-10
This file was deleted.

lib/queries/dataproduct/gcpGetColumns.ts

-12
This file was deleted.

lib/queries/dataproduct/gcpGetDatasets.ts

-7
This file was deleted.

lib/queries/dataproduct/gcpGetTables.ts

-11
This file was deleted.

lib/rest/bigquery.ts

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import { useEffect, useState } from "react"
2+
import { fetchBQColumnsUrl, fetchBQDatasetsUrl, fetchBQTablesUrl, fetchTemplate } from "./restApi"
3+
4+
export const fetchBQDatasets = async (projectID: string) => {
5+
const url = fetchBQDatasetsUrl(projectID)
6+
return fetchTemplate(url)
7+
}
8+
9+
export const fetchBQTables = async (projectID: string, datasetID: string) => {
10+
const url = fetchBQTablesUrl(projectID, datasetID)
11+
return fetchTemplate(url)
12+
}
13+
14+
export const fetchBQColumns = async (projectID: string, datasetID: string, tableID: string) => {
15+
const url = fetchBQColumnsUrl(projectID, datasetID, tableID)
16+
return fetchTemplate(url)
17+
}
18+
19+
export const useFetchBQDatasets = (projectID: string) => {
20+
const [datasets, setDatasets] = useState<any[]>([])
21+
const [loading, setLoading] = useState(false)
22+
const [error, setError] = useState<any>(null)
23+
24+
useEffect(() => {
25+
setLoading(true)
26+
fetchBQDatasets(projectID).then((res) => res.json())
27+
.then((data) => {
28+
setError(null)
29+
setDatasets(data.bqDatasets)
30+
})
31+
.catch((err) => {
32+
setError(err)
33+
setDatasets([])
34+
}).finally(() => {
35+
setLoading(false)
36+
})
37+
}, [projectID])
38+
39+
return { bqDatasets: datasets, loading, error }
40+
}
41+
42+
export const useFetchBQTables = (projectID: string, datasetID: string) => {
43+
const [tables, setTables] = useState<any[]>([])
44+
const [loading, setLoading] = useState(false)
45+
const [error, setError] = useState<any>(null)
46+
47+
useEffect(() => {
48+
setLoading(true)
49+
fetchBQTables(projectID, datasetID).then((res) => res.json())
50+
.then((data) => {
51+
setError(null)
52+
setTables(data.bqTables)
53+
})
54+
.catch((err) => {
55+
setError(err)
56+
setTables([])
57+
}).finally(() => {
58+
setLoading(false)
59+
})
60+
}, [projectID, datasetID])
61+
62+
return { bqTables: tables, loading, error }
63+
}
64+
65+
export const useFetchBQcolumns = (projectID: string, datasetID: string, tableID: string) => {
66+
const [columns, setColumns] = useState<any[]>([])
67+
const [loading, setLoading] = useState(false)
68+
const [error, setError] = useState<any>(null)
69+
70+
useEffect(() => {
71+
setLoading(true)
72+
fetchBQColumns(projectID, datasetID, tableID).then((res) => res.json())
73+
.then((data) => {
74+
setError(null)
75+
setColumns(data.bqColumns)
76+
})
77+
.catch((err) => {
78+
setError(err)
79+
setColumns([])
80+
}).finally(() => {
81+
setLoading(false)
82+
})
83+
}, [projectID, datasetID, tableID])
84+
85+
return { bqColumns: columns, loading, error }
86+
}

lib/rest/restApi.ts

+3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ export const getProductAreaUrl = (id: string) => `${apiUrl()}/productareas/${id}
1616
export const fetchUserDataUrl = () => `${apiUrl()}/userData`
1717
export const fetchKeywordsUrl = () => `${apiUrl()}/keywords`
1818
export const fetchAccessRequestUrl = (datasetId: string) => `${apiUrl()}/accessRequests?datasetId=${datasetId}`
19+
export const fetchBQDatasetsUrl = (projectId: string) => `${apiUrl()}/bigquery/datasets?projectId=${projectId}`
20+
export const fetchBQTablesUrl = (projectId: string, datasetId: string) => `${apiUrl()}/bigquery/tables?projectId=${projectId}&datasetId=${datasetId}`
21+
export const fetchBQColumnsUrl = (projectId: string, datasetId: string, tableId: string) => `${apiUrl()}/bigquery/tables?projectId=${projectId}&datasetId=${datasetId}&tableId=${tableId}`
1922
export const searchTeamKatalogenUrl = (gcpGroups?: string[]) => {
2023
const parameters = gcpGroups?.length ? gcpGroups.map(group => `gcpGroups=${encodeURIComponent(group)}`).join('&') : ''
2124
const query = parameters ? `?${parameters}` : ''

0 commit comments

Comments
 (0)