-
Notifications
You must be signed in to change notification settings - Fork 100
New benchmark page #64
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| // Copyright (c) 2016 The Chromium Authors. All rights reserved. | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| import 'dart:async'; | ||
| import 'dart:convert' show JSON; | ||
| import 'dart:html'; | ||
| import 'dart:math' as math; | ||
|
|
||
| import 'package:angular2/angular2.dart'; | ||
| import 'package:cocoon/model.dart'; | ||
| import 'package:http/http.dart' as http; | ||
|
|
||
| @Component( | ||
| selector: 'benchmark-grid', | ||
| template: r''' | ||
| <div *ngIf="isLoading">Loading...</div> | ||
| <div *ngIf="!isLoading" class="card-container"> | ||
| <benchmark-card | ||
| *ngFor="let benchmark of benchmarks" | ||
| [data]="benchmark"> | ||
| </benchmark-card> | ||
| </div> | ||
| ''', | ||
| directives: const [NgIf, NgFor, NgClass, BenchmarkCard], | ||
| ) | ||
| class BenchmarkGrid implements OnInit, OnDestroy { | ||
| BenchmarkGrid(this._httpClient); | ||
|
|
||
| final http.Client _httpClient; | ||
| bool isLoading = true; | ||
| List<BenchmarkData> benchmarks; | ||
| Timer _reloadTimer; | ||
|
|
||
| @override | ||
| void ngOnInit() { | ||
| reloadData(); | ||
| _reloadTimer = new Timer.periodic(const Duration(seconds: 30), (_) => reloadData()); | ||
| } | ||
|
|
||
| @override | ||
| void ngOnDestroy() { | ||
| _reloadTimer?.cancel(); | ||
| } | ||
|
|
||
| Future<Null> reloadData() async { | ||
| isLoading = true; | ||
| Map<String, dynamic> statusJson = JSON.decode((await _httpClient.get('/api/get-benchmarks')).body); | ||
| GetBenchmarksResult result = GetBenchmarksResult.fromJson(statusJson); | ||
| benchmarks = result.benchmarks; | ||
| isLoading = false; | ||
| } | ||
| } | ||
|
|
||
| @Component( | ||
| selector: 'benchmark-card', | ||
| template: r''' | ||
| <div class="metric" *ngIf="latestValue != null"> | ||
| <span class="metric-value">{{latestValue}}</span> | ||
| <span class="metric-unit">{{unit}}</span> | ||
| </div> | ||
| <div class="metric-label">{{label}}</div> | ||
| <div #chartContainer></div> | ||
| ''', | ||
| directives: const [NgIf, NgFor, NgStyle], | ||
| ) | ||
| class BenchmarkCard implements AfterViewInit { | ||
| BenchmarkData _data; | ||
|
|
||
| @ViewChild('chartContainer') ElementRef chartContainer; | ||
|
|
||
| @Input() set data(BenchmarkData newData) { | ||
| chartContainer.nativeElement.children.clear(); | ||
| _data = newData; | ||
| } | ||
|
|
||
| String get id => _data.timeseries.timeseries.id; | ||
| String get label => _data.timeseries.timeseries.label; | ||
| String get unit => _data.timeseries.timeseries.unit; | ||
| String get latestValue { | ||
| if (_data.values.isEmpty) return null; | ||
| num value = _data.values.first.value; | ||
| if (value > 100) { | ||
| // Ignore fractions in large values. | ||
| value = value.round(); | ||
| } | ||
| if (value < 100000) { | ||
| return value.toString(); | ||
| } else { | ||
| // The value is too big to fit on the card; switch to thousands. | ||
| return '${value ~/ 1000}K'; | ||
| } | ||
| } | ||
|
|
||
| @override | ||
| void ngAfterViewInit() { | ||
| if (_data.values.isEmpty) return; | ||
| double maxValue = _data.values | ||
| .map((TimeseriesValue v) => v.value) | ||
| .reduce(math.max); | ||
|
|
||
| for (TimeseriesValue value in _data.values.reversed) { | ||
| DivElement bar = new DivElement() | ||
| ..classes.add('metric-value-bar') | ||
| ..style.height = '${80 * value.value / maxValue}px'; | ||
|
|
||
| DivElement tooltip; | ||
| bar.onMouseOver.listen((_) { | ||
| tooltip = new DivElement() | ||
| ..text = '${value.value}$unit\n' | ||
| 'Flutter revision: ${value.revision}\n' | ||
| 'Recorded on: ${new DateTime.fromMillisecondsSinceEpoch(value.createTimestamp)}' | ||
| ..classes.add('metric-value-tooltip') | ||
| ..style.top = '${bar.getBoundingClientRect().top}px' | ||
| ..style.left = '${bar.getBoundingClientRect().right + 5}px'; | ||
| bar.style.backgroundColor = '#11CC11'; | ||
| document.body.append(tooltip); | ||
| }); | ||
| bar.onMouseOut.listen((_) { | ||
| bar.style.backgroundColor = '#AAFFAA'; | ||
| tooltip?.remove(); | ||
| }); | ||
|
|
||
| chartContainer.nativeElement.children.add(bar); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| // Copyright (c) 2016 The Chromium Authors. All rights reserved. | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| import 'dart:async'; | ||
| import 'dart:convert'; | ||
| import 'dart:html'; | ||
|
|
||
| import 'package:angular2/core.dart'; | ||
| import 'package:http/browser_client.dart' as browser_http; | ||
| import 'package:http/http.dart' as http; | ||
|
|
||
| Future<http.Client> getAuthenticatedClientOrRedirectToSignIn() async { | ||
| http.Client client = new browser_http.BrowserClient(); | ||
| Map<String, dynamic> status = JSON.decode((await client.get('/api/get-authentication-status')).body); | ||
|
|
||
| document.querySelector('#logout-button').on['click'].listen((_) { | ||
| window.open(status['LogoutURL'], '_self'); | ||
| }); | ||
|
|
||
| document.querySelector('#login-button').on['click'].listen((_) { | ||
| window.open(status['LoginURL'], '_self'); | ||
| }); | ||
|
|
||
| if (status['Status'] == 'OK') | ||
| return client; | ||
|
|
||
| document.body.append(new DivElement() | ||
| ..text = 'You are not signed in, or signed in under an unauthorized account. ' | ||
| 'Use the buttons at the bottom of this page to sign in.'); | ||
| return null; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| // Copyright (c) 2016 The Chromium Authors. All rights reserved. | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
| @TestOn('dartium') | ||
|
|
||
| import 'dart:async'; | ||
| import 'dart:convert' show JSON; | ||
| import 'dart:html'; | ||
| import 'dart:math' as math; | ||
|
|
||
| import 'package:angular2/angular2.dart'; | ||
| import 'package:angular2/platform/browser.dart'; | ||
| import 'package:http/http.dart' as http; | ||
| import 'package:http/testing.dart'; | ||
| import 'package:test/test.dart'; | ||
|
|
||
| import 'package:cocoon/components/benchmark_grid.dart'; | ||
|
|
||
| void main() { | ||
| group('BenchmarkGrid', () { | ||
| setUp(() { | ||
| document.body.append(new Element.tag('benchmark-grid')); | ||
| }); | ||
|
|
||
| tearDown(() { | ||
| for (Node node in document.body.querySelectorAll('benchmark-grid')) { | ||
| node.remove(); | ||
| } | ||
| }); | ||
|
|
||
| test('should show a grid', () async { | ||
| List<String> httpCalls = <String>[]; | ||
|
|
||
| await bootstrap(BenchmarkGrid, [ | ||
| provide(http.Client, useValue: new MockClient((http.Request request) async { | ||
| httpCalls.add(request.url.path); | ||
| return new http.Response( | ||
| JSON.encode(_testData), | ||
| 200, | ||
| headers: {'content-type': 'application/json'}); | ||
| })), | ||
| ]); | ||
|
|
||
| // Flush microtasks to allow Angular do its thing. | ||
| await new Future.delayed(Duration.ZERO); | ||
|
|
||
| expect(httpCalls, <String>['/api/get-benchmarks']); | ||
| expect(document.querySelectorAll('benchmark-card'), | ||
| hasLength(_testData['Benchmarks'].length)); | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| final math.Random _rnd = new math.Random(1234); | ||
|
|
||
| final _testData = { | ||
| 'Benchmarks': new List.generate(5, (int i) => { | ||
| 'Timeseries': { | ||
| 'Key': 'key$i', | ||
| 'Timeseries': { | ||
| 'ID': 'series$i', | ||
| 'Label': 'Series $i', | ||
| 'Unit': 'ms', | ||
| }, | ||
| }, | ||
| 'Values': new List.generate(_rnd.nextInt(5), (int v) => { | ||
| 'CreateTimestamp': 1234567890000 - v, | ||
| 'Revision': 'rev$v', | ||
| 'Value': (50 + _rnd.nextInt(50)).toDouble(), | ||
| }), | ||
| }), | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Question: is Flutter style to push ctors above fields?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seen a lot of them above fields, e.g.:
https://github.com/flutter/flutter/blob/master/packages/flutter/lib/src/foundation/basic_types.dart#L64
So I'm guessing it's at least not against the style guide :)