import { Component, OnInit } from '@angular/core'; import { map } from 'rxjs/operators'; import { Breakpoints, BreakpointObserver } from '@angular/cdk/layout'; import { ServerDataService } from '../server-data.service'; import { Observable, Subscription } from 'rxjs'; import { FormControl, ReactiveFormsModule } from '@angular/forms'; import { HttpClient, HttpClientModule, HttpHeaders } from '@angular/common/http'; import { Validators } from '@angular/forms'; import { FormArray } from '@angular/forms'; import { RoleService, Roles } from '../role.service'; import { AuthenticationService } from '../authentication.service'; import { IssueService } from "../issue.service"; import { environment } from './../../environments/environment'; @Component({ selector: 'app-reservation', templateUrl: './reservation.component.html', styleUrls: ['./reservation.component.css'] }) export class ReservationComponent{ public returnData: any; public vmList : any; public systemList : any; public fwList : any; public priority : number = 0; public conflictString : string = ""; public conflict : boolean = false; public success : boolean = false; overrideObj: any; override: boolean = false; currentRole: any; currentSystem: string = ""; currentSystemData: any; sysNum: number = 0; currentVM : string = ""; currentVMData: any; static updatedSystemString : boolean = false; public auth: boolean = true; public role: Roles = Roles.NA; public roles = Roles; private user: string = ""; cards = this.breakpointObserver.observe(Breakpoints.Handset).pipe( map(({ matches }) => { if (matches) { return [ { title: 'Make a Reservation', cols: 2, rows: 3 } ]; } return [ { title: 'Make a Reservation', cols: 2, rows: 2 } ]; }) ); constructor( private breakpointObserver: BreakpointObserver, private serverDataService: ServerDataService, private roleService: RoleService, private http: HttpClient, private authService: AuthenticationService) { this.printVMList(); this.currentRole = this.roleService.roleSrc; } printVMList() { console.log(this.vmList); } setSystem( system: string, index: number ) { this.sysNum = index; this.currentSystem = system; this.currentSystemData = this.systemList[index]; ReservationComponent.updatedSystemString = true; } setVM( vm: any ) { this.currentVM = vm.vm; this.currentVMData = vm; console.log( "VM\t" + vm + "\tselected" ); } ngOnInit(){ this.serverDataService.systemList.subscribe((data: any) => { this.systemList = data; this.setSystem(this.systemList[this.sysNum].id, this.sysNum) if(this.currentSystemData === "") { this.currentSystem = this.systemList[this.sysNum].id; } //console.log(this.systemList); //this.setSystem(data[0].id, 0); // Attempting to fix bug where initial system is not selected //if( !ReservationComponent.updatedSystemString ) //{ // console.log("hello"); // this.setSystem( data[0] ); //} }); this.serverDataService.vmList.subscribe((data: any) => { this.vmList = data; //console.log(this.vmList); }); this.serverDataService.fwList.subscribe((data: any) => { this.fwList = data; //console.log(this.fwList); }); this.roleService.currentRole.subscribe( (result: Roles) => { this.role = result; console.log(this.role); }); this.authService.user.subscribe( (result: string) => { this.user = result; }); } validation = new FormControl('False'); reservation_day_s = new FormControl(''); reservation_day_e = new FormControl(''); description = new FormControl(''); setValidation(newValue: String) { this.validation.setValue(newValue); } updateVals(valid:String, res_start_d:String, res_end_d:String, desc:String) { this.validation.setValue(valid); this.reservation_day_s.setValue(res_start_d); this.reservation_day_e.setValue(res_end_d); this.description.setValue(desc); return this.validation, this.reservation_day_s, this.reservation_day_e, this.description; } resetVals() { this.validation.reset(); this.reservation_day_s.reset(); this.reservation_day_e.reset(); this.description.reset(); } // assumes start date is before the end date calculateDateDifference(startDate: any, endDate: any) { var diff = endDate.getTime() - startDate.getTime(); var days = Math.floor(diff / (60 * 60 * 24 * 1000)); var hours = Math.floor(diff / (60 * 60 * 1000)) - (days * 24); var minutes = Math.floor(diff / (60 * 1000)) - ((days * 24 * 60) + (hours * 60)); var seconds = Math.floor(diff / 1000) - ((days * 24 * 60 * 60) + (hours * 60 * 60) + (minutes * 60)); return { day: days, hour: hours, minute: minutes, second: seconds }; } // calculates the current priority of the reservation to be made in case // it is overwritten later, and performs action based on the conflicts found calculatePriorityAndSend(role: number, res_start_d: string, res_end_d: string, desc: string ) { if(this.currentVM == "" || (role < 1 || role > 4) || res_start_d == "" || res_end_d == "") { this.conflict = true; this.conflictString = "A required field was left empty, please fill them all"; } else { this.conflict = false; this.conflictString = ""; let res_start_t = "00:00"; let res_end_t = "00:00"; console.log(res_start_d + "\t" + res_end_d) console.log(res_start_t + "\t" + res_end_t) var difference = this.calculateDateDifference(new Date(res_start_d + " " + res_start_t ), new Date(res_end_d + " " + res_end_t )); console.log(difference); var conflicts = []; var schedule = JSON.parse(this.currentSystemData.schedule); var startDate = (new Date(res_start_d + " " + res_start_t)).getTime(); var endDate = (new Date(res_end_d + " " + res_end_t )).getTime(); var resStart = {}; var resEnd = {}; var noSend = false; for(let res of schedule) { if(res.vm === this.currentVM) { resStart = (new Date(res.start)).getTime(); resEnd = (new Date(res.end)).getTime(); if((startDate >= resStart && startDate <= resEnd) || (endDate >= resStart && endDate <= resEnd)) { conflicts.push(res); } } } this.priority = 9; if(this.validation.value == "False") { switch(role) { case Roles.Manager: this.priority = 1; break; case Roles.FW: // TODO 'only if Debug VM is attached to Test VM' // if less than one day, priority of 2 if( (difference.day == 1 && ( difference.hour == 0 && difference.minute == 0 && difference.second == 0 ) ) || difference.day == 0 ) { this.priority = 2; } break; case Roles.Dev: // if less than or equal to 3 days, priority of 3 if( (difference.day == 3 && ( difference.hour == 0 && difference.minute == 0 && difference.second == 0 ) ) || difference.day < 3 ) { this.priority = 3; } // if more than 3 days, priority value of 8 else { this.priority = 8; } break; case Roles.Exec: this.priority = 9; break; } } else { if(this.currentVMData.fw != "N/A") { this.priority = 4; } else if( (difference.day == 1 && ( difference.hour == 0 && difference.minute == 0 && difference.second == 0 ) ) || difference.day < 1 ) { this.priority = 5; } else if(difference.day >= 1 && difference.hour >= 1) { this.priority = 6; } else { this.priority = 7; } } // check for conflicts between reservation times for VM if(conflicts.length != 0) { this.conflictString = ""; this.conflict = true; for(let conflict of conflicts) { // if conflicts and same or lower priority, do nothing & notify user console.log(this.priority, conflict.priority); if(this.priority >= conflict.priority || noSend) { noSend = true; this.conflictString = this.conflictString + "Conflict with reservation \ during " + conflict.start + "-" + conflict.end + ", priority: " + conflict.priority + " : "; } // if conflicts and higher priority, ask user to confirm overwrite else if(!noSend){ this.overrideObj = {startD: startDate, endD: endDate, desc: desc, priority: this.priority}; this.conflictString = this.conflictString + "\nPlease Confirm Reservation Override"; this.override = true; } } } // if no conflict, send anyway else { this.conflict = false; this.sendData(res_start_d, res_end_d, desc, this.priority); } } } async overrideRes() { this.sendData(this.overrideObj.startD, this.overrideObj.endD, this.overrideObj.desc, this.overrideObj.priority); this.overrideObj = {}; this.override = false; } async sendData(res_start_d:string, res_end_d:string, desc:string, priority:number) { var startDate = new Date(res_start_d) var endDate = new Date(res_end_d) let auth = this.authService.authHeader.get('Authorization') || "NA NA"; //setting headers for request const httpOptions = { headers: new HttpHeaders({ 'Content-Type': 'application/json', Authorization: auth }) }; //setting payload let payload = {system: this.currentSystem, res: {priority: priority, desc: desc, user: this.user, start: startDate, end: endDate, system: this.currentSystem, vm: this.currentVM}}; console.log(payload); //http post request this.http.post(environment.ip + '/addRes', payload, httpOptions) .subscribe(data => { this.returnData = data; console.log(this.returnData); this.serverDataService.getDataFromServer(); this.success = true; return this.returnData; }); } }