@@ -16,127 +16,125 @@ import java.util.UUID
1616import scala .util .Try
1717
1818object MeasurementCassandraCodecs {
19- given Encoder [Measurement [Timing ]] =
20- (measurement : Measurement [Timing ]) =>
21- measurement match
22- case nc : Measurement .NetworkConfiguration =>
23- Encoder [Measurement .NetworkConfiguration ].mapJson(
24- _.mapObject(
25- _.add(
26- " measurement_type" ,
27- Json .fromString(" network_configuration" )
28- )
29- )
30- )(nc)
31- case nu : Measurement .NetworkUtilization =>
32- Encoder [Measurement .NetworkUtilization ].mapJson(
33- _.mapObject(
34- _.add(
35- " measurement_type" ,
36- Json .fromString(" network_utilization" )
37- )
38- )
39- )(nu)
40-
41- given Decoder [Measurement [Timing ]] =
42- (c : HCursor ) => {
43- val measurementTypeCursor = c.downField(" measurement_type" )
44- for {
45- measurementType <- measurementTypeCursor.as[String ]
46- measurement <- {
47- measurementType match
48- case " network_configuration" =>
49- c.as[Measurement .NetworkConfiguration ]
50- case " network_utilization" =>
51- c.as[Measurement .NetworkUtilization ]
52- case _ =>
53- Left (
54- DecodingFailure (
55- s " invalid measurement type $measurementType" ,
56- measurementTypeCursor.history
57- )
58- )
59- }
60- } yield measurement
61- }
62-
63- given Codec [Measurement .NetworkConfiguration ] =
64- Codec .forProduct6(
19+ given Codec [Measurement [Timing ]] =
20+ Codec .forProduct9(
6521 " agent_id" ,
6622 " timestamp" ,
23+ " end_timestamp" ,
24+ " measurement_type" ,
6725 " network_configuration_machine_id" ,
6826 " network_configuration_hostname" ,
6927 " network_configuration_interfaces" ,
70- " network_configuration_open_sockets"
28+ " network_configuration_open_sockets" ,
29+ " network_utilization_connections"
7130 )(
7231 (
7332 agentId : AgentId ,
7433 timestamp : Timing .Timestamp ,
34+ endTimestamp : Timing .Timestamp ,
35+ measurementType : String ,
7536 machinedId : MachineId ,
7637 hostname : Hostname ,
7738 interfaces : Seq [Interface ],
78- openSockets : Seq [OpenSocket ]
39+ openSockets : Seq [OpenSocket ],
40+ connections : Seq [Connection ]
7941 ) =>
80- Measurement .NetworkConfiguration (
81- agentId = agentId,
82- timing = timestamp,
83- machineId = machinedId,
84- hostname = hostname,
85- interfaces = interfaces,
86- openSockets = openSockets
42+ measurementType match
43+ case " network_configuration" =>
44+ Measurement .NetworkConfiguration (
45+ agentId = agentId,
46+ timing = timestamp,
47+ machineId = machinedId,
48+ hostname = hostname,
49+ interfaces = interfaces,
50+ openSockets = openSockets
51+ )
52+ case " network_utilization" =>
53+ Measurement .NetworkUtilization (
54+ agentId = agentId,
55+ timing = Timing .Timeframe (
56+ Interval (
57+ start = timestamp.instant,
58+ stop = endTimestamp.instant
59+ )
60+ ),
61+ connections = connections
62+ )
63+ case _ =>
64+ throw DecodingFailure (
65+ s " invalid measurement type $measurementType" ,
66+ List (CursorOp .DownField (" measurement_type" ))
67+ )
68+ )(_ match
69+ case Measurement .NetworkConfiguration (
70+ agentId,
71+ timing,
72+ machineId,
73+ hostname,
74+ interfaces,
75+ openSockets
76+ ) =>
77+ (
78+ agentId,
79+ timing,
80+ Timing .Timestamp (Instant .EPOCH ),
81+ " network_configuration" ,
82+ machineId,
83+ hostname,
84+ interfaces,
85+ openSockets,
86+ Seq .empty[Connection ]
87+ )
88+ case Measurement .NetworkUtilization (
89+ agentId,
90+ timing,
91+ connections
92+ ) =>
93+ (
94+ agentId,
95+ Timing .Timestamp (timing.value.normalizedStart),
96+ Timing .Timestamp (timing.value.normalizedStop),
97+ " network_utilization" ,
98+ MachineId (new UUID (0 , 0 )),
99+ Hostname .fromString(" a" ).get,
100+ Seq .empty[Interface ],
101+ Seq .empty[OpenSocket ],
102+ connections
87103 )
88- )(nc =>
89- (
90- nc.agentId,
91- nc.timing,
92- nc.machineId,
93- nc.hostname,
94- nc.interfaces,
95- nc.openSockets
96- )
97104 )
98105
99106 given Codec [Interface ] =
100- Codec .forProduct3(" name" , " is_up" , " networks" )(Interface .apply)(interface =>
107+ Codec .forProduct3(
108+ " name" ,
109+ " is_up" ,
110+ " networks"
111+ )(Interface .apply)(interface =>
101112 (interface.name, interface.isUp, interface.networks)
102113 )
103114
104115 given Codec [OpenSocket ] =
105- Codec .forProduct3(" socket" , " protocol" , " maybe_process_name" )(
106- OpenSocket .apply
107- )(openSocket =>
108- (openSocket.socket, openSocket.protocol, openSocket.maybeProcessName)
109- )
110-
111- given Codec [Measurement .NetworkUtilization ] =
112- Codec .forProduct4(
113- " agent_id" ,
114- " timestamp" ,
115- " end_timestamp" ,
116- " network_utilization_connections"
116+ Codec .forProduct3(
117+ " socket" ,
118+ " protocol" ,
119+ " maybe_process_name"
117120 )(
118121 (
119- agentId : AgentId ,
120- timestamp : Timing .Timestamp ,
121- endTimestamp : Timing .Timestamp ,
122- connections : Seq [Connection ]
122+ socket : SocketAddress [Host ],
123+ protocol : Protocol ,
124+ processNameValue : String
123125 ) =>
124- Measurement .NetworkUtilization (
125- agentId = agentId,
126- timing = Timing .Timeframe (
127- Interval (
128- start = timestamp.instant,
129- stop = endTimestamp.instant
130- )
131- ),
132- connections = connections
126+ OpenSocket (
127+ socket = socket,
128+ protocol = protocol,
129+ maybeProcessName =
130+ if (processNameValue.nonEmpty) Option (ProcessName (processNameValue))
131+ else None
133132 )
134- )(nu =>
133+ )(openSocket =>
135134 (
136- nu.agentId,
137- Timing .Timestamp (nu.timing.value.normalizedStart),
138- Timing .Timestamp (nu.timing.value.normalizedStop),
139- nu.connections
135+ openSocket.socket,
136+ openSocket.protocol,
137+ openSocket.maybeProcessName.fold(" " )(_.value)
140138 )
141139 )
142140
0 commit comments