slow_sql.rs buffer read lines
@@ -2,6 +2,8 @@ use std::fs;
use serde::{Deserialize, Serialize};
use chrono::{DateTime, Utc};
use std::collections::LinkedList;
+use std::fs::File;
+use std::io::{BufReader, BufRead};
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct SqlLog {
@@ -11,19 +13,21 @@ pub struct SqlLog {
}
pub fn find_slow_sql(log_path: &str) -> LinkedList<SqlLog>{
- let data_result = fs::read_to_string(log_path);
let mut use_time_vec = LinkedList::new();
- if data_result.is_err(){
+ let input_opt = File::open(log_path);
+ if input_opt.is_err(){
return use_time_vec;
}
- let data = data_result.unwrap();
+ let buffered = BufReader::new(input_opt.unwrap());
+
let mut temp = 0;
let mut time_start = None;
- let mut sql = "";
- for x in data.lines() {
+ let mut sql = "".to_string();
+
+ for x in buffered.lines().map(|r| r.unwrap()) {
if x.contains("Query:") || x.contains("Exec:") {
time_start = Some(x[0..x.find(" - ").unwrap()].to_string());
- sql = x;
+ sql = x.clone();
}
if time_start.is_some() && (x.contains("Total: <==") || x.contains("Affected: <==")) {
let time_end = x[0..x.find(" - ").unwrap()].to_string();
@@ -34,14 +38,14 @@ pub fn find_slow_sql(log_path: &str) -> LinkedList<SqlLog>{
if use_time > temp {
use_time_vec.push_front(SqlLog {
sql: sql.to_string(),
- return_data: x.to_string(),
+ return_data: x,
second: use_time,
});
temp = use_time;
}
//println!("wait: {}", use_time);
time_start = None;
- sql = "";
+ sql.clear();
}
}
return use_time_vec;
