-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.tsx
93 lines (85 loc) · 2.65 KB
/
index.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
import { request, gql } from 'graphql-request'
export const UNIFIED_GQL_API = 'https://api.splitgraph.com/gql/cloud/unified/graphql';
const DDN_API = 'https://data.splitgraph.com/sql/query/ddn';
export interface Tag {
tag: string;
}
export interface SocrataTagsGQL {
tags: {
nodes: Tag[]
}
}
/**
* Fetch all available tags
*
* Used to populate the UI (i.e. date picker) with only available dates.
*/
export const SocrataRepoTagsQuery = gql
`query SocrataRepoTags {
tags(
condition: { namespace: "splitgraph", repository: "socrata" }
orderBy: _CREATED_AT_DESC
) {
nodes {
tag
}
}
}
`
export const SplitgraphURLQuery = gql
`query getSocrataRepo($id: String!, $domain: String!, $name: String!) {
socrataExternalRepositories (datasets:
[{id: $id, domain: $domain, name: $name}]) {
namespace
repository
}
}
`
export const SplitgraphURLBatch = gql
`query getSocrataRepo($datasets: [SocrataDatasetKey!]!) {
socrataExternalRepositories (datasets: $datasets) {
namespace
repository
}
}
`
export const unifiedFetcher = (query: string, variables: any) => request(UNIFIED_GQL_API, query, variables)
/**
* I observed that "20220822" yields diffs but "20220822-180102"
* does not. Thus filter for 'day' tags
*/
export const filterDates = (tags: string[]): string[] => {
return tags.filter((n: string) => n.length === 8)
}
/**
* Return a SQL query that shows datasets added and deleted from Socrata during
* the given time range (i.e. between the specified tags)
*
* @param {string} oldTag the "old" tag
* @param {string} newTag the "new" tag
* @returns {string} SQL query intended for DDN
*/
export const getAddedDeletedDatasetsQuery = (oldTag: string, newTag: string) =>
`WITH old AS MATERIALIZED(SELECT * FROM "splitgraph/socrata:${oldTag}".datasets),
new AS MATERIALIZED(SELECT * FROM "splitgraph/socrata:${newTag}".datasets)
SELECT
COALESCE(old.domain, new.domain) AS domain,
COALESCE(old.id, new.id) AS id,
COALESCE(old.name, new.name) AS name,
COALESCE(old.description, new.description) AS description,
COALESCE(old.created_at, new.created_at) AS created_at,
COALESCE(old.updated_at, new.updated_at) AS updated_at,
old.id IS NULL AS is_added -- TRUE if added, FALSE if deleted
FROM old FULL OUTER JOIN new
ON old.domain = new.domain AND old.id = new.id
WHERE old.id IS NULL OR new.id IS NULL -- Only include added/deleted datasets
ORDER BY domain, name, is_added`
export const ddnFetcher = (query: string) => fetch(DDN_API, {
method: "POST",
headers: {
"content-type": "application/json"
},
body: JSON.stringify({
sql: query
})
}).then((res) => res.json());