Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
95f927386d | ||
|
|
efe7b3da42 | ||
|
|
54c2160f0d | ||
|
|
1f3789d82b | ||
|
|
3fab4c4922 | ||
|
|
df1b8a509d | ||
|
|
b97e9486e8 | ||
|
|
5ab29ef770 |
No files matched your search
@@ -2,7 +2,10 @@
|
||||
@if (isLoading()) {
|
||||
<div class="loading-overlay">
|
||||
<div class="loading-spinner">
|
||||
<mat-spinner></mat-spinner>
|
||||
<div class="spinner-container">
|
||||
<mat-spinner [mode]="'determinate'" [value]="loadingProgress()"></mat-spinner>
|
||||
<div class="spinner-text">{{ loadingProgress() }}%</div>
|
||||
</div>
|
||||
<p>Calcul en cours...</p>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -51,25 +51,32 @@
|
||||
}
|
||||
}
|
||||
|
||||
.spinner-container {
|
||||
position: relative;
|
||||
display: inline-block;
|
||||
|
||||
::ng-deep .mat-mdc-progress-spinner {
|
||||
// Override Material spinner size
|
||||
width: 100px;
|
||||
height: 100px;
|
||||
}
|
||||
|
||||
.spinner-text {
|
||||
position: absolute;
|
||||
top: 50%;
|
||||
left: 50%;
|
||||
transform: translate(-50%, -50%);
|
||||
font-size: 20px;
|
||||
font-weight: bold;
|
||||
color: #333;
|
||||
pointer-events: none;
|
||||
|
||||
@media (prefers-color-scheme: dark) {
|
||||
color: #ddd;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
.card {
|
||||
margin: 16px;
|
||||
}
|
||||
|
||||
@keyframes spin {
|
||||
0% {
|
||||
transform: rotate(0deg);
|
||||
}
|
||||
|
||||
100% {
|
||||
transform: rotate(360deg);
|
||||
}
|
||||
}
|
||||
|
||||
:root {
|
||||
@include mat.progress-spinner-overrides(
|
||||
(
|
||||
size: 0.5rem,
|
||||
active-indicator-width: 0.25rem,
|
||||
)
|
||||
);
|
||||
}
|
||||
@@ -7,6 +7,7 @@ import { CommonModule } from '@angular/common';
|
||||
import { MatProgressSpinner } from '@angular/material/progress-spinner';
|
||||
import { CalendarSettingsService } from '../../services/calendar-settings-service';
|
||||
import { DateCacheService } from '../../services/date-cache.service';
|
||||
import { LoggerService } from '../../services/logger.service';
|
||||
|
||||
@Component({
|
||||
selector: 'app-calendar-year',
|
||||
@@ -19,8 +20,10 @@ export class CalendarYear {
|
||||
private userInput = inject(UserInputService);
|
||||
private calendarService = inject(CalendarService);
|
||||
private calendarSettingsService = inject(CalendarSettingsService);
|
||||
private logger = inject(LoggerService);
|
||||
|
||||
protected isLoading = signal(true);
|
||||
protected loadingProgress = signal(0);
|
||||
|
||||
// Use DateCacheService instead of creating Date objects
|
||||
private get currentMonth() {
|
||||
@@ -39,18 +42,27 @@ export class CalendarYear {
|
||||
console.log('Samedi Malin setting:', c);
|
||||
console.log('Optimizing vacations with data:', vacationData);
|
||||
this.isLoading.set(true);
|
||||
// Simulate server-side calculation with a small delay
|
||||
setTimeout(() => {
|
||||
if (this.selectedDates) {
|
||||
// Create a copy to avoid mutating the original signal data
|
||||
this.selectedDates.optimizeVacations(
|
||||
{ ...vacationData },
|
||||
this.calendarSettingsService,
|
||||
this.calendarService,
|
||||
);
|
||||
}
|
||||
this.isLoading.set(false);
|
||||
}, 1);
|
||||
this.loadingProgress.set(0);
|
||||
// Defer execution to allow UI to update
|
||||
queueMicrotask(() => {
|
||||
void (async () => {
|
||||
if (this.selectedDates) {
|
||||
// Create a copy to avoid mutating the original signal data
|
||||
const remainingDays = await this.selectedDates.optimizeVacations(
|
||||
{ ...vacationData },
|
||||
this.calendarSettingsService,
|
||||
this.calendarService,
|
||||
(progress: number) => {
|
||||
this.loadingProgress.set(progress);
|
||||
},
|
||||
this.logger,
|
||||
);
|
||||
// Update the remaining vacation days signal
|
||||
this.userInput.remainingVacationDaysSignal.set(remainingDays);
|
||||
}
|
||||
this.isLoading.set(false);
|
||||
})();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -14,5 +14,16 @@
|
||||
<input matInput type="number" max="365" min="0" formControlName="Others" />
|
||||
</mat-form-field>
|
||||
</form>
|
||||
@if (hasRemainingDays()) {
|
||||
<mat-card class="mat-elevation-z2 warning-card">
|
||||
<mat-card-content class="warning-content">
|
||||
<mat-icon color="warn" aria-hidden="true">warning</mat-icon>
|
||||
<span>
|
||||
{{ remainingDays() }} jour{{ remainingDays() > 1 ? 's' : '' }} de vacances non
|
||||
placé{{ remainingDays() > 1 ? 's' : '' }}. Veuillez les placer manuellement.
|
||||
</span>
|
||||
</mat-card-content>
|
||||
</mat-card>
|
||||
}
|
||||
<app-calendar-settings></app-calendar-settings>
|
||||
</div>
|
||||
@@ -13,6 +13,16 @@
|
||||
font-size: 0.875rem;
|
||||
margin-top: 0.25rem;
|
||||
}
|
||||
|
||||
.warning-card {
|
||||
margin-bottom: 1rem;
|
||||
}
|
||||
|
||||
.warning-content {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 0.5rem;
|
||||
}
|
||||
}
|
||||
|
||||
form {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { Component, OnInit, OnDestroy, effect } from '@angular/core';
|
||||
import { Component, OnInit, OnDestroy, effect, computed } from '@angular/core';
|
||||
import { MatFormField, MatLabel } from '@angular/material/form-field';
|
||||
import { MatInputModule } from '@angular/material/input';
|
||||
import { FormControl, FormGroup, ReactiveFormsModule } from '@angular/forms';
|
||||
@@ -6,6 +6,8 @@ import { UserInputService } from '../../services/user-input-service';
|
||||
import { VacationsNumber } from '../../services/calendar-service';
|
||||
import { debounceTime, takeUntil, Subject } from 'rxjs';
|
||||
import { CalendarSettings } from '../calendar-settings/calendar-settings';
|
||||
import { MatCard, MatCardContent } from '@angular/material/card';
|
||||
import { MatIcon } from '@angular/material/icon';
|
||||
|
||||
@Component({
|
||||
selector: 'app-user-input',
|
||||
@@ -15,7 +17,9 @@ import { CalendarSettings } from '../calendar-settings/calendar-settings';
|
||||
MatInputModule,
|
||||
ReactiveFormsModule,
|
||||
CalendarSettings,
|
||||
CalendarSettings,
|
||||
MatCard,
|
||||
MatCardContent,
|
||||
MatIcon,
|
||||
],
|
||||
templateUrl: './user-input.html',
|
||||
styleUrl: './user-input.scss',
|
||||
@@ -44,6 +48,13 @@ export class UserInput implements OnInit, OnDestroy {
|
||||
Others: new FormControl<number | null>(null),
|
||||
});
|
||||
|
||||
protected remainingDays = computed(() => {
|
||||
const remaining = this.userInputService.remainingVacationDaysSignal();
|
||||
return remaining.cp + remaining.rtt + remaining.other;
|
||||
});
|
||||
|
||||
protected hasRemainingDays = computed(() => this.remainingDays() > 0);
|
||||
|
||||
ngOnInit() {
|
||||
this.userInputForm.valueChanges
|
||||
.pipe(debounceTime(1500), takeUntil(this.destroy$))
|
||||
|
||||
@@ -6,6 +6,7 @@ import weekOfYear from 'dayjs/plugin/weekOfYear';
|
||||
import isoWeek from 'dayjs/plugin/isoWeek';
|
||||
import { CalendarSettingsService } from './calendar-settings-service';
|
||||
import { DateCacheService } from './date-cache.service';
|
||||
import { LoggerService } from './logger.service';
|
||||
|
||||
dayjs.extend(weekOfYear);
|
||||
dayjs.extend(isoWeek);
|
||||
@@ -21,6 +22,7 @@ const allTypesOptions: HolidaysTypes.Options = {
|
||||
})
|
||||
export class CalendarService {
|
||||
private dateCache = inject(DateCacheService);
|
||||
private logger = inject(LoggerService);
|
||||
|
||||
// Cache for holiday timestamps
|
||||
private holidayTimestampsCache: Set<number> | null = null;
|
||||
@@ -298,6 +300,7 @@ export class SelectedDates implements SelectedDateInterface {
|
||||
|
||||
// Constant to indicate no valid strategy was found
|
||||
private static readonly NO_VALID_STRATEGY_HEURISTIC = -1;
|
||||
private static readonly MAX_DAYS_WITH_SAME_HEURISTIC = 9;
|
||||
|
||||
update(): void {
|
||||
this.datesSelected = this._datesSelected.slice();
|
||||
@@ -501,7 +504,7 @@ export class SelectedDates implements SelectedDateInterface {
|
||||
|
||||
// Check if adjacent (next day) - this is the best case!
|
||||
if (nextStartTime - currentEndTime === oneDayInMs) {
|
||||
totalScore += 10; // Big bonus for adjacency
|
||||
totalScore += 15; // 50% increased bonus for adjacency; adjacency is highly valued in the scoring system
|
||||
} else if (nextStartTime > currentEndTime) {
|
||||
// Penalty based on days between the two dates
|
||||
const daysBetween = Math.floor((nextStartTime - currentEndTime) / oneDayInMs) - 1;
|
||||
@@ -535,18 +538,44 @@ export class SelectedDates implements SelectedDateInterface {
|
||||
return totalScore;
|
||||
}
|
||||
|
||||
optimizeVacations(
|
||||
async optimizeVacations(
|
||||
vacationsNumber: VacationsNumber,
|
||||
calendarSettingsService: CalendarSettingsService,
|
||||
calendarService: CalendarService,
|
||||
): void {
|
||||
progressCallback?: (progress: number) => void,
|
||||
logger?: LoggerService,
|
||||
): Promise<VacationsNumber> {
|
||||
if (calendarSettingsService.get('samediMalin')()) this.samediMalin(vacationsNumber);
|
||||
|
||||
const initialVacations = { ...vacationsNumber };
|
||||
const totalDays = initialVacations.cp + initialVacations.rtt + initialVacations.other;
|
||||
let processedDays = 0;
|
||||
|
||||
// Helper function to update progress, throttled to every 5%
|
||||
let lastReportedProgress = 0;
|
||||
const PROGRESS_STEP = 5; // percent
|
||||
const updateProgress = async () => {
|
||||
if (progressCallback && totalDays > 0) {
|
||||
const progress = Math.round((processedDays / totalDays) * 100);
|
||||
if (progress - lastReportedProgress >= PROGRESS_STEP || progress === 100) {
|
||||
progressCallback(progress);
|
||||
lastReportedProgress = progress;
|
||||
// Allow UI to update with a microtask
|
||||
await new Promise((resolve) => setTimeout(resolve, 0));
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
await updateProgress();
|
||||
|
||||
while (vacationsNumber.cp > 0) {
|
||||
const result = this.lookForVacation(DayType.CP, calendarService);
|
||||
if (result.heuristic === SelectedDates.NO_VALID_STRATEGY_HEURISTIC) {
|
||||
break;
|
||||
}
|
||||
vacationsNumber.cp -= 1;
|
||||
processedDays++;
|
||||
await updateProgress();
|
||||
}
|
||||
while (vacationsNumber.rtt > 0) {
|
||||
const result = this.lookForVacation(DayType.RTT, calendarService);
|
||||
@@ -554,6 +583,8 @@ export class SelectedDates implements SelectedDateInterface {
|
||||
break;
|
||||
}
|
||||
vacationsNumber.rtt -= 1;
|
||||
processedDays++;
|
||||
await updateProgress();
|
||||
}
|
||||
while (vacationsNumber.other > 0) {
|
||||
const result = this.lookForVacation(DayType.OTHER, calendarService);
|
||||
@@ -561,7 +592,25 @@ export class SelectedDates implements SelectedDateInterface {
|
||||
break;
|
||||
}
|
||||
vacationsNumber.other -= 1;
|
||||
processedDays++;
|
||||
await updateProgress();
|
||||
}
|
||||
|
||||
// Final progress update (100%)
|
||||
if (progressCallback) {
|
||||
progressCallback(100);
|
||||
}
|
||||
|
||||
// Inform user if not all days were placed
|
||||
const remainingDays = vacationsNumber.cp + vacationsNumber.rtt + vacationsNumber.other;
|
||||
if (remainingDays > 0 && logger) {
|
||||
logger.warn(
|
||||
`Optimization stopped: ${remainingDays} of ${totalDays} vacation days remain unplaced. ` +
|
||||
`Too many days have equivalent placement quality - please place remaining days manually.`,
|
||||
);
|
||||
}
|
||||
|
||||
return vacationsNumber;
|
||||
}
|
||||
|
||||
samediMalin(vacationsNumber: VacationsNumber): void {
|
||||
@@ -751,6 +800,7 @@ export class SelectedDates implements SelectedDateInterface {
|
||||
const tempDatesBackup = [...this._datesSelected];
|
||||
let bestStrategy: { apply: () => void; heuristic: number; value: Date } | null = null;
|
||||
let bestHeuristic = -Infinity;
|
||||
let daysWithBestHeuristic = 0;
|
||||
|
||||
// Evaluate each candidate day
|
||||
for (const day of candidateDays) {
|
||||
@@ -767,6 +817,7 @@ export class SelectedDates implements SelectedDateInterface {
|
||||
// Track only if better than current best
|
||||
if (heuristic > bestHeuristic) {
|
||||
bestHeuristic = heuristic;
|
||||
daysWithBestHeuristic = 1;
|
||||
const capturedDay = day;
|
||||
bestStrategy = {
|
||||
apply: () => {
|
||||
@@ -781,9 +832,25 @@ export class SelectedDates implements SelectedDateInterface {
|
||||
heuristic: heuristic,
|
||||
value: capturedDay,
|
||||
};
|
||||
} else if (heuristic === bestHeuristic) {
|
||||
daysWithBestHeuristic++;
|
||||
}
|
||||
}
|
||||
|
||||
// Check if too many days have the same best heuristic after evaluating all days
|
||||
if (daysWithBestHeuristic > SelectedDates.MAX_DAYS_WITH_SAME_HEURISTIC) {
|
||||
// Restore backup before stopping
|
||||
this._datesSelected = [...tempDatesBackup];
|
||||
this.datesSelected = this._datesSelected.slice();
|
||||
this.grouping();
|
||||
|
||||
// Stop optimization - let user decide manually
|
||||
return {
|
||||
apply: () => {},
|
||||
heuristic: SelectedDates.NO_VALID_STRATEGY_HEURISTIC,
|
||||
};
|
||||
}
|
||||
|
||||
// Restore backup before returning
|
||||
this._datesSelected = [...tempDatesBackup];
|
||||
this.datesSelected = this._datesSelected.slice();
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
import { TestBed } from '@angular/core/testing';
|
||||
import { LoggerService } from './logger.service';
|
||||
|
||||
describe('LoggerService', () => {
|
||||
let service: LoggerService;
|
||||
let consoleLogSpy: jasmine.Spy;
|
||||
let consoleWarnSpy: jasmine.Spy;
|
||||
let consoleErrorSpy: jasmine.Spy;
|
||||
let consoleDebugSpy: jasmine.Spy;
|
||||
|
||||
beforeEach(() => {
|
||||
TestBed.configureTestingModule({});
|
||||
service = TestBed.inject(LoggerService);
|
||||
|
||||
// Spy on console methods
|
||||
consoleLogSpy = spyOn(console, 'log');
|
||||
consoleWarnSpy = spyOn(console, 'warn');
|
||||
consoleErrorSpy = spyOn(console, 'error');
|
||||
consoleDebugSpy = spyOn(console, 'debug');
|
||||
});
|
||||
|
||||
it('should be created', () => {
|
||||
expect(service).toBeTruthy();
|
||||
});
|
||||
|
||||
it('should log messages', () => {
|
||||
service.log('test message');
|
||||
expect(consoleLogSpy).toHaveBeenCalledWith('test message');
|
||||
});
|
||||
|
||||
it('should log warnings', () => {
|
||||
service.warn('test warning');
|
||||
expect(consoleWarnSpy).toHaveBeenCalledWith('test warning');
|
||||
});
|
||||
|
||||
it('should log errors', () => {
|
||||
service.error('test error');
|
||||
expect(consoleErrorSpy).toHaveBeenCalledWith('test error');
|
||||
});
|
||||
|
||||
it('should log debug messages', () => {
|
||||
service.debug('test debug');
|
||||
expect(consoleDebugSpy).toHaveBeenCalledWith('test debug');
|
||||
});
|
||||
|
||||
it('should pass additional arguments to console methods', () => {
|
||||
const arg1 = { key: 'value' };
|
||||
const arg2 = [1, 2, 3];
|
||||
service.log('test', arg1, arg2);
|
||||
expect(consoleLogSpy).toHaveBeenCalledWith('test', arg1, arg2);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,47 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
|
||||
/**
|
||||
* Logger service that wraps console methods.
|
||||
* Can be configured to disable logging in production environments.
|
||||
*/
|
||||
@Injectable({
|
||||
providedIn: 'root',
|
||||
})
|
||||
export class LoggerService {
|
||||
private isProduction = false; // Can be configured based on environment
|
||||
|
||||
/**
|
||||
* Log an informational message
|
||||
*/
|
||||
log(message: string, ...args: unknown[]): void {
|
||||
if (!this.isProduction) {
|
||||
console.log(message, ...args);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Log a warning message
|
||||
*/
|
||||
warn(message: string, ...args: unknown[]): void {
|
||||
if (!this.isProduction) {
|
||||
console.warn(message, ...args);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Log an error message
|
||||
*/
|
||||
error(message: string, ...args: unknown[]): void {
|
||||
// Always log errors, even in production
|
||||
console.error(message, ...args);
|
||||
}
|
||||
|
||||
/**
|
||||
* Log a debug message
|
||||
*/
|
||||
debug(message: string, ...args: unknown[]): void {
|
||||
if (!this.isProduction) {
|
||||
console.debug(message, ...args);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -18,4 +18,9 @@ export class UserInputService {
|
||||
}
|
||||
|
||||
readonly vacationNumberSignal = signal<VacationsNumber>(this.getInitialValues());
|
||||
readonly remainingVacationDaysSignal = signal<VacationsNumber>({
|
||||
cp: 0,
|
||||
rtt: 0,
|
||||
other: 0,
|
||||
});
|
||||
}
|
||||
Reference in new issue
Block a user