Skip to content

Commit

Permalink
Split up helper files
Browse files Browse the repository at this point in the history
Split out the subject set data fetching into `helpers/fetchSubjectSets`.
  • Loading branch information
eatyourgreens committed Mar 19, 2021
1 parent d5d899d commit 2b7446c
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 57 deletions.
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
}

1 change: 1 addition & 0 deletions packages/app-project/src/helpers/fetchSubjectSets/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from './fetchSubjectSets'
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { panoptes } from '@zooniverse/panoptes-js'
import fetch from 'node-fetch'

import { logToSentry } from '@helpers/logger'
import fetchSubjectSets from '@helpers/fetchSubjectSets'

async function fetchWorkflowData (activeWorkflows, env) {
try {
Expand All @@ -18,24 +19,6 @@ async function fetchWorkflowData (activeWorkflows, env) {
throw error
}
}

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 fetchDisplayNames (language, activeWorkflows, env) {
let displayNames = {}
try {
Expand All @@ -55,44 +38,6 @@ async function fetchDisplayNames (language, activeWorkflows, env) {
return displayNames
}

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) {
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
}

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
}

async function buildWorkflow(workflow, displayName, isDefault, env) {
const workflowData = {
completeness: workflow.completeness || 0,
Expand All @@ -103,7 +48,7 @@ async function buildWorkflow(workflow, displayName, isDefault, env) {
subjectSets: []
}
if (workflow.grouped) {
workflowData.subjectSets = await workflowSubjectSets(workflow, env)
workflowData.subjectSets = await fetchSubjectSets(workflow, env)
}

return workflowData
Expand Down

0 comments on commit 2b7446c

Please sign in to comment.