-
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.
Engaging Crowds: Subject set completeness (#2016)
Adds a query which gets available subject numbers, for a given workflow, from the Cellect API. Removes included subject sets from the workflows API query (see #678.) Subject sets are fetched separately, by ID, from the Panoptes API. This increases the number of API requests required to build each page, but allows us to limit subject set requests to grouped workflows. Updates the API mocks, in data-fetching tests, to include grouped/non-grouped cases and to add the /subject_sets endpoint. Refactors data-fetching into `fetchWorkflowsHelper` and `fetchSubjectSets`. Wraps the data-fetching requests in try/catch, so that we can log API errors to Sentry. Co-authored-by: Shaun A. Noordin <[email protected]>
- Loading branch information
1 parent
f9a9174
commit cdf9e49
Showing
9 changed files
with
610 additions
and
329 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
64 changes: 64 additions & 0 deletions
64
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,64 @@ | ||
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 fetchSubjectSets(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' |
89 changes: 47 additions & 42 deletions
89
packages/app-project/src/helpers/fetchWorkflowsHelper/fetchWorkflowsHelper.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
Oops, something went wrong.