Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(lib-classifier): record classification times between the first and last annotation #6449

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import cuid from 'cuid'
import { types, getSnapshot, getType } from 'mobx-state-tree'
import { types, getSnapshot, getType, addDisposer } from 'mobx-state-tree'
import * as tasks from '@plugins/tasks'
import AnnotationsStore from '@store/AnnotationsStore'
import Resource from '@store/Resource'
import ClassificationMetadata from './ClassificationMetadata'
import { autorun } from 'mobx'

const annotationModels = Object.values(tasks).map(task => task.AnnotationModel)

Expand All @@ -19,6 +20,17 @@ const Classification = types
metadata: types.maybe(ClassificationMetadata)
})
.views(self => ({
/**
* Returns false until we start updating task annotations.
*/
get inProgress() {
let inProgress = false
self.annotations.forEach(annotation => {
inProgress ||= annotation._inProgress || annotation._choiceInProgress
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_choiceInProgress is used by the survey task. All other tasks use _inProgress.

})
return inProgress
},

toSnapshot () {
let snapshot = getSnapshot(self)
let annotations = []
Expand Down Expand Up @@ -58,5 +70,23 @@ const Classification = types
newSnapshot.annotations = Object.values(snapshot.annotations)
return newSnapshot
})
.actions(self => {
function _onAnnotationsChange () {
// set started at when inProgress changes from false to true
if (self.inProgress) {
self.setStartedAt()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you comment this out, the tests should fail.

}
}

return ({
afterAttach () {
addDisposer(self, autorun(_onAnnotationsChange))
},

setStartedAt () {
self.metadata.startedAt = new Date().toISOString()
}
})
})

export default types.compose('ClassificationResource', Resource, AnnotationsStore, Classification)
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@ const ClassificationMetadata = types.model('ClassificationMetadata', {
.actions(self => ({
afterAttach() {
function _onLocaleChange() {
self.update({
userLanguage: getRoot(self)?.locale
})
const userLanguage = getRoot(self)?.locale
if (userLanguage) {
self.update({ userLanguage })
}
Comment on lines +39 to +42
Copy link
Contributor Author

@eatyourgreens eatyourgreens Nov 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't related to the timestamps bug but fixes the store error in #6448.

}
addDisposer(self, autorun(_onLocaleChange))
},
Expand Down
57 changes: 57 additions & 0 deletions packages/lib-classifier/src/store/ClassificationStore.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ describe('Model > ClassificationStore', function () {
describe('when a subject advances', function () {
let classifications
let rootStore

beforeEach(function () {
rootStore = setupStores({
dataVisAnnotating: {},
Expand Down Expand Up @@ -106,6 +107,62 @@ describe('Model > ClassificationStore', function () {
})

describe('on complete classification', function () {
describe('submitted classifications', function () {
let classifications
let rootStore
let clock
let submittedClassification

before(function () {
clock = sinon.useFakeTimers(new Date('2024-11-06T13:00:00Z'))
rootStore = setupStores({
dataVisAnnotating: {},
drawing: {},
feedback: {},
fieldGuide: {},
subjectViewer: {},
tutorials: {},
workflowSteps: {},
userProjectPreferences: {}
})

classifications = rootStore.classifications
classifications.setOnComplete(snapshot => {
submittedClassification = snapshot
})
const taskSnapshot = Object.assign({}, singleChoiceTaskSnapshot, { taskKey: singleChoiceAnnotationSnapshot.task })
taskSnapshot.createAnnotation = () => SingleChoiceAnnotation.create(singleChoiceAnnotationSnapshot)
const classification = classifications.active
const annotation = classification.createAnnotation(taskSnapshot)
clock.tick(1 * 60 * 60 * 1000) // wait for one hour before starting the classification.
annotation.update(0)
clock.tick(30 * 1000) // wait for 30 seconds before finishing the classification.
annotation.update(1)
classifications.completeClassification()
})

after(function () {
clock.restore()
})

it('should record the started at time', function () {
const startedAt = submittedClassification.metadata.started_at
expect(startedAt).to.equal('2024-11-06T14:00:00.000Z')
})

it('should record the finished at time', function () {
const finishedAt = submittedClassification.metadata.finished_at
expect(finishedAt).to.equal('2024-11-06T14:00:30.000Z')
})

it('should only record time spent classifying', function () {
const startedAt = submittedClassification.metadata.started_at
const finishedAt = submittedClassification.metadata.finished_at
const timeSpent = (new Date(finishedAt) - new Date(startedAt)) / 1000
expect(timeSpent).to.equal(30)
})
})

describe('with invalid feedback', function () {
let classifications
let rootStore
Expand Down