|
use std::error::Error; |
|
use std::fmt; |
|
use std::fs::File; |
|
use std::io::{BufRead, BufReader}; |
|
use std::path::Path; |
|
use serde::Deserialize; |
|
use rand::Rng; |
|
use rand::seq::SliceRandom; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const NUMBER_OF_CLUSTERS: u8 = 10; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Clone, Debug)] |
|
pub struct PopularProgramContainer { |
|
cluster_program_ids: Vec<Vec<u32>>, |
|
} |
|
|
|
impl PopularProgramContainer { |
|
pub fn load(path: &Path) -> Result<Self, Box<dyn Error>> { |
|
let file = File::open(path)?; |
|
let mut reader = BufReader::new(file); |
|
process_csv_into_clusters(&mut reader) |
|
} |
|
|
|
#[allow(dead_code)] |
|
pub fn cluster_program_ids(&self) -> &Vec<Vec<u32>> { |
|
&self.cluster_program_ids |
|
} |
|
|
|
#[allow(dead_code)] |
|
pub fn choose_weighted_by_popularity<R: Rng + ?Sized>(&self, rng: &mut R) -> Option<u32> { |
|
let cluster_weight_vec: Vec<(usize,usize)> = vec![ |
|
(0, 1), |
|
(1, 2), |
|
(2, 4), |
|
(3, 8), |
|
(4, 16), |
|
(5, 32), |
|
(6, 64), |
|
(7, 128), |
|
(8, 256), |
|
(9, 512), |
|
]; |
|
assert!(cluster_weight_vec.len() == (NUMBER_OF_CLUSTERS as usize)); |
|
let cluster_id: &usize = &cluster_weight_vec.choose_weighted(rng, |item| item.1).unwrap().0; |
|
let program_ids: &Vec<u32> = &self.cluster_program_ids[*cluster_id]; |
|
if program_ids.is_empty() { |
|
|
|
|
|
|
|
return None; |
|
} |
|
let program_id: u32 = match program_ids.choose(rng) { |
|
Some(program_id) => *program_id, |
|
None => { |
|
|
|
return None; |
|
} |
|
}; |
|
Some(program_id) |
|
} |
|
|
|
fn choose_from_cluster<R: Rng + ?Sized>(&self, rng: &mut R, cluster_id: u8) -> Option<u32> { |
|
assert!(self.cluster_program_ids.len() == (NUMBER_OF_CLUSTERS as usize)); |
|
assert!((cluster_id as usize) < self.cluster_program_ids.len()); |
|
let program_ids: &Vec<u32> = &self.cluster_program_ids[cluster_id as usize]; |
|
if program_ids.is_empty() { |
|
|
|
|
|
|
|
return None; |
|
} |
|
let program_id: u32 = match program_ids.choose(rng) { |
|
Some(program_id) => *program_id, |
|
None => { |
|
|
|
return None; |
|
} |
|
}; |
|
Some(program_id) |
|
} |
|
|
|
#[allow(dead_code)] |
|
pub fn choose_most_popular<R: Rng + ?Sized>(&self, rng: &mut R) -> Option<u32> { |
|
self.choose_from_cluster(rng, 9) |
|
} |
|
|
|
#[allow(dead_code)] |
|
pub fn choose_medium_popular<R: Rng + ?Sized>(&self, rng: &mut R) -> Option<u32> { |
|
let cluster_id: u8 = rng.gen_range(1..8); |
|
self.choose_from_cluster(rng, cluster_id) |
|
} |
|
|
|
#[allow(dead_code)] |
|
pub fn choose_least_popular<R: Rng + ?Sized>(&self, rng: &mut R) -> Option<u32> { |
|
self.choose_from_cluster(rng, 0) |
|
} |
|
} |
|
|
|
#[derive(Debug)] |
|
pub enum ProgramPopularityError { |
|
PopularityClusterIdOutOfBounds, |
|
} |
|
|
|
impl fmt::Display for ProgramPopularityError { |
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
|
match self { |
|
Self::PopularityClusterIdOutOfBounds => |
|
write!(f, "Cluster id is out of bounds") |
|
} |
|
} |
|
} |
|
|
|
impl Error for ProgramPopularityError {} |
|
|
|
fn process_csv_into_clusters(reader: &mut dyn BufRead) -> Result<PopularProgramContainer, Box<dyn Error>> { |
|
let records: Vec<Record> = process_csv_data(reader)?; |
|
convert_records_to_clusters(records) |
|
} |
|
|
|
#[derive(Debug, Deserialize)] |
|
struct Record { |
|
#[serde(rename = "program id")] |
|
program_id: u32, |
|
|
|
#[serde(rename = "popularity")] |
|
popularity_cluster_id: u8, |
|
} |
|
|
|
impl Record { |
|
#[cfg(test)] |
|
fn new(program_id: u32, popularity_cluster_id: u8) -> Self { |
|
Self { |
|
program_id: program_id, |
|
popularity_cluster_id: popularity_cluster_id, |
|
} |
|
} |
|
} |
|
|
|
fn process_csv_data(reader: &mut dyn BufRead) -> Result<Vec<Record>, Box<dyn Error>> { |
|
let mut records = Vec::<Record>::new(); |
|
let mut csv_reader = csv::ReaderBuilder::new() |
|
.delimiter(b';') |
|
.from_reader(reader); |
|
for result in csv_reader.deserialize() { |
|
let record: Record = result?; |
|
records.push(record); |
|
} |
|
Ok(records) |
|
} |
|
|
|
fn convert_records_to_clusters(records: Vec<Record>) -> Result<PopularProgramContainer, Box<dyn Error>> { |
|
|
|
let mut max_cluster_id: u8 = 0; |
|
for record in &records { |
|
if max_cluster_id < record.popularity_cluster_id { |
|
max_cluster_id = record.popularity_cluster_id; |
|
} |
|
} |
|
if max_cluster_id >= NUMBER_OF_CLUSTERS { |
|
return Err(Box::new(ProgramPopularityError::PopularityClusterIdOutOfBounds)); |
|
} |
|
|
|
|
|
let mut clusters: Vec<Vec<u32>> = vec!(); |
|
for cluster_id in 0..NUMBER_OF_CLUSTERS { |
|
let mut program_ids: Vec<u32> = vec!(); |
|
for record in &records { |
|
if record.popularity_cluster_id == cluster_id { |
|
program_ids.push(record.program_id); |
|
} |
|
} |
|
clusters.push(program_ids); |
|
} |
|
|
|
let container = PopularProgramContainer { |
|
cluster_program_ids: clusters |
|
}; |
|
Ok(container) |
|
} |
|
|
|
#[cfg(test)] |
|
mod tests { |
|
use super::*; |
|
|
|
#[test] |
|
fn test_10000_process_csv_data() { |
|
let data = "\ |
|
program id;popularity |
|
4;1 |
|
|
|
5;9 |
|
6;8 |
|
7;3 |
|
"; |
|
let mut input: &[u8] = data.as_bytes(); |
|
let records: Vec<Record> = process_csv_data(&mut input).unwrap(); |
|
let strings: Vec<String> = records.iter().map(|record| { |
|
format!("{} {}", record.program_id, record.popularity_cluster_id) |
|
}).collect(); |
|
let strings_joined: String = strings.join(","); |
|
assert_eq!(strings_joined, "4 1,5 9,6 8,7 3"); |
|
} |
|
|
|
#[test] |
|
fn test_10001_convert_records_to_clusters_error_too_many_clusters() { |
|
let records: Vec<Record> = vec![ |
|
|
|
|
|
Record::new(666, 10), |
|
]; |
|
let result = convert_records_to_clusters(records); |
|
assert_eq!(result.is_err(), true); |
|
} |
|
|
|
#[test] |
|
fn test_10002_convert_records_to_clusters_success() { |
|
let records: Vec<Record> = vec![ |
|
|
|
Record::new(101, 0), |
|
Record::new(102, 0), |
|
Record::new(103, 0), |
|
|
|
|
|
Record::new(301, 4), |
|
|
|
|
|
Record::new(901, 9), |
|
Record::new(902, 9), |
|
]; |
|
let container: PopularProgramContainer = convert_records_to_clusters(records).unwrap(); |
|
let cluster_program_ids: &Vec<Vec<u32>> = container.cluster_program_ids(); |
|
assert_eq!(cluster_program_ids.len(), 10); |
|
assert_eq!(cluster_program_ids[0].len(), 3); |
|
assert_eq!(cluster_program_ids[4].len(), 1); |
|
assert_eq!(cluster_program_ids[9].len(), 2); |
|
} |
|
} |
|
|