Skip to content

Commit c3ec9ac

Browse files
authored
Update upstream data download implementation (#54)
* Replaced TIFF file download request URL with general file download request URL * Added logic to display upstream visits according to their instrument of origin * Moved 'getInstrumentInfo' to general Loaders page * Added logic to 'UpstreamVisitsCard' to lookup and display human readable instrument names * Display message when no visits are found, and hide card entirely when no upstream directories are configured
1 parent 91d62e0 commit c3ec9ac

4 files changed

Lines changed: 128 additions & 35 deletions

File tree

src/components/upstreamVisitsCard.tsx

Lines changed: 104 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,124 @@
11
import { Card, CardBody, Button, CardHeader } from '@chakra-ui/react'
2+
import { useQuery } from '@tanstack/react-query'
23
import { getUpstreamVisits, upstreamDataDownloadRequest } from 'loaders/general'
4+
import { getInstrumentInfo } from 'loaders/general'
35
import React, { useCallback, useEffect } from 'react'
46
import { MdFileDownload } from 'react-icons/md'
57

6-
interface SessionId {
8+
const InstrumentUpstreamVisitsCard = ({
9+
sessid,
10+
instrumentName,
11+
displayName,
12+
instrumentVisits,
13+
}: {
714
sessid: number
15+
instrumentName: string
16+
displayName: string
17+
instrumentVisits: Record<string, string>
18+
}) => {
19+
// Display upstream visits for a single instrument
20+
// Parameters to take: instrument name and upstream visits dict
21+
return (
22+
<Card alignItems="left" cursor={'default'}>
23+
<CardHeader fontWeight="bold" cursor="default">
24+
{displayName}
25+
</CardHeader>
26+
<CardBody cursor="default">
27+
{/* Map each visit to a button */}
28+
{!!Object.keys(instrumentVisits).length ? (
29+
Object.entries(instrumentVisits).map(
30+
([visitName, visitPath]: [string, string]) => {
31+
return (
32+
<Button
33+
rightIcon={<MdFileDownload />}
34+
cursor="pointer"
35+
onClick={() =>
36+
upstreamDataDownloadRequest(
37+
instrumentName,
38+
sessid,
39+
visitName,
40+
visitPath
41+
)
42+
}
43+
>
44+
{visitName}
45+
</Button>
46+
)
47+
}
48+
)
49+
) : (
50+
<>No related visits found</>
51+
)}
52+
</CardBody>
53+
</Card>
54+
)
855
}
956

10-
export const UpstreamVisitCard = ({ sessid }: SessionId) => {
11-
const [upstreamVisits, setUpstreamVisits] = React.useState({})
57+
export const UpstreamVisitsCard = ({ sessid }: { sessid: number }) => {
58+
const [upstreamVisits, setUpstreamVisits] = React.useState<
59+
Record<string, Record<string, string>>
60+
>({})
1261

62+
// Load all visits associated with current session
1363
const resolveVisits = useCallback(async () => {
1464
const visits = await getUpstreamVisits(sessid)
65+
if (!visits) return // Handle null or false-y cases
1566
setUpstreamVisits(visits)
16-
console.log(visits)
1767
}, [sessid])
68+
1869
useEffect(() => {
1970
resolveVisits()
2071
}, [sessid, resolveVisits])
2172

22-
return upstreamVisits ? (
23-
<Card alignItems="center">
24-
<CardHeader>Upstream Visit Data Download</CardHeader>
25-
{Object.keys(upstreamVisits).map((k) => {
26-
return (
27-
<CardBody>
28-
<Button
29-
rightIcon={<MdFileDownload />}
30-
onClick={() => upstreamDataDownloadRequest(k, sessid)}
31-
>
32-
{k}
33-
</Button>
34-
</CardBody>
35-
)
36-
})}
73+
// Set up queryClient to load names of all instruments
74+
type InstrumentInfo = {
75+
instrument_name: string
76+
display_name: string
77+
instrument_url: string
78+
}
79+
const { data: instrumentInfo } = useQuery<InstrumentInfo[]>({
80+
queryKey: ['instrumentInfo'],
81+
queryFn: getInstrumentInfo,
82+
staleTime: 60000,
83+
})
84+
85+
// Set up function to get the corresponding display name of the instrument
86+
const getDisplayName = (instrumentName: string) => {
87+
if (!instrumentInfo) return instrumentName
88+
const result = instrumentInfo.find(
89+
(item) => item.instrument_name === instrumentName
90+
)
91+
// Use instrument name if no results were found or if display name wasn't set
92+
return result
93+
? result.display_name
94+
? result.display_name
95+
: instrumentName
96+
: instrumentName
97+
}
98+
99+
return !!Object.keys(upstreamVisits).length && !!instrumentInfo ? (
100+
<Card alignItems="left" cursor={'default'}>
101+
<CardHeader fontWeight="bold" cursor="default">
102+
Upstream Visit Data Download
103+
</CardHeader>
104+
<CardBody cursor="default">
105+
{/* Map each instrument to its own card */}
106+
{Object.entries(upstreamVisits).map(
107+
([instrumentName, instrumentVisits]: [
108+
string,
109+
Record<string, string>,
110+
]) => {
111+
return (
112+
<InstrumentUpstreamVisitsCard
113+
sessid={sessid}
114+
instrumentName={instrumentName}
115+
displayName={getDisplayName(instrumentName)}
116+
instrumentVisits={instrumentVisits}
117+
/>
118+
)
119+
}
120+
)}
121+
</CardBody>
37122
</Card>
38123
) : (
39124
<></>

src/loaders/general.tsx

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
import { client } from 'utils/api/client'
22

3+
export const getInstrumentInfo = async () => {
4+
const response = await client.hub_get(`instruments`)
5+
6+
if (response.status !== 200) {
7+
return null
8+
}
9+
10+
return response.data
11+
}
12+
313
export const getInstrumentName = async () => {
414
const response = await client.get(
515
`display/instruments/${sessionStorage.getItem('instrumentName')}/instrument_name`
@@ -25,7 +35,9 @@ export const getInstrumentConnectionStatus = async () => {
2535
return response.data
2636
}
2737

28-
export const getUpstreamVisits = async (sessid: number) => {
38+
export const getUpstreamVisits = async (
39+
sessid: number
40+
): Promise<Record<string, Record<string, string>> | null> => {
2941
const response = await client.get(
3042
`session_info/correlative/sessions/${sessid}/upstream_visits`
3143
)
@@ -37,12 +49,17 @@ export const getUpstreamVisits = async (sessid: number) => {
3749
}
3850

3951
export const upstreamDataDownloadRequest = async (
52+
instrumentName: string,
53+
sessid: number,
4054
visitName: string,
41-
sessid: number
55+
visitPath: string
4256
) => {
4357
const response = await client.post(
44-
`instrument_server/visits/${visitName}/sessions/${sessid}/upstream_tiff_data_request`,
45-
{}
58+
`instrument_server/visits/${visitName}/sessions/${sessid}/upstream_file_data_request`,
59+
{
60+
upstream_instrument: instrumentName,
61+
upstream_visit_path: visitPath,
62+
}
4663
)
4764

4865
if (response.status !== 200) {

src/loaders/hub.tsx

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,6 @@
11
import { QueryClient } from '@tanstack/react-query'
2-
import { client } from 'utils/api/client'
32

4-
const getInstrumentInfo = async () => {
5-
const response = await client.hub_get(`instruments`)
6-
7-
if (response.status !== 200) {
8-
return null
9-
}
10-
11-
return response.data
12-
}
3+
import { getInstrumentInfo } from './general'
134

145
const query = {
156
queryKey: ['instrumentInfo'],

src/routes/Session.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import {
2727
import { useQuery } from '@tanstack/react-query'
2828
import { InstrumentCard } from 'components/instrumentCard'
2929
import { RsyncCard } from 'components/rsyncCard'
30-
import { UpstreamVisitCard } from 'components/upstreamVisitsCard'
30+
import { UpstreamVisitsCard } from 'components/upstreamVisitsCard'
3131
import { getInstrumentConnectionStatus } from 'loaders/general'
3232
import { sessionTokenCheck, sessionHandshake } from 'loaders/jwt'
3333
import { getMachineConfigData } from 'loaders/machineConfig'
@@ -573,7 +573,7 @@ export const Session = () => {
573573
</Button>
574574
</Link>
575575
<InstrumentCard />
576-
<UpstreamVisitCard sessid={parseInt(sessid ?? '0')} />
576+
<UpstreamVisitsCard sessid={parseInt(sessid ?? '0')} />
577577
</Stack>
578578
</Flex>
579579
</Box>

0 commit comments

Comments
 (0)