1// Copyright (C) 2020 The Android Open Source Project 2// 3// Licensed under the Apache License, Version 2.0 (the "License"); 4// you may not use this file except in compliance with the License. 5// You may obtain a copy of the License at 6// 7// http://www.apache.org/licenses/LICENSE-2.0 8// 9// Unless required by applicable law or agreed to in writing, software 10// distributed under the License is distributed on an "AS IS" BASIS, 11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12// See the License for the specific language governing permissions and 13// limitations under the License. 14 15import m from 'mithril'; 16 17import {Actions} from '../common/actions'; 18import {globals} from './globals'; 19import {createPage} from './pages'; 20import {Button} from './widgets/button'; 21 22function getCurrSelectedMetric() { 23 const {availableMetrics, selectedIndex} = globals.state.metrics; 24 if (!availableMetrics) return undefined; 25 if (selectedIndex === undefined) return undefined; 26 return availableMetrics[selectedIndex]; 27} 28 29class MetricResult implements m.ClassComponent { 30 view() { 31 const metricResult = globals.metricResult; 32 if (metricResult === undefined) return undefined; 33 const currSelection = getCurrSelectedMetric(); 34 if (!(metricResult && metricResult.name === currSelection)) { 35 return undefined; 36 } 37 if (metricResult.error !== undefined) { 38 return m('pre.metric-error', metricResult.error); 39 } 40 if (metricResult.resultString !== undefined) { 41 return m('pre', metricResult.resultString); 42 } 43 return undefined; 44 } 45} 46 47class MetricPicker implements m.ClassComponent { 48 view() { 49 const {availableMetrics, selectedIndex} = globals.state.metrics; 50 if (availableMetrics === undefined) return 'Loading metrics...'; 51 if (availableMetrics.length === 0) return 'No metrics available'; 52 if (selectedIndex === undefined) { 53 throw Error('Should not happen when avaibleMetrics is non-empty'); 54 } 55 56 return m('div', [ 57 'Select a metric:', 58 m('select', 59 { 60 selectedIndex: globals.state.metrics.selectedIndex, 61 onchange: (e: InputEvent) => { 62 globals.dispatch(Actions.setMetricSelectedIndex( 63 {index: (e.target as HTMLSelectElement).selectedIndex})); 64 }, 65 }, 66 availableMetrics.map( 67 (metric) => m('option', {value: metric, key: metric}, metric))), 68 m(Button, { 69 onclick: () => globals.dispatch(Actions.requestSelectedMetric({})), 70 label: 'Run', 71 }), 72 ]); 73 } 74} 75 76export const MetricsPage = createPage({ 77 view() { 78 return m( 79 '.metrics-page', 80 m(MetricPicker), 81 m(MetricResult), 82 ); 83 }, 84}); 85