From bf03dd8739b64fce56981189a208a222eadb1414 Mon Sep 17 00:00:00 2001 From: hunteraraujo Date: Mon, 18 Sep 2023 19:55:01 -0700 Subject: [PATCH] Refactor runBenchmark in SkillTreeViewModel for New Report Generation Flow This commit updates the runBenchmark method in the SkillTreeViewModel class to align with the new report generation flow. The updated method does the following: 1. Checks if a benchmark is already running to prevent overlapping runs. 2. Sets a flag to indicate that the benchmark is running and notifies the UI. 3. Reverses the selected node hierarchy for report generation. 4. Loops through each node in the reversed hierarchy to: - Generate a unique UUID for each test run. - Create a ReportRequestBody object. - Call the generateSingleReport method in the BenchmarkService. - Update the UI after each single report is generated. 5. After all single reports are generated, it calls the generateCombinedReport method in the BenchmarkService, passing in all the generated UUIDs. 6. Finally, it sets the benchmark running flag to false and notifies the UI. This change improves the report generation flow and allows for both individual and combined reports. --- .../lib/viewmodels/skill_tree_viewmodel.dart | 59 ++++++++++++++----- .../lib/views/task_queue/task_queue_view.dart | 9 +-- 2 files changed, 44 insertions(+), 24 deletions(-) diff --git a/frontend/lib/viewmodels/skill_tree_viewmodel.dart b/frontend/lib/viewmodels/skill_tree_viewmodel.dart index 727e1e64..c137c2f9 100644 --- a/frontend/lib/viewmodels/skill_tree_viewmodel.dart +++ b/frontend/lib/viewmodels/skill_tree_viewmodel.dart @@ -7,6 +7,7 @@ import 'package:collection/collection.dart'; import 'package:flutter/foundation.dart'; import 'package:flutter/services.dart'; import 'package:graphview/GraphView.dart'; +import 'package:uuid/uuid.dart'; class SkillTreeViewModel extends ChangeNotifier { // TODO: Potentially move to task queue view model when we create one @@ -130,34 +131,60 @@ class SkillTreeViewModel extends ChangeNotifier { } } -// TODO: Update to actual implementation - Future runBenchmark(ReportRequestBody reportRequestBody) async { + // TODO: Update to actual implementation + Future runBenchmark() async { + // Set the benchmark running flag to true isBenchmarkRunning = true; notifyListeners(); + // Initialize an empty list to collect unique UUIDs for test runs + List testRunIds = []; + try { - final result = await benchmarkService.generateReport(reportRequestBody); + // Reverse the selected node hierarchy + final reversedSelectedNodeHierarchy = + List.from(_selectedNodeHierarchy!.reversed); + + // Loop through the reversed node hierarchy to generate reports for each node + for (var node in reversedSelectedNodeHierarchy) { + // Generate a unique UUID for the test run + final uuid = const Uuid().v4(); + + // Create a ReportRequestBody object + final reportRequestBody = ReportRequestBody( + test: node.data.name, testRunId: uuid, mock: true); + + // Call generateSingleReport with the created ReportRequestBody object + final singleReport = + await benchmarkService.generateSingleReport(reportRequestBody); + print("Single report generated: $singleReport"); + + // Add the unique UUID to the list + // TODO: We should check if the test passed. If not we short circuit. + // TODO: We should create a model to track our active tests + testRunIds.add(uuid); + + // Notify the UI + notifyListeners(); + } + + // Generate a combined report using all the unique UUIDs + final combinedReport = + await benchmarkService.generateCombinedReport(testRunIds); + // Pretty-print the JSON result - String prettyResult = JsonEncoder.withIndent(' ').convert(result); - print("Report generated: $prettyResult"); + String prettyResult = + JsonEncoder.withIndent(' ').convert(combinedReport); + print("Combined report generated: $prettyResult"); } catch (e) { - print("Failed to generate report: $e"); + print("Failed to generate reports: $e"); } + // Set the benchmark running flag to false isBenchmarkRunning = false; notifyListeners(); } -// TODO: Update to actual implementation - Future requestBenchmarkStatusUpdate(int lastUpdateTime) async { - try { - final result = await benchmarkService.pollUpdates(lastUpdateTime); - print("Updates polled: $result"); - } catch (e) { - print("Failed to poll updates: $e"); - } - } - // Getter to expose nodes for the View List get skillTreeNodes => _skillTreeNodes; diff --git a/frontend/lib/views/task_queue/task_queue_view.dart b/frontend/lib/views/task_queue/task_queue_view.dart index 20a98499..edc79cfe 100644 --- a/frontend/lib/views/task_queue/task_queue_view.dart +++ b/frontend/lib/views/task_queue/task_queue_view.dart @@ -56,15 +56,8 @@ class TaskQueueView extends StatelessWidget { onPressed: viewModel.isBenchmarkRunning ? null : () { - // Create a ReportRequestBody with hardcoded values - ReportRequestBody reportRequestBody = ReportRequestBody( - category: "", - tests: testNames, - mock: true, - ); - // Call runBenchmark method from SkillTreeViewModel - viewModel.runBenchmark(reportRequestBody); + viewModel.runBenchmark(); }, child: Row( mainAxisAlignment: