Skip to content

Commit

Permalink
Merge "ui: explore page persistent state/charts." into main
Browse files Browse the repository at this point in the history
  • Loading branch information
Lydia Tse authored and Gerrit Code Review committed Dec 4, 2024
2 parents 20850ce + 49275b0 commit 7cc5422
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 26 deletions.
45 changes: 21 additions & 24 deletions ui/src/plugins/dev.perfetto.ExplorePage/explore_page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ import {
CollapsiblePanelVisibility,
} from '../../components/widgets/collapsible_panel';

interface ExploreTableState {
export interface ExploreTableState {
sqlTableState?: SqlTableState;
selectedTable?: ExplorableTable;
}
Expand All @@ -56,20 +56,18 @@ interface ExplorableTable {
columns: (TableColumn | TableColumnSet)[];
}

export class ExplorePage implements m.ClassComponent<PageWithTraceAttrs> {
private readonly state: ExploreTableState;
private readonly charts: Chart[];
private visibility: CollapsiblePanelVisibility;
interface ExplorePageAttrs extends PageWithTraceAttrs {
readonly state: ExploreTableState;
readonly charts: Chart[];
}

constructor() {
this.charts = [];
this.state = {};
this.visibility = CollapsiblePanelVisibility.VISIBLE;
}
export class ExplorePage implements m.ClassComponent<ExplorePageAttrs> {
private visibility = CollapsiblePanelVisibility.VISIBLE;

// Show menu with standard library tables
private renderSelectableTablesMenuItems(
trace: Trace,
state: ExploreTableState,
): m.Vnode<MenuItemAttrs, unknown>[] {
// TODO (lydiatse@): The following is purely for prototyping and
// should be derived from the actual stdlib itself rather than
Expand Down Expand Up @@ -118,16 +116,13 @@ export class ExplorePage implements m.ClassComponent<PageWithTraceAttrs> {
return m(MenuItem, {
label: table.name,
onclick: () => {
if (
this.state.selectedTable &&
table.name === this.state.selectedTable.name
) {
if (state.selectedTable && table.name === state.selectedTable.name) {
return;
}

this.state.selectedTable = table;
state.selectedTable = table;

this.state.sqlTableState = new SqlTableState(
state.sqlTableState = new SqlTableState(
trace,
{
name: table.name,
Expand All @@ -140,8 +135,8 @@ export class ExplorePage implements m.ClassComponent<PageWithTraceAttrs> {
});
}

private renderSqlTable() {
const sqlTableState = this.state.sqlTableState;
private renderSqlTable(state: ExploreTableState, charts: Chart[]) {
const sqlTableState = state.sqlTableState;

if (sqlTableState === undefined) return;

Expand Down Expand Up @@ -181,27 +176,29 @@ export class ExplorePage implements m.ClassComponent<PageWithTraceAttrs> {
sqlTableState,
),
chartOptions: [ChartOption.HISTOGRAM],
addChart: (chart) => this.charts.push(chart),
addChart: (chart) => charts.push(chart),
}),
}),
);
}

view({attrs}: m.CVnode<PageWithTraceAttrs>) {
view({attrs}: m.CVnode<ExplorePageAttrs>) {
const {trace, state, charts} = attrs;

return m(
'.page.explore-page',
m(
'.chart-container',
m(Menu, this.renderSelectableTablesMenuItems(attrs.trace)),
this.charts.map((chart) => renderChartComponent(chart)),
m(Menu, this.renderSelectableTablesMenuItems(trace, state)),
charts.map((chart) => renderChartComponent(chart)),
),
this.state.selectedTable &&
state.selectedTable &&
m(CollapsiblePanel, {
visibility: this.visibility,
setVisibility: (visibility) => {
this.visibility = visibility;
},
tabs: [this.renderSqlTable()],
tabs: [this.renderSqlTable(state, charts)],
}),
);
}
Expand Down
18 changes: 16 additions & 2 deletions ui/src/plugins/dev.perfetto.ExplorePage/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,29 @@
// See the License for the specific language governing permissions and
// limitations under the License.

import m from 'mithril';
import {PerfettoPlugin} from '../../public/plugin';
import {Trace} from '../../public/trace';
import {ExplorePage} from './explore_page';
import {ExplorePage, ExploreTableState} from './explore_page';
import {Chart} from '../../components/widgets/charts/chart';

export default class implements PerfettoPlugin {
static readonly id = 'dev.perfetto.ExplorePage';

// The following allows us to have persistent
// state/charts for the lifecycle of a single
// trace.
private readonly state: ExploreTableState = {};
private readonly charts: Chart[] = [];

async onTraceLoad(trace: Trace): Promise<void> {
trace.pages.registerPage({route: '/explore', page: ExplorePage});
trace.pages.registerPage({
route: '/explore',
page: {
view: ({attrs}) =>
m(ExplorePage, {...attrs, state: this.state, charts: this.charts}),
},
});
trace.sidebar.addMenuItem({
section: 'current_trace',
text: 'Explore',
Expand Down

0 comments on commit 7cc5422

Please sign in to comment.