You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

utils.ts 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2024 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. import { each, memoize, omit, omitBy, pickBy, sortBy } from 'lodash';
  21. import { formatMeasure } from '../../helpers/measures';
  22. import { cleanQuery, parseAsArray, parseAsString, serializeStringArray } from '../../helpers/query';
  23. import {
  24. RawQuery,
  25. SysInfoAppNode,
  26. SysInfoBase,
  27. SysInfoCluster,
  28. SysInfoLogging,
  29. SysInfoSearchNode,
  30. SysInfoSection,
  31. SysInfoStandalone,
  32. SysInfoValueObject,
  33. } from '../../types/types';
  34. export interface Query {
  35. expandedCards: string[];
  36. }
  37. export enum LogsLevels {
  38. INFO = 'INFO',
  39. DEBUG = 'DEBUG',
  40. TRACE = 'TRACE',
  41. }
  42. export const LOGS_LEVELS = Object.values(LogsLevels);
  43. const DEFAULT_LOG_LEVEL = LogsLevels.INFO;
  44. export const APP_NODES_FIELD = 'Application Nodes';
  45. export const ALMS_FIELD = 'ALMs';
  46. export const BUNDLED_FIELD = 'Bundled';
  47. export const CE_FIELD_PREFIX = 'Compute Engine';
  48. export const CE_LOGGING_FIELD = 'Compute Engine Logging';
  49. export const HA_FIELD = 'High Availability';
  50. export const HEALTH_CAUSES_FIELD = 'Health Causes';
  51. export const HEALTH_FIELD = 'Health';
  52. export const LOGS_LEVEL_FIELD = 'Logs Level';
  53. export const NAME_FIELD = 'Name';
  54. export const NCLOC_FIELD = 'ncloc';
  55. export const PLUGINS_FIELD = 'Plugins';
  56. export const SEARCH_NODES_FIELD = 'Search Nodes';
  57. export const SEARCH_PREFIX = 'Search';
  58. export const SERVER_ID_FIELD = 'Server ID';
  59. export const SETTINGS_FIELD = 'Settings';
  60. export const STATE_FIELD = 'State';
  61. export const STATS_FIELD = 'Statistics';
  62. export const SYSTEM_FIELD = 'System';
  63. export const VERSION_FIELD = 'Version';
  64. export const WEB_LOGGING_FIELD = 'Web Logging';
  65. export const WEB_PREFIX = 'Web';
  66. export function ignoreInfoFields(sysInfoObject: SysInfoValueObject) {
  67. return omit(sysInfoObject, [
  68. ALMS_FIELD,
  69. BUNDLED_FIELD,
  70. HEALTH_FIELD,
  71. HEALTH_CAUSES_FIELD,
  72. NAME_FIELD,
  73. PLUGINS_FIELD,
  74. SETTINGS_FIELD,
  75. SERVER_ID_FIELD,
  76. VERSION_FIELD,
  77. ]);
  78. }
  79. export function getHealth(sysInfoObject: SysInfoBase) {
  80. return sysInfoObject[HEALTH_FIELD];
  81. }
  82. export function getHealthCauses(sysInfoObject: SysInfoBase) {
  83. return sysInfoObject[HEALTH_CAUSES_FIELD];
  84. }
  85. export function getLogsLevel(sysInfoObject?: SysInfoValueObject): LogsLevels {
  86. if (sysInfoObject !== undefined) {
  87. if (isLogInfoBlock(sysInfoObject)) {
  88. return sysInfoObject[LOGS_LEVEL_FIELD] as LogsLevels;
  89. } else if (hasLoggingInfo(sysInfoObject)) {
  90. return sortBy(
  91. [
  92. getLogsLevel(sysInfoObject[WEB_LOGGING_FIELD]),
  93. getLogsLevel(sysInfoObject[CE_LOGGING_FIELD]),
  94. ],
  95. (logLevel: LogsLevels) => LOGS_LEVELS.indexOf(logLevel),
  96. )[1];
  97. }
  98. }
  99. return DEFAULT_LOG_LEVEL;
  100. }
  101. export function getAppNodes(sysInfoData: SysInfoCluster): SysInfoAppNode[] {
  102. return sysInfoData[APP_NODES_FIELD];
  103. }
  104. export function getSearchNodes(sysInfoData: SysInfoCluster): SysInfoSearchNode[] {
  105. return sysInfoData[SEARCH_NODES_FIELD];
  106. }
  107. export function isCluster(
  108. sysInfoData: SysInfoCluster | SysInfoStandalone,
  109. ): sysInfoData is SysInfoCluster {
  110. return sysInfoData[SYSTEM_FIELD] && sysInfoData[SYSTEM_FIELD][HA_FIELD] === true;
  111. }
  112. export function isLogInfoBlock(sysInfoObject: SysInfoValueObject): sysInfoObject is SysInfoLogging {
  113. return sysInfoObject[LOGS_LEVEL_FIELD] !== undefined;
  114. }
  115. export function hasLoggingInfo(
  116. sysInfoObject: SysInfoValueObject,
  117. ): sysInfoObject is SysInfoStandalone | SysInfoAppNode {
  118. return Boolean(sysInfoObject[WEB_LOGGING_FIELD] || sysInfoObject[CE_LOGGING_FIELD]);
  119. }
  120. export function getServerId(sysInfoData: SysInfoCluster | SysInfoStandalone): string {
  121. return sysInfoData && sysInfoData[SYSTEM_FIELD][SERVER_ID_FIELD];
  122. }
  123. export function getVersion(sysInfoData: SysInfoStandalone): string | undefined {
  124. return sysInfoData && sysInfoData[SYSTEM_FIELD][VERSION_FIELD];
  125. }
  126. export function getClusterVersion(sysInfoData: SysInfoCluster): string | undefined {
  127. const appNodes = getAppNodes(sysInfoData);
  128. return appNodes.length > 0 ? appNodes[0][SYSTEM_FIELD][VERSION_FIELD] : undefined;
  129. }
  130. export function getSystemLogsLevel(sysInfoData: SysInfoCluster | SysInfoStandalone): string {
  131. if (isCluster(sysInfoData)) {
  132. const logLevels = sortBy(getAppNodes(sysInfoData).map(getLogsLevel), (logLevel: LogsLevels) =>
  133. LOGS_LEVELS.indexOf(logLevel),
  134. );
  135. return logLevels.length > 0 ? logLevels[logLevels.length - 1] : DEFAULT_LOG_LEVEL;
  136. }
  137. return getLogsLevel(sysInfoData);
  138. }
  139. export function getNodeName(nodeInfo: SysInfoAppNode | SysInfoSearchNode): string {
  140. return nodeInfo[NAME_FIELD];
  141. }
  142. function getSystemData(sysInfoData: SysInfoBase): SysInfoValueObject {
  143. const statData: SysInfoValueObject = {};
  144. const statistics = sysInfoData[STATS_FIELD] as SysInfoValueObject; // TODO
  145. if (statistics) {
  146. statData['Lines of Code'] = formatMeasure(statistics[NCLOC_FIELD] as number, 'INT');
  147. }
  148. return { ...sysInfoData[SYSTEM_FIELD], ...statData };
  149. }
  150. export function getClusterMainCardSection(sysInfoData: SysInfoCluster): SysInfoValueObject {
  151. return {
  152. ...getSystemData(sysInfoData),
  153. ...omit(sysInfoData, [
  154. APP_NODES_FIELD,
  155. PLUGINS_FIELD,
  156. SEARCH_NODES_FIELD,
  157. SETTINGS_FIELD,
  158. STATS_FIELD,
  159. SYSTEM_FIELD,
  160. ]),
  161. };
  162. }
  163. export function getStandaloneMainSections(sysInfoData: SysInfoBase): SysInfoValueObject {
  164. return {
  165. ...getSystemData(sysInfoData),
  166. ...(omitBy(
  167. sysInfoData,
  168. (value, key) =>
  169. value == null ||
  170. [PLUGINS_FIELD, SETTINGS_FIELD, STATS_FIELD, SYSTEM_FIELD].includes(key) ||
  171. key.startsWith(CE_FIELD_PREFIX) ||
  172. key.startsWith(SEARCH_PREFIX) ||
  173. key.startsWith(WEB_PREFIX),
  174. ) as SysInfoValueObject),
  175. };
  176. }
  177. export function getStandaloneSecondarySections(sysInfoData: SysInfoBase): SysInfoSection {
  178. return {
  179. Web: pickBy(sysInfoData, (_, key) => key.startsWith(WEB_PREFIX)) as SysInfoValueObject,
  180. 'Compute Engine': pickBy(sysInfoData, (_, key) =>
  181. key.startsWith(CE_FIELD_PREFIX),
  182. ) as SysInfoValueObject,
  183. 'Search Engine': pickBy(sysInfoData, (_, key) =>
  184. key.startsWith(SEARCH_PREFIX),
  185. ) as SysInfoValueObject,
  186. };
  187. }
  188. export function getFileNameSuffix(suffix?: string) {
  189. const now = new Date();
  190. return (
  191. `${suffix ? suffix + '-' : ''}` +
  192. `${now.getFullYear()}-${now.getMonth() + 1}-` +
  193. `${now.getDate()}-${now.getHours()}-${now.getMinutes()}`
  194. );
  195. }
  196. export function groupSections(sysInfoData: SysInfoValueObject) {
  197. const mainSection: SysInfoValueObject = {};
  198. const sections: SysInfoSection = {};
  199. each(sysInfoData, (item, key) => {
  200. if (typeof item !== 'object' || item instanceof Array) {
  201. mainSection[key] = item;
  202. } else {
  203. sections[key] = item;
  204. }
  205. });
  206. return { mainSection, sections };
  207. }
  208. export const parseQuery = memoize(
  209. (urlQuery: RawQuery): Query => ({
  210. expandedCards: parseAsArray(urlQuery.expand, parseAsString),
  211. }),
  212. );
  213. export const serializeQuery = memoize(
  214. (query: Query): RawQuery =>
  215. cleanQuery({
  216. expand: serializeStringArray(query.expandedCards),
  217. }),
  218. );