1// Copyright (C) 2021 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 {createEmptyRecordConfig} from '../controller/record_config_types'; 16import { 17 Aggregation, 18} from '../frontend/pivot_table_types'; 19import { 20 autosaveConfigStore, 21 recordTargetStore, 22} from '../frontend/record_config'; 23 24import {featureFlags} from './feature_flags'; 25import { 26 defaultTraceTime, 27 NonSerializableState, 28 State, 29 STATE_VERSION, 30} from './state'; 31 32const AUTOLOAD_STARTED_CONFIG_FLAG = featureFlags.register({ 33 id: 'autoloadStartedConfig', 34 name: 'Auto-load last used recording config', 35 description: 'Starting a recording automatically saves its configuration. ' + 36 'This flag controls whether this config is automatically loaded.', 37 defaultValue: true, 38}); 39 40export function keyedMap<T>( 41 keyFn: (key: T) => string, ...values: T[]): Map<string, T> { 42 const result = new Map<string, T>(); 43 44 for (const value of values) { 45 result.set(keyFn(value), value); 46 } 47 48 return result; 49} 50 51export const COUNT_AGGREGATION: Aggregation = { 52 aggregationFunction: 'COUNT', 53 // Exact column is ignored for count aggregation because it does not matter 54 // what to count, use empty strings. 55 column: {kind: 'regular', table: '', column: ''}, 56}; 57 58export function createEmptyNonSerializableState(): NonSerializableState { 59 return { 60 pivotTable: { 61 queryResult: null, 62 selectedPivots: [{kind: 'regular', table: 'slice', column: 'name'}], 63 selectedAggregations: [ 64 { 65 aggregationFunction: 'SUM', 66 column: {kind: 'regular', table: 'slice', column: 'dur'}, 67 sortDirection: 'DESC', 68 }, 69 { 70 aggregationFunction: 'SUM', 71 column: {kind: 'regular', table: 'slice', column: 'thread_dur'}, 72 }, 73 COUNT_AGGREGATION, 74 ], 75 constrainToArea: true, 76 queryRequested: false, 77 argumentNames: [], 78 }, 79 }; 80} 81 82export function createEmptyState(): State { 83 return { 84 version: STATE_VERSION, 85 nextId: '-1', 86 newEngineMode: 'USE_HTTP_RPC_IF_AVAILABLE', 87 traceTime: {...defaultTraceTime}, 88 tracks: {}, 89 uiTrackIdByTraceTrackId: {}, 90 utidToThreadSortKey: {}, 91 aggregatePreferences: {}, 92 trackGroups: {}, 93 visibleTracks: [], 94 pinnedTracks: [], 95 scrollingTracks: [], 96 areas: {}, 97 queries: {}, 98 metrics: {}, 99 permalink: {}, 100 notes: {}, 101 visualisedArgs: [], 102 103 recordConfig: AUTOLOAD_STARTED_CONFIG_FLAG.get() ? 104 autosaveConfigStore.get() : 105 createEmptyRecordConfig(), 106 displayConfigAsPbtxt: false, 107 lastLoadedConfig: {type: 'NONE'}, 108 109 frontendLocalState: { 110 visibleState: { 111 ...defaultTraceTime, 112 lastUpdate: 0, 113 resolution: 0n, 114 }, 115 }, 116 117 omniboxState: { 118 omnibox: '', 119 mode: 'SEARCH', 120 }, 121 122 logsPagination: { 123 offset: 0, 124 count: 0, 125 }, 126 127 ftracePagination: { 128 offset: 0, 129 count: 0, 130 }, 131 132 ftraceFilter: { 133 excludedNames: [], 134 }, 135 136 status: {msg: '', timestamp: 0}, 137 currentSelection: null, 138 currentFlamegraphState: null, 139 traceConversionInProgress: false, 140 141 perfDebug: false, 142 sidebarVisible: true, 143 hoveredUtid: -1, 144 hoveredPid: -1, 145 hoverCursorTimestamp: -1n, 146 hoveredNoteTimestamp: -1n, 147 highlightedSliceId: -1, 148 focusedFlowIdLeft: -1, 149 focusedFlowIdRight: -1, 150 searchIndex: -1, 151 152 recordingInProgress: false, 153 recordingCancelled: false, 154 extensionInstalled: false, 155 flamegraphModalDismissed: false, 156 recordingTarget: recordTargetStore.getValidTarget(), 157 availableAdbDevices: [], 158 159 fetchChromeCategories: false, 160 chromeCategories: undefined, 161 nonSerializableState: createEmptyNonSerializableState(), 162 163 logFilteringCriteria: { 164 // The first two log priorities are ignored. 165 minimumLevel: 2, 166 tags: [], 167 textEntry: '', 168 hideNonMatching: true, 169 }, 170 }; 171} 172