Skip to content

Commit

Permalink
Log endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
dqbd committed Oct 9, 2023
1 parent 3d00432 commit 4b54bc8
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 0 deletions.
45 changes: 45 additions & 0 deletions src/pages/api/data/[folder]/log/slice.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { NextApiRequest, NextApiResponse } from "next"

import { createPersistentDatabase } from "shared/database"
import { loadServerConfig } from "shared/config"
import { runMiddleware } from "utils/middleware"
import cors from "cors"

const dbRef = createPersistentDatabase()

function parseNumberQuery(value: string | string[] | undefined) {
if (typeof value === "string") {
return Number(value)
}
return undefined
}
export default async function handler(
req: NextApiRequest,
res: NextApiResponse
) {
await runMiddleware(req, res, cors({ methods: ["GET", "HEAD"] }))

const { authConfig } = await loadServerConfig()
const db = dbRef.create(authConfig.database)

const folder = req.query.folder as string
if (!folder) return res.status(400).send("Missing from parameter")

const fromSec = parseNumberQuery(req.query.from)
let toSec = parseNumberQuery(req.query.to)
const length = parseNumberQuery(req.query.length)

if (fromSec == null || (toSec == null && length == null))
return res.status(400).send("No query parameters set")

if (toSec != null && length != null)
return res
.status(400)
.send("Invalid parameters, can't have `to` and `length` at the same time")

if (length) toSec = fromSec + length

if (toSec == null) return res.status(400).send("Missing to parameter")

res.send(await db.logRange(folder, fromSec, toSec))
}
29 changes: 29 additions & 0 deletions src/pages/api/data/[folder]/log/stream.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { NextApiRequest, NextApiResponse } from "next"
import { loadServerConfig } from "shared/config"
import { createPersistentDatabase } from "shared/database"
import { runMiddleware } from "utils/middleware"
import cors from "cors"

const dbRef = createPersistentDatabase()

const RANGE = 10 * 2 // 20s

export default async function handler(
req: NextApiRequest,
res: NextApiResponse
) {
await runMiddleware(req, res, cors({ methods: ["GET", "HEAD"] }))

const { authConfig } = await loadServerConfig()
const db = dbRef.create(authConfig.database)

const folder = req.query.folder as string
const shift = Number(req.query.shift || 0)

if (!folder) return res.status(400).send("Invalid parameters")
const timestampSec = Math.floor((Date.now() - shift * 1000) / 1000)

res.send(
await db.logRange(folder, timestampSec - RANGE, timestampSec + RANGE)
)
}
11 changes: 11 additions & 0 deletions src/shared/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,17 @@ export class Database {
.select()
}

async logRange(camera: string, fromSec: number, toSec: number) {
const from = new Date(fromSec * 1000)
const to = new Date(toSec * 1000)

return this.knex
.select("*")
.from<LogTable>(`${camera}_log`)
.whereBetween("timestamp", [from, to])
.orderBy("timestamp", "asc")
}

async seekFrom(
camera: string,
fromSec: number,
Expand Down

0 comments on commit 4b54bc8

Please sign in to comment.