Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

i22 - paging #23

Merged
merged 4 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ on:
- main
pull_request:

env:
BRANCH_NAME: ${{ github.head_ref || github.ref_name }}

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
Expand Down
1 change: 1 addition & 0 deletions scripts/common
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ set -e
PACKAGE_ROOT=$(realpath $HERE/..)
PACKAGE_NAME=seroviz
PACKAGE_ORG=seroanalytics
SEROVIZR_VERSION=paging

if [[ -v "GITHUB_SHA" ]]; then
GIT_SHA=${GITHUB_SHA:0:7}
Expand Down
2 changes: 1 addition & 1 deletion scripts/run
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ NETWORK=seroviz_nw

docker network create $NETWORK

docker run -d -p 8888:8888 --network=$NETWORK --name serovizr seroanalytics/serovizr:main
docker run -d -p 8888:8888 --network=$NETWORK --name serovizr seroanalytics/serovizr:$SEROVIZR_VERSION
docker run -d -p 80:80 -p 443:443 --network=$NETWORK --name seroviz $DOCKER_COMMIT_TAG localhost
docker exec seroviz self-signed-certificate /run/proxy
7 changes: 5 additions & 2 deletions scripts/run-dependencies.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
#!/usr/bin/env bash
set -ex
HERE=$(dirname $0)
. $HERE/common

TAG=seroanalytics/serovizr:main

docker pull $TAG
TAG=seroanalytics/serovizr:$SEROVIZR_VERSION

docker pull $TAG || true
docker run -p 8888:8888 -d --rm --name serovizr $TAG
60 changes: 36 additions & 24 deletions src/components/IndividualPlots.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {dataService} from "../services/dataService";
import {ErrorDetail} from "../generated";
import PlotError from "./PlotError";
import {toFilename} from "../services/utils";
import PageNav from "./PageNav";

export function IndividualPlots() {
const state = useContext(RootContext);
Expand All @@ -17,6 +18,9 @@ export function IndividualPlots() {
const [warnings, setWarnings] = useState<string[]>([]);
const [layout, setLayout] = useState<any>(null);
const [plotError, setPlotError] = useState<ErrorDetail | null>(null);
const [loading, setLoading] = useState(false);
const [page, setPage] = useState(1);
const [numPages, setNumPages] = useState(1);

const scale = state.datasetSettings[state.selectedDataset].scale;
const settings = state.datasetSettings[state.selectedDataset].individualSettings
Expand All @@ -26,62 +30,70 @@ export function IndividualPlots() {
setPlotError(null);
setData([]);
setWarnings([]);
setLoading(true);
const result = await dataService(state.language, dispatch)
.getIndividualData(state.selectedDataset, scale, settings);
.getIndividualData(state.selectedDataset, scale, settings, page);

if (result && result.data) {
const data = result.data.data.filter(d => d.x instanceof Array && d.y instanceof Array);
const warnings = [result.data.warnings];
const omittedPanels = result.data.data.length - data.length;
if (omittedPanels > 0) {
if (omittedPanels === 1) {
warnings.push("1 trace contained single data points and was omitted.")
}
if (omittedPanels > 1) {
warnings.push(omittedPanels + " traces contained single data points and were omitted.")
}
setData(data);
setLayout(result.data.layout);
setWarnings(warnings.flat().filter(w => w) as string[]);
setNumPages(result.data.numPages);
}
if (result && result.errors?.length) {
setPlotError(result.errors[0])
}
setLoading(false);
}

if (settings.pid) {
fetchData();
}

}, [state.language, dispatch, state.selectedDataset, scale, settings], 100);
}, [state.language, dispatch, state.selectedDataset, scale, settings, page], 100);

const title = settings.filter || "individual trajectories";

return <Row>
<SideBar/>
<Col sm={8}>
<Row className={"pt-3"}>
{warnings.length > 0 && <Alert className={"rounded-0 border-0 mb-1 ms-4"}
variant={"warning"}>
<Col sm={8} className={"mt-2"}>
{warnings.length > 0 &&
<Alert className={"rounded-0 border-0 mb-1 ms-4"}
variant={"warning"}>
Plot generated some warnings:
<ul>{
warnings.map(w =>
<li key={w}>{w}</li>)}
</ul>
</Alert>}
{!!plotError && <PlotError title={title} error={plotError}/>}
{!!settings.pid && data.length > 0 && <Plot data={data}
layout={{
...layout,
autosize: true
}}
useResizeHandler={true}
style={{
minWidth: "400px",
width: "100%",
height: "800px"
}}
config={{toImageButtonOptions: {filename: toFilename(title)}}} />
}
{!settings.pid &&
<p className={"mt-3"}>Please select an id column</p>}
</Row>
{loading && <h2>Loading</h2>}
{!!plotError && <PlotError title={title} error={plotError}/>}
{!!settings.pid && data.length > 0 && <Plot data={data}
layout={{
...layout,
autosize: true
}}
useResizeHandler={true}
style={{
minWidth: "400px",
width: "100%",
height: "800px"
}}
config={{toImageButtonOptions: {filename: toFilename(title)}}}/>
}
{!settings.pid &&
<p className={"mt-3"}>Please select an id column</p>}
{!!settings.pid && data.length > 0 && <PageNav currentPage={page} numPages={numPages}
setPage={setPage}></PageNav>}
</Col>
</Row>
}
24 changes: 24 additions & 0 deletions src/components/PageNav.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import {Nav, Pagination} from "react-bootstrap";

interface Props {
currentPage: number;
numPages: number;
setPage: (num: number) => void;
}

