Skip to content

Commit

Permalink
Merge pull request #74 from GSI-Xapiens-CSIRO/feat/save-query
Browse files Browse the repository at this point in the history
Feat/save query
  • Loading branch information
titus-zx authored Dec 17, 2024
2 parents 6cf86d9 + 15a7243 commit 05640b2
Show file tree
Hide file tree
Showing 11 changed files with 68 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ <h1 mat-dialog-title>Create New User</h1>
mat-raised-button
color="primary"
(click)="createUser()"
[disabled]="newUserForm.invalid"
[disabled]="newUserForm.invalid || !costEstimation"
>
Create User
</button>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import { MatSnackBar } from '@angular/material/snack-bar';
import { AwsService } from 'src/app/services/aws.service';
import { DportalService } from 'src/app/services/dportal.service';
import { gigabytesToBytes } from 'src/app/utils/file';
import { UserQuotaService } from 'src/app/services/userquota.service';

@Component({
selector: 'app-admin-create-user-dialog',
Expand Down Expand Up @@ -71,7 +72,7 @@ export class AdminCreateUserComponent implements OnInit {
private adminServ: AdminService,
private sb: MatSnackBar,
private aws: AwsService,
private dp: DportalService,
private uq: UserQuotaService,
) {
this.newUserForm = this.fb.group({
firstName: ['', Validators.required],
Expand Down Expand Up @@ -165,7 +166,7 @@ export class AdminCreateUserComponent implements OnInit {
}

addUserQuota(sub: string): void {
this.dp
this.uq
.upsertUserQuota(sub, this.costEstimation, {
quotaSize: gigabytesToBytes(this.newUserForm.value.quotaSize),
quotaQueryCount: this.newUserForm.value.quotaQueryCount,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ <h1 mat-dialog-title>Update {{ data.name }}</h1>
mat-raised-button
color="primary"
(click)="done()"
[disabled]="form.invalid"
[disabled]="form.invalid || !costEstimation"
>
Update
</button>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import { MatInputModule } from '@angular/material/input';
import { DportalService } from 'src/app/services/dportal.service';
import { AwsService } from 'src/app/services/aws.service';
import { bytesToGigabytes, gigabytesToBytes } from 'src/app/utils/file';
import { UserQuotaService } from 'src/app/services/userquota.service';

@Component({
selector: 'app-admin-user-click-dialog',
Expand Down Expand Up @@ -67,7 +68,7 @@ export class AdminUserClickDialogComponent implements OnInit {
public dialogRef: MatDialogRef<AdminUserClickDialogComponent>,
private fb: FormBuilder,
private as: AdminService,
private dp: DportalService,
private uq: UserQuotaService,
private aws: AwsService,
private dg: MatDialog,
@Inject(MAT_DIALOG_DATA) public data: any,
Expand Down Expand Up @@ -108,7 +109,7 @@ export class AdminUserClickDialogComponent implements OnInit {
getUserGroups() {
this.loading = true;
// Define both observables
const userQuota$ = this.dp
const userQuota$ = this.uq
.getUserQuota(this.data.sub)
.pipe(catchError(() => of(null)));

Expand Down Expand Up @@ -192,7 +193,7 @@ export class AdminUserClickDialogComponent implements OnInit {
}

updateQuota() {
return this.dp
return this.uq
.upsertUserQuota(this.data.sub, this.costEstimation, {
quotaSize: gigabytesToBytes(this.form.value.quotaSize),
quotaQueryCount: this.form.value.quotaQueryCount,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@ import { MatIconModule } from '@angular/material/icon';
import { Storage } from 'aws-amplify';
import { MatTooltipModule } from '@angular/material/tooltip';
import { MatDialog, MatDialogModule } from '@angular/material/dialog';
import { DUMMY_DATA_STORAGE } from 'src/app/utils/data';
import { AuthService } from 'src/app/services/auth.service';
import { catchError, filter, firstValueFrom, of } from 'rxjs';
import { DportalService } from 'src/app/services/dportal.service';
import { formatBytes, getTotalSize } from 'src/app/utils/file';
import { formatBytes, getTotalStorageSize } from 'src/app/utils/file';
import { UserQuotaService } from 'src/app/services/userquota.service';

@Component({
Expand Down Expand Up @@ -51,28 +50,18 @@ export class UserFileListComponent implements OnInit {
}

generateTotalSize(files: any[]) {
const bytesTotal = getTotalSize(files);
const bytesTotal = getTotalStorageSize(files);

this.totalSize = bytesTotal;
this.totalSizeFormatted = formatBytes(bytesTotal);
}

dummyList() {
this.myFiles = DUMMY_DATA_STORAGE.results;
this.generateTotalSize(this.myFiles);
}

async list() {
// this.dummyList();

const res = await Storage.list(``, {
pageSize: 'ALL',
level: 'private',
});

console.log('res storage list (res)', res);
console.log('res storage results (res.results)', res.results);
// TODO: Update this.myFiles with the results from the Storage.list call
this.myFiles = res.results;
this.generateTotalSize(this.myFiles);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@ import { MatCardModule } from '@angular/material/card';
import { DportalService } from 'src/app/services/dportal.service';
import { MatDialog, MatDialogModule } from '@angular/material/dialog';
import { SpinnerService } from 'src/app/services/spinner.service';
import { catchError, from, of } from 'rxjs';
import { catchError, filter, firstValueFrom, from, of, switchMap } from 'rxjs';
import { MatSnackBar, MatSnackBarModule } from '@angular/material/snack-bar';
import { Storage } from 'aws-amplify';
import { getTotalStorageSize } from 'src/app/utils/file';
import { UserQuotaService } from 'src/app/services/userquota.service';
import { AuthService } from 'src/app/services/auth.service';

@Component({
selector: 'app-query-result-viewer-container',
Expand Down Expand Up @@ -51,6 +54,7 @@ export class QueryResultViewerContainerComponent implements OnChanges {
private dg: MatDialog,
private ss: SpinnerService,
private sb: MatSnackBar,
private uq: UserQuotaService,
) {}

ngOnChanges(): void {
Expand All @@ -69,6 +73,7 @@ export class QueryResultViewerContainerComponent implements OnChanges {
type: 'text/json;charset=utf-8;',
});
const url = URL.createObjectURL(blob);

const a = document.createElement('a');
a.href = url;
a.download = 'data.json';
Expand All @@ -77,7 +82,52 @@ export class QueryResultViewerContainerComponent implements OnChanges {
document.body.removeChild(a);
}

// Calculate total size from storage and current query result
async totalStorage(queryResults: any) {
// Get files in the storage
const res = await Storage.list(``, {
pageSize: 'ALL',
level: 'private',
});

// Get total size from storage
const bytesTotal = getTotalStorageSize(res.results);

// Get size from current query result
const blob = new Blob([JSON.stringify(queryResults, null, 2)], {
type: 'text/json;charset=utf-8;',
});

return bytesTotal + blob.size;
}

updateUserQuota(userQuota: any, currentTotalSize: number) {
this.uq
.upsertUserQuota(userQuota.userSub, userQuota.costEstimation, {
quotaSize: userQuota.quotaSize,
quotaQueryCount: userQuota.quotaQueryCount,
usageSize: currentTotalSize,
usageCount: userQuota.usageCount,
})
.pipe(catchError(() => of(null)));
}

async save(content: any) {
const userQuota = await firstValueFrom(this.uq.getCurrentUsage());
const currentTotalSize = await this.totalStorage(content);

// Check if the current total size is greater than the user's quota size
if (currentTotalSize >= userQuota.quotaSize) {
this.sb.open(
'Cannot Save Query because Quota Limit reached. Please contact administrator to increase your quota.',
'Okay',
{
duration: 60000,
},
);
return;
}

const { TextInputDialogComponent } = await import(
'../../../../../components/text-input-dialog/text-input-dialog.component'
);
Expand All @@ -91,6 +141,7 @@ export class QueryResultViewerContainerComponent implements OnChanges {
placeholder: 'My query results',
},
});

dialog.afterClosed().subscribe((name) => {
if (name) {
this.ss.start();
Expand All @@ -105,6 +156,8 @@ export class QueryResultViewerContainerComponent implements OnChanges {
if (!res) {
this.sb.open('Saving failed', 'Okay', { duration: 60000 });
}

this.updateUserQuota(userQuota, currentTotalSize);
this.ss.end();
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ export class QueryTabComponent implements OnInit, AfterViewInit, OnDestroy {
);

if (usageCount >= quotaQueryCount) {
this.sb.open('Run Query is reach Quota Limit.', 'Okay', {
this.sb.open('Run Query is reach quota limit.', 'Okay', {
duration: 60000,
});
this.ss.end();
Expand Down Expand Up @@ -367,7 +367,7 @@ export class QueryTabComponent implements OnInit, AfterViewInit, OnDestroy {
this.endpoint = endpoint;
this.scope = form.customReturn ? form.return : form.scope;

this.dps.incrementUsageCount(userSub).subscribe(() => {
this.uq.incrementUsageCount(userSub).subscribe(() => {
console.log('usage count incremented');
});
} else {
Expand Down
32 changes: 0 additions & 32 deletions webgui/webapp/src/app/services/dportal.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -328,36 +328,4 @@ export class DportalService {
),
);
}

getUserQuota(id: string) {
console.log('get user quota');
return from(
API.get(environment.api_endpoint_sbeacon.name, `dportal/quota/${id}`, {}),
);
}

upsertUserQuota(id: string, costEstimation: number | null, usage: any) {
console.log('upsert user quota');
return from(
API.post(environment.api_endpoint_sbeacon.name, 'dportal/quota', {
body: {
IdentityUser: id,
CostEstimation: costEstimation,
Usage: usage,
},
}),
);
}

incrementUsageCount(id: string) {
debugger;
console.log('incrementUsageCount');
return from(
API.post(
environment.api_endpoint_sbeacon.name,
`dportal/quota/${id}/increment_usagecount`,
{},
),
);
}
}
1 change: 0 additions & 1 deletion webgui/webapp/src/app/services/userquota.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ export class UserQuotaService {
}

incrementUsageCount(id: string) {
debugger;
console.log('incrementUsageCount');
return from(
API.post(
Expand Down
23 changes: 0 additions & 23 deletions webgui/webapp/src/app/utils/data.ts

This file was deleted.

2 changes: 1 addition & 1 deletion webgui/webapp/src/app/utils/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,6 @@ export function bytesToGigabytes(bytes: number): number {
* getTotalSize(dataStorage); // 123456789
* ```
*/
export function getTotalSize(dataStorage: any): number {
export function getTotalStorageSize(dataStorage: any): number {
return dataStorage.reduce((total: number, item: any) => total + item.size, 0);
}

0 comments on commit 05640b2

Please sign in to comment.