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.

status.component.ts 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import { Component, OnInit } from '@angular/core';
  2. import {StatusService} from '../../services/status.service';
  3. import {SiteViewModel} from '../../view-models/site.view-model';
  4. import {HostStatusViewModel} from '../../view-models/host-status.view-model';
  5. import {StatusEnumViewModel} from '../../view-models/status-enum.view-model';
  6. @Component({
  7. selector: 'app-status',
  8. templateUrl: './status.component.html',
  9. styleUrls: [ './status.component.css' ]
  10. })
  11. export class StatusComponent implements OnInit {
  12. hostsStatus: SiteViewModel[] = [];
  13. promise = null;
  14. constructor(private statusService: StatusService) { }
  15. ngOnInit() {
  16. this.getStatus();
  17. }
  18. getStatus(): void {
  19. this.promise = this.statusService.getHosts()
  20. .subscribe(hostsStatus => {
  21. const hostsStatusData = hostsStatus.data;
  22. this.hostsStatus = hostsStatusData.map(site => {
  23. const siteVm = new SiteViewModel();
  24. siteVm.name = site.name;
  25. siteVm.hosts = site.hosts.map(host => {
  26. const hostVM = new HostStatusViewModel();
  27. hostVM.ip = host.hostStatus.ip;
  28. hostVM.hostname = host.hostStatus.hostname;
  29. hostVM.times = host.hostStatus.times;
  30. let allNull = true;
  31. let oneNull = false;
  32. hostVM.timesMin = Number.MAX_SAFE_INTEGER;
  33. hostVM.timesAvg = 0;
  34. hostVM.timesMax = 0;
  35. hostVM.timesLost = 0;
  36. hostVM.times.forEach(p => {
  37. if (p == null) {
  38. ++hostVM.timesLost;
  39. oneNull = true;
  40. } else {
  41. allNull = false;
  42. if (p < hostVM.timesMin) {
  43. hostVM.timesMin = p;
  44. }
  45. if (p > hostVM.timesMax) {
  46. hostVM.timesMax = p;
  47. }
  48. hostVM.timesAvg += p;
  49. }
  50. });
  51. if (hostVM.times.length - hostVM.timesLost !== 0) {
  52. hostVM.timesAvg = hostVM.timesAvg / (hostVM.times.length - hostVM.timesLost);
  53. } else {
  54. hostVM.timesMin = null;
  55. hostVM.timesAvg = null;
  56. hostVM.timesMax = null;
  57. }
  58. if (allNull) {
  59. hostVM.status = StatusEnumViewModel.Error;
  60. } else if (oneNull) {
  61. hostVM.status = StatusEnumViewModel.Warning;
  62. } else {
  63. hostVM.status = StatusEnumViewModel.Ok;
  64. }
  65. return hostVM;
  66. });
  67. const status: {[id: number]: number} = {};
  68. status[StatusEnumViewModel.Ok] = 0;
  69. status[StatusEnumViewModel.Warning] = 0;
  70. status[StatusEnumViewModel.Error] = 0;
  71. siteVm.hosts.forEach(host => {
  72. ++status[host.status];
  73. });
  74. if (status[StatusEnumViewModel.Error] === siteVm.hosts.length) {
  75. siteVm.status = StatusEnumViewModel.Error;
  76. } else if (status[StatusEnumViewModel.Error] !== 0 || status[StatusEnumViewModel.Warning] !== 0) {
  77. siteVm.status = StatusEnumViewModel.Warning;
  78. } else {
  79. siteVm.status = StatusEnumViewModel.Ok;
  80. }
  81. return siteVm;
  82. });
  83. });
  84. }
  85. onclick(): void {
  86. this.getStatus();
  87. }
  88. }