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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
use std::collections::HashMap;

use mongodb::bson::DateTime;
use serde::{Deserialize, Serialize};

/// Type of mongo instance.
/// Can be determined with `isMaster` command, see `CommandIsMaster`.
#[derive(Debug, PartialEq, Eq)]
pub enum NodeType {
    Mongod,  // MongoDB daemon
    Mongos,  // Mongo sharding server
    Replset, // <https://docs.mongodb.com/manual/reference/glossary/#term-replica-set>
}

/// <https://docs.mongodb.com/manual/reference/command/isMaster/>
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CommandIsMaster {
    pub msg: Option<String>,
    pub set_name: Option<String>,
    pub hosts: Option<Vec<String>>,
}

/// <https://docs.mongodb.com/manual/reference/command/buildInfo/>
#[derive(Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct CommandBuildInfo {
    pub version: String,
    pub git_version: String,
    pub bits: i64,
    pub debug: bool,
    pub max_bson_object_size: i64,
}

/// <https://docs.mongodb.com/manual/reference/command/serverStatus/>
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CommandServerStatus {
    #[serde(flatten)]
    pub instance: CommandServerStatusInstance,
    pub asserts: CommandServerStatusAsserts,
    pub connections: CommandServerStatusConnections,
    #[serde(rename = "extra_info")]
    pub extra_info: CommandServerStatusExtraInfo,
    #[serde(rename = "mem")]
    pub memory: CommandServerStatusMem,
    pub global_lock: CommandServerStatusGlobalLock,
    pub locks: HashMap<String, CommandServerStatusLock>,
    pub metrics: CommandServerStatusMetrics,
    pub op_latencies: HashMap<String, CommandServerStatusOpLatenciesStat>,
    pub storage_engine: CommandServerStatusStorageEngine,
    pub wired_tiger: Option<CommandServerStatusWiredTiger>,
    pub network: CommandServerStatusNetwork,
    pub opcounters: HashMap<String, i64>,
    pub opcounters_repl: HashMap<String, i64>,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CommandServerStatusInstance {
    pub uptime: f64,
    pub uptime_estimate: i64,
    pub local_time: DateTime,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CommandServerStatusAsserts {
    pub regular: i64,
    pub warning: i64,
    pub msg: i64,
    pub user: i64,
    pub rollovers: i64,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CommandServerStatusConnections {
    pub active: i64,
    pub available: i64,
    pub current: i64,
}

#[derive(Debug, Deserialize)]
pub struct CommandServerStatusExtraInfo {
    pub heap_usage_bytes: Option<i64>,
    pub page_faults: i64,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CommandServerStatusMem {
    pub resident: i64,
    pub r#virtual: i64,
    pub mapped: Option<i64>,
    pub mapped_with_journal: Option<i64>,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CommandServerStatusGlobalLock {
    pub total_time: i64,
    pub active_clients: CommandServerStatusGlobalLockInner,
    pub current_queue: CommandServerStatusGlobalLockInner,
}

#[derive(Debug, Deserialize)]
pub struct CommandServerStatusGlobalLockInner {
    pub total: i64,
    pub readers: i64,
    pub writers: i64,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CommandServerStatusLock {
    pub time_acquiring_micros: Option<CommandServerStatusLockModes>,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CommandServerStatusLockModes {
    #[serde(rename = "r")]
    pub read: Option<i64>,
    #[serde(rename = "w")]
    pub write: Option<i64>,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CommandServerStatusMetrics {
    pub cursor: CommandServerStatusMetricsCursor,
    pub document: CommandServerStatusMetricsDocument,
    pub get_last_error: CommandServerStatusMetricsGetLastError,
    pub operation: CommandServerStatusMetricsOperation,
    pub query_executor: CommandServerStatusMetricsQueryExecutor,
    pub record: Option<CommandServerStatusMetricsRecord>,
    pub repl: CommandServerStatusMetricsRepl,
    pub ttl: CommandServerStatusMetricsReplTtl,
}

#[derive(Debug, Deserialize)]
pub struct CommandServerStatusMetricsCursor {
    #[serde(rename = "timedOut")]
    pub timed_out: i64,
    pub open: CommandServerStatusMetricsCursorOpen,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CommandServerStatusMetricsCursorOpen {
    pub no_timeout: i64,
    pub pinned: i64,
    pub total: i64,
    // Only mongos
    // pub single_target: Option<i64>,
    // pub multi_target: Option<i64>,
}

#[derive(Debug, Deserialize)]
pub struct CommandServerStatusMetricsDocument {
    pub deleted: i64,
    pub inserted: i64,
    pub returned: i64,
    pub updated: i64,
}

#[derive(Debug, Deserialize)]
pub struct CommandServerStatusMetricsGetLastError {
    pub wtime: CommandServerStatusMetricsGetLastErrorWtime,
    pub wtimeouts: i64,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CommandServerStatusMetricsGetLastErrorWtime {
    pub num: i64,
    pub total_millis: i64,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CommandServerStatusMetricsOperation {
    pub scan_and_order: i64,
    pub write_conflicts: i64,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CommandServerStatusMetricsQueryExecutor {
    pub scanned: i64,
    pub scanned_objects: i64,
    pub collection_scans: Option<CommandServerStatusMetricsQueryExecutorCollections>,
}

#[derive(Debug, Deserialize)]
pub struct CommandServerStatusMetricsQueryExecutorCollections {
    pub total: i64,
}

#[derive(Debug, Deserialize)]
pub struct CommandServerStatusMetricsRecord {
    pub moves: i64,
}

#[derive(Debug, Deserialize)]
pub struct CommandServerStatusMetricsRepl {
    pub apply: CommandServerStatusMetricsReplApply,
    pub buffer: CommandServerStatusMetricsReplBuffer,
    pub executor: CommandServerStatusMetricsReplExecutor,
    pub network: CommandServerStatusMetricsReplNetwork,
}

#[derive(Debug, Deserialize)]
pub struct CommandServerStatusMetricsReplApply {
    pub batches: CommandServerStatusMetricsReplApplyBatches,
    pub ops: i64,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CommandServerStatusMetricsReplApplyBatches {
    pub num: i64,
    pub total_millis: i64,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CommandServerStatusMetricsReplBuffer {
    pub count: i64,
    pub max_size_bytes: i64,
    pub size_bytes: i64,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CommandServerStatusMetricsReplExecutor {
    pub queues: CommandServerStatusMetricsReplExecutorQueues,
    pub unsignaled_events: i64,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CommandServerStatusMetricsReplExecutorQueues {
    pub network_in_progress: i64,
    pub sleepers: i64,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CommandServerStatusMetricsReplNetwork {
    pub bytes: i64,
    pub getmores: CommandServerStatusMetricsReplNetworkGetmores,
    pub ops: i64,
    pub readers_created: i64,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CommandServerStatusMetricsReplNetworkGetmores {
    pub num: i64,
    pub total_millis: i64,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CommandServerStatusMetricsReplTtl {
    pub deleted_documents: i64,
    pub passes: i64,
}

/// <https://docs.mongodb.com/manual/reference/operator/aggregation/collStats/#latency-stats-document>
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CommandServerStatusOpLatenciesStat {
    pub latency: i64,
    pub ops: i64,
    pub histogram: Vec<CommandServerStatusOpLatenciesStatHistBucket>,
}

#[derive(Debug, Deserialize)]
pub struct CommandServerStatusOpLatenciesStatHistBucket {
    pub(crate) micros: i64,
    pub count: i64,
}

#[derive(Debug, Deserialize)]
pub struct CommandServerStatusStorageEngine {
    pub name: String,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CommandServerStatusWiredTiger {
    #[serde(rename = "block-manager")]
    pub block_manager: CommandServerStatusWiredTigerBlockManager,
    pub cache: CommandServerStatusWiredTigerCache,
    pub concurrent_transactions: CommandServerStatusWiredTigerConcurrentTransactions,
    pub log: CommandServerStatusWiredTigerLog,
    pub session: CommandServerStatusWiredTigerSession,
    pub transaction: CommandServerStatusWiredTigerTransaction,
}

#[derive(Debug, Deserialize)]
pub struct CommandServerStatusWiredTigerBlockManager {
    #[serde(rename = "blocks pre-loaded")]
    pub blocks_pre_loaded: i64,
    #[serde(rename = "blocks read")]
    pub blocks_read: i64,
    #[serde(rename = "blocks written")]
    pub blocks_written: i64,
    #[serde(rename = "bytes read")]
    pub bytes_read: i64,
    #[serde(rename = "bytes written")]
    pub bytes_written: i64,
    #[serde(rename = "mapped blocks read")]
    pub mapped_blocks_read: i64,
    #[serde(rename = "mapped bytes read")]
    pub mapped_bytes_read: i64,
}

#[derive(Debug, Deserialize)]
pub struct CommandServerStatusWiredTigerCache {
    #[serde(rename = "bytes currently in the cache")]
    pub bytes_total: i64,
    #[serde(rename = "maximum bytes configured")]
    pub max_bytes: f64,
    #[serde(rename = "modified pages evicted")]
    pub evicted_modified: i64,
    #[serde(rename = "pages currently held in the cache")]
    pub pages_total: i64,
    #[serde(rename = "pages read into cache")]
    pub pages_read_into: i64,
    #[serde(rename = "pages written from cache")]
    pub pages_written_from: i64,
    #[serde(rename = "percentage overhead")]
    pub percent_overhead: i64,
    #[serde(rename = "tracked bytes belonging to internal pages in the cache")]
    pub bytes_internal_pages: i64,
    #[serde(rename = "tracked bytes belonging to leaf pages in the cache")]
    pub bytes_leaf_pages: i64,
    #[serde(rename = "tracked dirty bytes in the cache")]
    pub bytes_dirty: i64,
    #[serde(rename = "tracked dirty pages in the cache")]
    pub pages_dirty: i64,
    #[serde(rename = "unmodified pages evicted")]
    pub evicted_unmodified: i64,
}

#[derive(Debug, Deserialize)]
pub struct CommandServerStatusWiredTigerConcurrentTransactions {
    pub write: CommandServerStatusWiredTigerConcurrentTransactionsStats,
    pub read: CommandServerStatusWiredTigerConcurrentTransactionsStats,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CommandServerStatusWiredTigerConcurrentTransactionsStats {
    pub out: i64,
    pub available: i64,
    pub total_tickets: i64,
}

#[derive(Debug, Deserialize)]
pub struct CommandServerStatusWiredTigerLog {
    #[serde(rename = "log bytes of payload data")]
    pub bytes_payload_data: i64,
    #[serde(rename = "log bytes written")]
    pub bytes_written: i64,
    #[serde(rename = "log flush operations")]
    pub log_flushes: i64,
    #[serde(rename = "log records compressed")]
    pub records_compressed: i64,
    #[serde(rename = "log records not compressed")]
    pub records_uncompressed: i64,
    #[serde(rename = "log scan operations")]
    pub log_scans: i64,
    #[serde(rename = "log scan records requiring two reads")]
    pub log_scans_double: i64,
    #[serde(rename = "log sync operations")]
    pub log_syncs: i64,
    #[serde(rename = "log sync_dir operations")]
    pub log_sync_dirs: i64,
    #[serde(rename = "log write operations")]
    pub log_writes: i64,
    #[serde(rename = "records processed by log scan")]
    pub records_processed_log_scan: i64,
}

#[derive(Debug, Deserialize)]
pub struct CommandServerStatusWiredTigerSession {
    #[serde(rename = "open session count")]
    pub sessions: i64,
}

#[derive(Debug, Deserialize)]
pub struct CommandServerStatusWiredTigerTransaction {
    #[serde(rename = "transaction begins")]
    pub begins: i64,
    #[serde(rename = "transaction checkpoints")]
    pub checkpoints: i64,
    #[serde(rename = "transaction checkpoint currently running")]
    pub checkpoints_running: i64,
    #[serde(rename = "transaction checkpoint max time (msecs)")]
    pub checkpoint_max_ms: i64,
    #[serde(rename = "transaction checkpoint min time (msecs)")]
    pub checkpoint_min_ms: i64,
    #[serde(rename = "transaction checkpoint total time (msecs)")]
    pub checkpoint_total_ms: i64,
    #[serde(rename = "transactions committed")]
    pub committed: i64,
    #[serde(rename = "transactions rolled back")]
    pub rolled_back: i64,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CommandServerStatusNetwork {
    pub bytes_in: i64,
    pub bytes_out: i64,
    pub num_requests: i64,
}