Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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://monitoring.yandex-team.ru/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).toContain('database+%3D+%22testdb%22');
// Original query parts should still be present
expect(result).toContain('project+%3D+%22kikimr%22');
expect(result).toContain('service+%3D+%22ydb%22%');
expect(result).toContain('cluster+%3D+%22ydb-ru-prestable%22');
});

test('should handle empty query parameters', () => {
const loggingData = {
url: 'https://monitoring.yandex-team.ru/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).toContain('database+%3D+%22testdb%22');
});

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

expect(
getLogsLink({
logging: JSON.stringify({}),
dbName: 'testdb',
}),
).toBe('');
});
});
37 changes: 37 additions & 0 deletions src/utils/logs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
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 updatedQuery = decodedQuery.replace(/\{([^}]*)\}/, (_match, contents) => {
const trimmedContents = contents.trim();
return `{${trimmedContents}${trimmedContents ? ', ' : ''}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