Skip to content

Commit

Permalink
Merge pull request #36 from MLH-Fellowship/migration-to-v2-data
Browse files Browse the repository at this point in the history
Migration to V2 data
  • Loading branch information
kartikcho committed Jul 2, 2020
2 parents b5c3dfb + 9876341 commit 6b25ecd
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 7 deletions.
33 changes: 30 additions & 3 deletions src/App.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
// @flow

import type {FlamechartData, ReactProfilerData} from './types';
import type {
FlamechartData,
ReactProfilerData,
ReactProfilerDataV2,
} from './types';

import React, {useState, useCallback} from 'react';
import {unstable_batchedUpdates} from 'react-dom';
Expand All @@ -14,6 +18,10 @@ export default function App() {
const [profilerData, setProfilerData] = useState<ReactProfilerData | null>(
null,
);
const [
profilerDataV2,
setProfilerDataV2,
] = useState<ReactProfilerDataV2 | null>(null);
const [flamechart, setFlamechart] = useState<FlamechartData | null>(null);
const [schedulerCanvasHeight, setSchedulerCanvasHeight] = useState<number>(0);

Expand All @@ -34,15 +42,34 @@ export default function App() {
},
);

if (profilerData && flamechart) {
// TODO: Migrate and completely remove V2 stuff
const handleDataImportedV2 = useCallback(
(
importedProfilerData: ReactProfilerDataV2,
importedFlamechart: FlamechartData,
) => {
unstable_batchedUpdates(() => {
setProfilerDataV2(importedProfilerData);
setFlamechart(importedFlamechart);
});
},
);

if (profilerData && profilerDataV2 && flamechart) {
return (
<CanvasPage
profilerData={profilerData}
profilerDataV2={profilerDataV2}
flamechart={flamechart}
schedulerCanvasHeight={schedulerCanvasHeight}
/>
);
} else {
return <ImportPage onDataImported={handleDataImported} />;
return (
<ImportPage
onDataImported={handleDataImported}
onDataImportedV2={handleDataImportedV2}
/>
);
}
}
14 changes: 12 additions & 2 deletions src/CanvasPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import type {
ReactHoverContextInfo,
ReactMeasure,
ReactProfilerData,
ReactProfilerDataV2,
} from './types';

type ContextMenuContextData = {|
Expand All @@ -41,11 +42,17 @@ type ContextMenuContextData = {|

type Props = {|
profilerData: ReactProfilerData,
profilerDataV2: ReactProfilerDataV2,
flamechart: FlamechartData,
schedulerCanvasHeight: number,
|};

function CanvasPage({profilerData, flamechart, schedulerCanvasHeight}: Props) {
function CanvasPage({
profilerData,
profilerDataV2,
flamechart,
schedulerCanvasHeight,
}: Props) {
return (
<div
className={styles.CanvasPage}
Expand All @@ -54,6 +61,7 @@ function CanvasPage({profilerData, flamechart, schedulerCanvasHeight}: Props) {
{({height, width}: {height: number, width: number}) => (
<AutoSizedCanvas
data={profilerData}
dataV2={profilerDataV2}
flamechart={flamechart}
height={height}
schedulerCanvasHeight={schedulerCanvasHeight}
Expand Down Expand Up @@ -96,6 +104,7 @@ const zoomToBatch = (

type AutoSizedCanvasProps = {|
data: ReactProfilerData,
dataV2: ReactProfilerDataV2,
flamechart: FlamechartData,
height: number,
schedulerCanvasHeight: number,
Expand All @@ -104,6 +113,7 @@ type AutoSizedCanvasProps = {|

function AutoSizedCanvas({
data,
dataV2,
flamechart,
height,
schedulerCanvasHeight,
Expand All @@ -117,7 +127,7 @@ function AutoSizedCanvas({
canvasWidth: width,
fixedColumnWidth: LABEL_FIXED_WIDTH,
fixedHeaderHeight: HEADER_HEIGHT_FIXED,
unscaledContentWidth: data.duration,
unscaledContentWidth: dataV2.duration,
unscaledContentHeight:
schedulerCanvasHeight +
flamechart.layers.length * FLAMECHART_FRAME_HEIGHT,
Expand Down
31 changes: 29 additions & 2 deletions src/ImportPage.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,34 @@
// @flow

import type {TimelineEvent} from './speedscope/import/chrome';
import type {FlamechartData, ReactProfilerData} from './types';
import type {
FlamechartData,
ReactProfilerData,
ReactProfilerDataV2,
} from './types';

import React, {useEffect} from 'react';

import preprocessData from './util/preprocessData';
import preprocessDataV2 from './util/preprocessDataV2';
import preprocessFlamechart from './util/preprocessFlamechart';

// TODO: Add import button but keep a static path until canvas layout is ready
import JSON_PATH from 'url:../static/small-devtools.json';
import JSON_PATHV2 from 'url:../static/Profile-20200625T133129.json';

type Props = {|
onDataImported: (
profilerData: ReactProfilerData,
flamechart: FlamechartData,
) => void,
onDataImportedV2: (
profilerData: ReactProfilerDataV2,
flamechart: FlamechartData,
) => void,
|};

export default function ImportPage({onDataImported}: Props) {
export default function ImportPage({onDataImported, onDataImportedV2}: Props) {
useEffect(() => {
fetch(JSON_PATH)
.then(res => res.json())
Expand All @@ -36,6 +46,23 @@ export default function ImportPage({onDataImported}: Props) {
});
}, []);

useEffect(() => {
fetch(JSON_PATHV2)
.then(res => res.json())
.then((events: TimelineEvent[]) => {
// Filter null entries and sort by timestamp.
// I would not expect to have to do either of this,
// but some of the data being passed in requires it.
events = events.filter(Boolean).sort((a, b) => (a.ts > b.ts ? 1 : -1));

if (events.length > 0) {
const processedData = preprocessDataV2(events);
const processedFlamechart = preprocessFlamechart(events);
onDataImportedV2(processedData, processedFlamechart);
}
});
}, []);

return (
<div>
LOADING. TODO: Turn this into an import page. This page currently just
Expand Down

0 comments on commit 6b25ecd

Please sign in to comment.