health.service.ts 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. import { Injectable } from '@angular/core';
  2. import { HttpClient } from '@angular/common/http';
  3. import { Observable } from 'rxjs';
  4. import { SERVER_API_URL } from 'app/app.constants';
  5. @Injectable({ providedIn: 'root' })
  6. export class JhiHealthService {
  7. separator: string;
  8. constructor(private http: HttpClient) {
  9. this.separator = '.';
  10. }
  11. checkHealth(): Observable<any> {
  12. return this.http.get(SERVER_API_URL + 'management/health');
  13. }
  14. transformHealthData(data): any {
  15. const response = [];
  16. this.flattenHealthData(response, null, data.details);
  17. return response;
  18. }
  19. getBaseName(name): string {
  20. if (name) {
  21. const split = name.split('.');
  22. return split[0];
  23. }
  24. }
  25. getSubSystemName(name): string {
  26. if (name) {
  27. const split = name.split('.');
  28. split.splice(0, 1);
  29. const remainder = split.join('.');
  30. return remainder ? ' - ' + remainder : '';
  31. }
  32. }
  33. /* private methods */
  34. private addHealthObject(result, isLeaf, healthObject, name): any {
  35. const healthData: any = {
  36. name
  37. };
  38. const details = {};
  39. let hasDetails = false;
  40. for (const key in healthObject) {
  41. if (healthObject.hasOwnProperty(key)) {
  42. const value = healthObject[key];
  43. if (key === 'status' || key === 'error') {
  44. healthData[key] = value;
  45. } else {
  46. if (!this.isHealthObject(value)) {
  47. details[key] = value;
  48. hasDetails = true;
  49. }
  50. }
  51. }
  52. }
  53. // Add the details
  54. if (hasDetails) {
  55. healthData.details = details;
  56. }
  57. // Only add nodes if they provide additional information
  58. if (isLeaf || hasDetails || healthData.error) {
  59. result.push(healthData);
  60. }
  61. return healthData;
  62. }
  63. private flattenHealthData(result, path, data): any {
  64. for (const key in data) {
  65. if (data.hasOwnProperty(key)) {
  66. const value = data[key];
  67. if (this.isHealthObject(value)) {
  68. if (this.hasSubSystem(value)) {
  69. this.addHealthObject(result, false, value, this.getModuleName(path, key));
  70. this.flattenHealthData(result, this.getModuleName(path, key), value);
  71. } else {
  72. this.addHealthObject(result, true, value, this.getModuleName(path, key));
  73. }
  74. }
  75. }
  76. }
  77. return result;
  78. }
  79. private getModuleName(path, name): string {
  80. let result;
  81. if (path && name) {
  82. result = path + this.separator + name;
  83. } else if (path) {
  84. result = path;
  85. } else if (name) {
  86. result = name;
  87. } else {
  88. result = '';
  89. }
  90. return result;
  91. }
  92. private hasSubSystem(healthObject): boolean {
  93. let result = false;
  94. for (const key in healthObject) {
  95. if (healthObject.hasOwnProperty(key)) {
  96. const value = healthObject[key];
  97. if (value && value.status) {
  98. result = true;
  99. }
  100. }
  101. }
  102. return result;
  103. }
  104. private isHealthObject(healthObject): boolean {
  105. let result = false;
  106. for (const key in healthObject) {
  107. if (healthObject.hasOwnProperty(key)) {
  108. if (key === 'status') {
  109. result = true;
  110. }
  111. }
  112. }
  113. return result;
  114. }
  115. }