From: Scott Worley Date: Tue, 10 Oct 2023 23:34:12 +0000 (-0700) Subject: server: Write reports as CSV X-Git-Tag: v0.1.0~12 X-Git-Url: http://git.scottworley.com/tattlekey/commitdiff_plain/79d78cde52a33539a4739df85a234c0aa0c34735?ds=inline server: Write reports as CSV --- diff --git a/server/Cargo.lock b/server/Cargo.lock index 1dbac3d..316d831 100644 --- a/server/Cargo.lock +++ b/server/Cargo.lock @@ -2,6 +2,104 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "csv" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac574ff4d437a7b5ad237ef331c17ccca63c46479e5b5453eb8e10bb99a759fe" +dependencies = [ + "csv-core", + "itoa", + "ryu", + "serde", +] + +[[package]] +name = "csv-core" +version = "0.1.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5efa2b3d7902f4b634a20cae3c9c4e6209dc4779feb6863329607560143efa70" +dependencies = [ + "memchr", +] + +[[package]] +name = "itoa" +version = "1.0.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "af150ab688ff2122fcef229be89cb50dd66af9e01a4ff320cc137eecc9bacc38" + +[[package]] +name = "memchr" +version = "2.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f665ee40bc4a3c5590afb1e9677db74a508659dfd71e126420da8274909a0167" + +[[package]] +name = "proc-macro2" +version = "1.0.69" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "134c189feb4956b20f6f547d2cf727d4c0fe06722b20a0eec87ed445a97f92da" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.33" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5267fca4496028628a95160fc423a33e8b2e6af8a5302579e322e4b520293cae" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "ryu" +version = "1.0.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ad4cc8da4ef723ed60bced201181d83791ad433213d8c24efffda1eec85d741" + +[[package]] +name = "serde" +version = "1.0.188" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf9e0fcba69a370eed61bcf2b728575f726b50b55cba78064753d708ddc7549e" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.188" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4eca7ac642d82aa35b60049a6eccb4be6be75e599bd2e9adb5f875a737654af2" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "syn" +version = "2.0.38" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e96b79aaa137db8f61e26363a0c9b47d8b4ec75da28b7d1d614c2303e232408b" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + [[package]] name = "tattlekey-server" version = "0.0.1" +dependencies = [ + "csv", + "serde", +] + +[[package]] +name = "unicode-ident" +version = "1.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" diff --git a/server/Cargo.toml b/server/Cargo.toml index a77bdd3..9fbdf1f 100644 --- a/server/Cargo.toml +++ b/server/Cargo.toml @@ -11,3 +11,5 @@ version = "0.0.1" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +csv = "1.3.0" +serde = { version = "1.0", features = ["derive"] } diff --git a/server/src/main.rs b/server/src/main.rs index 37de6bd..071c4b2 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -14,13 +14,14 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . +use serde::Serialize; use std::collections::HashMap; use std::net::UdpSocket; use std::time::{Duration, SystemTime}; const MESSAGE_SIZE: usize = 12; -#[derive(Eq, Debug, Hash, PartialEq)] +#[derive(Eq, Debug, Hash, PartialEq, Serialize)] struct MessageKey { epoch: u32, device: u16, @@ -79,6 +80,7 @@ impl Range { } fn main() { + let mut log = csv::Writer::from_writer(std::io::stdout()); let mut presses = HashMap::::new(); let socket = UdpSocket::bind("0.0.0.0:29803").expect("couldn't bind to address"); loop { @@ -92,13 +94,14 @@ fn main() { continue; } let message = Message::try_from(filled_buf).expect("I can't count"); + log.serialize((&message.key, message.t)) + .expect("Couldn't write log"); + log.flush().expect("Couldn't flush log"); if let Some(r) = presses.get_mut(&message.key) { if !r.contains(&message.t) { r.extend(&message.t); - println!("Updated press: {:?}: {r:?}", message.key); } } else { - println!("Got new press: {:?}: {:?}", message.key, message.t); presses.insert(message.key, Range::new(&message.t)); } }