Skip to content

Commit

Permalink
Set started_at when annotation._inProgress changes
Browse files Browse the repository at this point in the history
A new, empty annotation is created when the subject first loads, so wait until `annotation._inProgress` is true before setting the classification start time.
  • Loading branch information
eatyourgreens committed Nov 7, 2024
1 parent d6fbdbe commit 837a0bd
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 7 deletions.
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,19 @@ 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 => {
if (annotation._inProgress) {
inProgress = true
}
})
return inProgress
},

toSnapshot () {
let snapshot = getSnapshot(self)
let annotations = []
Expand Down Expand Up @@ -58,5 +72,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()
}
}

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

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

export default types.compose('ClassificationResource', Resource, AnnotationsStore, Classification)
4 changes: 0 additions & 4 deletions packages/lib-classifier/src/store/ClassificationStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,6 @@ const ClassificationStore = types

if (validClassificationReference) {
const classification = self.active
if (classification?.annotations.size === 0) {
// update startedAt if we're starting a new classification
classification.metadata.startedAt = (new Date()).toISOString()
}
if (classification) {
return classification.addAnnotation(task, annotationValue)
}
Expand Down
6 changes: 4 additions & 2 deletions packages/lib-classifier/src/store/ClassificationStore.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,12 @@ describe('Model > ClassificationStore', function () {
})
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.
classifications.addAnnotation(taskSnapshot, singleChoiceAnnotationSnapshot.value)
annotation.update(0)
clock.tick(30 * 1000) // wait for 30 seconds before finishing the classification.
classifications.addAnnotation(taskSnapshot, 1)
annotation.update(1)
classifications.completeClassification()
})

Expand Down

0 comments on commit 837a0bd

Please sign in to comment.