/
main.topscript
251 lines (251 loc) · 6.38 KB
/
main.topscript
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
type host_info =
{ hostname : string;
os_name : string;
cpu_arch : string;
timestamp : Time.t;
};;
#part 1
#require "core_extended";;
open Core_extended.Std;;
let my_host =
let sh = Shell.sh_one_exn in
{ hostname = sh "hostname";
os_name = sh "uname -s";
cpu_arch = sh "uname -p";
timestamp = Time.now ();
};;
#part 2
my_host.cpu_arch;;
#part 3
type 'a timestamped = { item: 'a; time: Time.t };;
#part 4
let first_timestamped list =
List.reduce list ~f:(fun a b -> if a.time < b.time then a else b)
;;
#part 5
let host_info_to_string { hostname = h; os_name = os;
cpu_arch = c; timestamp = ts;
} =
sprintf "%s (%s / %s, on %s)" h os c (Time.to_sec_string ts);;
host_info_to_string my_host;;
#part 6
type host_info =
{ hostname : string;
os_name : string;
cpu_arch : string;
os_release : string;
timestamp : Time.t;
} ;;
#part 7
#warnings "+9";;
let host_info_to_string { hostname = h; os_name = os;
cpu_arch = c; timestamp = ts;
} =
sprintf "%s (%s / %s, on %s)" h os c (Time.to_sec_string ts);;
#part 8
let host_info_to_string { hostname = h; os_name = os;
cpu_arch = c; timestamp = ts; _
} =
sprintf "%s (%s / %s, on %s)" h os c (Time.to_sec_string ts);;
#part 9
let host_info_to_string { hostname; os_name; cpu_arch; timestamp; _ } =
sprintf "%s (%s / %s) <%s>" hostname os_name cpu_arch
(Time.to_string timestamp);;
#part 10
let my_host =
let sh cmd = Shell.sh_one_exn cmd in
let hostname = sh "hostname" in
let os_name = sh "uname -s" in
let cpu_arch = sh "uname -p" in
let os_release = sh "uname -r" in
let timestamp = Time.now () in
{ hostname; os_name; cpu_arch; os_release; timestamp };;
#part 11
let create_host_info ~hostname ~os_name ~cpu_arch ~os_release =
{ os_name; cpu_arch; os_release;
hostname = String.lowercase hostname;
timestamp = Time.now () };;
#part 12
let create_host_info
~hostname:hostname ~os_name:os_name
~cpu_arch:cpu_arch ~os_release:os_release =
{ os_name = os_name;
cpu_arch = cpu_arch;
os_release = os_release;
hostname = String.lowercase hostname;
timestamp = Time.now () };;
#part 13
type log_entry =
{ session_id: string;
time: Time.t;
important: bool;
message: string;
}
type heartbeat =
{ session_id: string;
time: Time.t;
status_message: string;
}
type logon =
{ session_id: string;
time: Time.t;
user: string;
credentials: string;
}
;;
#part 14
let get_session_id t = t.session_id;;
#part 15
let get_heartbeat_session_id (t:heartbeat) = t.session_id;;
#part 16
let status_and_session t = (t.status_message, t.session_id);;
let session_and_status t = (t.session_id, t.status_message);;
let session_and_status (t:heartbeat) = (t.session_id, t.status_message);;
#part 17
module Log_entry = struct
type t =
{ session_id: string;
time: Time.t;
important: bool;
message: string;
}
end
module Heartbeat = struct
type t =
{ session_id: string;
time: Time.t;
status_message: string;
}
end
module Logon = struct
type t =
{ session_id: string;
time: Time.t;
user: string;
credentials: string;
}
end;;
#part 18
let create_log_entry ~session_id ~important message =
{ Log_entry.time = Time.now (); Log_entry.session_id;
Log_entry.important; Log_entry.message }
;;
#part 19
let create_log_entry ~session_id ~important message =
{ Log_entry.
time = Time.now (); session_id; important; message }
;;
#part 20
let message_to_string { Log_entry.important; message; _ } =
if important then String.uppercase message else message
;;
#part 21
let is_important t = t.Log_entry.important;;
#part 22
type client_info =
{ addr: Unix.Inet_addr.t;
port: int;
user: string;
credentials: string;
last_heartbeat_time: Time.t;
};;
let register_heartbeat t hb =
{ addr = t.addr;
port = t.port;
user = t.user;
credentials = t.credentials;
last_heartbeat_time = hb.Heartbeat.time;
};;
#part 23
let register_heartbeat t hb =
{ t with last_heartbeat_time = hb.Heartbeat.time };;
#part 24
type client_info =
{ addr: Unix.Inet_addr.t;
port: int;
user: string;
credentials: string;
last_heartbeat_time: Time.t;
last_heartbeat_status: string;
};;
#part 25
let register_heartbeat t hb =
{ t with last_heartbeat_time = hb.Heartbeat.time;
last_heartbeat_status = hb.Heartbeat.status_message;
};;
#part 26
type client_info =
{ addr: Unix.Inet_addr.t;
port: int;
user: string;
credentials: string;
mutable last_heartbeat_time: Time.t;
mutable last_heartbeat_status: string;
};;
#part 27
let register_heartbeat t hb =
t.last_heartbeat_time <- hb.Heartbeat.time;
t.last_heartbeat_status <- hb.Heartbeat.status_message
;;
#part 28
let get_users logons =
List.dedup (List.map logons ~f:(fun x -> x.Logon.user));;
#part 29
module Logon = struct
type t =
{ session_id: string;
time: Time.t;
user: string;
credentials: string;
}
with fields
end;;
#part 30
let get_users logons = List.dedup (List.map logons ~f:Logon.user);;
#part 31
Field.get Logon.Fields.user;;
#part 32
Field.get;;
#part 33
let show_field field to_string record =
let name = Field.name field in
let field_string = to_string (Field.get field record) in
name ^ ": " ^ field_string
;;
#part 34
let logon = { Logon.
session_id = "26685";
time = Time.now ();
user = "yminsky";
credentials = "Xy2d9W"; }
;;
show_field Logon.Fields.user Fn.id logon;;
show_field Logon.Fields.time Time.to_string logon;;
#part 35
Logon.Fields.iter;;
#part 36
let print_logon logon =
let print to_string field =
printf "%s\n" (show_field field to_string logon)
in
Logon.Fields.iter
~session_id:(print Fn.id)
~time:(print Time.to_string)
~user:(print Fn.id)
~credentials:(print Fn.id)
;;
print_logon logon;;
#part 37
#part 38
#part 39
#part 40
#part 41
#part 42
#part 43
#part 44
#part 45
#part 46
#part 47
#part 48
#part 49
#part 50