|
| 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 | + |
1 | 12 | interface GetLogsLinkProps { |
2 | 13 | dbName: string; |
3 | 14 | logging: string; |
4 | 15 | } |
5 | 16 |
|
6 | 17 | export type GetLogsLink = (props: GetLogsLinkProps) => string; |
7 | 18 |
|
| 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 | + |
8 | 29 | export function getLogsLink({dbName, logging}: GetLogsLinkProps): string { |
9 | 30 | try { |
10 | | - const data = JSON.parse(logging); |
| 31 | + const data = JSON.parse(logging) as ParsedLogging; |
11 | 32 |
|
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 | + } |
17 | 39 |
|
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`); |
19 | 43 |
|
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}"}`; |
23 | 45 |
|
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); |
27 | 53 |
|
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); |
30 | 64 |
|
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 | + } |
32 | 76 | } |
33 | 77 | } catch {} |
34 | 78 |
|
|
0 commit comments