package octez-shell-libs

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file client_event_logging_commands.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
(*****************************************************************************)
(*                                                                           *)
(* Open Source License                                                       *)
(* Copyright (c) 2018 Dynamic Ledger Solutions, Inc. <contact@tezos.com>     *)
(*                                                                           *)
(* Permission is hereby granted, free of charge, to any person obtaining a   *)
(* copy of this software and associated documentation files (the "Software"),*)
(* to deal in the Software without restriction, including without limitation *)
(* the rights to use, copy, modify, merge, publish, distribute, sublicense,  *)
(* and/or sell copies of the Software, and to permit persons to whom the     *)
(* Software is furnished to do so, subject to the following conditions:      *)
(*                                                                           *)
(* The above copyright notice and this permission notice shall be included   *)
(* in all copies or substantial portions of the Software.                    *)
(*                                                                           *)
(* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR*)
(* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,  *)
(* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL   *)
(* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER*)
(* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING   *)
(* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER       *)
(* DEALINGS IN THE SOFTWARE.                                                 *)
(*                                                                           *)
(*****************************************************************************)

let group =
  Tezos_clic.
    {
      name = "event-logging-framework";
      title = "Commands to inspect the event-logging framework";
    }

let date_parameter option_name build =
  let open Tezos_clic in
  let open Lwt_result_syntax in
  parameter (fun _ s ->
      let problem fmt = Printf.ksprintf invalid_arg fmt in
      try
        if String.length s <> 8 then problem "date should be `YYYYMMDD`" ;
        String.iteri
          (fun idx -> function
            | '0' .. '9' -> ()
            | other -> problem "character %d is not a digit: '%c'." idx other)
          s ;
        let month = int_of_string (String.sub s 4 2) - 1 in
        if month < 0 then problem "The month cannot be '00'" ;
        if month > 11 then problem "The month cannot be more than '12'" ;
        let day = int_of_string (String.sub s 6 2) in
        if day > 31 then problem "The month cannot be more than '31'" ;
        let t =
          let tm =
            Unix.
              {
                tm_sec = 0;
                tm_min = 0;
                tm_hour = 0;
                tm_mday = day;
                tm_mon = month;
                tm_year = int_of_string (String.sub s 0 4) - 1900;
                tm_wday = 0;
                tm_yday = 0;
                tm_isdst = false;
              }
          in
          Unix.mktime tm |> fst
        in
        return (build t)
      with
      | Invalid_argument e -> failwith "In `%s %S`, %s" option_name s e
      | e -> failwith "Exn: %s" (Printexc.to_string e))

let flat_pp pp o =
  Format.(
    asprintf
      "%a"
      (fun fmt () ->
        pp_set_margin fmt 2_000_000 ;
        pp fmt o)
      ())

