26
26
27
27
-module (entop_view ).
28
28
29
- -include (" entop.hrl" ).
30
29
-include_lib (" cecho/include/cecho.hrl" ).
31
30
32
31
% % Module API
35
34
% % Defines
36
35
-define (MAX_HLINE , 300 ).
37
36
37
+ % % Records
38
+ -record (state , { callback = entop_format ,
39
+ remote_module = entop_collector ,
40
+ maxyx ,
41
+ columns ,
42
+ cbstate ,
43
+ node ,
44
+ otp_version ,
45
+ erts_version ,
46
+ os_fam ,
47
+ os ,
48
+ os_version ,
49
+ node_flags ,
50
+ interval = 2000 ,
51
+ reverse_sort = true ,
52
+ sort = 0 ,
53
+ connected = false ,
54
+ last_reductions = dict :new () }).
55
+
38
56
% % =============================================================================
39
57
% % Module API
40
58
% % =============================================================================
41
- start (State ) ->
59
+ start (Node ) ->
42
60
Parent = self (),
43
- NState = load_remote_static_data (State ),
44
- remote_load_code (NState # state .remote_module , State # state .node ),
45
- ViewPid = erlang :spawn (fun () -> init (Parent , NState ) end ),
46
- receive continue -> ok end ,
47
- ViewPid .
61
+ case net_kernel :connect (Node ) of
62
+ true ->
63
+ State = # state { node = Node , connected = true },
64
+ NState = load_remote_static_data (State ),
65
+ remote_load_code (NState # state .remote_module , State # state .node ),
66
+ ViewPid = erlang :spawn (fun () -> init (Parent , NState ) end ),
67
+ receive continue -> ok end ,
68
+ {ok , ViewPid };
69
+ false ->
70
+ {error , cant_connect }
71
+ end .
48
72
49
73
reload (ViewPid ) ->
50
74
ViewPid ! reload .
@@ -70,28 +94,32 @@ remote_load_code(Module, Node) ->
70
94
{_ , Binary , Filename } = code :get_object_code (Module ),
71
95
rpc :call (Node , code , load_binary , [Module , Filename , Binary ]).
72
96
73
- init (Parent , State ) ->
97
+ init (Parent , State0 ) ->
74
98
process_flag (trap_exit , true ),
75
99
application :start (cecho ),
76
100
ok = cecho :cbreak (),
77
101
ok = cecho :noecho (),
78
102
ok = cecho :curs_set (? ceCURS_INVISIBLE ),
79
103
ok = cecho :keypad (? ceSTDSCR , true ),
80
- NState = init_callback (State ),
81
- print_nodeinfo (NState ),
104
+ % keep track of the max screen size so we can resize if necessary
105
+ MaxYX = cecho :getmaxyx (),
106
+ State1 = State0 # state { maxyx = MaxYX },
107
+
108
+ State2 = init_callback (State1 ),
109
+ print_nodeinfo (State2 ),
82
110
Parent ! continue ,
83
111
self () ! time_update ,
84
- loop (Parent , NState ).
112
+ loop (Parent , State2 ).
85
113
86
114
init_callback (State ) ->
87
115
case (State # state .callback ):init (State # state .node ) of
88
- {ok , {Columns , DefaultSort }, CBState } when DefaultSort =< length (Columns )
89
- andalso DefaultSort >= 1 ->
90
- NSort = DefaultSort ;
116
+ {ok , {Columns , DefaultSort }, CBState }
117
+ when DefaultSort >= 0 andalso DefaultSort < length (Columns ) ->
118
+ % sorting is based on 0 indices so all columns can be sorted on
119
+ State # state { columns = Columns , cbstate = CBState , sort = DefaultSort };
91
120
{ok , {Columns , _ }, CBState } ->
92
- NSort = 1
93
- end ,
94
- State # state { columns = Columns , cbstate = CBState , sort = NSort }.
121
+ State # state { columns = Columns , cbstate = CBState , sort = 0 }
122
+ end .
95
123
96
124
print_nodeinfo (State ) ->
97
125
cecho :move (0 , 0 ),
@@ -139,10 +167,11 @@ loop(Parent, State) ->
139
167
State2 = update_sort_screen (State , N ),
140
168
loop (Parent , State2 );
141
169
{sort , Direction } ->
142
- case Direction of
143
- next -> State2 = update_sort_screen (State , State # state .sort + 1 );
144
- prev -> State2 = update_sort_screen (State , State # state .sort - 1 )
145
- end ,
170
+ State2 =
171
+ case Direction of
172
+ next -> update_sort_screen (State , State # state .sort + 1 );
173
+ prev -> update_sort_screen (State , State # state .sort - 1 )
174
+ end ,
146
175
loop (Parent , State2 );
147
176
reverse_sort ->
148
177
State2 = fetch_and_update (State # state { reverse_sort = (not State # state .reverse_sort ) }, true ),
@@ -171,29 +200,39 @@ fetch_and_update(State, IsForced) ->
171
200
end .
172
201
173
202
update_sort_screen (State , N ) ->
174
- if N >= 1 andalso N = < length (State # state .columns ) ->
175
- fetch_and_update (State # state { sort = N }, true );
176
- true -> State
203
+ case N >= 0 andalso N < length (State # state .columns ) of
204
+ true -> fetch_and_update (State # state { sort = N }, true );
205
+ false -> State
177
206
end .
178
207
179
- update_screen (Time , HeaderData , RowDataList , State ) ->
180
- print_nodeinfo (State ),
181
- draw_title_bar (State ),
182
- print_showinfo (State , Time ),
183
- {Headers , State1 } = process_header_data (HeaderData , State ),
208
+ update_screen (Time , HeaderData , RowDataList , State0 ) ->
209
+ % check to see if the screen has changed and if it has resize it
210
+ NewMaxYX = {MaxY , MaxX } = cecho :getmaxyx (),
211
+ State1 =
212
+ case NewMaxYX =:= State0 # state .maxyx of
213
+ true -> State0 ;
214
+ false ->
215
+ % then resize the columns if it has
216
+ {ok , Columns } =
217
+ (State0 # state .callback ):resize (MaxX , State0 # state .cbstate ),
218
+ State0 # state {columns = Columns }
219
+ end ,
220
+
221
+ print_nodeinfo (State1 ),
222
+ draw_title_bar (State1 ),
223
+ print_showinfo (State1 , Time ),
224
+ {Headers , State2 } = process_header_data (HeaderData , State1 ),
184
225
lists :foldl (fun (Header , Y ) ->
185
226
cecho :hline ($ , ? MAX_HLINE ),
186
227
cecho :mvaddstr (Y , 0 , Header ), Y + 1
187
228
end , 1 , Headers ),
188
- {RowList , State2 } = process_row_data (RowDataList , State1 ),
189
- SortedRowList = sort (RowList , State ),
190
- {Y , X } = cecho :getmaxyx (),
191
- {ok , Columns } = (State # state .callback ):resize (X , State2 # state .cbstate ),
192
- StartY = (Y - (Y - 8 )),
193
- lists :foreach (fun (N ) -> cecho :move (N , 0 ), cecho :hline ($ , ? MAX_HLINE ) end , lists :seq (StartY , Y )),
194
- update_rows (SortedRowList , Columns , StartY , Y ),
229
+ {RowList , State3 } = process_row_data (RowDataList , State2 ),
230
+ SortedRowList = sort (RowList , State3 ),
231
+ StartY = (MaxY - (MaxY - 8 )),
232
+ lists :foreach (fun (N ) -> cecho :move (N , 0 ), cecho :hline ($ , ? MAX_HLINE ) end , lists :seq (StartY , MaxY )),
233
+ update_rows (SortedRowList , State3 # state .columns , StartY , MaxY ),
195
234
cecho :refresh (),
196
- State2 # state { columns = Columns } .
235
+ State3 .
197
236
198
237
draw_title_bar (State ) ->
199
238
cecho :move (7 , 0 ),
@@ -211,7 +250,7 @@ draw_title_bar([{Title, Width, Options}|Rest], Offset) ->
211
250
print_showinfo (State , RoundTripTime ) ->
212
251
cecho :move (6 , 0 ),
213
252
cecho :hline ($ , ? MAX_HLINE ),
214
- ColName = element (1 ,lists :nth (State # state .sort , State # state .columns )),
253
+ ColName = element (1 ,lists :nth (State # state .sort + 1 , State # state .columns )),
215
254
SortName = if State # state .reverse_sort -> " Descending" ; true -> " Ascending" end ,
216
255
Showing = io_lib :format (" Interval ~p ms, Sorting on ~p (~s ), Retrieved in ~p ms" ,
217
256
[State # state .interval , ColName , SortName , RoundTripTime div 1000 ]),
@@ -241,7 +280,7 @@ prd([RowData|Rest], #state{last_reductions = LRD} = State, FullAcc = {Acc, LRDAc
241
280
end .
242
281
243
282
sort (ProcList , State ) ->
244
- Sorted = lists :keysort (State # state .sort , ProcList ),
283
+ Sorted = lists :keysort (State # state .sort + 1 , ProcList ),
245
284
case State # state .reverse_sort of
246
285
true ->
247
286
lists :reverse (Sorted );
0 commit comments