<commit_before>import { Component, OnInit, Inject, ViewChild } from '@angular/core';

import { VmService } from './vm.service';
import { VirtualMachine } from './vm.model';
import { MdlDialogService } from 'angular2-mdl';
import { TranslateService } from 'ng2-translate';
import { IStorageService } from '../shared/services/storage.service';
import { VmCreateComponent } from './vm-create.component';
import {
  JobsNotificationService,
  INotificationStatus
} from '../shared/services/jobs-notification.service';

import { IVmAction } from './vm.model';
import { IAsyncJob } from '../shared/models/async-job.model';
import { AsyncJobService } from '../shared/services/async-job.service';
import { VmStatisticsComponent } from './vm-statistics.component';
import * as UUID from 'uuid';


interface IVmActionEvent {
  id: string;
  action: IVmAction;
  vm: VirtualMachine;
  templateId?: string;
}

@Component({
  selector: 'cs-vm-list',
  templateUrl: './vm-list.component.html',
  styleUrls: ['./vm-list.component.scss']
})
export class VmListComponent implements OnInit {

  @ViewChild(VmStatisticsComponent) public vmStats: VmStatisticsComponent;
  @ViewChild(VmCreateComponent) public vmCreationForm: VmCreateComponent;

  private vmList: Array<VirtualMachine>;
  private selectedVm: VirtualMachine;
  private isDetailOpen: boolean;

  constructor (
    private vmService: VmService,
    private dialogService: MdlDialogService,
    private translateService: TranslateService,
    @Inject('IStorageService') protected storageService: IStorageService,
    private jobsNotificationService: JobsNotificationService,
    private asyncJobService: AsyncJobService
  ) { }

  public ngOnInit() {
    this.vmService.getList()
      .then(vmList => {
        this.vmList = vmList;

        if (this.vmList.length) {
          return;
        }

        if (this.storageService.read('askToCreateVm') === 'false') {
          return;
        }

        this.translateService.get([
          'YES',
          'NO',
          'NO_DONT_ASK',
          'WOULD_YOU_LIKE_TO_CREATE_VM'
        ]).subscribe(translations => {
          this.showDialog(translations);
        });
      });
    this.asyncJobService.event.subscribe((job: IAsyncJob<any>) => {
      if (job.jobResult && job.jobResult.state === 'Destroyed') {
        this.vmList.splice(this.vmList.findIndex(vm => vm.id === job.jobResult.id), 1);
        if (this.selectedVm && this.selectedVm.id === job.jobResult.id) {
          this.isDetailOpen = false;
        }
        this.vmStats.updateStats();
      }
    });
    this.vmService.resubscribe().then(observables => {
      observables.forEach(observable => {
        observable.subscribe(job => {
          const action = VirtualMachine.getAction(job.cmd);
          this.translateService.get([
            'YES',
            'NO',
            action.confirmMessage,
            action.progressMessage,
            action.successMessage
          ]).subscribe(strs => {
            this.jobsNotificationService.add({
              id: UUID.v4(),
              message: strs[action.successMessage],
              status: INotificationStatus.Finished
            });
          });
        });
      });
    });
  }

  public onVmAction(e: IVmActionEvent) {
    this.translateService.get([
      'YES',
      'NO',
      e.action.confirmMessage,
      e.action.progressMessage,
      e.action.successMessage
    ]).subscribe(strs => {
      this.dialogService.confirm(strs[e.action.confirmMessage], strs.NO, strs.YES)
        .subscribe(r => {
          if (e.vm) {
            e.vm.state = e.action.vmStateOnAction;
          }
          let id = this.jobsNotificationService.add(strs[e.action.progressMessage]);
          this.vmService.command(e.action.nameLower, e.vm.id)
            .subscribe(result => {
              this.jobsNotificationService.add({
                id,
                message: strs[e.action.successMessage],
                status: INotificationStatus.Finished
              });
            }
          );
        },
        // handle error comes from cancel button
        () => {});
    });
  }

