-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #163 from lsst-sqre/tickets/DM-43461
DM-43461: Enable background page recomputation in Times Square
- Loading branch information
Showing
22 changed files
with
537 additions
and
229 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
'squareone': minor | ||
--- | ||
|
||
Implement background recomputation for cached Times Square pages. The "Recompute" button submits a request to Times Square's `DELETE /v1/pages/:page/html?{params}` endpoint, which causes a background recomputation of the notebook and re-rendering of the cached HTML. | ||
|
||
The new `TimesSquareHtmlEventsProvider` is a React context provider that provides real-time updates from Times Square about the status of an HTML rendering for a given set of parameters using Times Square's `/v1/pages/:page/html/events/{params}` endpoint. Squareone uses `@microsoft/fetch-event-source` to subscribe to this server-sent events (SSE) endpoint. Using this provider, the UI is able to show new data to the user, including the status of the computation, and once the computation is complete, the date/age of computation and the execution time. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'squareone': minor | ||
--- | ||
|
||
The Times Square "Update" and "Reset" buttons are now disabled when appropriate. The Update button is disabled when the parameter inputs have not been changed relative to their current state. Likewise, the Reset button is disabled when the parameters are unchanged from the current state. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'squareone': minor | ||
--- | ||
|
||
New `TimesSquareUrlParametersProvider` component. This React context provides the URL-based state to Times Square components, such as the page being viewed, its notebook parameters values, and the display settings. This change simplifies the structure of the React pages by refactoring all of the URL parsing into a common component. As well, this context eliminates "prop drilling" to provide this URL-based state to all components in the Times Square application. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
apps/squareone/src/components/TimesSquareGitHubPagePanel/ExecStats.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* | ||
* ExecStats provides a summary of the execution status and timing of the | ||
* notebook execution. It also provides a button to request the recomputation | ||
* of the already-executed notebook. | ||
*/ | ||
|
||
import React from 'react'; | ||
import styled from 'styled-components'; | ||
import { parseISO, formatDistanceToNow } from 'date-fns'; | ||
|
||
import { TimesSquareHtmlEventsContext } from '../TimesSquareHtmlEventsProvider'; | ||
import { GhostButton } from '../Button'; | ||
|
||
export default function ExecStats({}) { | ||
const htmlEvent = React.useContext(TimesSquareHtmlEventsContext); | ||
|
||
const handleRecompute = async (event) => { | ||
event.preventDefault(); | ||
|
||
await fetch(htmlEvent.htmlUrl, { | ||
method: 'DELETE', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
}); | ||
}; | ||
|
||
if (htmlEvent.executionStatus == 'complete') { | ||
const dateFinished = parseISO(htmlEvent.dateFinished); | ||
const formattedDuration = Number(htmlEvent.executionDuration).toFixed(1); | ||
return ( | ||
<StyledContainer> | ||
<StyledContent> | ||
Computed{' '} | ||
<time | ||
dateTime={htmlEvent.dateFinished} | ||
title={htmlEvent.dateFinished} | ||
> | ||
{formatDistanceToNow(dateFinished, { addSuffix: true })} | ||
</time>{' '} | ||
in {formattedDuration} seconds. | ||
</StyledContent> | ||
<GhostButton onClick={handleRecompute}>Recompute</GhostButton> | ||
</StyledContainer> | ||
); | ||
} | ||
|
||
if (htmlEvent.executionStatus == 'in_progress') { | ||
return ( | ||
<StyledContainer> | ||
<p>Computing…</p> | ||
</StyledContainer> | ||
); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
const StyledContainer = styled.div` | ||
display: flex; | ||
flex-direction: column; | ||
gap: 0.5rem; | ||
margin-top: 2rem; | ||
`; | ||
|
||
const StyledContent = styled.p` | ||
font-size: 0.8rem; | ||
margin-bottom: 0; | ||
`; |
56 changes: 56 additions & 0 deletions
56
apps/squareone/src/components/TimesSquareGitHubPagePanel/ExecStats.stories.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import React from 'react'; | ||
|
||
import { TimesSquareHtmlEventsContext } from '../TimesSquareHtmlEventsProvider'; | ||
import ExecStats from './ExecStats'; | ||
|
||
export default { | ||
component: ExecStats, | ||
title: 'Components/TimesSquare/ExecStats', | ||
parameters: { | ||
viewport: { | ||
viewports: { | ||
sidebar: { | ||
name: 'Sidebar', | ||
styles: { | ||
width: '280px', | ||
height: '900px', | ||
}, | ||
}, | ||
}, | ||
}, | ||
defaultViewport: 'sidebar', | ||
}, | ||
}; | ||
|
||
const Template = (args) => ( | ||
<TimesSquareHtmlEventsContext.Provider value={args}> | ||
<ExecStats /> | ||
</TimesSquareHtmlEventsContext.Provider> | ||
); | ||
|
||
export const Default = Template.bind({}); | ||
Default.args = { | ||
dateSubmitted: '2021-09-01T12:00:00Z', | ||
dateStarted: '2021-09-01T12:00:01Z', | ||
dateFinished: '2021-09-01T12:00:10Z', | ||
executionStatus: 'complete', | ||
executionDuration: 10.12, | ||
}; | ||
|
||
export const InProgressNew = Template.bind({}); | ||
InProgressNew.args = { | ||
dateSubmitted: '2021-09-01T12:00:10Z', | ||
dateStarted: null, | ||
dateFinished: null, | ||
executionStatus: 'in_progress', | ||
executionDuration: null, | ||
}; | ||
|
||
export const InProgressExisting = Template.bind({}); | ||
InProgressExisting.args = { | ||
dateSubmitted: '2021-09-01T12:00:00Z', | ||
dateStarted: '2021-09-01T12:00:01Z', | ||
dateFinished: '2021-09-01T12:00:10Z', | ||
executionStatus: 'in_progress', | ||
executionDuration: 10.12, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
apps/squareone/src/components/TimesSquareHtmlEventsProvider/TimesSquareHtmlEventsProvider.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* Context provider for the Times Square /pages/:page/html/events endpoint. | ||
*/ | ||
|
||
import React from 'react'; | ||
import { fetchEventSource } from '@microsoft/fetch-event-source'; | ||
|
||
import useTimesSquarePage from '../../hooks/useTimesSquarePage'; | ||
import { TimesSquareUrlParametersContext } from '../TimesSquareUrlParametersProvider'; | ||
|
||
export const TimesSquareHtmlEventsContext = React.createContext(); | ||
|
||
export default function TimesSquareHtmlEventsProvider({ children }) { | ||
const [htmlEvent, setHtmlEvent] = React.useState(null); | ||
|
||
const urlParameters = React.useContext(TimesSquareUrlParametersContext); | ||
const timesSquarePage = useTimesSquarePage(); | ||
|
||
const urlQueryString = urlParameters.urlQueryString; | ||
const htmlEventsUrl = timesSquarePage.htmlEventsUrl; | ||
const fullHtmlEventsUrl = htmlEventsUrl | ||
? `${htmlEventsUrl}?${urlQueryString}` | ||
: null; | ||
|
||
React.useEffect(() => { | ||
const abortController = new AbortController(); | ||
|
||
async function runEffect() { | ||
if (htmlEventsUrl) { | ||
await fetchEventSource(fullHtmlEventsUrl, { | ||
method: 'GET', | ||
signal: abortController.signal, | ||
onopen(res) { | ||
if (res.status >= 400 && res.status < 500 && res.status !== 429) { | ||
console.log(`Client side error ${fullHtmlEventsUrl}`, res); | ||
} | ||
}, | ||
onmessage(event) { | ||
const parsedData = JSON.parse(event.data); | ||
setHtmlEvent(parsedData); | ||
}, | ||
onclose() {}, | ||
onerror(err) {}, | ||
}); | ||
} | ||
} | ||
runEffect(); | ||
|
||
return () => { | ||
// Clean up: close the event source connection | ||
abortController.abort(); | ||
}; | ||
}, [fullHtmlEventsUrl, htmlEventsUrl]); | ||
|
||
const contextValue = { | ||
dateSubmitted: htmlEvent ? htmlEvent.date_submitted : null, | ||
dateStarted: htmlEvent ? htmlEvent.date_started : null, | ||
dateFinished: htmlEvent ? htmlEvent.date_finished : null, | ||
executionStatus: htmlEvent ? htmlEvent.execution_status : null, | ||
executionDuration: htmlEvent ? htmlEvent.execution_duration : null, | ||
htmlHash: htmlEvent ? htmlEvent.html_hash : null, | ||
htmlUrl: htmlEvent ? htmlEvent.html_url : null, | ||
}; | ||
|
||
return ( | ||
<TimesSquareHtmlEventsContext.Provider value={{ ...contextValue }}> | ||
{children} | ||
</TimesSquareHtmlEventsContext.Provider> | ||
); | ||
} |
2 changes: 2 additions & 0 deletions
2
apps/squareone/src/components/TimesSquareHtmlEventsProvider/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './TimesSquareHtmlEventsProvider'; | ||
export { default } from './TimesSquareHtmlEventsProvider'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.