Skip to content

Commit

Permalink
Split code path for grouped workflows
Browse files Browse the repository at this point in the history
Only use the Cellect API for grouped workflows. Return a default set of workflow data otherwise. Update tests to test for grouped and non-grouped workflows. Update mocks to represent Cellect responses without the grouped attribute.
  • Loading branch information
eatyourgreens committed Feb 19, 2021
1 parent 91c2444 commit 023f8b0
Show file tree
Hide file tree
Showing 4 changed files with 387 additions and 243 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import { panoptes } from '@zooniverse/panoptes-js'
import fetch from 'node-fetch'

async function fetchWorkflowData (activeWorkflows, env) {
const response = await panoptes
.get('/workflows', {
complete: false,
env,
fields: 'completeness,grouped',
id: activeWorkflows.join(','),
include: 'subject_sets'
})
const query = {
complete: false,
env,
fields: 'completeness,grouped',
id: activeWorkflows.join(','),
include: 'subject_sets'
}
const response = await panoptes.get('/workflows', query)
const { workflows, linked } = response.body
const subjectSets = linked ? linked.subject_sets : []
await Promise.allSettled(subjectSets.map(subjectSet => fetchPreviewImage(subjectSet, env)))
Expand All @@ -33,7 +33,7 @@ async function fetchWorkflowCellectStatus(workflow) {
const workflowURL = `https://cellect.zooniverse.org/workflows/${workflow.id}/status`
const response = await fetch(workflowURL)
const body = await response.json()
const { groups } = body ?? {}
const groups = body.groups ?? {}
return groups
}

Expand All @@ -50,6 +50,17 @@ async function fetchPreviewImage (subjectSet, env) {
}

async function buildWorkflow(workflow, displayName, subjectSets, isDefault) {
const workflowData = {
completeness: workflow.completeness || 0,
default: isDefault,
displayName,
grouped: false,
id: workflow.id,
subjectSets: []
}
if (!workflow.grouped) {
return workflowData
}
const subjectSetCounts = await fetchWorkflowCellectStatus(workflow)
const workflowSubjectSets = workflow.links.subject_sets
.map(subjectSetID => {
Expand All @@ -59,14 +70,7 @@ async function buildWorkflow(workflow, displayName, subjectSets, isDefault) {
})
.filter(Boolean)

return {
completeness: workflow.completeness || 0,
default: isDefault,
displayName,
grouped: workflow.grouped,
id: workflow.id,
subjectSets: workflowSubjectSets
}
return Object.assign(workflowData, { grouped: true, subjectSets: workflowSubjectSets })
}

async function fetchWorkflowsHelper (language = 'en', activeWorkflows, defaultWorkflow, env) {
Expand All @@ -80,7 +84,7 @@ async function fetchWorkflowsHelper (language = 'en', activeWorkflows, defaultWo
return buildWorkflow(workflow, displayName, subjectSets, isDefault)
})
const workflowStatuses = await Promise.allSettled(awaitWorkflows)
const workflowsWithSubjectSets = workflowStatuses.map(result => result.value)
const workflowsWithSubjectSets = workflowStatuses.map(result => result.value || result.reason)
return workflowsWithSubjectSets
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ describe('Helpers > fetchWorkflowsHelper', function () {
{
id: '2',
completeness: 0.7,
grouped: false,
grouped: true,
links: {
subject_sets: ['1', '2', '3']
}
Expand Down Expand Up @@ -56,9 +56,7 @@ describe('Helpers > fetchWorkflowsHelper', function () {
const cellect = nock('https://cellect.zooniverse.org')
.persist()
.get('/workflows/1/status')
.reply(200, {
groups: availableSubjects
})
.reply(200, {})
.get('/workflows/2/status')
.reply(200, {
groups: availableSubjects
Expand Down Expand Up @@ -98,11 +96,7 @@ describe('Helpers > fetchWorkflowsHelper', function () {
grouped: false,
id: '1',
displayName: 'Foo',
subjectSets: [
Object.assign(subjectSet('1'), { availableSubjects: availableSubjects[1]}),
Object.assign(subjectSet('2'), { availableSubjects: availableSubjects[2]}),
Object.assign(subjectSet('3'), { availableSubjects: availableSubjects[3]})
]
subjectSets: []
}
])
})
Expand Down Expand Up @@ -135,16 +129,12 @@ describe('Helpers > fetchWorkflowsHelper', function () {
grouped: false,
id: '1',
displayName: 'Foo',
subjectSets: [
Object.assign(subjectSet('1'), { availableSubjects: availableSubjects[1]}),
Object.assign(subjectSet('2'), { availableSubjects: availableSubjects[2]}),
Object.assign(subjectSet('3'), { availableSubjects: availableSubjects[3]})
]
subjectSets: []
},
{
completeness: 0.7,
default: false,
grouped: false,
grouped: true,
id: '2',
displayName: 'Bar',
subjectSets: [
Expand Down Expand Up @@ -185,16 +175,12 @@ describe('Helpers > fetchWorkflowsHelper', function () {
grouped: false,
id: '1',
displayName: 'Foo',
subjectSets: [
Object.assign(subjectSet('1'), { availableSubjects: availableSubjects[1]}),
Object.assign(subjectSet('2'), { availableSubjects: availableSubjects[2]}),
Object.assign(subjectSet('3'), { availableSubjects: availableSubjects[3]})
]
subjectSets: []
},
{
completeness: 0.7,
default: true,
grouped: false,
grouped: true,
id: '2',
displayName: 'Bar',
subjectSets: [
Expand Down
Loading

0 comments on commit 023f8b0

Please sign in to comment.