-
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: enable indexed subject selection (#2148)
* Add subjectSet.isIndexed * Add workflow.hasIndexedSubjects * Add getIndexedSubjects helper getIndexedSubjects gets all subjects, in order, from a prioritised subject set, starting from an optional priority. * Add getIndexedSubjects to SubjectStore Add getIndexedSubjects to subjects.populateQueue(). Add subjects.last as a helper to get the last subject from the queue. * Add subjectSelectionStrategy helper Add a helper which generates the URL and query params for each subject selection strategy: specific subjects; indexed, ordered subjects; subject groups; grouped random selection; random selection. * Test getIndexedSubjects * Loop back to the beginning of an indexed set
- Loading branch information
1 parent
dbee4f5
commit 9bfa439
Showing
13 changed files
with
499 additions
and
18 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
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
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
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
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
10 changes: 10 additions & 0 deletions
10
packages/lib-classifier/src/store/helpers/getIndexedSubjects/getIndexedSubjects.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,10 @@ | ||
export default async function getIndexedSubjects(subjectSetId, priority = -1) { | ||
const subjectSetURL = `https://subject-set-search-api.zooniverse.org/subjects/${subjectSetId}.json` | ||
const query = `priority__gt=${priority}&_sort=priority` | ||
const mode = 'cors' | ||
const response = await fetch(`${subjectSetURL}?${query}`, { mode }) | ||
const { columns, rows } = await response.json() | ||
const subjectIDColumn = columns.indexOf('subject_id') | ||
const subjectIds = rows.map(row => row[subjectIDColumn]).slice(0,10) | ||
return subjectIds.join(',') | ||
} |
50 changes: 50 additions & 0 deletions
50
packages/lib-classifier/src/store/helpers/getIndexedSubjects/getIndexedSubjects.spec.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,50 @@ | ||
import nock from 'nock' | ||
|
||
import getIndexedSubjects from './getIndexedSubjects' | ||
|
||
describe('Store > Helpers > getIndexedSubjects', function () { | ||
let subjectIDs | ||
|
||
before(async function () { | ||
nock('https://subject-set-search-api.zooniverse.org') | ||
.get('/subjects/2.json') | ||
.query(query => query.priority__gt === '-1') | ||
.reply(200, { | ||
columns: ['subject_id', 'priority'], | ||
rows: [ | ||
['12345', '1'], | ||
['34567', '2'], | ||
['56789', '3'] | ||
] | ||
}) | ||
.get('/subjects/2.json') | ||
.query(query => query.priority__gt === '1') | ||
.reply(200, { | ||
columns: ['subject_id', 'priority'], | ||
rows: [ | ||
['34567', '2'], | ||
['56789', '3'] | ||
] | ||
}) | ||
}) | ||
|
||
describe('with a subject priority', function () { | ||
before(async function () { | ||
subjectIDs = await getIndexedSubjects('2', 1) | ||
}) | ||
|
||
it('should return the next subjects from the set', function () { | ||
expect(subjectIDs).to.equal('34567,56789') | ||
}) | ||
}) | ||
|
||
describe('without a subject priority', function () { | ||
before(async function () { | ||
subjectIDs = await getIndexedSubjects('2') | ||
}) | ||
|
||
it('should return the first subjects from the set', function () { | ||
expect(subjectIDs).to.equal('12345,34567,56789') | ||
}) | ||
}) | ||
}) |
1 change: 1 addition & 0 deletions
1
packages/lib-classifier/src/store/helpers/getIndexedSubjects/index.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 @@ | ||
export { default } from './getIndexedSubjects' |
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 |
---|---|---|
@@ -1,2 +1,4 @@ | ||
export { default as convertWorkflowToUseSteps } from './convertWorkflowToUseSteps' | ||
export { default as createStore } from './createStore' | ||
export { default as getIndexedSubjects } from './getIndexedSubjects' | ||
export { default as subjectSelectionStrategy } from './subjectSelectionStrategy' |
1 change: 1 addition & 0 deletions
1
packages/lib-classifier/src/store/helpers/subjectSelectionStrategy/index.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 @@ | ||
export { default } from './subjectSelectionStrategy' |
76 changes: 76 additions & 0 deletions
76
...ges/lib-classifier/src/store/helpers/subjectSelectionStrategy/subjectSelectionStrategy.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,76 @@ | ||
import { getIndexedSubjects } from '@store/helpers' | ||
|
||
export default async function subjectSelectionStrategy(workflow, subjectIDs, priority = -1) { | ||
const workflow_id = workflow.id | ||
|
||
/** Fetch specific subjects for any workflow */ | ||
if (subjectIDs) { | ||
const apiUrl = '/subjects/selection' | ||
const ids = subjectIDs.join(',') | ||
const params = { | ||
ids, | ||
workflow_id | ||
} | ||
return { | ||
apiUrl, | ||
params | ||
} | ||
} | ||
|
||
/** Fetch subject groups for workflows that use the subject group viewer */ | ||
if (workflow.configuration.subject_viewer === 'subjectGroup') { | ||
const apiUrl = '/subjects/grouped' | ||
const num_rows = workflow.configuration.subject_viewer_config?.grid_rows || 1 | ||
const num_columns = workflow.configuration.subject_viewer_config?.grid_columns || 1 | ||
const params = { | ||
num_columns, | ||
num_rows, | ||
workflow_id | ||
} | ||
return { | ||
apiUrl, | ||
params | ||
} | ||
} | ||
|
||
/** fetch ordered subjects for indexed subject sets */ | ||
if (workflow.hasIndexedSubjects) { | ||
const apiUrl = '/subjects/selection' | ||
let ids = await getIndexedSubjects(workflow.subjectSetId, priority) | ||
if (ids === '') { | ||
ids = await getIndexedSubjects(workflow.subjectSetId) | ||
} | ||
const params = { | ||
ids, | ||
workflow_id | ||
} | ||
return { | ||
apiUrl, | ||
params | ||
} | ||
} | ||
|
||
/** Random grouped selection for grouped workflows */ | ||
if (workflow.grouped) { | ||
const apiUrl = '/subjects/queued' | ||
const subject_set_id = workflow.subjectSetId | ||
const params = { | ||
subject_set_id, | ||
workflow_id | ||
} | ||
return { | ||
apiUrl, | ||
params | ||
} | ||
} | ||
|
||
/** default random queued selection */ | ||
const apiUrl = '/subjects/queued' | ||
const params = { | ||
workflow_id | ||
} | ||
return { | ||
apiUrl, | ||
params | ||
} | ||
} |
Oops, something went wrong.