import { Component } from '@angular/core'; import {DatePipe, DecimalPipe, NgForOf} from "@angular/common"; import axios from "axios"; import {firstValueFrom} from "rxjs"; import {DevelopmentStore} from "../../store/DevelopmentStore"; import {AuthStore} from "../../store/authStore"; import {Router} from "@angular/router"; import {FormatFileSizePipePipe} from "../format-file-size-pipe.pipe"; @Component({ selector: 'app-adminui', standalone: true, imports: [ DatePipe, DecimalPipe, NgForOf, FormatFileSizePipePipe ], templateUrl: './adminui.component.html', styleUrl: './adminui.component.scss' }) export class AdminuiComponent { fileUploads: any[] = []; expiredFileUploads: any[] = []; totalFileSizeOnDisk: number = 0; totalFileDownloads = 0; constructor(private developmentStore: DevelopmentStore, private authStore: AuthStore, private router: Router) { this.verifyToken(); } async verifyToken() { if(await firstValueFrom(this.authStore.token$) === "") { console.log("No token present, redirecting to login..."); await this.router.navigate(['/login']); return; } await this.fetchFileUploads(); await this.fetchExpiredFileUploads(); await this.calculateStatistics(); } async calculateStatistics() { console.log("Calculating statistics..."); console.log(this.fileUploads) for(let fileUpload of this.fileUploads) { this.totalFileSizeOnDisk += fileUpload.fileSize; } for(let fileUpload of this.expiredFileUploads) { this.totalFileDownloads += fileUpload.downloadCount; } for(let fileUpload of this.fileUploads) { this.totalFileDownloads += fileUpload.downloadCount; } } async fetchFileUploads() { try { const response = await axios({ method: 'get', url: this.developmentStore.getBaseUrl() + 'api/v1/secure/upload-history', headers: { 'Content-Type': 'application/json', 'Authorization': 'Bearer ' + await firstValueFrom(this.authStore.token$) } }); this.fileUploads = response.data; } catch (error) { console.error(error); } } async fetchExpiredFileUploads() { try { const response = await axios({ method: 'get', url: this.developmentStore.getBaseUrl() + 'api/v1/secure/expired-upload-history', headers: { 'Content-Type': 'application/json', 'Authorization': 'Bearer ' + await firstValueFrom(this.authStore.token$) } }); this.expiredFileUploads = response.data; } catch (error) { console.error(error); } } }