Skip to content
This repository has been archived by the owner on Nov 24, 2021. It is now read-only.

Commit

Permalink
chore: fix clippy
Browse files Browse the repository at this point in the history
  • Loading branch information
d33d33 committed Jul 4, 2018
1 parent a5a6148 commit 8675b88
Show file tree
Hide file tree
Showing 13 changed files with 384 additions and 540 deletions.
4 changes: 2 additions & 2 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::process::Command;
use std::path::Path;
use std::fs::File;
use std::io::prelude::*;
use std::path::Path;
use std::process::Command;

fn main() {
let output = Command::new("git")
Expand Down
259 changes: 145 additions & 114 deletions src/config.rs

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions src/lib/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ pub fn add_labels(line: &str, labels: &str) -> Result<String, Box<Error>> {
return Ok(String::from(line));
}

let mut parts = line.splitn(2, "{");
let mut parts = line.splitn(2, '{');

let class = parts.next().ok_or("no_class")?;
let class = String::from(class);

let plabels = parts.next().ok_or("no_labels")?;
let plabels = String::from(plabels);

let sep = if plabels.trim().starts_with("}") {
let sep = if plabels.trim().starts_with('}') {
""
} else {
","
Expand Down
22 changes: 10 additions & 12 deletions src/lib/transcompiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,15 @@ pub struct Transcompiler<'a> {
impl<'a> Transcompiler<'a> {
pub fn new(format: &config::ScraperFormat) -> Transcompiler {
let start = time::now_utc();
let now = start.to_timespec().sec * 1000 * 1000 + (start.to_timespec().nsec as i64 / 1000);
Transcompiler {
format: format,
now: now,
}
let now = start.to_timespec().sec * 1000 * 1000
+ (i64::from(start.to_timespec().nsec) as i64 / 1000);
Transcompiler { format, now }
}

pub fn format(&self, line: &str) -> Result<String, Box<Error>> {
match self.format {
&config::ScraperFormat::Sensision => format_warp10(line),
&config::ScraperFormat::Prometheus => format_prometheus(line, self.now),
match *self.format {
config::ScraperFormat::Sensision => format_warp10(line),
config::ScraperFormat::Prometheus => format_prometheus(line, self.now),
}
}
}
Expand All @@ -36,12 +34,12 @@ fn format_prometheus(line: &str, now: i64) -> Result<String, Box<Error>> {
let line = line.trim();

// Skip comments
if line.starts_with("#") {
if line.starts_with('#') {
return Ok(String::new());
}

// Extract Prometheus metric
let index = if line.contains("{") {
let index = if line.contains('{') {
line.rfind('}').ok_or("bad class")?
} else {
line.find(' ').ok_or("bad class")?
Expand All @@ -62,7 +60,7 @@ fn format_prometheus(line: &str, now: i64) -> Result<String, Box<Error>> {
.unwrap_or(now);

// Format class
let mut parts = class.splitn(2, "{");
let mut parts = class.splitn(2, '{');
let class = String::from(parts.next().ok_or("no_class")?);
let class = class.trim();
let plabels = parts.next();
Expand All @@ -78,7 +76,7 @@ fn format_prometheus(line: &str, now: i64) -> Result<String, Box<Error>> {
.map(|v| v.replace(r"\n", "%0A")) // unescape
.fold(String::new(), |acc, x| {
// skip invalid values
if !x.contains("=") {
if !x.contains('=') {
return acc
}
acc + &x + ","
Expand Down
23 changes: 11 additions & 12 deletions src/log.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
//! # Log module.
//!
//! The Config module provides the log facility.
use slog::Logger;
use slog::Level;
use slog::Drain;
use slog::Duplicate;
use slog::Level;
use slog::LevelFilter;
use slog::Drain;
use slog::Logger;
use slog_async;
use slog_term;
use slog_scope;
use slog_syslog;
use slog_syslog::Facility;
use slog_scope;
use std::fs::OpenOptions;
use std::os::unix::fs::OpenOptionsExt;
use slog_term;
use std;
use std::path::Path;
use std::error::Error;
use std::fs;
use std::fs::OpenOptions;
use std::os::unix::fs::OpenOptionsExt;
use std::path::Path;

use config;

Expand All @@ -33,10 +33,9 @@ pub fn bootstrap() {
/// Send log to console and log file, also handle log level.
pub fn log(parameters: &config::Parameters, verbose: u64) -> Result<(), Box<Error>> {
// Ensure log directory is present
match Path::new(&parameters.log_file).parent() {
Some(log_path) => fs::create_dir_all(log_path)?,
None => {}
};
if let Some(log_path) = Path::new(&parameters.log_file).parent() {
fs::create_dir_all(log_path)?
}

// Stdout drain
let term_decorator = slog_term::TermDecorator::new().build();
Expand Down
14 changes: 7 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,18 @@ extern crate tokio_timer;
extern crate yaml_rust;

use clap::App;
use std::thread;
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};
use std::fs;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use std::thread;
use std::time::Duration;

mod config;
mod scraper;
mod router;
mod sink;
mod lib;
mod log;
mod router;
mod scraper;
mod sink;

include!("version.rs");

Expand Down Expand Up @@ -126,7 +126,7 @@ fn main() {
handles.push(thread::spawn(move || {
slog_scope::scope(
&slog_scope::logger().new(o!("scraper" => scraper.name.clone())),
|| scraper::scraper(&scraper, &parameters, sigint),
|| scraper::scraper(&scraper, &parameters, &sigint),
);
}));
}
Expand Down
34 changes: 15 additions & 19 deletions src/router/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,32 @@ use std::fs;
use std::path::PathBuf;
use std::sync::{Arc, Mutex};
use std::thread;
use std::time::{Duration};
use std::time::Duration;

use time;

use futures::future::Shared;
use futures::sync::oneshot;


use config;
use router::RouterConfig;

pub fn fs_thread(
dir: &str,
period: u64,
todo: Arc<Mutex<VecDeque<PathBuf>>>,
sigint: Shared<oneshot::Receiver<()>>,
config: &RouterConfig,
todo: &Arc<Mutex<VecDeque<PathBuf>>>,
sigint: &Shared<oneshot::Receiver<()>>,
) {
let mut files: HashSet<PathBuf> = HashSet::new();

loop {
let start = time::now_utc();
match list(dir, &files) {
match list(&config.dir) {
Err(err) => error!("list fail: {}", err),
Ok((new, deleted)) => {
if new.len() > 0 {
Ok(entries) => {
let deleted: Vec<PathBuf> = files.difference(&entries).cloned().collect();
let new: Vec<PathBuf> = entries.difference(&files).cloned().collect();

if !new.is_empty() {
debug!("found {} files", new.len());
}

Expand All @@ -47,10 +49,10 @@ pub fn fs_thread(
}

let elapsed = (time::now_utc() - start).num_milliseconds() as u64;
let sleep_time = if elapsed > period {
let sleep_time = if elapsed > config.watch_period {
config::REST_TIME
} else {
cmp::max(period - elapsed, config::REST_TIME)
cmp::max(config.watch_period - elapsed, config::REST_TIME)
};
for _ in 0..sleep_time / config::REST_TIME {
thread::sleep(Duration::from_millis(config::REST_TIME));
Expand All @@ -61,10 +63,7 @@ pub fn fs_thread(
}
}

fn list(
dir: &str,
files: &HashSet<PathBuf>,
) -> Result<(Vec<PathBuf>, Vec<PathBuf>), Box<Error>> {
fn list(dir: &str) -> Result<HashSet<PathBuf>, Box<Error>> {
let entries: HashSet<PathBuf> = fs::read_dir(dir)?
.filter_map(|entry| {
if entry.is_err() {
Expand All @@ -79,8 +78,5 @@ fn list(
})
.collect();

let deleted = files.difference(&entries).cloned().collect();
let new = entries.difference(&files).cloned().collect();

Ok((new, deleted))
Ok(entries)
}
Loading

0 comments on commit 8675b88

Please sign in to comment.