  public onVmCreated(e) {
    this.vmList.push(e);<commit_msg>#2905 Remove unused variables<commit_after>import { Component, OnInit, Inject, ViewChild } from '@angular/core';

import { VmService } from './vm.service';
import { VirtualMachine } from './vm.model';
import { MdlDialogService } from 'angular2-mdl';
import { TranslateService } from 'ng2-translate';
import { IStorageService } from '../shared/services/storage.service';
import { VmCreateComponent } from './vm-create.component';
import {
  JobsNotificationService,
  INotificationStatus
} from '../shared/services/jobs-notification.service';

import { IVmAction } from './vm.model';
import { IAsyncJob } from '../shared/models/async-job.model';
import { AsyncJobService } from '../shared/services/async-job.service';
import { VmStatisticsComponent } from './vm-statistics.component';
import * as UUID from 'uuid';


interface IVmActionEvent {
  id: string;
  action: IVmAction;
  vm: VirtualMachine;
  templateId?: string;
}

@Component({
  selector: 'cs-vm-list',
  templateUrl: './vm-list.component.html',
  styleUrls: ['./vm-list.component.scss']
})
export class VmListComponent implements OnInit {

  @ViewChild(VmStatisticsComponent) public vmStats: VmStatisticsComponent;
  @ViewChild(VmCreateComponent) public vmCreationForm: VmCreateComponent;

  private vmList: Array<VirtualMachine>;
  private selectedVm: VirtualMachine;
  private isDetailOpen: boolean;

  constructor (
    private vmService: VmService,
    private dialogService: MdlDialogService,
    private translateService: TranslateService,
    @Inject('IStorageService') protected storageService: IStorageService,
    private jobsNotificationService: JobsNotificationService,
    private asyncJobService: AsyncJobService
  ) { }

  public ngOnInit() {
    this.vmService.getList()
      .then(vmList => {
        this.vmList = vmList;

        if (this.vmList.length) {
          return;
        }

        if (this.storageService.read('askToCreateVm') === 'false') {
          return;
        }

        this.translateService.get([
          'YES',
          'NO',
          'NO_DONT_ASK',
          'WOULD_YOU_LIKE_TO_CREATE_VM'
        ]).subscribe(translations => {
          this.showDialog(translations);
        });
      });
    this.asyncJobService.event.subscribe((job: IAsyncJob<any>) => {
      if (job.jobResult && job.jobResult.state === 'Destroyed') {
        this.vmList.splice(this.vmList.findIndex(vm => vm.id === job.jobResult.id), 1);
        if (this.selectedVm && this.selectedVm.id === job.jobResult.id) {
          this.isDetailOpen = false;
        }
        this.vmStats.updateStats();
      }
    });
    this.vmService.resubscribe().then(observables => {
      observables.forEach(observable => {
        observable.subscribe(job => {
          const action = VirtualMachine.getAction(job.cmd);
          this.translateService.get([
            'YES',
            'NO',
            action.confirmMessage,
            action.progressMessage,
            action.successMessage
          ]).subscribe(strs => {
            this.jobsNotificationService.add({
              id: UUID.v4(),
              message: strs[action.successMessage],
              status: INotificationStatus.Finished
            });
          });
        });
      });
    });
  }

  public onVmAction(e: IVmActionEvent) {
    this.translateService.get([
      'YES',
      'NO',
      e.action.confirmMessage,
      e.action.progressMessage,
      e.action.successMessage
    ]).subscribe(strs => {
      this.dialogService.confirm(strs[e.action.confirmMessage], strs.NO, strs.YES)
        .subscribe(() => {
          if (e.vm) {
            e.vm.state = e.action.vmStateOnAction;
          }
          let id = this.jobsNotificationService.add(strs[e.action.progressMessage]);
          this.vmService.command(e.action.nameLower, e.vm.id)
            .subscribe(() => {
              this.jobsNotificationService.add({
                id,
                message: strs[e.action.successMessage],
                status: INotificationStatus.Finished
              });
            }
          );
        },
        // handle error comes from cancel button
        () => {});
    });
  }

  public onVmCreated(e) {
    this.vmList.push(e);
{'fraction_non_alphanumeric': 0.1294509944027629, 'fraction_numerical': 0.0014290818149339049, 'mean_word_length': 2.496253122398002, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2467454', 'n_tokens_mistral': 2466, 'n_tokens_neox': 2415, 'n_words': 426}