-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathmonitoring.ts
108 lines (80 loc) · 2.72 KB
/
monitoring.ts
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
import type {ETenantType} from '../types/api/tenant';
type ParsedMonitoringData = {
monitoring_url: string;
serverless_dashboard?: string;
dedicated_dashboard?: string;
cluster_dashboard?: string;
host?: string;
slot?: string;
cluster_name?: string;
};
interface GetMonitoringLinkProps {
monitoring: string;
dbName: string;
dbType: ETenantType;
clusterName?: string;
}
export type GetMonitoringLink = typeof getMonitoringLink;
export function getMonitoringLink({
monitoring,
dbName,
dbType,
clusterName,
}: GetMonitoringLinkProps) {
try {
const data = parseMonitoringData(monitoring);
if (data) {
const host = data.host ?? 'cluster';
const slot = data.slot ?? 'static';
const finalClusterName = data.cluster_name || clusterName || '';
const url = new URL(data.monitoring_url);
if (!url.search) {
const dashboard =
dbType === 'Serverless' ? data.serverless_dashboard : data.dedicated_dashboard;
url.pathname += `/${dashboard}`;
}
if (!url.searchParams.has('p.cluster')) {
url.searchParams.set('p.cluster', finalClusterName);
}
url.searchParams.set('p.host', host);
url.searchParams.set('p.slot', slot);
url.searchParams.set('p.database', dbName);
return url.toString();
}
} catch {}
return '';
}
export type GetMonitoringClusterLink = typeof getMonitoringClusterLink;
export function getMonitoringClusterLink(monitoring: string, clusterName?: string) {
try {
const data = parseMonitoringData(monitoring);
if (data) {
const clusterDashboard = data.cluster_dashboard;
const finalClusterName = data.cluster_name || clusterName || '';
const url = new URL(data.monitoring_url);
if (!url.search && clusterDashboard) {
url.pathname += `/${clusterDashboard}/view`;
}
if (!url.searchParams.has('p.cluster')) {
url.searchParams.set('p.cluster', finalClusterName);
}
url.searchParams.set('p.database', '-');
return url.toString();
}
} catch {}
return '';
}
export function parseMonitoringData(monitoring: string): ParsedMonitoringData | undefined {
try {
const data = JSON.parse(monitoring);
if (typeof data === 'object' && 'monitoring_url' in data) {
return data;
}
} catch {}
return undefined;
}
interface GetLogsLinkProps {
dbName: string;
clusterName: string;
}
export type GetLogsLink = (props: GetLogsLinkProps) => string;