Skip to content

Commit

Permalink
Merge pull request #237 from amosproj/212-clean-up-3
Browse files Browse the repository at this point in the history
212 clean up 3
  • Loading branch information
ddeli authored Jan 10, 2025
2 parents c1b36bb + 6dd3ff9 commit 9662cc4
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 12 deletions.
33 changes: 33 additions & 0 deletions apps/backend/src/app/alerting/alerting.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,39 @@ describe('AlertingService', () => {
expect(service).toBeDefined();
});

describe('ensureAlertTypesExist', () => {
it('should add default alert types if they do not exist', async () => {
jest.spyOn(alertTypeRepository, 'findOneBy').mockResolvedValue(null);

await service.ensureAlertTypesExist();

expect(alertTypeRepository.save).toHaveBeenCalledTimes(3);
expect(alertTypeRepository.save).toHaveBeenCalledWith(
expect.objectContaining({ name: SIZE_ALERT })
);
expect(alertTypeRepository.save).toHaveBeenCalledWith(
expect.objectContaining({ name: CREATION_DATE_ALERT })
);
expect(alertTypeRepository.save).toHaveBeenCalledWith(
expect.objectContaining({ name: STORAGE_FILL_ALERT })
);
});

it('should not add alert types if they already exist', async () => {
jest.spyOn(alertTypeRepository, 'findOneBy').mockResolvedValue({
id: 'a',
name: SIZE_ALERT,
severity: SeverityType.WARNING,
user_active: true,
master_active: true,
});

await service.ensureAlertTypesExist();

expect(alertTypeRepository.save).not.toHaveBeenCalled();
});
});

describe('createSizeAlert', () => {
it('should create and save a size alert', async () => {
const createSizeAlertDto: CreateSizeAlertDto = {
Expand Down
40 changes: 39 additions & 1 deletion apps/backend/src/app/alerting/alerting.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
ConflictException,
Injectable,
NotFoundException,
OnModuleInit,
} from '@nestjs/common';
import { MailService } from '../utils/mail/mail.service';
import { InjectRepository } from '@nestjs/typeorm';
Expand All @@ -28,9 +29,10 @@ import {
SIZE_ALERT,
STORAGE_FILL_ALERT,
} from '../utils/constants';
import { SeverityType } from './dto/severityType';

@Injectable()
export class AlertingService {
export class AlertingService implements OnModuleInit {
alertRepositories: Repository<any>[] = [];

constructor(
Expand All @@ -52,6 +54,42 @@ export class AlertingService {
this.alertRepositories.push(this.storageFillRepository);
}

async onModuleInit() {
await this.ensureAlertTypesExist();
}

/**
* Init database table alert types with default values if not already present
*/
async ensureAlertTypesExist() {
const alertTypes: CreateAlertTypeDto[] = [
{
name: SIZE_ALERT,
master_active: true,
severity: SeverityType.WARNING,
},
{
name: CREATION_DATE_ALERT,
master_active: true,
severity: SeverityType.WARNING,
},
{
name: STORAGE_FILL_ALERT,
master_active: true,
severity: SeverityType.WARNING,
},
];

for (const alertType of alertTypes) {
const existingAlertType = await this.alertTypeRepository.findOneBy({
name: alertType.name,
});
if (!existingAlertType) {
await this.alertTypeRepository.save(alertType);
}
}
}

async createAlertType(createAlertTypeDto: CreateAlertTypeDto) {
const entity = await this.alertTypeRepository.findOneBy({
name: createAlertTypeDto.name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ export class AlertComponent implements OnInit, OnDestroy {
break;
case 'STORAGE_FILL_ALERT':
const storageFillAlert = alert as StorageFillAlert;
description = `The current storage fill is ${shortenBytes(
description = `The current storage fill of storage with name "${storageFillAlert.dataStoreName}" is ${shortenBytes(
storageFillAlert.filled * 1_000_000_000
)}, which is above the threshold of ${shortenBytes(
storageFillAlert.highWaterMark * 1_000_000_000
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,14 @@
}

.progress-bar-container {
display: flex;
align-items: center;
}

.progress-bar {
position: relative;
height: 12px;
width: 100%;
display: inline-block;
vertical-align: middle;
--clr-progress-bg-color: #C6DCF2;
--clr-progress-default-color: #0D233A;
Expand All @@ -24,6 +29,14 @@
background-color: #f44336;
position: absolute;
top: 0;
bottom: 0;
margin: auto 0;
}

.percentage {
margin-left: 10px;
display: inline-block;
width: 5%;
}

.card {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@
class="data-store">
<span class="data-store-name">{{ dataStore.displayName }}</span>
<div class="progress-bar-container">
<clr-progress-bar
[clrValue]="getFilledPercentage(dataStore)"
[clrMax]="100"
[clrLabeled]="true"
[clrDisplayval]="getFilledPercentage(dataStore) + '%'">
</clr-progress-bar>
<div class="high-water-mark" [style.left.%]="getHighWaterMarkPercentage(dataStore)"></div>
<div class="progress-bar">
<clr-progress-bar
[clrValue]="getFilledPercentage(dataStore)"
[clrMax]="100"
>
</clr-progress-bar>
<div class="high-water-mark" [style.left.%]="getHighWaterMarkPercentage(dataStore)"></div>
</div>
<div class="percentage">
{{ getFilledPercentage(dataStore) + '%' }}
</div>
</div>
</div>
<button *ngIf="((dataStores$ | async)?.length ?? 0) > 5" class="show-more-btn" (click)="toggleShowAll()">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,10 @@ export class DataStoresComponent implements OnDestroy, OnInit {
}

getHighWaterMarkPercentage(dataStore: DataStore): number {
return parseFloat(
const percentage = parseFloat(
((dataStore.highWaterMark / dataStore.capacity) * 100).toFixed(2)
);
return percentage > 100 ? 100 : percentage;
}

toggleShowAll(): void {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ <h3 class="modal-title">Notification Settings</h3>
[ngClass]="{
'badge-warning':
notification.get('severity')?.value === 'WARNING',
'badge-danger': notification.get('severity')?.value === 'ERROR',
'badge-danger': notification.get('severity')?.value === 'CRITICAL',
'badge-info': notification.get('severity')?.value === 'INFO'
}"
>
Expand Down

0 comments on commit 9662cc4

Please sign in to comment.