1// Copyright (C) 2018 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 {assertExists} from '../base/logging'; 16import {Actions} from '../common/actions'; 17import {Engine} from '../common/engine'; 18import {runQuery} from '../common/queries'; 19import {publishQueryResult} from '../frontend/publish'; 20 21import {Controller} from './controller'; 22import {globals} from './globals'; 23 24export interface QueryControllerArgs { 25 queryId: string; 26 engine: Engine; 27} 28 29export class QueryController extends Controller<'init'|'querying'> { 30 constructor(private args: QueryControllerArgs) { 31 super('init'); 32 } 33 34 run() { 35 switch (this.state) { 36 case 'init': 37 const config = assertExists(globals.state.queries[this.args.queryId]); 38 runQuery(this.args.queryId, config.query, this.args.engine) 39 .then(result => { 40 console.log(`Query ${config.query} took ${result.durationMs} ms`); 41 publishQueryResult({id: this.args.queryId, data: result}); 42 globals.dispatch( 43 Actions.deleteQuery({queryId: this.args.queryId})); 44 }); 45 this.setState('querying'); 46 break; 47 48 case 'querying': 49 // Nothing to do here, as soon as the deleteQuery is dispatched this 50 // controller will be destroyed (by the TraceController). 51 break; 52 53 default: 54 throw new Error(`Unexpected state ${this.state}`); 55 } 56 } 57} 58