Skip to content

Commit

Permalink
#593 create GET /detections/summary
Browse files Browse the repository at this point in the history
  • Loading branch information
grindarius committed Apr 25, 2024
1 parent 6724c0b commit f77df84
Show file tree
Hide file tree
Showing 6 changed files with 513 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 1.4.0 (2024-04-xx)
### Common
* **core**: Create `GET /detections/summary` and `GET /classifier-id/{jobId}/best-detections/summary` to get detection counts based on each validation status.

## 1.3.9 (2024-04-xx)
### Common
* **core**: `GET /detections` return `Total-items` in response headers
Expand Down
27 changes: 27 additions & 0 deletions core/_docs/modelSchemas.json
Original file line number Diff line number Diff line change
Expand Up @@ -1248,5 +1248,32 @@
"$ref": "#/components/schemas/ClassifierJobReviewStatus"
}
}
},
"DetectionsResultSummary": {
"type": "object",
"properties": {
"unreviewed": {
"type": "integer",
"example": 183
},
"confirmed": {
"type": "integer",
"example": 18
},
"uncertain": {
"type": "integer",
"example": 12
},
"rejected": {
"type": "integer",
"example": 0
}
},
"required": [
"unreviewed",
"confirmed",
"uncertain",
"rejected"
]
}
}
2 changes: 1 addition & 1 deletion core/detections/best-detections.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const Converter = require('../../common/converter')
/**
* @swagger
*
* /{jobId}/best-detections:
* /classifier-jobs/{jobId}/best-detections:
* get:
* summary: Get list of detections
* description:
Expand Down
41 changes: 41 additions & 0 deletions core/detections/dao/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const { toCamelObject } = require('../../_utils/formatters/string-cases')
const { getAccessibleObjectsIDs, STREAM, PROJECT } = require('../../roles/dao')
const { ValidationError } = require('../../../common/error-handling/errors')
const { REVIEW_STATUS_MAPPING } = require('./review')
const _ = require('lodash')

Check failure on line 10 in core/detections/dao/index.js

View workflow job for this annotation

GitHub Actions / Build, Lint, Test / build

'_' is assigned a value but never used

const availableIncludes = [
Stream.include(),
Expand Down Expand Up @@ -255,12 +256,52 @@ async function timeAggregatedQuery (start, end, streams, streamsOnlyPublic, clas
.then(detections => detections.map(propertyToFloat(aggregatedValueAttribute)))
}

/**
* Get a count of detections based on given where options.
* @param {*} filters Additional query options
* @param {string[]} filters.streams Filter by one or more stream identifiers
* @param {string[]} filters.projects Filter by one or more project identifiers
* @param {string[]} filters.classifiers Filter by one or more classifier identifiers
* @param {string[]} filters.classifications Filter by one or more classification values
* @param {string[]} filters.minConfidence Filter by minimum confidence
* @param {string[]} filters.isReviewed Filter by reviewed/unreviewed detections
* @param {string[]} filters.isPositive Filter by approved/rejected detections
* @param {*} options Additional get options
* @param {number} options.readableBy Include only if the detection is accessible to the given user id
* @param {string[]} options.fields Attributes and relations to include in results (defaults to lite attributes)
* @param {boolean} options.descending Order the results in descending date order
* @param {number} options.limit
* @param {number} options.offset
* @returns {Record<'unreviewed' | 'rejected' | 'uncertain' | 'confirmed', number>} Detection counts
*/
async function queryDetectionsSummary (filters, options) {
const opts = await defaultQueryOptions(filters, options)

const counts = await Detection.findAll({
attributes: [
'review_status',
[sequelize.literal('COUNT(1)::integer'), 'count']
],
where: opts.where,
group: ['"Detection"."review_status"'],
raw: true
})

return {
unreviewed: counts.find(c => c.review_status === null)?.count ?? 0,
rejected: counts.find(c => c.review_status === -1)?.count ?? 0,
uncertain: counts.find(c => c.review_status === 0)?.count ?? 0,
confirmed: counts.find(c => c.review_status === 1)?.count ?? 0
}
}

const DEFAULT_IGNORE_THRESHOLD = 0.5

module.exports = {
availableIncludes,
query,
queryBestDetections,
timeAggregatedQuery,
queryDetectionsSummary,
DEFAULT_IGNORE_THRESHOLD
}
Loading

0 comments on commit f77df84

Please sign in to comment.