Skip to content

Commit

Permalink
Merge pull request #36 from instaclustr/fix_ci
Browse files Browse the repository at this point in the history
Fix CI
  • Loading branch information
cjrolo authored Oct 6, 2023
2 parents 77633c7 + ecc5aa6 commit e1586cc
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 93 deletions.
12 changes: 6 additions & 6 deletions brro-compressor/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use std::{path::Path};
use std::path::Path;
use clap::{Parser, command, arg};
use log::{debug, error, info};
use log::debug;
use brro_compressor::compressor;

use brro_compressor::optimizer::optimizer;
use brro_compressor::optimizer;
use brro_compressor::utils::reader;
use brro_compressor::utils::writer;

Expand All @@ -16,8 +16,8 @@ fn process_args(input_path: &str, arguments: &Args) {
writer::initialize_directory(&base_dir).expect("Failed to initialize directory");

if arguments.directory {
let (file_contents, file_names) = reader::stream_reader(path).expect("TODO: panic message");
for (index, data) in file_contents.iter().enumerate() {
let files = reader::stream_reader(path).expect("TODO: panic message");
for (index, data) in files.contents.iter().enumerate() {
let (vec_data, tag) = data;
let optimizer_results = optimizer::process_data(vec_data, tag);

Expand All @@ -30,7 +30,7 @@ fn process_args(input_path: &str, arguments: &Args) {
compressed = compressor::constant::constant(&optimizer_results_f);
}

let file_name = writer::replace_extension(&file_names[index], "txt)");
let file_name = writer::replace_extension(&files.names[index], "txt)");
let new_path = base_dir.join(&file_name);
let mut file = writer::create_streaming_writer(&new_path).expect("TODO: panic message");
writer::write_data_to_stream(&mut file, &compressed).expect("Failed to write compressed data");
Expand Down
69 changes: 68 additions & 1 deletion brro-compressor/src/optimizer/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,68 @@
pub mod optimizer;
// Lucas - Once the project is far enough along I strongly reccomend reenabling dead code checks
#![allow(dead_code)]

use median::Filter;
use log::debug;
use types::metric_tag::MetricTag;
use crate::types;

impl MetricTag {
#[allow(clippy::wrong_self_convention)]
fn from_float(&self, x: f64) -> i64 {
match self {
MetricTag::Other => {
0
}
MetricTag::NotFloat | MetricTag::QuasiRandom => {
x as i64
}
MetricTag::Percent(y) => {
to_multiply_and_truncate(x, *y)
}
MetricTag::Duration(y) => {
to_multiply_and_truncate(x, *y)
}
MetricTag::Bytes(y) => {
(x as i64) / (*y as i64)
}
}
}
}

/// Converts a float via multiplication and truncation
fn to_multiply_and_truncate(number: f64, mul: i32) -> i64 {
(number * mul as f64) as i64
}

fn to_median_filter(data: &Vec<f64>) -> Vec<i64> {
let mut filtered = Vec::with_capacity(data.len());
// 10minutes of data
let mut filter = Filter::new(50);
for point in data {
let point_int = MetricTag::QuasiRandom.from_float(*point);
let median = filter.consume(point_int);
filtered.push(median)
}
filtered
}


pub fn process_data(wav_data: &Vec<f64>, tag: &MetricTag) -> Vec<i64> {
let mut _bitdepth = 64;
let mut _dc_component: i64 = 0;
let mut _fractional = true;

debug!("Tag: {:?}", tag);
let data = match tag {
MetricTag::Other => Vec::new(),
MetricTag::QuasiRandom => to_median_filter(wav_data),
_ => {
wav_data
.iter()
.map(|x| tag.from_float(*x))
.collect()
}
};
_fractional = false;
data
}
68 changes: 0 additions & 68 deletions brro-compressor/src/optimizer/optimizer.rs

This file was deleted.

22 changes: 14 additions & 8 deletions brro-compressor/src/utils/reader.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
// Implement a streaming reader here
use std::fs;
use std::io::{self, Read};
use std::path::{Path, PathBuf};
use std::path::Path;
use log::debug;
use regex::Regex;
use types::metric_tag::MetricTag;
use crate::types;


// Function to check if a file is a WAV file
fn is_wav_file(file_path: &Path) -> io::Result<bool> {
// Open the file for reading and read the first 12 bytes (header) of the file
Expand All @@ -25,7 +26,7 @@ fn process_wav_file(file_path: &Path) -> io::Result<(Vec<f64>, MetricTag)> {
let wav_data = read_metrics_from_wav(full_path_str);
// Depending on Metric Tag, apply a transformation
let tag = tag_metric(full_path_str);
return Ok((wav_data, tag));
Ok((wav_data, tag))
}

// Function to process a RAW file
Expand All @@ -35,11 +36,16 @@ fn process_raw_file(file_path: &Path) -> io::Result<()> {
Ok(())
}

pub struct Files {
pub contents: Vec<(Vec<f64>, MetricTag)>,
pub names: Vec<String>,
}

// Function to read and process files in a directory
pub fn stream_reader(directory_path: &Path) -> io::Result<(Vec<(Vec<f64>, MetricTag)>, Vec<String>)> {
let mut results: Vec<(Vec<f64>, MetricTag)> = Vec::new();
pub fn stream_reader(directory_path: &Path) -> io::Result<Files> {
let mut contents: Vec<(Vec<f64>, MetricTag)> = Vec::new();

let mut filenames: Vec<String> = Vec::new();
let mut names: Vec<String> = Vec::new();


// Iterate through entries (files and subdirectories) in the given directory
Expand All @@ -53,21 +59,21 @@ pub fn stream_reader(directory_path: &Path) -> io::Result<(Vec<(Vec<f64>, Metric
// Add the filename to the list
if let Some(filename) = file_path.file_name() {
if let Some(filename_str) = filename.to_str() {
filenames.push(filename_str.to_string());
names.push(filename_str.to_string());
}
}

// Check if the file is a WAV file
if is_wav_file(&file_path)? {
// If it's a WAV file, process it using the process_wav_file function
let wav_result = process_wav_file(&file_path)?;
results.push(wav_result);
contents.push(wav_result);
} else {
// If it's not a WAV file, process it as a RAW file using the process_raw_file function
process_raw_file(&file_path)?;
}
}
Ok((results, filenames))
Ok(Files {contents, names})
}

/*
Expand Down
13 changes: 3 additions & 10 deletions brro-compressor/src/utils/writer.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::fs::File;
use std::io::{self, Write};
use std::path::Path;
use std::path::{Path, PathBuf};

// Function to create a streaming writer for a file
pub fn create_streaming_writer(file_path: &Path) -> io::Result<File> {
Expand All @@ -10,18 +10,11 @@ pub fn create_streaming_writer(file_path: &Path) -> io::Result<File> {

// Function to write data to a streaming writer
pub fn write_data_to_stream(writer: &mut File, data: &[u8]) -> io::Result<()> {
// Convert each byte to a string representation and collect them into a Vec<String>
let strings: Vec<String> = data.iter().map(|&byte| byte.to_string()).collect();

// Join the Vec<String> into a single String, separated by spaces (or any other delimiter you prefer)
let output = strings.join(" ");

writer.write_all(output.as_bytes())?;
Ok(())
writer.write_all(data)
}
pub fn initialize_directory(base_dir: &Path) -> io::Result<()> {
if !base_dir.exists() {
fs::create_dir_all(base_dir)?;
std::fs::create_dir_all(base_dir)?;
}
Ok(())
}
Expand Down

0 comments on commit e1586cc

Please sign in to comment.