2017-10-12 20:46:47 +00:00
|
|
|
//
|
|
|
|
// imag - the personal information management suite for the commandline
|
2018-02-07 01:48:53 +00:00
|
|
|
// Copyright (C) 2015-2018 Matthias Beyer <mail@beyermatthias.de> and contributors
|
2017-10-12 20:46:47 +00:00
|
|
|
//
|
|
|
|
// This library is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU Lesser General Public
|
|
|
|
// License as published by the Free Software Foundation; version
|
|
|
|
// 2.1 of the License.
|
|
|
|
//
|
|
|
|
// This library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
// Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public
|
|
|
|
// License along with this library; if not, write to the Free Software
|
|
|
|
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
//
|
|
|
|
|
|
|
|
#![deny(
|
|
|
|
non_camel_case_types,
|
|
|
|
non_snake_case,
|
|
|
|
path_statements,
|
|
|
|
trivial_numeric_casts,
|
|
|
|
unstable_features,
|
|
|
|
unused_allocation,
|
|
|
|
unused_import_braces,
|
|
|
|
unused_imports,
|
|
|
|
unused_must_use,
|
|
|
|
unused_mut,
|
|
|
|
unused_qualifications,
|
|
|
|
while_true,
|
|
|
|
)]
|
|
|
|
|
|
|
|
extern crate clap;
|
2017-10-12 21:46:39 +00:00
|
|
|
extern crate toml;
|
|
|
|
extern crate toml_query;
|
2017-10-12 20:46:47 +00:00
|
|
|
|
2018-01-31 10:09:00 +00:00
|
|
|
#[macro_use] extern crate libimagrt;
|
2017-10-12 20:46:47 +00:00
|
|
|
extern crate libimagerror;
|
2017-10-13 14:42:37 +00:00
|
|
|
extern crate libimagentrylink;
|
2017-10-12 20:46:47 +00:00
|
|
|
extern crate libimagstore;
|
|
|
|
|
2018-02-14 22:49:52 +00:00
|
|
|
use std::io::Write;
|
|
|
|
|
2017-10-12 20:46:47 +00:00
|
|
|
use libimagrt::setup::generate_runtime_setup;
|
|
|
|
use libimagerror::trace::MapErrTrace;
|
2018-02-14 22:49:52 +00:00
|
|
|
use libimagerror::io::ToExitCode;
|
|
|
|
use libimagerror::exit::ExitUnwrap;
|
2017-10-12 21:46:39 +00:00
|
|
|
use libimagstore::store::FileLockEntry;
|
2017-10-13 14:52:56 +00:00
|
|
|
use libimagstore::storeid::StoreId;
|
2017-10-12 21:46:39 +00:00
|
|
|
use libimagstore::error::StoreError as Error;
|
2017-10-13 14:42:37 +00:00
|
|
|
use libimagentrylink::internal::*;
|
2017-10-12 21:46:39 +00:00
|
|
|
|
|
|
|
use toml::Value;
|
|
|
|
use toml_query::read::TomlValueReadExt;
|
|
|
|
|
|
|
|
use std::collections::BTreeMap;
|
2017-10-12 20:46:47 +00:00
|
|
|
|
|
|
|
mod ui;
|
|
|
|
|
2017-10-12 21:46:39 +00:00
|
|
|
struct Diagnostic {
|
2017-10-13 14:52:56 +00:00
|
|
|
pub id: StoreId,
|
2017-10-12 21:46:39 +00:00
|
|
|
pub entry_store_version: String,
|
|
|
|
pub header_sections: usize,
|
|
|
|
pub bytecount_content: usize,
|
|
|
|
pub overall_byte_size: usize,
|
|
|
|
pub verified: bool,
|
2017-10-13 14:42:37 +00:00
|
|
|
pub num_internal_links: usize,
|
2017-10-12 21:46:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> From<FileLockEntry<'a>> for Diagnostic {
|
|
|
|
|
|
|
|
fn from(entry: FileLockEntry<'a>) -> Diagnostic {
|
|
|
|
Diagnostic {
|
2017-10-13 14:52:56 +00:00
|
|
|
id: entry.get_location().clone(),
|
2017-10-12 21:46:39 +00:00
|
|
|
entry_store_version: entry
|
|
|
|
.get_header()
|
|
|
|
.read("imag.version")
|
|
|
|
.map(|opt| match opt {
|
|
|
|
Some(&Value::String(ref s)) => s.clone(),
|
|
|
|
Some(_) => "Non-String type in 'imag.version'".to_owned(),
|
|
|
|
None => "No version".to_owned(),
|
|
|
|
})
|
|
|
|
.unwrap_or("Error reading version".to_owned()),
|
|
|
|
header_sections: match entry.get_header() {
|
|
|
|
&Value::Table(ref map) => map.keys().count(),
|
|
|
|
_ => 0
|
|
|
|
},
|
|
|
|
bytecount_content: entry.get_content().as_str().len(),
|
|
|
|
overall_byte_size: entry.to_str().as_str().len(),
|
|
|
|
verified: entry.verify().is_ok(),
|
2017-10-13 14:42:37 +00:00
|
|
|
num_internal_links: entry.get_internal_links().map(Iterator::count).unwrap_or(0),
|
2017-10-12 21:46:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-12 20:46:47 +00:00
|
|
|
fn main() {
|
2018-01-31 10:09:00 +00:00
|
|
|
let version = make_imag_version!();
|
2017-10-12 20:46:47 +00:00
|
|
|
let rt = generate_runtime_setup("imag-diagnostics",
|
2018-01-31 10:09:00 +00:00
|
|
|
&version,
|
2017-10-12 20:46:47 +00:00
|
|
|
"Print diagnostics about imag and the imag store",
|
|
|
|
ui::build_ui);
|
|
|
|
|
2017-10-12 21:46:39 +00:00
|
|
|
let diags = rt.store()
|
2017-10-12 20:46:47 +00:00
|
|
|
.entries()
|
2018-01-22 11:48:28 +00:00
|
|
|
.map_err_trace_exit_unwrap(1)
|
2018-01-28 20:24:02 +00:00
|
|
|
.into_get_iter()
|
2017-10-12 21:46:39 +00:00
|
|
|
.map(|e| {
|
|
|
|
e.map_err_trace_exit_unwrap(1)
|
|
|
|
.ok_or(Error::from("Unable to get entry".to_owned()))
|
|
|
|
.map_err_trace_exit_unwrap(1)
|
|
|
|
})
|
|
|
|
.map(Diagnostic::from)
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
|
|
|
let mut version_counts : BTreeMap<String, usize> = BTreeMap::new();
|
|
|
|
let mut sum_header_sections = 0;
|
|
|
|
let mut sum_bytecount_content = 0;
|
|
|
|
let mut sum_overall_byte_size = 0;
|
2017-10-13 14:52:56 +00:00
|
|
|
let mut max_overall_byte_size : Option<(usize, StoreId)> = None;
|
2017-10-12 21:46:39 +00:00
|
|
|
let mut verified_count = 0;
|
|
|
|
let mut unverified_count = 0;
|
2017-10-13 14:42:37 +00:00
|
|
|
let mut num_internal_links = 0;
|
2017-10-13 14:52:56 +00:00
|
|
|
let mut max_internal_links : Option<(usize, StoreId)> = None;
|
2017-10-12 21:46:39 +00:00
|
|
|
|
|
|
|
for diag in diags.iter() {
|
|
|
|
sum_header_sections += diag.header_sections;
|
|
|
|
sum_bytecount_content += diag.bytecount_content;
|
|
|
|
sum_overall_byte_size += diag.overall_byte_size;
|
2017-10-13 14:52:56 +00:00
|
|
|
match max_overall_byte_size {
|
|
|
|
None => max_overall_byte_size = Some((diag.num_internal_links, diag.id.clone())),
|
|
|
|
Some((num, _)) => if num < diag.overall_byte_size {
|
|
|
|
max_overall_byte_size = Some((diag.overall_byte_size, diag.id.clone()));
|
|
|
|
}
|
|
|
|
}
|
2017-10-12 21:46:39 +00:00
|
|
|
|
|
|
|
let n = version_counts.get(&diag.entry_store_version).map(Clone::clone).unwrap_or(0);
|
|
|
|
version_counts.insert(diag.entry_store_version.clone(), n+1);
|
|
|
|
|
|
|
|
if diag.verified {
|
|
|
|
verified_count += 1;
|
|
|
|
} else {
|
|
|
|
unverified_count += 1;
|
|
|
|
}
|
2017-10-13 14:42:37 +00:00
|
|
|
|
|
|
|
num_internal_links += diag.num_internal_links;
|
2017-10-13 14:52:56 +00:00
|
|
|
match max_internal_links {
|
|
|
|
None => max_internal_links = Some((diag.num_internal_links, diag.id.clone())),
|
|
|
|
Some((num, _)) => if num < diag.num_internal_links {
|
|
|
|
max_internal_links = Some((diag.num_internal_links, diag.id.clone()));
|
|
|
|
}
|
|
|
|
}
|
2017-10-12 21:46:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let n = diags.len();
|
2017-10-12 20:46:47 +00:00
|
|
|
|
2018-02-14 22:49:52 +00:00
|
|
|
let mut out = ::std::io::stdout();
|
|
|
|
|
|
|
|
let _ = writeln!(out, "imag version {}", env!("CARGO_PKG_VERSION"))
|
|
|
|
.to_exit_code()
|
|
|
|
.unwrap_or_exit();
|
|
|
|
let _ = writeln!(out, "")
|
|
|
|
.to_exit_code()
|
|
|
|
.unwrap_or_exit();
|
|
|
|
let _ = writeln!(out, "{} entries", n)
|
|
|
|
.to_exit_code()
|
|
|
|
.unwrap_or_exit();
|
2017-10-12 21:46:39 +00:00
|
|
|
for (k, v) in version_counts {
|
2018-02-14 22:49:52 +00:00
|
|
|
let _ = writeln!(out, "{} entries with store version '{}'", v, k)
|
|
|
|
.to_exit_code()
|
|
|
|
.unwrap_or_exit();
|
2017-10-12 21:46:39 +00:00
|
|
|
}
|
|
|
|
if n != 0 {
|
2018-02-14 22:49:52 +00:00
|
|
|
let _ = writeln!(out, "{} header sections in the average entry", sum_header_sections / n)
|
|
|
|
.to_exit_code()
|
|
|
|
.unwrap_or_exit();
|
|
|
|
let _ = writeln!(out, "{} average content bytecount", sum_bytecount_content / n)
|
|
|
|
.to_exit_code()
|
|
|
|
.unwrap_or_exit();
|
|
|
|
let _ = writeln!(out, "{} average overall bytecount", sum_overall_byte_size / n)
|
|
|
|
.to_exit_code()
|
|
|
|
.unwrap_or_exit();
|
2017-10-13 14:52:56 +00:00
|
|
|
if let Some((num, path)) = max_overall_byte_size {
|
2018-02-14 22:49:52 +00:00
|
|
|
let _ = writeln!(out,
|
|
|
|
"Largest Entry ({bytes} bytes): {path}",
|
|
|
|
bytes = num,
|
|
|
|
path = path
|
|
|
|
.into_pathbuf()
|
|
|
|
.map_err_trace_exit_unwrap(1)
|
|
|
|
.to_str()
|
|
|
|
.unwrap_or("Failed converting path to string")
|
|
|
|
)
|
|
|
|
.to_exit_code()
|
|
|
|
.unwrap_or_exit();
|
2017-10-13 14:52:56 +00:00
|
|
|
}
|
2018-02-14 22:49:52 +00:00
|
|
|
let _ = writeln!(out, "{} average internal link count per entry", num_internal_links / n)
|
|
|
|
.to_exit_code()
|
|
|
|
.unwrap_or_exit();
|
2017-10-13 14:52:56 +00:00
|
|
|
if let Some((num, path)) = max_internal_links {
|
2018-02-14 22:49:52 +00:00
|
|
|
let _ = writeln!(out, "Entry with most internal links ({count}): {path}",
|
2017-10-13 14:52:56 +00:00
|
|
|
count = num,
|
|
|
|
path = path
|
|
|
|
.into_pathbuf()
|
|
|
|
.map_err_trace_exit_unwrap(1)
|
|
|
|
.to_str()
|
|
|
|
.unwrap_or("Failed converting path to string")
|
2018-02-14 22:49:52 +00:00
|
|
|
)
|
|
|
|
.to_exit_code()
|
|
|
|
.unwrap_or_exit();
|
2017-10-13 14:52:56 +00:00
|
|
|
}
|
2018-02-14 22:49:52 +00:00
|
|
|
let _ = writeln!(out, "{} verified entries", verified_count)
|
|
|
|
.to_exit_code()
|
|
|
|
.unwrap_or_exit();
|
|
|
|
let _ = writeln!(out, "{} unverified entries", unverified_count)
|
|
|
|
.to_exit_code()
|
|
|
|
.unwrap_or_exit();
|
2017-10-12 21:46:39 +00:00
|
|
|
}
|
2017-10-12 20:46:47 +00:00
|
|
|
}
|
|
|
|
|