Skip to content

Commit

Permalink
Merge pull request #14 from UniversalDataTool/create-session
Browse files Browse the repository at this point in the history
update post session to support summary mode & update it's test
  • Loading branch information
mrdadah committed Aug 4, 2020
2 parents 8027eb4 + 57fe895 commit 58ef384
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 29 deletions.
1 change: 0 additions & 1 deletion src/db/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ module.exports = (db) => {
CREATE TABLE IF NOT EXISTS session_state (
session_state_id INTEGER PRIMARY KEY AUTOINCREMENT,
short_id TEXT NOT NULL,
udt_json TEXT NOT NULL,
summary_samples TEXT NOT NULL,
summary_version INTEGER NOT NULL DEFAULT 0,
patch TEXT,
Expand Down
26 changes: 16 additions & 10 deletions src/endpoints/post-session.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,34 @@ module.exports = cors(async (req, res) => {
if (!body.udt) return error(res, 400, 'Body should have "udt" key')

const samples = body.udt.samples
delete body.udt.samples
const udt = JSON.stringify(body.udt)
const udt = body.udt
const shortId = randomstring.generate({ length: 6, readable: true })

await db
.prepare("INSERT INTO session_state (short_id, udt_json) VALUES (?, ?)")
.run(shortId, udt)

const samplesSummary = []
if (Array.isArray(samples)) {
const samplesQueries = []
samples.forEach((sample, index) => {
samplesQueries.push(
db
.prepare(
"INSERT INTO sample_state (session_short_id, session_sample_index, content) VALUES (?, ?, ?)"
)
db.prepare("INSERT INTO sample_state (session_short_id, session_sample_index, content) VALUES (?, ?, ?)")
.run(shortId, index, JSON.stringify(sample))
)

samplesSummary.push({ hasAnnotation: false, version: 1 })
})

await Promise.all(samplesQueries)
}

const summary_samples = {
interface: udt.interface,
summary: {
samples: samplesSummary
}
}

await db
.prepare("INSERT INTO session_state (short_id, summary_samples) VALUES (?, ?)")
.run(shortId, JSON.stringify(summary_samples))

return send(res, 200, { short_id: shortId, summary_version: 0 })
})
43 changes: 25 additions & 18 deletions tests/post-session.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ test("Create session", async (t) => {
availableLabels: ["valid", "invalid"],
regionTypesAllowed: ["bounding-box", "polygon", "point"],
},
taskData: [
samples: [
{
imageUrl:
"https://s3.amazonaws.com/asset.workaround.online/example-jobs/sticky-notes/image1.jpg",
Expand All @@ -26,32 +26,39 @@ test("Create session", async (t) => {
imageUrl:
"https://s3.amazonaws.com/asset.workaround.online/example-jobs/sticky-notes/image2.jpg",
},
],
taskOutput: [
[
{
regionType: "bounding-box",
centerX: 0.43416827231856137,
centerY: 0.3111753371868978,
width: 0.09248554913294799,
height: 0.0789980732177264,
color: "hsl(297,100%,50%)",
},
],
null,
],
]
},
});
t.assert(response.short_id);
const sessionAdded = db
.prepare(
`SELECT *
FROM latest_session_state
WHERE short_id = ?
LIMIT 1`
FROM latest_session_state
WHERE short_id = ?
LIMIT 1`
)
.get(response.short_id);
t.assert(sessionAdded);
t.deepEqual(JSON.parse(sessionAdded.summary_samples), {
"interface": {
type: "image_segmentation",
availableLabels: ["valid", "invalid"],
regionTypesAllowed: ["bounding-box", "polygon", "point"],
},
"summary": {
"samples": [
{
"hasAnnotation": false,
"version": 1
},
{
"hasAnnotation": false,
"version": 1
}
]
}
})

db.prepare(`DELETE FROM session_state WHERE short_id = ?`).run(
response.short_id
);
Expand Down

0 comments on commit 58ef384

Please sign in to comment.