-
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: switch to Datasette for subject browse & search (#2132)
* Add subjectSet.isIndexed * use Datasette to get subjects * use Datasette query params * Remove old demo values * Add tests * Encode URL query params Encode URL query params and add a test case with reserved characters.
- Loading branch information
1 parent
d981573
commit ddef799
Showing
15 changed files
with
215 additions
and
32 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
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
94 changes: 94 additions & 0 deletions
94
packages/app-project/src/shared/components/SubjectPicker/SubjectPicker.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,94 @@ | ||
import { mount, shallow } from 'enzyme' | ||
import { Grommet } from 'grommet' | ||
import nock from 'nock' | ||
import zooTheme from '@zooniverse/grommet-theme' | ||
|
||
import SubjectPicker, { StyledBox, SubjectDataTable } from './SubjectPicker' | ||
|
||
describe('Components > Subject Picker', function () { | ||
let wrapper | ||
const workflow = { | ||
id: '123345' | ||
} | ||
const subjectSet = { | ||
id: '4567', | ||
display_name: 'Test subject set', | ||
metadata: { | ||
indexFields: 'Date,Page' | ||
} | ||
} | ||
|
||
before(function () { | ||
wrapper = shallow( | ||
<SubjectPicker | ||
baseUrl="/workflow/12345/subject-set/4567" | ||
subjectSet={subjectSet} | ||
workflow={workflow} | ||
/> | ||
) | ||
}) | ||
|
||
it('should render', function () { | ||
expect(wrapper).to.be.ok() | ||
}) | ||
|
||
it('should show the subject set name', function () { | ||
const displayName = wrapper.find(StyledBox) | ||
expect(displayName).to.be.ok() | ||
}) | ||
|
||
describe('subject data table', function () { | ||
let wrapper | ||
|
||
before(async function () { | ||
const columns = [ | ||
'subject_id', | ||
'Page', | ||
'Date' | ||
] | ||
const rows = [ | ||
['1', '43', '23 January 1916'], | ||
['2', '44', '24 January 1916'], | ||
['3', '45', '25 January 1916'] | ||
] | ||
nock('https://subject-set-search-api.zooniverse.org/subjects') | ||
.get('/4567.json') | ||
.query(true) | ||
.reply(200, { | ||
columns, | ||
rows | ||
}) | ||
nock('https://panoptes-staging.zooniverse.org/api') | ||
.get('/subject_workflow_statuses') | ||
.query(true) | ||
.reply(200, { | ||
subject_workflow_statuses: [ | ||
{ classifications_count: 0, retired_at: null, links: { subject: '1' }}, | ||
{ classifications_count: 3, retired_at: null, links: { subject: '2' }}, | ||
{ classifications_count: 5, retired_at: "2018-01-30T21:09:49.396Z", links: { subject: '3' }} | ||
] | ||
}) | ||
wrapper = mount( | ||
<SubjectPicker | ||
subjectSet={subjectSet} | ||
workflow={workflow} | ||
/>, | ||
{ wrappingComponent: Grommet, wrappingComponentProps: { theme: zooTheme } } | ||
) | ||
// workaround to wait for the mock API calls to resolve | ||
await new Promise((resolve) => setTimeout(resolve, 100)); | ||
}) | ||
|
||
it('should have column headings, including indexed subject fields', function () { | ||
const dataTable = wrapper.find(SubjectDataTable) | ||
const headers = dataTable.prop('columns').map(column => column.header) | ||
expect(headers).to.deep.equal(['subject_id', 'Date', 'Page', 'status']) | ||
}) | ||
|
||
it('should have a row for each subject', function () { | ||
wrapper.update() | ||
const dataTable = wrapper.find(SubjectDataTable) | ||
expect(dataTable.prop('data').length).to.equal(3) | ||
}) | ||
}) | ||
}) |
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
36 changes: 36 additions & 0 deletions
36
packages/app-project/src/shared/components/SubjectPicker/helpers/checkRetiredStatus.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,36 @@ | ||
import nock from 'nock' | ||
import checkRetiredStatus from './checkRetiredStatus' | ||
|
||
describe('Components > Subject Picker > helpers > checkRetiredStatus', function () { | ||
let retirementStatuses | ||
|
||
before(async function () { | ||
const subject_ids = ['1', '2', '3'] | ||
const workflow = { | ||
id: '1' | ||
} | ||
const panoptes = nock('https://panoptes-staging.zooniverse.org/api') | ||
.get('/subject_workflow_statuses') | ||
.query(true) | ||
.reply(200, { | ||
subject_workflow_statuses: [ | ||
{ classifications_count: 0, retired_at: null, links: { subject: '1' }}, | ||
{ classifications_count: 3, retired_at: null, links: { subject: '2' }}, | ||
{ classifications_count: 5, retired_at: "2018-01-30T21:09:49.396Z", links: { subject: '3' }} | ||
] | ||
}) | ||
retirementStatuses = await checkRetiredStatus(subject_ids, workflow) | ||
}) | ||
|
||
it('should set the status of unclassified subjects', function () { | ||
expect(retirementStatuses['1']).to.equal('Unclassified') | ||
}) | ||
|
||
it('should set the status of classified subjects', function () { | ||
expect(retirementStatuses['2']).to.equal('In progress') | ||
}) | ||
|
||
it('should set the status of retired subjects', function () { | ||
expect(retirementStatuses['3']).to.equal('Retired') | ||
}) | ||
}) |
20 changes: 11 additions & 9 deletions
20
packages/app-project/src/shared/components/SubjectPicker/helpers/fetchRows.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 |
---|---|---|
@@ -1,15 +1,17 @@ | ||
import { checkRetiredStatus } from './' | ||
|
||
export default async function fetchRows(subjects, workflow, page_size) { | ||
const subject_ids = subjects.map(subject => subject.subject_id).join(',') | ||
const { columns, rows } = subjects | ||
const IDColumn = columns.indexOf('subject_id') | ||
const subject_ids = rows.map(row => row[IDColumn]).join(',') | ||
const retirementStatuses = await checkRetiredStatus(subject_ids, workflow, page_size) | ||
const rows = subjects.map(subject => { | ||
const { id, subject_id, ...fields } = subject | ||
return { | ||
subject_id, | ||
status: retirementStatuses[subject_id], | ||
...fields | ||
} | ||
const data = rows.map(row => { | ||
const subject = {} | ||
columns.forEach((column, index) => { | ||
subject[column] = row[index] | ||
}) | ||
subject.status = retirementStatuses[subject.subject_id] | ||
return subject | ||
}) | ||
return rows | ||
return data | ||
} |
42 changes: 42 additions & 0 deletions
42
packages/app-project/src/shared/components/SubjectPicker/helpers/fetchRows.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,42 @@ | ||
import nock from 'nock' | ||
import fetchRows from './fetchRows' | ||
|
||
describe('Components > Subject Picker > helpers > fetchRows', function () { | ||
let data | ||
const expectedData = [ | ||
{ subject_id: '1', Page: '43', Date: '23 January 1916', status: 'Unclassified' }, | ||
{ subject_id: '2', Page: '44', Date: '24 January 1916', status: 'In progress' }, | ||
{ subject_id: '3', Page: '45', Date: '25 January 1916', status: 'Retired' }, | ||
] | ||
|
||
before(async function () { | ||
const columns = [ | ||
'subject_id', | ||
'Page', | ||
'Date' | ||
] | ||
const rows = [ | ||
['1', '43', '23 January 1916'], | ||
['2', '44', '24 January 1916'], | ||
['3', '45', '25 January 1916'] | ||
] | ||
const workflow = { | ||
id: '1' | ||
} | ||
const panoptes = nock('https://panoptes-staging.zooniverse.org/api') | ||
.get('/subject_workflow_statuses') | ||
.query(true) | ||
.reply(200, { | ||
subject_workflow_statuses: [ | ||
{ classifications_count: 0, retired_at: null, links: { subject: '1' }}, | ||
{ classifications_count: 3, retired_at: null, links: { subject: '2' }}, | ||
{ classifications_count: 5, retired_at: "2018-01-30T21:09:49.396Z", links: { subject: '3' }} | ||
] | ||
}) | ||
data = await fetchRows({ columns, rows }, workflow) | ||
}) | ||
|
||
it('should generate subject data table rows with classification statuses', function () { | ||
expect(data).to.deep.equal(expectedData) | ||
}) | ||
}) |
11 changes: 5 additions & 6 deletions
11
packages/app-project/src/shared/components/SubjectPicker/helpers/fetchSubjects.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 |
---|---|---|
@@ -1,15 +1,14 @@ | ||
const API_HOST = 'https://redsea.zooniverse.org/search' | ||
const API_HOST = 'https://subject-set-search-api.zooniverse.org/subjects' | ||
|
||
export default async function fetchSubjects( | ||
subjectSetID, | ||
query='', | ||
sortField='subject_id', | ||
sortOrder='asc', | ||
sortField='priority', | ||
page_size=20 | ||
) { | ||
const url = `${API_HOST}/${subjectSetID}?${query}&limit=${page_size}&sort_field=${sortField}&sort_order=${sortOrder}` | ||
const url = `${API_HOST}/${subjectSetID}.json?${query}&_sort=${sortField}` | ||
const mode = 'cors' | ||
const response = await fetch(url, { mode }) | ||
const results = await response.json() | ||
return results | ||
const data = await response.json() | ||
return data | ||
} |
6 changes: 3 additions & 3 deletions
6
packages/app-project/src/shared/components/SubjectPicker/helpers/searchParams.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 |
---|---|---|
@@ -1,10 +1,10 @@ | ||
export default function searchParams(data) { | ||
let query = '' | ||
let query = [] | ||
Object.entries(data).forEach(([key, value]) => { | ||
if (value !== '') { | ||
query += `@${key}:${value}*` | ||
query.push(`${key}__contains=${encodeURIComponent(value)}`) | ||
} | ||
}) | ||
const urlParams = (query !== '') ? `filter_field=${query}` : '' | ||
const urlParams = query.join('&') | ||
return urlParams | ||
} |
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