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 * as m from 'mithril'; 16 17import {Actions} from '../common/actions'; 18import {globals} from './globals'; 19import {createPage} from './pages'; 20 21function getCurrSelectedMetric() { 22 const {availableMetrics, selectedIndex} = globals.state.metrics; 23 if (!availableMetrics) return undefined; 24 if (selectedIndex === undefined) return undefined; 25 return availableMetrics[selectedIndex]; 26} 27 28class MetricResult implements m.ClassComponent { 29 view() { 30 const metricResult = globals.metricResult; 31 if (metricResult === undefined) return undefined; 32 const currSelection = getCurrSelectedMetric(); 33 if (!(metricResult && metricResult.name === currSelection)) { 34 return undefined; 35 } 36 if (metricResult.error !== undefined) { 37 return m('pre.metric-error', metricResult.error); 38 } 39 if (metricResult.resultString !== undefined) { 40 return m('pre', metricResult.resultString); 41 } 42 return undefined; 43 } 44} 45 46class MetricPicker implements m.ClassComponent { 47 view() { 48 const {availableMetrics, selectedIndex} = globals.state.metrics; 49 if (availableMetrics === undefined) return 'Loading metrics...'; 50 if (availableMetrics.length === 0) return 'No metrics available'; 51 if (selectedIndex === undefined) { 52 throw Error('Should not happen when avaibleMetrics is non-empty'); 53 } 54 55 return m('div', [ 56 'Select a metric:', 57 m('select', 58 { 59 selectedIndex: globals.state.metrics.selectedIndex, 60 onchange: (e: InputEvent) => { 61 globals.dispatch(Actions.setMetricSelectedIndex( 62 {index: (e.target as HTMLSelectElement).selectedIndex})); 63 }, 64 }, 65 availableMetrics.map( 66 metric => m('option', {value: metric, key: metric}, metric))), 67 m('button.metric-run-button', 68 {onclick: () => globals.dispatch(Actions.requestSelectedMetric({}))}, 69 'Run'), 70 ]); 71 } 72} 73 74export const MetricsPage = createPage({ 75 view() { 76 return m( 77 '.metrics-page', 78 m(MetricPicker), 79 m(MetricResult), 80 ); 81 } 82}); 83