Skip to content

Commit 19528ee

Browse files
committed
feat: YDBOPS-11509 monium_cluster
1 parent fb07e1c commit 19528ee

File tree

4 files changed

+79
-18
lines changed

4 files changed

+79
-18
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
"history": "^4.10.1",
4141
"hotkeys-js": "^3.13.9",
4242
"lodash": "^4.17.21",
43+
"lz-string": "^1.5.0",
4344
"monaco-editor": "^0.52.2",
4445
"numeral": "^2.0.6",
4546
"path-to-regexp": "^3.3.0",

src/utils/__test__/logs.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,22 @@ describe('getLogsLink', () => {
3333
);
3434
});
3535

36+
test('should generate monitoring URL when monium_cluster is present', () => {
37+
const loggingData = {
38+
url: 'https://logging.url/projects/some_project/logs',
39+
monium_cluster: 'ydb-ru-prestable',
40+
};
41+
42+
const result = getLogsLink({
43+
logging: JSON.stringify(loggingData),
44+
dbName: 'testdb',
45+
});
46+
47+
expect(result).toBe(
48+
'https://logging.url/projects/kikimr/logs?query=%7Bproject+%3D+%22kikimr%22%2C+service+%3D+%22ydb%22%2C+cluster+%3D+%22ydb-ru-prestable%22%2C+database+%3D+%22testdb%22%7D&from=now-1d&to=now&columns=level%2Ctime%2Cmessage%2Chost&groupByField=level&chartType=line&linesMode=single',
49+
);
50+
});
51+
3652
test('should return empty string for invalid data', () => {
3753
expect(
3854
getLogsLink({

src/utils/logs.ts

Lines changed: 60 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,78 @@
1+
const DEFAULT_PROJECT = 'kikimr';
2+
const DEFAULT_SERVICE = 'ydb';
3+
const DEFAULT_TIME_RANGE = {
4+
from: 'now-1d',
5+
to: 'now',
6+
};
7+
const DEFAULT_COLUMNS = 'level,time,message,host';
8+
const DEFAULT_GROUP_BY = 'level';
9+
const DEFAULT_CHART_TYPE = 'line';
10+
const DEFAULT_LINES_MODE = 'single';
11+
112
interface GetLogsLinkProps {
213
dbName: string;
314
logging: string;
415
}
516

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

19+
interface ParsedLogging {
20+
url: string;
21+
monium_cluster?: string;
22+
}
23+
24+
function getBaseUrl(urlString: string): string {
25+
const url = new URL(urlString);
26+
return `${url.protocol}//${url.hostname}`;
27+
}
28+
829
export function getLogsLink({dbName, logging}: GetLogsLinkProps): string {
930
try {
10-
const data = JSON.parse(logging);
31+
const data = JSON.parse(logging) as ParsedLogging;
1132

12-
if (typeof data === 'object' && 'url' in data) {
13-
const logUrl = data.url;
14-
if (!logUrl) {
15-
return '';
16-
}
33+
if (typeof data === 'object') {
34+
if ('url' in data) {
35+
const logUrl = data.url;
36+
if (!logUrl) {
37+
return '';
38+
}
1739

18-
const url = new URL(logUrl);
40+
if (data.monium_cluster) {
41+
const baseUrl = getBaseUrl(logUrl);
42+
const url = new URL(`${baseUrl}/projects/${DEFAULT_PROJECT}/logs`);
1943

20-
const queryParam = url.searchParams.get('query');
21-
if (queryParam) {
22-
const decodedQuery = decodeURIComponent(queryParam);
44+
const query = `{project = "${DEFAULT_PROJECT}", service = "${DEFAULT_SERVICE}", cluster = "${data.monium_cluster}", database = "${dbName}"}`;
2345

24-
const queryBetweenBraces = decodedQuery.slice(1, -1);
25-
const witComma = queryBetweenBraces.length > 0;
26-
const updatedQuery = `{${queryBetweenBraces}${witComma ? ', ' : ''}database = "${dbName}"}`;
46+
url.searchParams.set('query', query);
47+
url.searchParams.set('from', DEFAULT_TIME_RANGE.from);
48+
url.searchParams.set('to', DEFAULT_TIME_RANGE.to);
49+
url.searchParams.set('columns', DEFAULT_COLUMNS);
50+
url.searchParams.set('groupByField', DEFAULT_GROUP_BY);
51+
url.searchParams.set('chartType', DEFAULT_CHART_TYPE);
52+
url.searchParams.set('linesMode', DEFAULT_LINES_MODE);
2753

28-
url.searchParams.set('query', updatedQuery);
29-
}
54+
// debug-only
55+
console.log('Monium_cluster branch');
56+
return url.toString();
57+
}
58+
59+
const url = new URL(logUrl);
60+
61+
const queryParam = url.searchParams.get('query');
62+
if (queryParam) {
63+
const decodedQuery = decodeURIComponent(queryParam);
3064

31-
return url.toString();
65+
const queryBetweenBraces = decodedQuery.slice(1, -1);
66+
const witComma = queryBetweenBraces.length > 0;
67+
const updatedQuery = `{${queryBetweenBraces}${witComma ? ', ' : ''}database = "${dbName}"}`;
68+
69+
url.searchParams.set('query', updatedQuery);
70+
}
71+
72+
// debug-only
73+
console.log('Url parsing branch');
74+
return url.toString();
75+
}
3276
}
3377
} catch {}
3478

0 commit comments

Comments
 (0)