mirror of
https://github.com/EFForg/rayhunter.git
synced 2026-05-29 22:29:27 -07:00
qmdl_store: replace end_time with last_message_time
This way, even if the program exits unexpectedly, there's a reasonable value for the "end time" of a log.
This commit is contained in:
@@ -33,7 +33,7 @@ pub fn run_diag_read_thread(task_tracker: &TaskTracker, mut dev: DiagDevice, mut
|
||||
task_tracker.spawn(async move {
|
||||
while let Some((entry_idx, new_size)) = rx.recv().await {
|
||||
let mut qmdl_store = qmdl_store_lock_clone.write().await;
|
||||
qmdl_store.update_entry_size(entry_idx, new_size).await
|
||||
qmdl_store.update_entry(entry_idx, new_size).await
|
||||
.expect("failed to update qmdl file size");
|
||||
}
|
||||
info!("QMDL store size updater thread exiting...");
|
||||
|
||||
@@ -37,7 +37,7 @@ pub struct Manifest {
|
||||
pub struct ManifestEntry {
|
||||
pub name: String,
|
||||
pub start_time: DateTime<Local>,
|
||||
pub end_time: Option<DateTime<Local>>,
|
||||
pub last_message_time: Option<DateTime<Local>>,
|
||||
pub size_bytes: usize,
|
||||
}
|
||||
|
||||
@@ -47,7 +47,7 @@ impl ManifestEntry {
|
||||
ManifestEntry {
|
||||
name: format!("{}", now.timestamp()),
|
||||
start_time: now,
|
||||
end_time: None,
|
||||
last_message_time: None,
|
||||
size_bytes: 0,
|
||||
}
|
||||
}
|
||||
@@ -128,18 +128,21 @@ impl QmdlStore {
|
||||
.map_err(QmdlStoreError::ReadFileError)
|
||||
}
|
||||
|
||||
// Sets the current entry's end_time, updates the manifest, and unsets the
|
||||
// current entry
|
||||
// Unsets the current entry
|
||||
pub async fn close_current_entry(&mut self) -> Result<(), QmdlStoreError> {
|
||||
let entry_index = self.current_entry.take()
|
||||
.ok_or(QmdlStoreError::NoCurrentEntry)?;
|
||||
self.manifest.entries[entry_index].end_time = Some(Local::now());
|
||||
self.write_manifest().await
|
||||
match self.current_entry {
|
||||
Some(_) => {
|
||||
self.current_entry = None;
|
||||
Ok(())
|
||||
},
|
||||
None => Err(QmdlStoreError::NoCurrentEntry)
|
||||
}
|
||||
}
|
||||
|
||||
// Sets the given entry's size, updating the manifest
|
||||
pub async fn update_entry_size(&mut self, entry_index: usize, size_bytes: usize) -> Result<(), QmdlStoreError> {
|
||||
// Sets the given entry's size and updates the last_message_time to now, updating the manifest
|
||||
pub async fn update_entry(&mut self, entry_index: usize, size_bytes: usize) -> Result<(), QmdlStoreError> {
|
||||
self.manifest.entries[entry_index].size_bytes = size_bytes;
|
||||
self.manifest.entries[entry_index].last_message_time = Some(Local::now());
|
||||
self.write_manifest().await
|
||||
}
|
||||
|
||||
@@ -185,17 +188,15 @@ mod tests {
|
||||
let _ = store.new_entry().await.unwrap();
|
||||
let entry_index = store.current_entry.unwrap();
|
||||
assert_eq!(QmdlStore::read_manifest(dir.path()).await.unwrap(), store.manifest);
|
||||
assert!(store.manifest.entries[entry_index].last_message_time.is_none());
|
||||
|
||||
store.update_entry_size(entry_index, 1000).await.unwrap();
|
||||
store.update_entry(entry_index, 1000).await.unwrap();
|
||||
let entry = store.entry_for_name(&store.manifest.entries[entry_index].name).unwrap();
|
||||
assert!(entry.last_message_time.is_some());
|
||||
assert_eq!(store.manifest.entries[entry_index].size_bytes, 1000);
|
||||
assert_eq!(QmdlStore::read_manifest(dir.path()).await.unwrap(), store.manifest);
|
||||
|
||||
assert!(store.manifest.entries[entry_index].end_time.is_none());
|
||||
store.close_current_entry().await.unwrap();
|
||||
let entry = store.entry_for_name(&store.manifest.entries[entry_index].name).unwrap();
|
||||
assert!(entry.end_time.is_some());
|
||||
assert_eq!(QmdlStore::read_manifest(dir.path()).await.unwrap(), store.manifest);
|
||||
|
||||
assert!(matches!(store.close_current_entry().await, Err(QmdlStoreError::NoCurrentEntry)));
|
||||
}
|
||||
|
||||
|
||||
@@ -23,7 +23,7 @@
|
||||
<tr>
|
||||
<th scope="col">Name</th>
|
||||
<th scope="col">Date Started</th>
|
||||
<th scope="col">Date Stopped</th>
|
||||
<th scope="col">Date of Last Message</th>
|
||||
<th scope="col">Size (bytes)</th>
|
||||
<th scope="col">PCAP</th>
|
||||
<th scope="col">QMDL</th>
|
||||
|
||||
@@ -29,7 +29,7 @@ function createEntryRow(entry) {
|
||||
name.scope = 'row';
|
||||
name.innerText = entry.name;
|
||||
row.appendChild(name);
|
||||
for (const key of ['start_time', 'end_time', 'size_bytes']) {
|
||||
for (const key of ['start_time', 'last_message_time', 'size_bytes']) {
|
||||
const td = document.createElement('td');
|
||||
td.innerText = entry[key];
|
||||
row.appendChild(td);
|
||||
@@ -57,10 +57,15 @@ async function getQmdlManifest() {
|
||||
const manifest = JSON.parse(await req('GET', '/api/qmdl-manifest'));
|
||||
if (manifest.current_entry) {
|
||||
manifest.current_entry.start_time = new Date(manifest.current_entry.start_time);
|
||||
if (manifest.current_entry.last_message_time === undefined) {
|
||||
manifest.current_entry.last_message_time = "N/A";
|
||||
} else {
|
||||
manifest.current_entry.last_message_time = new Date(manifest.current_entry.last_message_time);
|
||||
}
|
||||
}
|
||||
for (entry of manifest.entries) {
|
||||
entry.start_time = new Date(entry.start_time);
|
||||
entry.end_time = new Date(entry.end_time);
|
||||
entry.last_message_time = new Date(entry.last_message_time);
|
||||
}
|
||||
// sort them in reverse chronological order
|
||||
manifest.entries.reverse();
|
||||
|
||||
Reference in New Issue
Block a user