|
| 1 | +import gleam/dynamic |
| 2 | +import gleam/dynamic/decode |
| 3 | +import gleam/int |
| 4 | +import lustre |
| 5 | +import lustre/attribute |
| 6 | +import lustre/element.{type Element} |
| 7 | +import lustre/element/html |
| 8 | +import lustre/event |
| 9 | + |
| 10 | +pub type Model { |
| 11 | + Model( |
| 12 | + erlang_version: String, |
| 13 | + atom_count: Int, |
| 14 | + port_count: Int, |
| 15 | + process_count: Int, |
| 16 | + uptime: Int, |
| 17 | + total_memory_usage: Int, |
| 18 | + ) |
| 19 | +} |
| 20 | + |
| 21 | +pub type Msg { |
| 22 | + UserRequestedReload |
| 23 | +} |
| 24 | + |
| 25 | +pub fn component() { |
| 26 | + lustre.simple(init, update, view) |
| 27 | +} |
| 28 | + |
| 29 | +pub fn init(_flags) -> Model { |
| 30 | + compute_model() |
| 31 | +} |
| 32 | + |
| 33 | +pub fn update(_model: Model, msg: Msg) -> Model { |
| 34 | + case msg { |
| 35 | + UserRequestedReload -> compute_model() |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +pub fn view(model: Model) -> Element(Msg) { |
| 40 | + html.div( |
| 41 | + [ |
| 42 | + attribute.class( |
| 43 | + "mx-auto mt-10 p-6 bg-white rounded-xl shadow-md space-y-4", |
| 44 | + ), |
| 45 | + ], |
| 46 | + [ |
| 47 | + html.h2( |
| 48 | + [ |
| 49 | + attribute.class( |
| 50 | + "text-2xl font-bold text-gray-800 mb-4 flex items-center justify-between", |
| 51 | + ), |
| 52 | + ], |
| 53 | + [ |
| 54 | + html.text("BEAM Info"), |
| 55 | + html.button( |
| 56 | + [ |
| 57 | + attribute.class("ml-4 p-2 rounded hover:bg-gray-100"), |
| 58 | + attribute.title("Reload"), |
| 59 | + event.on_click(UserRequestedReload), |
| 60 | + ], |
| 61 | + [html.text("🔄")], |
| 62 | + ), |
| 63 | + ], |
| 64 | + ), |
| 65 | + html.dl([attribute.class("divide-y divide-gray-200")], [ |
| 66 | + html.div([attribute.class("py-2 flex justify-between")], [ |
| 67 | + html.dt([attribute.class("font-medium text-gray-600")], [ |
| 68 | + html.text("Erlang Version"), |
| 69 | + ]), |
| 70 | + html.dd([attribute.class("text-gray-900")], [ |
| 71 | + html.text(model.erlang_version), |
| 72 | + ]), |
| 73 | + ]), |
| 74 | + html.div([attribute.class("py-2 flex justify-between")], [ |
| 75 | + html.dt([attribute.class("font-medium text-gray-600")], [ |
| 76 | + html.text("Atoms Count"), |
| 77 | + ]), |
| 78 | + html.dd([attribute.class("text-gray-900")], [ |
| 79 | + html.text(int.to_string(model.atom_count)), |
| 80 | + ]), |
| 81 | + ]), |
| 82 | + html.div([attribute.class("py-2 flex justify-between")], [ |
| 83 | + html.dt([attribute.class("font-medium text-gray-600")], [ |
| 84 | + html.text("Ports Count"), |
| 85 | + ]), |
| 86 | + html.dd([attribute.class("text-gray-900")], [ |
| 87 | + html.text(int.to_string(model.port_count)), |
| 88 | + ]), |
| 89 | + ]), |
| 90 | + html.div([attribute.class("py-2 flex justify-between")], [ |
| 91 | + html.dt([attribute.class("font-medium text-gray-600")], [ |
| 92 | + html.text("Processes Count"), |
| 93 | + ]), |
| 94 | + html.dd([attribute.class("text-gray-900")], [ |
| 95 | + html.text(int.to_string(model.process_count)), |
| 96 | + ]), |
| 97 | + ]), |
| 98 | + html.div([attribute.class("py-2 flex justify-between")], [ |
| 99 | + html.dt([attribute.class("font-medium text-gray-600")], [ |
| 100 | + html.text("Uptime (s)"), |
| 101 | + ]), |
| 102 | + html.dd([attribute.class("text-gray-900")], [ |
| 103 | + html.text(int.to_string(model.uptime)), |
| 104 | + ]), |
| 105 | + ]), |
| 106 | + html.div([attribute.class("py-2 flex justify-between")], [ |
| 107 | + html.dt([attribute.class("font-medium text-gray-600")], [ |
| 108 | + html.text("Total Memory Usage (bytes)"), |
| 109 | + ]), |
| 110 | + html.dd([attribute.class("text-gray-900")], [ |
| 111 | + html.text(int.to_string(model.total_memory_usage)), |
| 112 | + ]), |
| 113 | + ]), |
| 114 | + ]), |
| 115 | + ], |
| 116 | + ) |
| 117 | +} |
| 118 | + |
| 119 | +fn compute_model() -> Model { |
| 120 | + let assert Ok(erlang_version) = decode_to_string(erlang_version()) |
| 121 | + let assert Ok(atom_count) = decode_to_int(atom_count()) |
| 122 | + let assert Ok(port_count) = decode_to_int(port_count()) |
| 123 | + let assert Ok(process_count) = decode_to_int(process_count()) |
| 124 | + let assert Ok(uptime) = decode_to_int(uptime()) |
| 125 | + let assert Ok(total_memory_usage) = decode_to_int(total_memory_usage()) |
| 126 | + |
| 127 | + Model( |
| 128 | + erlang_version:, |
| 129 | + atom_count:, |
| 130 | + port_count:, |
| 131 | + process_count:, |
| 132 | + uptime:, |
| 133 | + total_memory_usage:, |
| 134 | + ) |
| 135 | +} |
| 136 | + |
| 137 | +fn decode_to_string(value: dynamic.Dynamic) { |
| 138 | + decode.run(value, decode.string) |
| 139 | +} |
| 140 | + |
| 141 | +fn decode_to_int(value: dynamic.Dynamic) { |
| 142 | + decode.run(value, decode.int) |
| 143 | +} |
| 144 | + |
| 145 | +@external(erlang, "lissome_ffi", "erlang_version") |
| 146 | +fn erlang_version() -> dynamic.Dynamic |
| 147 | + |
| 148 | +@external(erlang, "lissome_ffi", "atoms_count") |
| 149 | +fn atom_count() -> dynamic.Dynamic |
| 150 | + |
| 151 | +@external(erlang, "lissome_ffi", "ports_count") |
| 152 | +fn port_count() -> dynamic.Dynamic |
| 153 | + |
| 154 | +@external(erlang, "lissome_ffi", "processes_count") |
| 155 | +fn process_count() -> dynamic.Dynamic |
| 156 | + |
| 157 | +@external(erlang, "lissome_ffi", "uptime") |
| 158 | +fn uptime() -> dynamic.Dynamic |
| 159 | + |
| 160 | +@external(erlang, "lissome_ffi", "total_memory_usage") |
| 161 | +fn total_memory_usage() -> dynamic.Dynamic |
0 commit comments