/src/visualiser/visualiser.ml

http://github.com/hhughes/ocaml-frui · OCaml · 86 lines · 75 code · 10 blank · 1 comment · 11 complexity · b9cbd870e491db309a6749600d507b29 MD5 · raw file

  1. open Logger
  2. open Msg
  3. open Thread
  4. open Printf
  5. open Fvar
  6. class visualiser (e : Dom.element) t0 t1 m0 m1 =
  7. object (self)
  8. val threads = Hashtbl.create 10
  9. method w = float_of_int (e#_get_offsetWidth - 4) (* would be nice if this were froc-ed *)
  10. method set_msg_loc msg msg_elt s t0 t1 =
  11. let d = t1 -. t0 in
  12. let w = self#w in
  13. let ts = Msg.timestamp msg in
  14. let l = ((ts -. s) *. w) /. d in
  15. ignore (msg_elt#_get_style#_set_left (string_of_int (int_of_float l)))
  16. method set_fun_loc f msg_elt fs fe ts t0 t1 =
  17. let fs = if fs < 0. then m0#get else fs in
  18. let fe = if fe < 0. then m1#get else fe in
  19. let d = t1 -. t0 in
  20. let w = self#w in
  21. let l = ((fs -. ts) *. w) /. d in
  22. let wi = ((fe -. fs) *. w) /. d in
  23. ignore (msg_elt#_get_style#_set_left (string_of_int (int_of_float l)));
  24. ignore (msg_elt#_get_style#_set_width (string_of_int (int_of_float wi)))
  25. method set_thread_loc thread_elt ts tf t0 t1 =
  26. let ts = if ts < 0. then m0#get else ts in
  27. let tf = if tf < 0. then m1#get else tf in
  28. let d = t1 -. t0 in
  29. let w = self#w in
  30. let l = ((ts -. t0) *. w) /. d in
  31. let r = ((tf -. t0) *. w) /. d in
  32. ignore (thread_elt#_get_style#_set_left (string_of_int (int_of_float l)));
  33. ignore (thread_elt#_get_style#_set_width (string_of_int (int_of_float (r -. l))))
  34. method create_event_div (thread_elt : Dom.element) classname title =
  35. let msg_elt = (Dom.document#createElement "div" : Dom.element) in
  36. ignore (msg_elt#_set_className classname);
  37. ignore (msg_elt#_set_title title);
  38. ignore (thread_elt#appendChild msg_elt);
  39. msg_elt
  40. method render_msg (thread_elt : Dom.element) thread = function
  41. | E_msg m ->
  42. let div = self#create_event_div thread_elt "msg" (Msg.desc m) in
  43. m#set_froc_loc (Froc.lift3 (self#set_msg_loc m div) thread#start#b t0 t1)
  44. | E_fn f ->
  45. let div = self#create_event_div thread_elt "fn" f#name in
  46. f#set_froc_loc (Froc.lift5 (self#set_fun_loc f div) f#start#b f#finish#b thread#start#b t0 t1)
  47. | Dummy -> ()
  48. method render_thread id thread =
  49. let thread_elt = (Dom.document#createElement "div" : Dom.element) in
  50. ignore (thread_elt#_set_className "thread");
  51. ignore (thread_elt#_get_style#_set_top (string_of_int (25 * thread#index)));
  52. ignore (Froc.lift4 (self#set_thread_loc thread_elt) thread#start#b thread#finish#b t0 t1);
  53. ignore (e#appendChild thread_elt);
  54. thread_elt
  55. method add_to_thread msg =
  56. (*debug (sprintf "event of type %s" (Msg.ty msg));*)
  57. let tid = Msg.threadId msg in
  58. (if not (Hashtbl.mem threads tid) then
  59. begin
  60. let thread = new thread tid in
  61. Hashtbl.add threads tid (thread);
  62. let thread_elt = self#render_thread tid thread in
  63. thread#msgs#lift (self#render_msg thread_elt thread)
  64. end);
  65. let thread = Hashtbl.find threads tid in
  66. thread#parse_msg msg;
  67. let ts = Msg.timestamp msg in
  68. if ts >= 0. then
  69. begin
  70. if ts < m0#get or m0#get = (-1.) then m0#set ts;
  71. if ts > m1#get or m1#get = (-1.) then m1#set ts;
  72. end;
  73. end
  74. let init (elt : Dom.element) t0 t1 m0 m1 =
  75. let v = new visualiser elt t0 t1 m0 m1 in
  76. v