Implement a basic cache dumper
[khatus.git] / src / ocaml / lib / khatus_msg.ml
CommitLineData
c6a7396e
SK
1module Time = Khatus_time
2
3type content =
4 | Alert of {priority : [`low | `med | `hi]; subject : string; body : string}
5 | Data of {key : string list; value : string}
6 | Cache of
7 { mtime : Time.t
8 ; node : string
9 ; modul : string
10 ; key : string list
11 ; value : string
12 }
13 | Error of string
14 | Log of {location : string; level : [`info | `error]; msg : string}
15 | Status_bar of string
16
17type t =
18 {node : string; modul : string; content : content}
19
20let sep_1 = "|"
21let sep_2 = ":"
22
23let to_string {node; modul; content} =
24 match content with
25 | Alert {priority; subject; body} ->
26 let priority =
27 match priority with
28 | `hi -> "hi"
29 | `med -> "med"
30 | `low -> "low"
31 in
32 String.concat sep_1 [node; modul; "alert"; priority; subject; body]
33 | Data {key; value} ->
34 let key = String.concat sep_2 key in
35 String.concat sep_1 [node; modul; "data"; key; value]
36 | Cache {mtime; node=node'; modul=modul'; key; value} ->
37 let key = String.concat sep_2 key in
38 let mtime = Time.to_string mtime in
39 String.concat
40 sep_1
41 [node; modul; "cache"; mtime; node'; modul'; key; value]
42 | Error text ->
43 String.concat sep_1 [node; modul; "error"; text]
44 | Log {location; level; msg} ->
45 let level =
46 match level with
47 | `info -> "info"
48 | `error -> "error"
49 in
50 String.concat sep_1 [node; modul; "log"; location; level; msg]
51 | Status_bar text ->
52 String.concat sep_1 [node; modul; "status_bar"; text]
53
54let next_time t ~node ~time:time0 =
55 match t with
56 | { modul = "khatus_sensor_datetime"
57 ; content = Data {key = ["epoch"]; value = time1}
58 ; node = node'
59 } when node' = node ->
60 (* TODO: Going forawrd, perhaps throwing exceptions is the wrong way. *)
61 (* TODO: Should we check this one at msg parse time? *)
62 Time.of_string time1
63 | {content = Data _; _}
64 | {content = Alert _; _}
65 | {content = Cache _; _}
66 | {content = Error _; _}
67 | {content = Log _; _}
68 | {content = Status_bar _; _}
69 ->
70 time0
This page took 0.025375 seconds and 4 git commands to generate.