80 lines
1.7 KiB
Nim
80 lines
1.7 KiB
Nim
import json, options, times, timeutils, uuids
|
|
|
|
type
|
|
User* = object
|
|
id*: UUID
|
|
displayName*: string
|
|
email*: string
|
|
hashedPwd*: string
|
|
salt*: string
|
|
isAdmin*: bool
|
|
|
|
ApiToken* = object
|
|
id*: UUID
|
|
userId*: UUID
|
|
name*:string
|
|
created*: DateTime
|
|
expires*: Option[DateTime]
|
|
hashedToken*: string
|
|
|
|
Measure* = object
|
|
id*: UUID
|
|
userId*: UUID
|
|
slug*: string
|
|
name*: string
|
|
description*: string
|
|
config*: JsonNode
|
|
|
|
Measurement* = object
|
|
id*: UUID
|
|
measureId*: UUID
|
|
value*: float
|
|
timestamp*: DateTime
|
|
extData*: JsonNode
|
|
|
|
ClientLogEntry* = object
|
|
id*: int
|
|
userId*: UUID
|
|
level*: string
|
|
message*: string
|
|
scope*: string
|
|
stacktrace*: string
|
|
timestamp*: DateTime
|
|
|
|
proc `$`*(u: User): string =
|
|
return "User " & ($u.id)[0..6] & " - " & u.displayName & " <" & u.email & ">"
|
|
|
|
proc `$`*(tok: ApiToken): string =
|
|
result = "ApiToken " & ($tok.id)[0..6] & " - " & tok.name
|
|
if tok.expires.isSome: result &= " (expires " & tok.expires.get.formatIso8601 & ")"
|
|
else: result &= " (no expiry)"
|
|
|
|
proc `$`*(m: Measure): string =
|
|
return "Measure " & ($m.id)[0..6] & " - " & m.slug
|
|
|
|
proc `$`*(v: Measurement): string =
|
|
return "Measurement " & ($v.id)[0..6] & " - " & ($v.measureId)[0..6] & " = " & $v.value
|
|
|
|
proc `%`*(uuid: UUID): JsonNode = %($uuid)
|
|
|
|
proc `%`*(dt: DateTime): JsonNode = %(dt.formatIso8601)
|
|
|
|
proc `%`*(u: User): JsonNode =
|
|
result = %*{
|
|
"id": $u.id,
|
|
"email": u.email,
|
|
"displayName": u.displayName,
|
|
"isAdmin": u.isAdmin
|
|
}
|
|
|
|
proc `%`*(tok: ApiToken): JsonNode =
|
|
result = %*{
|
|
"id": $tok.id,
|
|
"userId": $tok.userId,
|
|
"name": tok.name,
|
|
"created": tok.created
|
|
}
|
|
|
|
if tok.expires.isSome:
|
|
result["expires"] = %(tok.expires.get.formatIso8601)
|