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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
#![allow(clippy::style)]
#![allow(clippy::complexity)]
#![allow(clippy::large_enum_variant)]
#![allow(clippy::mutable_key_type)]
#![allow(clippy::stable_sort_primitive)]
#![allow(clippy::map_entry)]
#![allow(clippy::box_default)]
#![warn(clippy::bool_comparison)]
#![warn(clippy::clone_on_ref_ptr)]
#![warn(clippy::no_effect)]
#![warn(clippy::unnecessary_unwrap)]
#![warn(clippy::dbg_macro)]
#![warn(clippy::todo)]
#![warn(clippy::wildcard_dependencies)]
#![warn(clippy::zero_prefixed_literal)]
#![warn(clippy::borrowed_box)]
#![warn(clippy::deref_addrof)]
#![warn(clippy::double_must_use)]
#![warn(clippy::double_parens)]
#![warn(clippy::extra_unused_lifetimes)]
#![warn(clippy::needless_borrow)]
#![warn(clippy::needless_question_mark)]
#![warn(clippy::needless_return)]
#![warn(clippy::redundant_pattern)]
#![warn(clippy::redundant_slicing)]
#![warn(clippy::redundant_static_lifetimes)]
#![warn(clippy::single_component_path_imports)]
#![warn(clippy::unnecessary_cast)]
#![warn(clippy::useless_asref)]
#![warn(clippy::useless_conversion)]
#![warn(clippy::builtin_type_shadow)]
#![warn(clippy::duplicate_underscore_argument)]
#![warn(clippy::double_neg)]
#![warn(clippy::unnecessary_mut_passed)]
#![warn(clippy::wildcard_in_or_patterns)]
#![warn(clippy::collapsible_if)]
#![warn(clippy::collapsible_else_if)]
#![warn(clippy::crosspointer_transmute)]
#![warn(clippy::excessive_precision)]
#![warn(clippy::overflow_check_conditional)]
#![warn(clippy::as_conversions)]
#![warn(clippy::match_overlapping_arm)]
#![warn(clippy::zero_divided_by_zero)]
#![warn(clippy::must_use_unit)]
#![warn(clippy::suspicious_assignment_formatting)]
#![warn(clippy::suspicious_else_formatting)]
#![warn(clippy::suspicious_unary_op_formatting)]
#![warn(clippy::mut_mutex_lock)]
#![warn(clippy::print_literal)]
#![warn(clippy::same_item_push)]
#![warn(clippy::useless_format)]
#![warn(clippy::write_literal)]
#![warn(clippy::redundant_closure)]
#![warn(clippy::redundant_closure_call)]
#![warn(clippy::unnecessary_lazy_evaluations)]
#![warn(clippy::partialeq_ne_impl)]
#![warn(clippy::redundant_field_names)]
#![warn(clippy::transmutes_expressible_as_ptr_casts)]
#![warn(clippy::unused_async)]
#![warn(clippy::disallowed_methods)]
#![warn(clippy::disallowed_macros)]
#![warn(clippy::disallowed_types)]
#![warn(clippy::from_over_into)]
use serde::{Deserialize, Serialize};
use mz_ore::now::EpochMillis;
#[derive(Clone, Debug, Serialize, Deserialize, PartialOrd, PartialEq, Eq, Ord, Hash)]
pub enum VersionedEvent {
V1(EventV1),
}
impl VersionedEvent {
pub fn new(
id: u64,
event_type: EventType,
object_type: ObjectType,
details: EventDetails,
user: Option<String>,
occurred_at: EpochMillis,
) -> Self {
Self::V1(EventV1::new(
id,
event_type,
object_type,
details,
user,
occurred_at,
))
}
pub fn deserialize(data: &[u8]) -> Result<Self, anyhow::Error> {
Ok(serde_json::from_slice(data)?)
}
pub fn serialize(&self) -> Vec<u8> {
serde_json::to_vec(self).expect("must serialize")
}
pub fn sortable_id(&self) -> u64 {
match self {
VersionedEvent::V1(ev) => ev.id,
}
}
}
#[derive(Clone, Debug, Serialize, Deserialize, PartialOrd, PartialEq, Eq, Ord, Hash)]
#[serde(rename_all = "kebab-case")]
pub enum EventType {
Create,
Drop,
Alter,
}
impl EventType {
pub fn as_title_case(&self) -> &'static str {
match self {
EventType::Create => "Created",
EventType::Drop => "Dropped",
EventType::Alter => "Altered",
}
}
}
serde_plain::derive_display_from_serialize!(EventType);
#[derive(Clone, Debug, Serialize, Deserialize, PartialOrd, PartialEq, Eq, Ord, Hash)]
#[serde(rename_all = "kebab-case")]
pub enum ObjectType {
Cluster,
ClusterReplica,
Connection,
Database,
Func,
Index,
MaterializedView,
Role,
Secret,
Schema,
Sink,
Source,
Table,
Type,
View,
}
impl ObjectType {
pub fn as_title_case(&self) -> &'static str {
match self {
ObjectType::Cluster => "Cluster",
ObjectType::ClusterReplica => "Cluster Replica",
ObjectType::Connection => "Connection",
ObjectType::Database => "Database",
ObjectType::Func => "Function",
ObjectType::Index => "Index",
ObjectType::MaterializedView => "Materialized View",
ObjectType::Role => "Role",
ObjectType::Schema => "Schema",
ObjectType::Secret => "Secret",
ObjectType::Sink => "Sink",
ObjectType::Source => "Source",
ObjectType::Table => "Table",
ObjectType::Type => "Type",
ObjectType::View => "View",
}
}
}
serde_plain::derive_display_from_serialize!(ObjectType);
#[derive(Clone, Debug, Serialize, Deserialize, PartialOrd, PartialEq, Eq, Ord, Hash)]
pub enum EventDetails {
#[serde(rename = "CreateComputeReplicaV1")] CreateClusterReplicaV1(CreateClusterReplicaV1),
#[serde(rename = "DropComputeReplicaV1")] DropClusterReplicaV1(DropClusterReplicaV1),
CreateSourceSinkV1(CreateSourceSinkV1),
CreateSourceSinkV2(CreateSourceSinkV2),
AlterSourceSinkV1(AlterSourceSinkV1),
IdFullNameV1(IdFullNameV1),
RenameItemV1(RenameItemV1),
IdNameV1(IdNameV1),
SchemaV1(SchemaV1),
}
#[derive(Clone, Debug, Serialize, Deserialize, PartialOrd, PartialEq, Eq, Ord, Hash)]
pub struct IdFullNameV1 {
pub id: String,
#[serde(flatten)]
pub name: FullNameV1,
}
#[derive(Clone, Debug, Serialize, Deserialize, PartialOrd, PartialEq, Eq, Ord, Hash)]
pub struct FullNameV1 {
pub database: String,
pub schema: String,
pub item: String,
}
#[derive(Clone, Debug, Serialize, Deserialize, PartialOrd, PartialEq, Eq, Ord, Hash)]
pub struct IdNameV1 {
pub id: String,
pub name: String,
}
#[derive(Clone, Debug, Serialize, Deserialize, PartialOrd, PartialEq, Eq, Ord, Hash)]
pub struct RenameItemV1 {
pub id: String,
pub old_name: FullNameV1,
pub new_name: FullNameV1,
}
#[derive(Clone, Debug, Serialize, Deserialize, PartialOrd, PartialEq, Eq, Ord, Hash)]
pub struct DropClusterReplicaV1 {
pub cluster_id: String,
pub cluster_name: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub replica_id: Option<String>,
pub replica_name: String,
}
#[derive(Clone, Debug, Serialize, Deserialize, PartialOrd, PartialEq, Eq, Ord, Hash)]
pub struct CreateClusterReplicaV1 {
pub cluster_id: String,
pub cluster_name: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub replica_id: Option<String>,
pub replica_name: String,
pub logical_size: String,
}
#[derive(Clone, Debug, Serialize, Deserialize, PartialOrd, PartialEq, Eq, Ord, Hash)]
pub struct CreateSourceSinkV1 {
pub id: String,
#[serde(flatten)]
pub name: FullNameV1,
pub size: Option<String>,
}
#[derive(Clone, Debug, Serialize, Deserialize, PartialOrd, PartialEq, Eq, Ord, Hash)]
pub struct CreateSourceSinkV2 {
pub id: String,
#[serde(flatten)]
pub name: FullNameV1,
pub size: Option<String>,
#[serde(rename = "type")]
pub external_type: String,
}
#[derive(Clone, Debug, Serialize, Deserialize, PartialOrd, PartialEq, Eq, Ord, Hash)]
pub struct AlterSourceSinkV1 {
pub id: String,
#[serde(flatten)]
pub name: FullNameV1,
pub old_size: Option<String>,
pub new_size: Option<String>,
}
#[derive(Clone, Debug, Serialize, Deserialize, PartialOrd, PartialEq, Eq, Ord, Hash)]
pub struct SchemaV1 {
pub id: String,
pub name: String,
pub database_name: String,
}
impl EventDetails {
pub fn as_json(&self) -> serde_json::Value {
match self {
EventDetails::CreateClusterReplicaV1(v) => {
serde_json::to_value(v).expect("must serialize")
}
EventDetails::DropClusterReplicaV1(v) => {
serde_json::to_value(v).expect("must serialize")
}
EventDetails::IdFullNameV1(v) => serde_json::to_value(v).expect("must serialize"),
EventDetails::RenameItemV1(v) => serde_json::to_value(v).expect("must serialize"),
EventDetails::IdNameV1(v) => serde_json::to_value(v).expect("must serialize"),
EventDetails::SchemaV1(v) => serde_json::to_value(v).expect("must serialize"),
EventDetails::CreateSourceSinkV1(v) => serde_json::to_value(v).expect("must serialize"),
EventDetails::CreateSourceSinkV2(v) => serde_json::to_value(v).expect("must serialize"),
EventDetails::AlterSourceSinkV1(v) => serde_json::to_value(v).expect("must serialize"),
}
}
}
#[derive(Clone, Debug, Serialize, Deserialize, PartialOrd, PartialEq, Eq, Ord, Hash)]
pub struct EventV1 {
pub id: u64,
pub event_type: EventType,
pub object_type: ObjectType,
pub details: EventDetails,
pub user: Option<String>,
pub occurred_at: EpochMillis,
}
impl EventV1 {
fn new(
id: u64,
event_type: EventType,
object_type: ObjectType,
details: EventDetails,
user: Option<String>,
occurred_at: EpochMillis,
) -> EventV1 {
EventV1 {
id,
event_type,
object_type,
details,
user,
occurred_at,
}
}
}
#[test]
fn test_audit_log() -> Result<(), anyhow::Error> {
let cases: Vec<(VersionedEvent, &'static str)> = vec![(
VersionedEvent::V1(EventV1::new(
2,
EventType::Drop,
ObjectType::ClusterReplica,
EventDetails::IdNameV1(IdNameV1 {
id: "u1".to_string(),
name: "name".into(),
}),
None,
2,
)),
r#"{"V1":{"id":2,"event_type":"drop","object_type":"cluster-replica","details":{"IdNameV1":{"id":"u1","name":"name"}},"user":null,"occurred_at":2}}"#,
)];
for (event, expected_bytes) in cases {
let event_bytes = serde_json::to_vec(&event).unwrap();
assert_eq!(
event_bytes,
expected_bytes.as_bytes(),
"expected bytes {}, got {}",
expected_bytes,
std::str::from_utf8(&event_bytes).unwrap(),
);
}
Ok(())
}
#[derive(Clone, Debug, Serialize, Deserialize, PartialOrd, PartialEq, Eq, Ord, Hash)]
pub enum VersionedStorageUsage {
V1(StorageUsageV1),
}
#[derive(Clone, Debug, Serialize, Deserialize, PartialOrd, PartialEq, Eq, Ord, Hash)]
pub struct StorageUsageV1 {
pub id: u64,
pub shard_id: Option<String>,
pub size_bytes: u64,
pub collection_timestamp: EpochMillis,
}
impl StorageUsageV1 {
pub fn new(
id: u64,
shard_id: Option<String>,
size_bytes: u64,
collection_timestamp: EpochMillis,
) -> StorageUsageV1 {
StorageUsageV1 {
id,
shard_id,
size_bytes,
collection_timestamp,
}
}
}
impl VersionedStorageUsage {
pub fn new(
id: u64,
object_id: Option<String>,
size_bytes: u64,
collection_timestamp: EpochMillis,
) -> Self {
Self::V1(StorageUsageV1::new(
id,
object_id,
size_bytes,
collection_timestamp,
))
}
pub fn deserialize(data: &[u8]) -> Result<Self, anyhow::Error> {
Ok(serde_json::from_slice(data)?)
}
pub fn serialize(&self) -> Vec<u8> {
serde_json::to_vec(self).expect("must serialize")
}
pub fn timestamp(&self) -> EpochMillis {
match self {
VersionedStorageUsage::V1(StorageUsageV1 {
collection_timestamp,
..
}) => *collection_timestamp,
}
}
}