let commands () =
  let open Lwt_result_syntax in
  let open Tezos_clic in
  let command ~desc = command ~group ~desc in
  [
    command
      ~desc:"Query the events from an event sink."
      (args7
         (arg
            ~doc:"Filter on event names"
            ~long:"names"
            ~placeholder:"LIST"
            (parameter (fun _ s -> return (String.split_on_char ',' s))))
         (arg
            ~doc:"Filter on event sections (use '_' for no-section)"
            ~long:"sections"
            ~placeholder:"LIST"
            (parameter (fun _ s ->
                 return
                   (String.split_on_char ',' s
                   |> List.map (function "_" -> None | other -> Some other)))))
         (arg
            ~doc:"Filter out events before DATE"
            ~long:"since"
            ~placeholder:"DATE"
            (date_parameter "--since" (fun s -> `Date (`Ge, s))))
         (arg
            ~doc:"Filter out events after DATE"
            ~long:"until"
            ~placeholder:"DATE"
            (date_parameter "--until" (fun s -> `Date (`Le, s))))
         (switch
            ~doc:"Display events as JSON instead of pretty-printing them"
            ~long:"as-json"
            ())
         (switch ~doc:"Try to display unknown events" ~long:"dump-unknown" ())
         (Scriptable.clic_arg ()))
      (prefixes ["query"; "events"; "from"]
      @@ param
           ~name:"Sink-Name"
           ~desc:"The URI of the SINK to query"
           (parameter (fun _ s -> return (Uri.of_string s)))
      @@ stop)
      (fun ( only_names,
             only_sections,
             since,
             until,
             as_json,
             dump_unknown,
             scriptable )
           uri
           (cctxt : #Client_context.full) ->
        match Uri.scheme uri with
        | None | Some "unix-files" -> (
            let script_row kind date evname data () =
              [kind; date; evname; data]
            in
            let* () =
              Scriptable.output_for_human scriptable (fun () ->
                  let*! () = cctxt#message "### Events" in
                  return_unit)
            in
            let on_unknown =
              if not dump_unknown then None
              else
                Some
                  (fun path ->
                    Scriptable.output_row
                      scriptable
                      ~for_human:(fun () ->
                        let*! () = cctxt#error "Unknown: %s" path in
                        let*! () =
                          Lwt_stream.iter_s
                            (fun line -> cctxt#message "    |%s" line)
                            (Lwt_io.lines_of_file path)
                        in
                        return_unit)
                      ~for_script:(script_row "unknown-event" "-" "-" path))
            in
            let time_query =
              Option.merge (fun a b -> `And (a, b)) since until
            in
            let* errors_and_warnings, () =
              File_event_sink.Query.fold
                ?only_names
                ?on_unknown
                ?only_sections
                ?time_query
                uri
                ~init:()
                ~f:(fun () ~time_stamp ev ->
                  let o = Internal_event.Generic.explode_event ev in
                  let time_string time_value =
                    let open Unix in
                    let tm = gmtime time_value in
                    Printf.sprintf
                      "%04d%02d%02d-%02d%02d%02d-%04d"
                      (1900 + tm.tm_year)
                      (tm.tm_mon + 1)
                      tm.tm_mday
                      tm.tm_hour
                      tm.tm_min
                      tm.tm_sec
                      ((time_value -. floor time_value) *. 10_000.
                      |> int_of_float)
                  in
                  let pp fmt o =
                    if as_json then Data_encoding.Json.pp fmt o#json
                    else o#pp fmt ()
                  in
                  Scriptable.output_row
                    scriptable
                    ~for_human:(fun () ->
                      let*! () =
                        cctxt#message
                          "@[<2>* [%s %s]@ %a@]"
                          (time_string time_stamp)
                          o#name
                          pp
                          o
                      in
                      return_unit)
                    ~for_script:(fun () ->
                      let text = flat_pp pp o in
                      script_row "event" (time_string time_stamp) o#name text ()))
            in
            match errors_and_warnings with
            | [] -> return_unit
            | errors_and_warnings ->
                let open Format in
                Scriptable.output
                  scriptable
                  ~for_human:(fun () ->
                    let*! () =
                      cctxt#warning
                        "### Some things were not perfect:@.@[<2>%a@]"
                        (pp_print_list
                           ~pp_sep:(fun fmt () -> fprintf fmt "@.")
                           (fun fmt item ->
                             fprintf
                               fmt
                               "* %a"
                               File_event_sink.Query.Report.pp
                               item))
                        errors_and_warnings
                    in
                    return_unit)
                  ~for_script:(fun () ->
                    let make_row e =
                      let text = flat_pp File_event_sink.Query.Report.pp e in
                      let tag =
                        match e with
                        | `Error _ -> "error"
                        | `Warning _ -> "warning"
                      in
                      script_row tag "-" "-" text ()
                    in
                    List.map make_row errors_and_warnings))
        | Some other ->
            let*! () =
              cctxt#message "URI scheme %S not handled as of now." other
            in
            return_unit);
    command
      ~desc:
        "Display configuration/state information about the internal-event \
         logging framework."
      no_options
      (prefixes ["show"; "event-logging"] @@ stop)
      (fun () (cctxt : #Client_context.full) ->
        let pp_event_definitions fmt schs =
          let open Format in
          pp_open_box fmt 0 ;
          pp_print_list
            ~pp_sep:(fun fmt () -> fprintf fmt "@;")
            (fun fmt obj_schema ->
              pp_open_box fmt 2 ;
              fprintf fmt "* `%s`:@ " obj_schema#name ;
              pp_print_text fmt obj_schema#doc ;
              pp_close_box fmt ())
            fmt
            schs ;
          pp_close_box fmt ()
        in
        let*! () =
          cctxt#message
            "Event logging framework:@.Sinks state:@ %a@.Events registered:@ %a"
            Internal_event.All_sinks.pp_state
            ()
            pp_event_definitions
            Internal_event.(
              All_definitions.get () |> List.map Generic.json_schema)
        in
        return_unit);
    command
      ~desc:"Output the JSON schema of an internal-event."
      no_options
      (prefixes ["output"; "schema"; "of"]
      @@ param
           ~name:"Event-Name"
           ~desc:"Name of the event"
           (parameter (fun _ s -> return s))
      @@ prefix "to"
      @@ param
           ~name:"File-path"
           ~desc:"Path to a JSON file"
           (parameter (fun _ s -> return s))
      @@ stop)
      (fun () event path (cctxt : #Client_context.full) ->
        let open Internal_event in
        match All_definitions.find (( = ) event) with
        | None -> failwith "Event %S not found" event
        | Some ev ->
            let o = Generic.json_schema ev in
            let*! () =
              Lwt_io.with_file ~mode:Lwt_io.output path (fun chan ->
                  let v = Format.asprintf "%a" Json_schema.pp o#schema in
                  Lwt_io.write chan v)
            in
            let*! () = cctxt#message "Wrote schema of %s to %s" event path in
            return_unit);
  ]
OCaml

Innovation. Community. Security.