aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-web/src/main/js/apps/system/utils.ts
blob: 5514ae4e70568a89248a9fcfdaf7b4013ddfdab5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
 * SonarQube
 * Copyright (C) 2009-2017 SonarSource SA
 * mailto:info AT sonarsource DOT com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
import { each, memoize, omit, omitBy, pickBy, sortBy } from 'lodash';
import {
  cleanQuery,
  parseAsArray,
  parseAsString,
  RawQuery,
  serializeStringArray
} from '../../helpers/query';
import {
  ClusterSysInfo,
  HealthType,
  NodeInfo,
  SysInfo,
  SysInfoSection,
  SysValueObject
} from '../../api/system';

export interface Query {
  expandedCards: string[];
}

export const LOGS_LEVELS = ['INFO', 'DEBUG', 'TRACE'];
export const HA_FIELD = 'High Availability';
export const HEALTH_FIELD = 'Health';
export const HEALTHCAUSES_FIELD = 'Health Causes';
export const PLUGINS_FIELD = 'Plugins';
export const SETTINGS_FIELD = 'Settings';

export function ignoreInfoFields(sysInfoObject: SysValueObject): SysValueObject {
  return omit(sysInfoObject, [HEALTH_FIELD, HEALTHCAUSES_FIELD, 'Name', SETTINGS_FIELD]);
}

export function getHealth(sysInfoObject: SysValueObject): HealthType {
  return sysInfoObject[HEALTH_FIELD] as HealthType;
}

export function getHealthCauses(sysInfoObject: SysValueObject): string[] {
  return sysInfoObject[HEALTHCAUSES_FIELD] as string[];
}

export function getLogsLevel(sysInfoObject?: SysValueObject): string {
  if (!sysInfoObject) {
    return LOGS_LEVELS[0];
  }
  if (sysInfoObject['Web Logging'] || sysInfoObject['Compute Engine Logging']) {
    return sortBy(
      [
        getLogsLevel((sysInfoObject as NodeInfo)['Web Logging']),
        getLogsLevel((sysInfoObject as NodeInfo)['Compute Engine Logging'])
      ],
      logLevel => LOGS_LEVELS.indexOf(logLevel)
    )[1];
  }
  if (sysInfoObject['System']) {
    return getLogsLevel((sysInfoObject as SysInfo)['System']);
  }
  return (sysInfoObject['Logs Level'] || LOGS_LEVELS[0]) as string;
}

export function getAppNodes(sysInfoData: ClusterSysInfo): NodeInfo[] {
  return sysInfoData['Application Nodes'];
}

export function getSearchNodes(sysInfoData: ClusterSysInfo): NodeInfo[] {
  return sysInfoData['Search Nodes'];
}

export function isCluster(sysInfoData?: SysInfo): boolean {
  return (
    sysInfoData != undefined && sysInfoData['System'] && sysInfoData['System'][HA_FIELD] === true
  );
}

export function getSystemLogsLevel(sysInfoData?: SysInfo): string {
  const defaultLevel = LOGS_LEVELS[0];
  if (!sysInfoData) {
    return defaultLevel;
  }
  if (isCluster(sysInfoData)) {
    const logLevels = sortBy(
      getAppNodes(sysInfoData as ClusterSysInfo).map(getLogsLevel),
      logLevel => LOGS_LEVELS.indexOf(logLevel)
    );
    return logLevels.length > 0 ? logLevels[logLevels.length - 1] : defaultLevel;
  } else {
    return getLogsLevel(sysInfoData);
  }
}

export function getNodeName(nodeInfo: NodeInfo): string {
  return nodeInfo['Name'];
}

export function getClusterMainCardSection(sysInfoData: ClusterSysInfo): SysValueObject {
  return {
    ...sysInfoData['System'],
    ...omit(sysInfoData, [
      'Application Nodes',
      PLUGINS_FIELD,
      'Search Nodes',
      SETTINGS_FIELD,
      'Statistics',
      'System'
    ])
  };
}

export function getStandaloneMainSections(sysInfoData: SysInfo): SysValueObject {
  return {
    ...sysInfoData['System'],
    ...omitBy(
      sysInfoData,
      (value, key) =>
        value == null ||
        [PLUGINS_FIELD, SETTINGS_FIELD, 'Statistics', 'System'].includes(key) ||
        key.startsWith('Compute Engine') ||
        key.startsWith('Search') ||
        key.startsWith('Web')
    )
  };
}

export function getStandaloneSecondarySections(sysInfoData: SysInfo): SysInfoSection {
  return {
    Web: pickBy(sysInfoData, (_, key) => key.startsWith('Web')),
    'Compute Engine': pickBy(sysInfoData, (_, key) => key.startsWith('Compute Engine')),
    Search: pickBy(sysInfoData, (_, key) => key.startsWith('Search'))
  };
}

export function groupSections(sysInfoData: SysValueObject) {
  let mainSection: SysValueObject = {};
  let sections: SysInfoSection = {};
  each(sysInfoData, (item, key) => {
    if (typeof item !== 'object' || item instanceof Array) {
      mainSection[key] = item;
    } else {
      sections[key] = item;
    }
  });
  return { mainSection, sections };
}

export const parseQuery = memoize((urlQuery: RawQuery): Query => ({
  expandedCards: parseAsArray(urlQuery.expand, parseAsString)
}));

export const serializeQuery = memoize((query: Query): RawQuery =>
  cleanQuery({
    expand: serializeStringArray(query.expandedCards)
  })
);