From 3d0f4c98a838492704ec632257aa275eeaf5adcf Mon Sep 17 00:00:00 2001 From: Aydar Farrakhov Date: Mon, 27 Dec 2021 16:01:47 +0300 Subject: [PATCH 1/4] [BEAM-13557] playground show code execution time --- .../modules/editor/components/run_button.dart | 16 +++++++- .../playground/states/playground_state.dart | 41 +++++++++++++++++++ 2 files changed, 56 insertions(+), 1 deletion(-) diff --git a/playground/frontend/lib/modules/editor/components/run_button.dart b/playground/frontend/lib/modules/editor/components/run_button.dart index d946dea7ae35..d7c656d76dd2 100644 --- a/playground/frontend/lib/modules/editor/components/run_button.dart +++ b/playground/frontend/lib/modules/editor/components/run_button.dart @@ -20,8 +20,12 @@ import 'package:flutter/material.dart'; import 'package:playground/constants/sizes.dart'; import 'package:playground/modules/shortcuts/components/shortcut_tooltip.dart'; import 'package:playground/modules/shortcuts/constants/global_shortcuts.dart'; +import 'package:playground/pages/playground/states/playground_state.dart'; +import 'package:provider/provider.dart'; const kRunText = 'Run'; +const kMsToSec = 1000; +const kSecondsFractions = 1; class RunButton extends StatelessWidget { final bool isRunning; @@ -44,7 +48,17 @@ class RunButton extends StatelessWidget { ), ) : const Icon(Icons.play_arrow), - label: const Text(kRunText), + label: StreamBuilder( + stream: Provider.of(context).executionTime, + builder: (context, AsyncSnapshot state) { + final seconds = (state.data ?? 0) / kMsToSec; + if (seconds > 0) { + return Text( + '$kRunText (${seconds.toStringAsFixed(kSecondsFractions)} s)', + ); + } + return const Text(kRunText); + }), onPressed: !isRunning ? runCode : null, ), ); diff --git a/playground/frontend/lib/pages/playground/states/playground_state.dart b/playground/frontend/lib/pages/playground/states/playground_state.dart index 6308958c1298..838cbe0926c3 100644 --- a/playground/frontend/lib/pages/playground/states/playground_state.dart +++ b/playground/frontend/lib/pages/playground/states/playground_state.dart @@ -16,6 +16,7 @@ * limitations under the License. */ +import 'dart:async'; import 'dart:math'; import 'package:flutter/material.dart'; @@ -27,6 +28,7 @@ import 'package:playground/modules/examples/models/example_model.dart'; import 'package:playground/modules/sdk/models/sdk.dart'; const kTitleLength = 15; +const kExecutionTimeUpdate = 100; const kPrecompiledDelay = Duration(seconds: 1); const kTitle = 'Catalog'; const kPipelineOptionsParseError = @@ -40,6 +42,7 @@ class PlaygroundState with ChangeNotifier { RunCodeResult? _result; String _pipelineOptions = ''; DateTime? resetKey; + StreamController? _executionTime; PlaygroundState({ SDK sdk = SDK.java, @@ -70,6 +73,8 @@ class PlaygroundState with ChangeNotifier { String get pipelineOptions => _pipelineOptions; + Stream? get executionTime => _executionTime?.stream; + setExample(ExampleModel example) { _selectedExample = example; _pipelineOptions = example.pipelineOptions ?? ''; @@ -121,6 +126,7 @@ class PlaygroundState with ChangeNotifier { notifyListeners(); return; } + _executionTime = _createExecutionTimeStream(); if (_selectedExample?.source == source && _selectedExample?.outputs != null && !_arePipelineOptionsChanges) { @@ -135,9 +141,11 @@ class PlaygroundState with ChangeNotifier { _result = event; if (event.isFinished && onFinish != null) { onFinish(); + _executionTime?.close(); } notifyListeners(); }); + notifyListeners(); } } @@ -156,6 +164,39 @@ class PlaygroundState with ChangeNotifier { status: RunCodeStatus.finished, output: _selectedExample!.outputs, ); + _executionTime?.close(); notifyListeners(); } + + StreamController _createExecutionTimeStream() { + StreamController? streamController; + Timer? timer; + Duration timerInterval = const Duration(milliseconds: kExecutionTimeUpdate); + int ms = 0; + + void stopTimer() { + if (timer != null) { + timer?.cancel(); + streamController?.close(); + } + } + + void tick(_) { + ms += kExecutionTimeUpdate; + streamController?.add(ms); + } + + void startTimer() { + timer = Timer.periodic(timerInterval, tick); + } + + streamController = StreamController( + onListen: startTimer, + onCancel: stopTimer, + onResume: startTimer, + onPause: stopTimer, + ); + + return streamController; + } } From 21cc30dfcde1393e38967fcb847222d495469141 Mon Sep 17 00:00:00 2001 From: Aydar Farrakhov Date: Mon, 27 Dec 2021 17:56:47 +0300 Subject: [PATCH 2/4] [BEAM-13557] fix playground reset --- .../frontend/lib/pages/playground/states/playground_state.dart | 1 + 1 file changed, 1 insertion(+) diff --git a/playground/frontend/lib/pages/playground/states/playground_state.dart b/playground/frontend/lib/pages/playground/states/playground_state.dart index 838cbe0926c3..b9eaf1dc2e33 100644 --- a/playground/frontend/lib/pages/playground/states/playground_state.dart +++ b/playground/frontend/lib/pages/playground/states/playground_state.dart @@ -101,6 +101,7 @@ class PlaygroundState with ChangeNotifier { _source = _selectedExample?.source ?? ''; _pipelineOptions = selectedExample?.pipelineOptions ?? ''; resetKey = DateTime.now(); + _executionTime = null; notifyListeners(); } From 96e848483938204eb7e1bc4a30e1305a0b2bd0f8 Mon Sep 17 00:00:00 2001 From: Aydar Farrakhov Date: Tue, 28 Dec 2021 16:18:36 +0300 Subject: [PATCH 3/4] [BEAM-13557] playground - fix changing example --- .../frontend/lib/pages/playground/states/playground_state.dart | 1 + 1 file changed, 1 insertion(+) diff --git a/playground/frontend/lib/pages/playground/states/playground_state.dart b/playground/frontend/lib/pages/playground/states/playground_state.dart index b9eaf1dc2e33..8bc596de1f11 100644 --- a/playground/frontend/lib/pages/playground/states/playground_state.dart +++ b/playground/frontend/lib/pages/playground/states/playground_state.dart @@ -80,6 +80,7 @@ class PlaygroundState with ChangeNotifier { _pipelineOptions = example.pipelineOptions ?? ''; _source = example.source ?? ''; _result = null; + _executionTime = null; notifyListeners(); } From 7d24dca6e45abba380058a34c25035becf39af93 Mon Sep 17 00:00:00 2001 From: Aydar Farrakhov Date: Wed, 12 Jan 2022 17:35:47 +0300 Subject: [PATCH 4/4] [BEAM-13557] improve null checks --- .../lib/pages/playground/states/playground_state.dart | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/playground/frontend/lib/pages/playground/states/playground_state.dart b/playground/frontend/lib/pages/playground/states/playground_state.dart index 26263992b865..a481a5186ab2 100644 --- a/playground/frontend/lib/pages/playground/states/playground_state.dart +++ b/playground/frontend/lib/pages/playground/states/playground_state.dart @@ -128,6 +128,7 @@ class PlaygroundState with ChangeNotifier { notifyListeners(); return; } + _executionTime?.close(); _executionTime = _createExecutionTimeStream(); if (_selectedExample?.source == source && _selectedExample?.outputs != null && @@ -178,10 +179,8 @@ class PlaygroundState with ChangeNotifier { int ms = 0; void stopTimer() { - if (timer != null) { - timer?.cancel(); - streamController?.close(); - } + timer?.cancel(); + streamController?.close(); } void tick(_) {