Skip to content

Commit 74f7a58

Browse files
astandrikCopilot
andauthored
feat: parse logging link as default value (#2036)
Co-authored-by: Copilot <[email protected]>
1 parent 63d5afd commit 74f7a58

File tree

6 files changed

+135
-28
lines changed

6 files changed

+135
-28
lines changed

src/containers/AppWithClusters/AppWithClusters.tsx

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import React from 'react';
33
import type {Store} from '@reduxjs/toolkit';
44
import type {History} from 'history';
55

6+
import {getLogsLink as getLogsLinkDefault} from '../../utils/logs';
67
import type {
78
GetLogsLink,
89
GetMonitoringClusterLink,
@@ -31,7 +32,7 @@ export interface AppWithClustersProps {
3132
export function AppWithClusters({
3233
store,
3334
history,
34-
getLogsLink,
35+
getLogsLink = getLogsLinkDefault,
3536
getMonitoringLink = getMonitoringLinkDefault,
3637
getMonitoringClusterLink = getMonitoringClusterLinkDefault,
3738
userSettings,

src/containers/AppWithClusters/ExtendedCluster/ExtendedCluster.tsx

+42-23
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,19 @@ const getAdditionalBalancerInfo = (balancer: string) => {
3838
};
3939
};
4040

41-
const getAdditionalClusterProps = (
42-
clusterName: string | undefined,
43-
monitoring: string | undefined,
44-
balancer: string | undefined,
45-
getMonitoringClusterLink?: GetMonitoringClusterLink,
46-
) => {
41+
interface GetAdditionalClusterProps {
42+
clusterName: string | undefined;
43+
monitoring: string | undefined;
44+
balancer: string | undefined;
45+
getMonitoringClusterLink?: GetMonitoringClusterLink;
46+
}
47+
48+
const getAdditionalClusterProps = ({
49+
clusterName,
50+
monitoring,
51+
balancer,
52+
getMonitoringClusterLink,
53+
}: GetAdditionalClusterProps) => {
4754
const additionalClusterProps: AdditionalClusterProps = {};
4855

4956
if (monitoring && getMonitoringClusterLink) {
@@ -61,14 +68,25 @@ const getAdditionalClusterProps = (
6168
return additionalClusterProps;
6269
};
6370

64-
const getAdditionalTenantsProps = (
65-
clusterName: string | undefined,
66-
monitoring: string | undefined,
67-
balancer: string | undefined,
68-
useClusterBalancerAsBackend: boolean | undefined,
69-
getMonitoringLink?: GetMonitoringLink,
70-
getLogsLink?: GetLogsLink,
71-
) => {
71+
interface GetAdditionalTenantsProps {
72+
clusterName: string | undefined;
73+
monitoring: string | undefined;
74+
balancer: string | undefined;
75+
logging: string | undefined;
76+
useClusterBalancerAsBackend: boolean | undefined;
77+
getMonitoringLink?: GetMonitoringLink;
78+
getLogsLink?: GetLogsLink;
79+
}
80+
81+
const getAdditionalTenantsProps = ({
82+
clusterName,
83+
monitoring,
84+
balancer,
85+
logging,
86+
useClusterBalancerAsBackend,
87+
getMonitoringLink,
88+
getLogsLink,
89+
}: GetAdditionalTenantsProps) => {
7290
const additionalTenantsProps: AdditionalTenantsProps = {};
7391

7492
additionalTenantsProps.prepareTenantBackend = (
@@ -104,12 +122,12 @@ const getAdditionalTenantsProps = (
104122
};
105123
}
106124

107-
if (clusterName && getLogsLink) {
125+
if (logging && getLogsLink) {
108126
additionalTenantsProps.getLogsLink = (dbName?: string) => {
109127
if (dbName) {
110128
return getLogsLink({
111129
dbName,
112-
clusterName,
130+
logging,
113131
});
114132
}
115133

@@ -133,27 +151,28 @@ export function ExtendedCluster({
133151
getLogsLink,
134152
}: ExtendedClusterProps) {
135153
const additionalNodesProps = useAdditionalNodesProps();
136-
const {name, balancer, monitoring} = useClusterBaseInfo();
154+
const {name, balancer, monitoring, logging} = useClusterBaseInfo();
137155

138156
const [useClusterBalancerAsBackend] = useSetting<boolean>(USE_CLUSTER_BALANCER_AS_BACKEND_KEY);
139157

140158
return (
141159
<div className={b()}>
142160
<ClusterComponent
143-
additionalClusterProps={getAdditionalClusterProps(
144-
name,
161+
additionalClusterProps={getAdditionalClusterProps({
162+
clusterName: name,
145163
monitoring,
146164
balancer,
147165
getMonitoringClusterLink,
148-
)}
149-
additionalTenantsProps={getAdditionalTenantsProps(
150-
name,
166+
})}
167+
additionalTenantsProps={getAdditionalTenantsProps({
168+
clusterName: name,
151169
monitoring,
152170
balancer,
171+
logging,
153172
useClusterBalancerAsBackend,
154173
getMonitoringLink,
155174
getLogsLink,
156-
)}
175+
})}
157176
additionalNodesProps={additionalNodesProps}
158177
/>
159178
</div>

src/containers/AppWithClusters/ExtendedTenant/ExtendedTenant.tsx

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export function ExtendedTenant({
1515
getMonitoringLink,
1616
getLogsLink,
1717
}: ExtendedTenantProps) {
18-
const {monitoring, name: clusterName} = useClusterBaseInfo();
18+
const {monitoring, logging} = useClusterBaseInfo();
1919
const additionalNodesProps = useAdditionalNodesProps();
2020

2121
const additionalTenantProps = {
@@ -31,10 +31,10 @@ export function ExtendedTenant({
3131
return null;
3232
},
3333
getLogsLink: (dbName?: string) => {
34-
if (clusterName && dbName && getLogsLink) {
34+
if (logging && dbName && getLogsLink) {
3535
return getLogsLink({
3636
dbName,
37-
clusterName,
37+
logging,
3838
});
3939
}
4040

src/utils/__test__/logs.test.ts

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import {getLogsLink} from '../logs';
2+
3+
describe('getLogsLink', () => {
4+
test('should insert dbName into logs URL query', () => {
5+
const loggingData = {
6+
url: 'https://logging.url/projects/kikimr/logs?from=now-1h&to=now&query=%7Bproject+%3D+%22kikimr%22%2C+service+%3D+%22ydb%22%2C+cluster+%3D+%22ydb-ru-prestable%22%7D',
7+
};
8+
9+
const result = getLogsLink({
10+
logging: JSON.stringify(loggingData),
11+
dbName: 'testdb',
12+
});
13+
14+
// The URL should contain the dbName in the query parameter
15+
expect(result).toBe(
16+
'https://logging.url/projects/kikimr/logs?from=now-1h&to=now&query=%7Bproject+%3D+%22kikimr%22%2C+service+%3D+%22ydb%22%2C+cluster+%3D+%22ydb-ru-prestable%22%2C+database+%3D+%22testdb%22%7D',
17+
);
18+
});
19+
20+
test('should handle empty query parameters', () => {
21+
const loggingData = {
22+
url: 'https://logging.url/projects/kikimr/logs?from=now-1h&to=now&query=%7B%7D',
23+
};
24+
25+
const result = getLogsLink({
26+
logging: JSON.stringify(loggingData),
27+
dbName: 'testdb',
28+
});
29+
30+
// Should add dbName to empty query
31+
expect(result).toBe(
32+
'https://logging.url/projects/kikimr/logs?from=now-1h&to=now&query=%7Bdatabase+%3D+%22testdb%22%7D',
33+
);
34+
});
35+
36+
test('should return empty string for invalid data', () => {
37+
expect(
38+
getLogsLink({
39+
logging: 'invalid json',
40+
dbName: 'testdb',
41+
}),
42+
).toBe('');
43+
44+
expect(
45+
getLogsLink({
46+
logging: JSON.stringify({}),
47+
dbName: 'testdb',
48+
}),
49+
).toBe('');
50+
});
51+
});

src/utils/logs.ts

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
interface GetLogsLinkProps {
2+
dbName: string;
3+
logging: string;
4+
}
5+
6+
export type GetLogsLink = (props: GetLogsLinkProps) => string;
7+
8+
export function getLogsLink({dbName, logging}: GetLogsLinkProps): string {
9+
try {
10+
const data = JSON.parse(logging);
11+
12+
if (typeof data === 'object' && 'url' in data) {
13+
const logUrl = data.url;
14+
if (!logUrl) {
15+
return '';
16+
}
17+
18+
const url = new URL(logUrl);
19+
20+
const queryParam = url.searchParams.get('query');
21+
if (queryParam) {
22+
const decodedQuery = decodeURIComponent(queryParam);
23+
24+
const queryBetweenBraces = decodedQuery.slice(1, -1);
25+
const witComma = queryBetweenBraces.length > 0;
26+
const updatedQuery = `{${queryBetweenBraces}${witComma ? ', ' : ''}database = "${dbName}"}`;
27+
28+
url.searchParams.set('query', updatedQuery);
29+
}
30+
31+
return url.toString();
32+
}
33+
} catch {}
34+
35+
return '';
36+
}

src/utils/monitoring.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ export function parseMonitoringData(monitoring: string): ParsedMonitoringData |
102102

103103
interface GetLogsLinkProps {
104104
dbName: string;
105-
clusterName: string;
105+
logging: string;
106106
}
107107

108108
export type GetLogsLink = (props: GetLogsLinkProps) => string;

0 commit comments

Comments
 (0)