Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: parse logging link as default value #2036

Merged
merged 6 commits into from
Mar 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/containers/AppWithClusters/AppWithClusters.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import React from 'react';
import type {Store} from '@reduxjs/toolkit';
import type {History} from 'history';

import {getLogsLink as getLogsLinkDefault} from '../../utils/logs';
import type {
GetLogsLink,
GetMonitoringClusterLink,
Expand Down Expand Up @@ -31,7 +32,7 @@ export interface AppWithClustersProps {
export function AppWithClusters({
store,
history,
getLogsLink,
getLogsLink = getLogsLinkDefault,
getMonitoringLink = getMonitoringLinkDefault,
getMonitoringClusterLink = getMonitoringClusterLinkDefault,
userSettings,
Expand Down
65 changes: 42 additions & 23 deletions src/containers/AppWithClusters/ExtendedCluster/ExtendedCluster.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,19 @@ const getAdditionalBalancerInfo = (balancer: string) => {
};
};

const getAdditionalClusterProps = (
clusterName: string | undefined,
monitoring: string | undefined,
balancer: string | undefined,
getMonitoringClusterLink?: GetMonitoringClusterLink,
) => {
interface GetAdditionalClusterProps {
clusterName: string | undefined;
monitoring: string | undefined;
balancer: string | undefined;
getMonitoringClusterLink?: GetMonitoringClusterLink;
}

const getAdditionalClusterProps = ({
clusterName,
monitoring,
balancer,
getMonitoringClusterLink,
}: GetAdditionalClusterProps) => {
const additionalClusterProps: AdditionalClusterProps = {};

if (monitoring && getMonitoringClusterLink) {
Expand All @@ -61,14 +68,25 @@ const getAdditionalClusterProps = (
return additionalClusterProps;
};

const getAdditionalTenantsProps = (
clusterName: string | undefined,
monitoring: string | undefined,
balancer: string | undefined,
useClusterBalancerAsBackend: boolean | undefined,
getMonitoringLink?: GetMonitoringLink,
getLogsLink?: GetLogsLink,
) => {
interface GetAdditionalTenantsProps {
clusterName: string | undefined;
monitoring: string | undefined;
balancer: string | undefined;
logging: string | undefined;
useClusterBalancerAsBackend: boolean | undefined;
getMonitoringLink?: GetMonitoringLink;
getLogsLink?: GetLogsLink;
}

const getAdditionalTenantsProps = ({
clusterName,
monitoring,
balancer,
logging,
useClusterBalancerAsBackend,
getMonitoringLink,
getLogsLink,
}: GetAdditionalTenantsProps) => {
const additionalTenantsProps: AdditionalTenantsProps = {};

additionalTenantsProps.prepareTenantBackend = (
Expand Down Expand Up @@ -104,12 +122,12 @@ const getAdditionalTenantsProps = (
};
}

if (clusterName && getLogsLink) {
if (logging && getLogsLink) {
additionalTenantsProps.getLogsLink = (dbName?: string) => {
if (dbName) {
return getLogsLink({
dbName,
clusterName,
logging,
});
}

Expand All @@ -133,27 +151,28 @@ export function ExtendedCluster({
getLogsLink,
}: ExtendedClusterProps) {
const additionalNodesProps = useAdditionalNodesProps();
const {name, balancer, monitoring} = useClusterBaseInfo();
const {name, balancer, monitoring, logging} = useClusterBaseInfo();

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

return (
<div className={b()}>
<ClusterComponent
additionalClusterProps={getAdditionalClusterProps(
name,
additionalClusterProps={getAdditionalClusterProps({
clusterName: name,
monitoring,
balancer,
getMonitoringClusterLink,
)}
additionalTenantsProps={getAdditionalTenantsProps(
name,
})}
additionalTenantsProps={getAdditionalTenantsProps({
clusterName: name,
monitoring,
balancer,
logging,
useClusterBalancerAsBackend,
getMonitoringLink,
getLogsLink,
)}
})}
additionalNodesProps={additionalNodesProps}
/>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export function ExtendedTenant({
getMonitoringLink,
getLogsLink,
}: ExtendedTenantProps) {
const {monitoring, name: clusterName} = useClusterBaseInfo();
const {monitoring, logging} = useClusterBaseInfo();
const additionalNodesProps = useAdditionalNodesProps();

const additionalTenantProps = {
Expand All @@ -31,10 +31,10 @@ export function ExtendedTenant({
return null;
},
getLogsLink: (dbName?: string) => {
if (clusterName && dbName && getLogsLink) {
if (logging && dbName && getLogsLink) {
return getLogsLink({
dbName,
clusterName,
logging,
});
}

Expand Down
51 changes: 51 additions & 0 deletions src/utils/__test__/logs.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import {getLogsLink} from '../logs';

describe('getLogsLink', () => {
test('should insert dbName into logs URL query', () => {
const loggingData = {
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',
};

const result = getLogsLink({
logging: JSON.stringify(loggingData),
dbName: 'testdb',
});

// The URL should contain the dbName in the query parameter
expect(result).toBe(
'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',
);
});

test('should handle empty query parameters', () => {
const loggingData = {
url: 'https://logging.url/projects/kikimr/logs?from=now-1h&to=now&query=%7B%7D',
};

const result = getLogsLink({
logging: JSON.stringify(loggingData),
dbName: 'testdb',
});

// Should add dbName to empty query
expect(result).toBe(
'https://logging.url/projects/kikimr/logs?from=now-1h&to=now&query=%7Bdatabase+%3D+%22testdb%22%7D',
);
});

test('should return empty string for invalid data', () => {
expect(
getLogsLink({
logging: 'invalid json',
dbName: 'testdb',
}),
).toBe('');

expect(
getLogsLink({
logging: JSON.stringify({}),
dbName: 'testdb',
}),
).toBe('');
});
});
36 changes: 36 additions & 0 deletions src/utils/logs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
interface GetLogsLinkProps {
dbName: string;
logging: string;
}

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

export function getLogsLink({dbName, logging}: GetLogsLinkProps): string {
try {
const data = JSON.parse(logging);

if (typeof data === 'object' && 'url' in data) {
const logUrl = data.url;
if (!logUrl) {
return '';
}

const url = new URL(logUrl);

const queryParam = url.searchParams.get('query');
if (queryParam) {
const decodedQuery = decodeURIComponent(queryParam);

const queryBetweenBraces = decodedQuery.slice(1, -1);
const witComma = queryBetweenBraces.length > 0;
const updatedQuery = `{${queryBetweenBraces}${witComma ? ', ' : ''}database = "${dbName}"}`;

url.searchParams.set('query', updatedQuery);
}

return url.toString();
}
} catch {}

return '';
}
2 changes: 1 addition & 1 deletion src/utils/monitoring.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ export function parseMonitoringData(monitoring: string): ParsedMonitoringData |

interface GetLogsLinkProps {
dbName: string;
clusterName: string;
logging: string;
}

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