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.
Remove included subject sets from the workflow request and request them separately, based on the Cellect response.
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 22, 2021
1 parent 91c2444 commit f92626b
Show file tree
Hide file tree
Showing 4 changed files with 441 additions and 286 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,31 @@ import { panoptes } from '@zooniverse/panoptes-js'
import fetch from 'node-fetch'

async function fetchWorkflowData (activeWorkflows, env) {
const response = await panoptes
.get('/workflows', {
complete: false,
const query = {
complete: false,
env,
fields: 'completeness,display_name,grouped',
id: activeWorkflows.join(',')
}
const response = await panoptes.get('/workflows', query)
const { workflows } = response.body
return workflows
}

async function fetchSubjectSetData(subjectSetIDs, env) {
let subject_sets = []
try {
const query = {
env,
fields: 'completeness,grouped',
id: activeWorkflows.join(','),
include: 'subject_sets'
})
const { workflows, linked } = response.body
const subjectSets = linked ? linked.subject_sets : []
await Promise.allSettled(subjectSets.map(subjectSet => fetchPreviewImage(subjectSet, env)))
return { subjectSets, workflows }
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)
}
return subject_sets
}

function fetchDisplayNames (language, activeWorkflows, env) {
Expand All @@ -30,10 +43,17 @@ function fetchDisplayNames (language, activeWorkflows, env) {
}

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 ?? {}
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)
}
}
return groups
}

Expand All @@ -49,38 +69,44 @@ async function fetchPreviewImage (subjectSet, env) {
subjectSet.subjects = linked.subjects
}

async function buildWorkflow(workflow, displayName, subjectSets, isDefault) {
async function workflowSubjectSets(workflow, env) {
const subjectSetCounts = await fetchWorkflowCellectStatus(workflow)
const workflowSubjectSets = workflow.links.subject_sets
.map(subjectSetID => {
const subjectSet = subjectSets.find(subjectSet => subjectSet.id === subjectSetID)
subjectSet.availableSubjects = subjectSetCounts[subjectSetID]
return subjectSet
})
.filter(Boolean)
const subjectSetIDs = Object.keys(subjectSetCounts)
const subjectSets = await fetchSubjectSetData(subjectSetIDs, env)
subjectSets.forEach(subjectSet => {
subjectSet.availableSubjects = subjectSetCounts[subjectSet.id]
})
return subjectSets
}

return {
async function buildWorkflow(workflow, displayName, isDefault, env) {
const workflowData = {
completeness: workflow.completeness || 0,
default: isDefault,
displayName,
grouped: workflow.grouped,
id: workflow.id,
subjectSets: workflowSubjectSets
subjectSets: []
}
if (workflow.grouped) {
workflowData.subjectSets = await workflowSubjectSets(workflow, env)
}

return workflowData
}

async function fetchWorkflowsHelper (language = 'en', activeWorkflows, defaultWorkflow, env) {
const { subjectSets, workflows } = await fetchWorkflowData(activeWorkflows, env)
const workflows = await fetchWorkflowData(activeWorkflows, env)
const workflowIds = workflows.map(workflow => workflow.id)
const displayNames = await fetchDisplayNames(language, workflowIds, env)

const awaitWorkflows = workflows.map(workflow => {
const isDefault = workflows.length === 1 || workflow.id === defaultWorkflow
const displayName = displayNames[workflow.id]
return buildWorkflow(workflow, displayName, subjectSets, isDefault)
const displayName = displayNames[workflow.id] || workflow.display_name
return buildWorkflow(workflow, displayName, isDefault, env)
})
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 All @@ -79,14 +77,16 @@ describe('Helpers > fetchWorkflowsHelper', function () {
.get('/workflows')
.query(true)
.reply(200, {
workflows: WORKFLOWS.slice(0, 1),
linked: {
subject_sets: [
subjectSet('1'),
subjectSet('2'),
subjectSet('3')
]
}
workflows: WORKFLOWS.slice(0, 1)
})
.get('/subject_sets')
.query(query => query.id === '1,2,3')
.reply(200, {
subject_sets: [
subjectSet('1'),
subjectSet('2'),
subjectSet('3')
]
})

const result = await fetchWorkflowsHelper('en', ['1'])
Expand All @@ -98,11 +98,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 All @@ -117,14 +113,16 @@ describe('Helpers > fetchWorkflowsHelper', function () {
.get('/workflows')
.query(true)
.reply(200, {
workflows: WORKFLOWS,
linked: {
subject_sets: [
subjectSet('1'),
subjectSet('2'),
subjectSet('3')
]
}
workflows: WORKFLOWS
})
.get('/subject_sets')
.query(true)
.reply(200, {
subject_sets: [
subjectSet('1'),
subjectSet('2'),
subjectSet('3')
]
})

const result = await fetchWorkflowsHelper('en', ['1', '2'])
Expand All @@ -135,16 +133,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 All @@ -167,14 +161,16 @@ describe('Helpers > fetchWorkflowsHelper', function () {
.get('/workflows')
.query(true)
.reply(200, {
workflows: WORKFLOWS,
linked: {
subject_sets: [
subjectSet('1'),
subjectSet('2'),
subjectSet('3')
]
}
workflows: WORKFLOWS
})
.get('/subject_sets')
.query(true)
.reply(200, {
subject_sets: [
subjectSet('1'),
subjectSet('2'),
subjectSet('3')
]
})

const result = await fetchWorkflowsHelper('en', ['1', '2'], '2')
Expand All @@ -185,16 +181,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 Expand Up @@ -238,14 +230,7 @@ describe('Helpers > fetchWorkflowsHelper', function () {
.get('/workflows')
.query(true)
.reply(200, {
workflows: WORKFLOWS,
linked: {
subject_sets: [
subjectSet('1'),
subjectSet('2'),
subjectSet('3')
]
}
workflows: WORKFLOWS
})

try {
Expand Down
Loading

0 comments on commit f92626b

Please sign in to comment.