Skip to content

Commit

Permalink
devices: show data usage
Browse files Browse the repository at this point in the history
  • Loading branch information
franzmueller committed Dec 12, 2023
1 parent 86b23c8 commit 99c31b6
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ export class LadonService {
environment.costApiUrl,
environment.costApiUrl + '/estimation/flow',
environment.billingApiUrl + '/billing-components',
environment.timescaleAPIURL + '/usage'
]

ServiceEndpoints.forEach(endpointURL => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,15 @@
<senergy-state-icon [state]="device.log_state"></senergy-state-icon>
</td>
</ng-container>

<ng-container matColumnDef="usage">
<th mat-header-cell *matHeaderCellDef>Storage
</th>
<td mat-cell *matCellDef="let device">
{{formatBytes(getUsage(device)?.bytes || 0)}}
</td>
</ng-container>

<ng-container matColumnDef="shared">
<th mat-header-cell *matHeaderCellDef mat-sort-header disableClear class="button-column">Shared</th>
<td mat-cell *matCellDef="let device">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import { createSpyFromClass, Spy } from 'jasmine-auto-spies';
import { DeviceInstancesService } from './shared/device-instances.service';
import { of } from 'rxjs';
import { DeviceTypeService } from '../../metadata/device-types-overview/shared/device-type.service';
import { ExportDataService } from 'src/app/widgets/shared/export-data.service';

describe('DeviceInstancesComponent', () => {
let component: DeviceInstancesComponent;
Expand All @@ -45,6 +46,9 @@ describe('DeviceInstancesComponent', () => {
const deviceTypeServiceSpy: Spy<DeviceTypeService> = createSpyFromClass(DeviceTypeService)
deviceTypeServiceSpy.userHasPermSearchAuthorization.and.returnValue(true)

const exportDataServiceSpy: Spy<ExportDataService> = createSpyFromClass(ExportDataService);
exportDataServiceSpy.userHasUsageAuthroization.and.returnValue(false)

beforeEach(
waitForAsync(() => {
TestBed.configureTestingModule({
Expand All @@ -62,7 +66,8 @@ describe('DeviceInstancesComponent', () => {
{ provide: KeycloakService, useClass: MockKeycloakService },
{ provide: Router, useClass: RouterStub },
{ provide: DeviceInstancesService, useValue: deviceInstanceServiceSpy },
{ provide: DeviceTypeService, useValue: deviceTypeServiceSpy }
{ provide: DeviceTypeService, useValue: deviceTypeServiceSpy },
{ provide: ExportDataService, useValue: exportDataServiceSpy },
],
}).compileComponents();
}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import { forkJoin, Observable, map, Subscription, of } from 'rxjs';
import { SearchbarService } from 'src/app/core/components/searchbar/shared/searchbar.service';
import { DeviceInstancesFilterDialogComponent } from './dialogs/device-instances-filter-dialog/device-instances-filter-dialog.component';
import { MatDialog } from '@angular/material/dialog';
import { ExportDataService } from 'src/app/widgets/shared/export-data.service';

export interface DeviceInstancesRouterState {
type: DeviceInstancesRouterStateTypesEnum | undefined | null;
Expand Down Expand Up @@ -76,7 +77,8 @@ export class DeviceInstancesComponent implements OnInit, AfterViewInit {
private dialogsService: DialogsService,
private deviceTypesService: DeviceTypeService,
private searchbarService: SearchbarService,
private dialog: MatDialog
private dialog: MatDialog,
private exportDataService: ExportDataService,
) {
this.getRouterParams();
}
Expand All @@ -88,6 +90,11 @@ export class DeviceInstancesComponent implements OnInit, AfterViewInit {
offset = 0;
ready = false;
searchText = '';
usage : {
deviceId: string;
updateAt: Date;
bytes: number;
}[] = [];

@ViewChild('paginator', { static: false }) paginator!: MatPaginator;

Expand Down Expand Up @@ -124,6 +131,10 @@ export class DeviceInstancesComponent implements OnInit, AfterViewInit {
}

checkAuthorization() {
if (this.exportDataService.userHasUsageAuthroization()){
this.displayedColumns.splice(4, 0, 'usage')
}

this.userHasUpdateAuthorization = this.deviceInstancesService.userHasUpdateAuthorization()
if(this.userHasUpdateAuthorization) {
this.displayedColumns.push("edit")
Expand Down Expand Up @@ -218,6 +229,10 @@ export class DeviceInstancesComponent implements OnInit, AfterViewInit {
map((deviceInstances: DeviceInstancesModel[]) => {
this.setDevices(deviceInstances);
return deviceInstances
}),
map(d => {
this.exportDataService.getTimescaleDeviceUsage(d.map(di => di.id)).subscribe(r => this.usage.push(...r))
return d
})
);
}
Expand All @@ -232,6 +247,7 @@ export class DeviceInstancesComponent implements OnInit, AfterViewInit {
this.ready = false;
this.pageSize = 20;
this.selectionClear();
this.usage = [];

forkJoin(this.load(), this.getTotalCount()).subscribe({
next: (_) => this.ready = true,
Expand Down Expand Up @@ -342,4 +358,20 @@ export class DeviceInstancesComponent implements OnInit, AfterViewInit {
});
});
}

getUsage(d: DeviceInstancesModel) {
return this.usage.find(u => u.deviceId === d.id);
}

formatBytes(bytes: number, decimals = 2) {
if (!+bytes) return '0 Bytes'

const k = 1024
const dm = decimals < 0 ? 0 : decimals
const sizes = ['Bytes', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB']

const i = Math.floor(Math.log(bytes) / Math.log(k))

return `${parseFloat((bytes / Math.pow(k, i)).toFixed(dm))} ${sizes[i]}`
}
}
28 changes: 26 additions & 2 deletions src/app/widgets/shared/export-data.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,18 @@ import {
} from './export-data.model';
import {HttpClient} from '@angular/common/http';
import {map} from 'rxjs/operators';

import { PermissionTestResponse } from 'src/app/modules/admin/permissions/shared/permission.model';
import { LadonService } from 'src/app/modules/admin/permissions/shared/services/ladom.service';

@Injectable({
providedIn: 'root',
})
export class ExportDataService {
constructor(private http: HttpClient) {
usageAuthorizations: PermissionTestResponse

constructor(private http: HttpClient, private ladonService: LadonService) {
this.usageAuthorizations = this.ladonService.getUserAuthorizationsForURI(environment.billingApiUrl + '/billing-components')

}

getLastValuesInflux(requestElements: LastValuesRequestElementInfluxModel[]): Observable<TimeValuePairModel[]> {
Expand Down Expand Up @@ -120,4 +125,23 @@ export class ExportDataService {
return res;
}));
}

userHasUsageAuthroization(): boolean {
return this.usageAuthorizations["POST"]
}

getTimescaleDeviceUsage(deviceIds: string[]): Observable<{
deviceId: string;
updateAt: Date;
bytes: number;
}[]> {
return this.http.post<{
deviceId: string;
updateAt: Date;
bytes: number;
}[]>(environment.timescaleAPIURL + '/usage/devices', deviceIds).pipe(map(res => {
res.forEach(r => r.updateAt = new Date(r.updateAt))
return res;
}))
}
}

0 comments on commit 99c31b6

Please sign in to comment.