export default function PageNav({currentPage, numPages, setPage}: Props) {
let items = [];
for (let number = 1; number <= numPages; number++) {
items.push(
<Pagination.Item key={number} active={number === currentPage}
onClick={() => setPage(number)}>
{number}
</Pagination.Item>,
);
}
return <Nav>
<Pagination size={"sm"} className={"justify-content-center"}>
{items}
</Pagination>
</Nav>
}
2 changes: 2 additions & 0 deletions src/generated.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ export interface Plotly {
[k: string]: unknown;
};
warnings: string | null | string[];
numPages: number;
page: number;
}
export interface ResponseFailure {
status: "failure";
Expand Down
9 changes: 9 additions & 0 deletions src/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,12 @@ body {
--bs-btn-disabled-bg: var(--bs-secondary-bg);
--bs-btn-disabled-border-color: var(--bs-secondary-bg);
}

.pagination {
--bs-pagination-active-bg: var(--primary-color);
--bs-pagination-active-border-color: var(--primary-color-darken);
--bs-pagination-border-color: var(--primary-color-darken);
--bs-pagination-color: var(--primary-color);
--bs-pagination-focus-color: var(--primary-color);
--bs-pagination-hover-color: var(--primary-color);
}
2 changes: 1 addition & 1 deletion src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const root = ReactDOM.createRoot(

const getApiUrl = () => {
if (process.env.NODE_ENV === "development") {
return "http://localhost:8888";
return "http://localhost:8888/api";
}
return `https://${window.location.host}/api`;
};
Expand Down
4 changes: 3 additions & 1 deletion src/services/dataService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,12 @@ export class DataService {

async getIndividualData(selectedDataset: string,
scale: "log" | "natural" | "log2",
individualSettings: IndividualSettings) {
individualSettings: IndividualSettings,
page: number) {

let queryString = `?color=${encodeURIComponent(individualSettings.color)}&`
queryString += `linetype=${encodeURIComponent(individualSettings.linetype)}&`
queryString += `page=${page}&`
if (individualSettings.filter) {
queryString += `filter=${encodeURIComponent(individualSettings.filter)}&`
}
Expand Down
69 changes: 69 additions & 0 deletions test/components/IndividualPlots.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
import {render, waitFor, screen} from "@testing-library/react";
import {RootContext} from "../../src/RootContext";
import {IndividualPlots} from "../../src/components/IndividualPlots";
import {userEvent} from "@testing-library/user-event";

describe("<IndividualPlots/>", () => {

Expand Down Expand Up @@ -204,5 +205,73 @@ describe("<IndividualPlots/>", () => {
await waitFor(() => expect(mockAxios.history.get.length)
.toBe(1));

const alert = screen.getByRole("alert");
expect(alert).toHaveClass("alert-warning");
expect(alert.textContent)
.toBe("Plot generated some warnings:1 trace contained single data points and was omitted.");
});

test("can select page", async () => {
mockAxios.onGet()
.reply(200, mockSuccess(mockPlotlyData({
data: [
{
x: [1, 2],
y: [3, 4]
}
],
numPages: 3,
page: 1
})));

const state = mockAppState({
selectedPlot: "individual",
selectedDataset: "d1",
datasetSettings: {
"d1": mockDatasetSettings({
individualSettings: mockIndividualSettings({
pid: "pid"
})
})
},
datasetMetadata: mockDatasetMetadata({
biomarkers: ["ab", "ba"]
})
});
render(<RootContext.Provider value={state}>
<IndividualPlots/>
</RootContext.Provider>);

await waitFor(() => expect(mockAxios.history.get.length)
.toBe(1));

let pagination = screen.getAllByRole("listitem");
expect(pagination.length).toBe(3);

expect(pagination[0]).toHaveClass("active");
expect(pagination[0].textContent).toBe("1(current)");
expect(pagination[1]).not.toHaveClass("active");
expect(pagination[1].textContent).toBe("2");
expect(pagination[2]).not.toHaveClass("active");
expect(pagination[2].textContent).toBe("3");

const paginationButtons = screen.getAllByRole("button") as HTMLButtonElement[];
expect(paginationButtons.length).toBe(2);

expect(paginationButtons[0].textContent).toBe("2");

await userEvent.click(paginationButtons[0]);

await waitFor(() => expect(mockAxios.history.get.length)
.toBe(2));

expect(mockAxios.history.get[1].url).toContain("page=2");
pagination = screen.getAllByRole("listitem");
expect(pagination[0]).not.toHaveClass("active");
expect(pagination[0].textContent).toBe("1");
expect(pagination[1]).toHaveClass("active");
expect(pagination[1].textContent).toBe("2(current)");
expect(pagination[2]).not.toHaveClass("active");
expect(pagination[2].textContent).toBe("3");
});
});
2 changes: 1 addition & 1 deletion test/integration/dataService.itest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ describe("DataService", () => {
linetype: "biomarker",
pid: "day",
filter: "sex:M"
}) as GenericResponse<Plotly>;
}, 1) as GenericResponse<Plotly>;

expect(res.data!!.data.length).toBe(4);
expect(res.data!!.data.map(d => d.name)).toEqual([
Expand Down
2 changes: 2 additions & 0 deletions test/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ export function mockPlotlyData(data: Partial<Plotly> = {}): Plotly {
data: [],
layout: {},
warnings: null,
page: 1,
numPages: 2,
...data
}
}
Expand Down
2 changes: 1 addition & 1 deletion test/setupIntegrationTests.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export const apiUrl = 'http://localhost:8888';
export const apiUrl = 'http://localhost:8888/api';
(global as any).apiUrl = apiUrl;