diff --git a/src/app/components/layout/layout.component.ts b/src/app/components/layout/layout.component.ts index 0aab866..4b1f993 100644 --- a/src/app/components/layout/layout.component.ts +++ b/src/app/components/layout/layout.component.ts @@ -75,10 +75,10 @@ export class LayoutComponent implements OnInit { selectedDatasource: number | undefined; onDatasourceChange(event: any) { - this.storageService.getDatasource(event.value).then((dataset: Datasource) => { + this.storageService.getDatasource(event.value).then(async (dataset: Datasource) => { this.toastr.success('Selected datasource: ' + dataset.name); this.selectedDatasource = dataset.id!; - this.storageService.saveAppSettings({selectedDatasourceId: dataset.id!}); + await this.storageService.saveAppSettings({selectedDatasourceId: dataset.id!}); }); } @@ -86,13 +86,9 @@ export class LayoutComponent implements OnInit { this.refreshDatasources(); } - refreshDatasources(): void { - this.storageService.getAllDatasources().then((datasource: Datasource[]) => { - this.datasources = datasource; - if (this.datasources.length > 0) { - this.selectedDatasource = this.datasources[0].id!; - } - }); + async refreshDatasources() { + this.datasources = await this.storageService.getAllDatasources(); + this.selectedDatasource = (await this.storageService.getAppSettings()).selectedDatasourceId; } async login() { diff --git a/src/app/components/monte-carlo/monte-carlo-page.component.html b/src/app/components/monte-carlo/monte-carlo-page.component.html index 591a644..7bfba60 100644 --- a/src/app/components/monte-carlo/monte-carlo-page.component.html +++ b/src/app/components/monte-carlo/monte-carlo-page.component.html @@ -1,5 +1,20 @@

Monte Carlo Simulation Results

+

Steps

+ = { labels: [], datasets: [ @@ -30,10 +34,17 @@ export class MonteCarloPageComponent implements OnInit { responsive: true, scales: { x: { - + title: { + display: true, + text: 'Sum Value' + } }, y: { - beginAtZero: true + beginAtZero: true, + title: { + display: true, + text: 'Occurrences' + } } } }; @@ -42,39 +53,53 @@ export class MonteCarloPageComponent implements OnInit { async ngOnInit(): Promise { const simulationResult = await this.runSimulation(1000); - this.results = simulationResult.results; - console.log(`Counts for each sum: ${simulationResult.counts}`); + this.results = Array.from(simulationResult.keys()).sort((a, b) => a - b); + this.counts = this.results.map(result => simulationResult.get(result) || 0); this.updateChartData(); } updateChartData(): void { + // Update chart data this.chartData.labels = this.results.map((_, index) => index.toString()); - this.chartData.datasets[0].data = this.results; + this.chartData.datasets[0].data = this.counts; + this.chart?.update(); } - private async runSimulation(number: number): Promise<{ results: number[], counts: number[] }> { + private async runSimulation(number: number): Promise> { // Step 1: Read the throughputs from the last 20 days const throughputs = await this.getThroughputsFromLast20Days(); const results: number[] = []; - const counts: number[] = new Array(number).fill(0); + const sumFrequency: Map = new Map(); + for (let i = 0; i < number; i++) { // Step 2: Select 14 random values and sum them const sum = this.getRandomSum(throughputs, 14); // Step 3: Save the sum and increment the counter results.push(sum); - counts[sum] = (counts[sum] || 0) + 1; + sumFrequency.set(sum, (sumFrequency.get(sum) || 0) + 1); } + // Step 4: Return the results and the counts - return { results, counts }; + return sumFrequency; } private async getThroughputsFromLast20Days(): Promise { const throughputEntries = await this.storageService.getThroughputData(); - const last20DaysThroughputs = throughputEntries - .sort((a, b) => b.date.getTime() - a.date.getTime()) - .slice(0, 20) - .map(entry => entry.throughput); + const today = new Date(); + const last20DaysThroughputs: number[] = []; + + for (let i = 0; i < 20; i++) { + const date = new Date(today); + date.setDate(today.getDate() - i); + const entry = throughputEntries.find(entry => + entry.date.getFullYear() === date.getFullYear() && + entry.date.getMonth() === date.getMonth() && + entry.date.getDate() === date.getDate() + ); + last20DaysThroughputs.push(entry ? entry.throughput : 0); + } + return last20DaysThroughputs; } diff --git a/src/app/services/storage.service.ts b/src/app/services/storage.service.ts index c076083..4ce3dea 100644 --- a/src/app/services/storage.service.ts +++ b/src/app/services/storage.service.ts @@ -210,7 +210,7 @@ export class StorageService { async getAppSettings(): Promise { this.dbService.selectDb(dbConfigCore.name); - return firstValueFrom(this.dbService.getAll(TableNames.APP_SETTINGS)).then(datasets => datasets[0]); + return firstValueFrom(this.dbService.getAll(TableNames.APP_SETTINGS)).then(settings => settings[0]); } async deleteIssueDatabase() {