-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split out the subject set data fetching into `helpers/fetchSubjectSets`.
- Loading branch information
1 parent
d5d899d
commit 2b7446c
Showing
3 changed files
with
68 additions
and
57 deletions.
There are no files selected for viewing
65 changes: 65 additions & 0 deletions
65
packages/app-project/src/helpers/fetchSubjectSets/fetchSubjectSets.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,65 @@ | ||
import { panoptes } from '@zooniverse/panoptes-js' | ||
import fetch from 'node-fetch' | ||
|
||
import { logToSentry } from '@helpers/logger' | ||
|
||
async function fetchSubjectSetData(subjectSetIDs, env) { | ||
let subject_sets = [] | ||
try { | ||
const query = { | ||
env, | ||
id: subjectSetIDs.join(',') | ||
} | ||
const response = await panoptes.get('/subject_sets', query) | ||
subject_sets = response.body.subject_sets | ||
await Promise.allSettled(subject_sets.map(subjectSet => fetchPreviewImage(subjectSet, env))) | ||
} catch (error) { | ||
console.error(error) | ||
logToSentry(error) | ||
} | ||
return subject_sets | ||
} | ||
|
||
async function fetchWorkflowCellectStatus(workflow) { | ||
let groups = {} | ||
if (workflow.grouped) { | ||
try { | ||
const workflowURL = `https://cellect.zooniverse.org/workflows/${workflow.id}/status` | ||
const response = await fetch(workflowURL) | ||
const body = await response.json() | ||
groups = body.groups ?? {} | ||
} catch (error) { | ||
console.error(error) | ||
logToSentry(error) | ||
} | ||
} | ||
return groups | ||
} | ||
|
||
async function fetchPreviewImage (subjectSet, env) { | ||
try { | ||
const response = await panoptes | ||
.get('/set_member_subjects', { | ||
env, | ||
subject_set_id: subjectSet.id, | ||
include: 'subject', | ||
page_size: 1 | ||
}) | ||
const { linked } = response.body | ||
subjectSet.subjects = linked.subjects | ||
} catch (error) { | ||
console.error(error) | ||
logToSentry(error) | ||
} | ||
} | ||
|
||
export default async function workflowSubjectSets(workflow, env) { | ||
const subjectSetCounts = await fetchWorkflowCellectStatus(workflow) | ||
const subjectSetIDs = Object.keys(subjectSetCounts) | ||
const subjectSets = await fetchSubjectSetData(subjectSetIDs, env) | ||
subjectSets.forEach(subjectSet => { | ||
subjectSet.availableSubjects = subjectSetCounts[subjectSet.id] | ||
}) | ||
return subjectSets | ||
} | ||
|
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 @@ | ||
export { default } from './fetchSubjectSets' |
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