file_name
large_stringlengths
4
140
prefix
large_stringlengths
0
12.1k
suffix
large_stringlengths
0
12k
middle
large_stringlengths
0
7.51k
fim_type
large_stringclasses
4 values
tags.rs
Unknown(String) } /// Supported canvas image formats. #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum ImageFormat { Png, Jpg, } /// Element in the HTML DOM that can be accessed by Rust interface. pub trait Element: Debug { /// Tag name of the element. fn tag_name(&self) -> TagName; /// HTML content of this element if it still exists. fn dom_html(&mut self) -> Option<String> { let req = self.view_mut().new_request(); let js = format!("\ var inner = document.getElementById('{}').outerHTML;\ window.external.invoke(JSON.stringify({{\ incmd: 'attribute', request: {},\ value: inner\ }})); ", self.id(), req.id()); let rx = req.run(js); let response = rx.recv(); if let Err(_) = response { return None; // likely because Null element was accessed. } let response = response.unwrap(); if let ResponseValue::Str(s) = response { if s.is_empty() { None } else { Some(s) } } else { // Inner HTML request cannot return any other response type. unreachable!(); } } /// Get attribute value of the element if any. Even if attribute is present but is empty /// None is returned. fn attribute(&self, name: &str) -> Option<String> { // Unsafe because we take immutable variable `self` as mutable. let request = unsafe { let this = &mut *(self as *const Self as *mut Self); this.view_mut().new_request() }; let id = request.id(); let js = format!("\ var attr = document.getElementById('{}').getAttribute('{}');\ attr = attr == null ? '' : attr;\ window.external.invoke(JSON.stringify({{\ incmd: 'attribute',\ request: {},\ value: attr\ }}));\ ", self.id(), name, id); let receiver = request.run(js); let attr = receiver.recv().unwrap(); if let ResponseValue::Str(s) = attr { if s == "" { None } else { Some(s) } } else { unreachable!() } } /// Set attribute with given name to given value. fn set_attribute(&mut self, name: &str, value: &str) { let id = self.id().to_owned(); self.view_mut().eval( format!( "document.getElementById('{}').setAttribute('{}', '{}');", id, name, crate::js_prefix_quotes(value) ) ); } /// Append given text to innerHTML field. fn append_inner_html(&mut self, html: &str) { let id = self.id().to_owned(); self.view_mut().eval( format!( "document.getElementById('{}').innerHTML += '{}';", id, crate::js_prefix_quotes(html) ) ); } /// Clears the outerHTML of the element to remove it from HTML completely. fn remove_from_html(&mut self) { let id = self.id().to_owned(); self.view_mut().eval( format!( "document.getElementById('{}').outerHTML = '';", id ) ); } /// Element ID. fn id(&self) -> &String; /// Change element ID. fn set_id(&mut self, new_id: &str) { self.set_attribute("id", new_id) } fn view(&self) -> &ViewWrap; fn view_mut(&mut self) -> &mut ViewWrap { let p = self.view() as *const ViewWrap as *mut ViewWrap; unsafe { &mut *p } } /// Check whether this element still exists. /// Actions on non-existing elements have no effect. fn exists(&mut self) -> bool { self.dom_html().is_some() } fn add_class(&mut self, class: &str) { let attr = self.attribute("class"); let mut attr = if let Some(s) = attr { s } else { String::with_capacity(class.len()) }; attr.push(' '); attr.push_str(class); self.set_attribute("class", &attr); } fn remove_class(&mut self, class: &str) { let attr = self.attribute("class"); if attr.is_none() { self.set_attribute("class", class); return; } let attr = attr.unwrap(); let split = attr.split_whitespace(); let mut new_str = String::with_capacity(attr.len()); for val in split { if val != class { new_str.push_str(val); } } self.set_attribute("class", &new_str); } fn has_class(&self, class: &str) -> bool { let attr = self.attribute("class"); if attr.is_none() { return false; } let attr = attr.unwrap(); let split = attr.split_whitespace(); for s in split { if s == class { return true; } } false } } /// Text content can be set to some text value and read this content back. pub trait TextContent: Element { /// Get text contained by this element. fn text(&self) -> String { if let Some(s) = self.attribute("textContent") { s } else { String::new() } } fn set_text<T: AsRef<str>>(&mut self, text: T) { self.set_attribute("textContent", text.as_ref()) } } pub trait ImageContent: Element { /// Set image data to this element. fn set_image(&mut self, img: Arc<Image>); /// Get image data of this element. fn image(&self) -> Option<&Arc<Image>>; /// Remove any supplied image data. fn remove_image(&mut self) -> Option<Arc<Image>>; } macro_rules! elm_impl { ($name: ident) => { impl Element for $name { fn view(&self) -> &ViewWrap { &self.view } fn id(&self) -> &String { &self.id } fn tag_name(&self) -> TagName { TagName::$name } } } } /// Wrap that gives access to the dynamic element which is known to be of given type. #[derive(Debug)] pub struct Wrap<T: Element> { element: Box<dyn Element>, _p: PhantomData<T>, } /// Image data of canvas. #[derive(Clone)] pub struct Image { base64: String, format: ImageFormat, } #[derive(Debug)] pub struct A { view: ViewWrap, id: String, onclick: OnClick<A>, } #[derive(Debug)] pub struct Canvas { view: ViewWrap, id: String, } #[derive(Clone, Debug)] pub struct H4 { view: ViewWrap, id: String, } #[derive(Clone, Debug)] pub struct H5 { view: ViewWrap, id: String, } #[derive(Clone, Debug)] pub struct Img { view: ViewWrap, id: String, data: Option<Arc<Image>>, } #[derive(Clone, Debug)] pub struct Li { view: ViewWrap, id: String, } #[derive(Clone, Debug)] pub struct P { view: ViewWrap, id: String, } #[derive(Clone, Debug)] pub struct Span { view: ViewWrap, id: String, } elm_impl!(A); elm_impl!(Canvas); elm_impl!(H4); elm_impl!(H5); elm_impl!(Img); elm_impl!(Li); elm_impl!(P); elm_impl!(Span); #[derive(Clone, Debug)] pub struct Unknown { view: ViewWrap, id: String, name: String, } impl<T> Wrap<T> where T: Element { /// Wrap given element. /// /// # Safety /// Programmer must be sure this element has expected type. pub unsafe fn new(element: Box<dyn Element>) -> Self { Wrap { element, _p: Default::default(), } } } impl<T> Deref for Wrap<T> where T: Element { type Target = Box<T>; fn deref(&self) -> &Box<T> { let b = &self.element; let ptr = b as *const Box<dyn Element> as *const Box<T>; unsafe { &*ptr } } } impl<T> DerefMut for Wrap<T> where T: Element { fn deref_mut(&mut self) -> &mut Box<T> { let b = &mut self.element; let ptr = b as *mut Box<dyn Element> as *mut Box<T>; unsafe { &mut *ptr } } } impl Debug for Image { fn fmt(&self, fmt: &mut Formatter) -> std::fmt::Result { write!(fmt, "Image {{ base64: [char; ")?; write!(fmt, "{}", self.base64.len())?; write!(fmt, "], format: ")?; write!(fmt, "{:?}", self.format)?; write!(fmt, " }}")?; Ok(()) } } impl From<&str> for TagName { fn from(s: &str) -> Self { use self::TagName::*; match s.to_lowercase().as_str() { "a
random_line_split
tags.rs
attr;\ window.external.invoke(JSON.stringify({{\ incmd: 'attribute',\ request: {},\ value: attr\ }}));\ ", self.id(), name, id); let receiver = request.run(js); let attr = receiver.recv().unwrap(); if let ResponseValue::Str(s) = attr { if s == "" { None } else { Some(s) } } else { unreachable!() } } /// Set attribute with given name to given value. fn set_attribute(&mut self, name: &str, value: &str) { let id = self.id().to_owned(); self.view_mut().eval( format!( "document.getElementById('{}').setAttribute('{}', '{}');", id, name, crate::js_prefix_quotes(value) ) ); } /// Append given text to innerHTML field. fn append_inner_html(&mut self, html: &str) { let id = self.id().to_owned(); self.view_mut().eval( format!( "document.getElementById('{}').innerHTML += '{}';", id, crate::js_prefix_quotes(html) ) ); } /// Clears the outerHTML of the element to remove it from HTML completely. fn remove_from_html(&mut self) { let id = self.id().to_owned(); self.view_mut().eval( format!( "document.getElementById('{}').outerHTML = '';", id ) ); } /// Element ID. fn id(&self) -> &String; /// Change element ID. fn set_id(&mut self, new_id: &str) { self.set_attribute("id", new_id) } fn view(&self) -> &ViewWrap; fn view_mut(&mut self) -> &mut ViewWrap { let p = self.view() as *const ViewWrap as *mut ViewWrap; unsafe { &mut *p } } /// Check whether this element still exists. /// Actions on non-existing elements have no effect. fn exists(&mut self) -> bool { self.dom_html().is_some() } fn add_class(&mut self, class: &str) { let attr = self.attribute("class"); let mut attr = if let Some(s) = attr { s } else { String::with_capacity(class.len()) }; attr.push(' '); attr.push_str(class); self.set_attribute("class", &attr); } fn remove_class(&mut self, class: &str) { let attr = self.attribute("class"); if attr.is_none() { self.set_attribute("class", class); return; } let attr = attr.unwrap(); let split = attr.split_whitespace(); let mut new_str = String::with_capacity(attr.len()); for val in split { if val != class { new_str.push_str(val); } } self.set_attribute("class", &new_str); } fn has_class(&self, class: &str) -> bool { let attr = self.attribute("class"); if attr.is_none() { return false; } let attr = attr.unwrap(); let split = attr.split_whitespace(); for s in split { if s == class { return true; } } false } } /// Text content can be set to some text value and read this content back. pub trait TextContent: Element { /// Get text contained by this element. fn text(&self) -> String { if let Some(s) = self.attribute("textContent") { s } else { String::new() } } fn set_text<T: AsRef<str>>(&mut self, text: T) { self.set_attribute("textContent", text.as_ref()) } } pub trait ImageContent: Element { /// Set image data to this element. fn set_image(&mut self, img: Arc<Image>); /// Get image data of this element. fn image(&self) -> Option<&Arc<Image>>; /// Remove any supplied image data. fn remove_image(&mut self) -> Option<Arc<Image>>; } macro_rules! elm_impl { ($name: ident) => { impl Element for $name { fn view(&self) -> &ViewWrap { &self.view } fn id(&self) -> &String { &self.id } fn tag_name(&self) -> TagName { TagName::$name } } } } /// Wrap that gives access to the dynamic element which is known to be of given type. #[derive(Debug)] pub struct Wrap<T: Element> { element: Box<dyn Element>, _p: PhantomData<T>, } /// Image data of canvas. #[derive(Clone)] pub struct Image { base64: String, format: ImageFormat, } #[derive(Debug)] pub struct A { view: ViewWrap, id: String, onclick: OnClick<A>, } #[derive(Debug)] pub struct Canvas { view: ViewWrap, id: String, } #[derive(Clone, Debug)] pub struct H4 { view: ViewWrap, id: String, } #[derive(Clone, Debug)] pub struct H5 { view: ViewWrap, id: String, } #[derive(Clone, Debug)] pub struct Img { view: ViewWrap, id: String, data: Option<Arc<Image>>, } #[derive(Clone, Debug)] pub struct Li { view: ViewWrap, id: String, } #[derive(Clone, Debug)] pub struct P { view: ViewWrap, id: String, } #[derive(Clone, Debug)] pub struct Span { view: ViewWrap, id: String, } elm_impl!(A); elm_impl!(Canvas); elm_impl!(H4); elm_impl!(H5); elm_impl!(Img); elm_impl!(Li); elm_impl!(P); elm_impl!(Span); #[derive(Clone, Debug)] pub struct Unknown { view: ViewWrap, id: String, name: String, } impl<T> Wrap<T> where T: Element { /// Wrap given element. /// /// # Safety /// Programmer must be sure this element has expected type. pub unsafe fn new(element: Box<dyn Element>) -> Self { Wrap { element, _p: Default::default(), } } } impl<T> Deref for Wrap<T> where T: Element { type Target = Box<T>; fn deref(&self) -> &Box<T> { let b = &self.element; let ptr = b as *const Box<dyn Element> as *const Box<T>; unsafe { &*ptr } } } impl<T> DerefMut for Wrap<T> where T: Element { fn deref_mut(&mut self) -> &mut Box<T> { let b = &mut self.element; let ptr = b as *mut Box<dyn Element> as *mut Box<T>; unsafe { &mut *ptr } } } impl Debug for Image { fn fmt(&self, fmt: &mut Formatter) -> std::fmt::Result { write!(fmt, "Image {{ base64: [char; ")?; write!(fmt, "{}", self.base64.len())?; write!(fmt, "], format: ")?; write!(fmt, "{:?}", self.format)?; write!(fmt, " }}")?; Ok(()) } } impl From<&str> for TagName { fn from(s: &str) -> Self { use self::TagName::*; match s.to_lowercase().as_str() { "a" => A, "canvas" => Canvas, "h4" => H4, "h5" => H5, "img" => Img, "li" => Li, "p" => P, "span" => Span, _ => Unknown(String::from(s)), } } } impl TagName { /// Create implementation of the tag by it's tag name. pub fn new_impl(&self, view: ViewWrap, id: String) -> Box<dyn Element> { match self { TagName::A => { let mut b = Box::new(A { view, id, onclick: unsafe { OnClick::null() }, }); let onclick = unsafe { OnClick::new(&mut *b) }; b.onclick = onclick; b }, TagName::Canvas => { Box::new(Canvas { view, id, }) }, TagName::H4 => Box::new( H4 { view, id, } ), TagName::H5 => Box::new( H4 { view, id, } ), TagName::Img => Box::new( Img { view, id, data: None, } ), TagName::Li => Box::new ( Li { view, id, } ), TagName::P => Box::new(P { view, id }), TagName::Span => Box::new(Span { view, id }), TagName::Unknown(name) => Box::new(Unknown { view, id, name: name.clone(), }), } } /// Try creating TagName from this node. pub fn
try_from_node
identifier_name
destination.rs
the i-th buffer. let mut buffer_column_index = vec![vec![]; nbuffers]; let mut column_buffer_index_cid: Vec<_> = column_buffer_index.iter().enumerate().collect(); column_buffer_index_cid.sort_by_key(|(_, blk)| *blk); for (cid, &(blkno, _)) in column_buffer_index_cid { buffer_column_index[blkno].push(cid); } self.nrows = Some(nrows); self.schema = Some(schema.to_vec()); self.buffers = Some(buffers); self.buffer_column_index = Some(buffer_column_index); self.dataframe = Some(df); } #[throws(ConnectorAgentError)] fn partition(&mut self, counts: &[usize]) -> Vec<Self::Partition<'_>> { assert_eq!( counts.iter().sum::<usize>(), self.nrows .ok_or_else(|| ConnectorAgentError::DestinationNotAllocated)?, "counts: {} != nrows: {:?}", counts.iter().sum::<usize>(), self.nrows ); let buffers = self.buffers.ok_or_else(|| anyhow!("got None buffers"))?; let schema = self .schema .as_ref() .ok_or_else(|| anyhow!("got None schema"))?; let buffer_column_index = self .buffer_column_index .as_ref() .ok_or_else(|| anyhow!("got None buffer_column_index"))?; let mut partitioned_columns: Vec<Vec<Box<dyn PandasColumnObject>>> = (0..schema.len()).map(|_| vec![]).collect(); for (buf, cids) in buffers.iter().zip_eq(buffer_column_index) { for &cid in cids { match schema[cid] { PandasTypeSystem::F64(_) => { let fblock = Float64Block::extract(buf).map_err(|e| anyhow!(e))?; let fcols = fblock.split()?; for (&cid, fcol) in cids.iter().zip_eq(fcols) { partitioned_columns[cid] = fcol .partition(&counts) .into_iter() .map(|c| Box::new(c) as _) .collect() } } PandasTypeSystem::I64(_) => { let ublock = Int64Block::extract(buf).map_err(|e| anyhow!(e))?; let ucols = ublock.split()?; for (&cid, ucol) in cids.iter().zip_eq(ucols) { partitioned_columns[cid] = ucol .partition(&counts) .into_iter() .map(|c| Box::new(c) as _) .collect() } } PandasTypeSystem::Bool(_) => { let bblock = BooleanBlock::extract(buf).map_err(|e| anyhow!(e))?; let bcols = bblock.split()?; for (&cid, bcol) in cids.iter().zip_eq(bcols) { partitioned_columns[cid] = bcol .partition(&counts) .into_iter() .map(|c| Box::new(c) as _) .collect() } } PandasTypeSystem::String(_) | PandasTypeSystem::BoxStr(_) | PandasTypeSystem::Str(_) | PandasTypeSystem::Char(_) => { let block = StringBlock::extract(buf).map_err(|e| anyhow!(e))?; let cols = block.split()?; for (&cid, col) in cids.iter().zip_eq(cols) { partitioned_columns[cid] = col .partition(&counts) .into_iter() .map(|c| Box::new(c) as _) .collect() } } PandasTypeSystem::Bytes(_) => { let block = BytesBlock::extract(buf).map_err(|e| anyhow!(e))?; let cols = block.split()?; for (&cid, col) in cids.iter().zip_eq(cols) { partitioned_columns[cid] = col .partition(&counts) .into_iter() .map(|c| Box::new(c) as _) .collect() } } PandasTypeSystem::DateTime(_) => { let block = DateTimeBlock::extract(buf).map_err(|e| anyhow!(e))?; let cols = block.split()?; for (&cid, col) in cids.iter().zip_eq(cols) { partitioned_columns[cid] = col .partition(&counts) .into_iter() .map(|c| Box::new(c) as _) .collect() } } } } } let mut par_destinations = vec![]; for &c in counts.into_iter().rev() { let mut columns = Vec::with_capacity(partitioned_columns.len()); for (i, partitions) in partitioned_columns.iter_mut().enumerate() { columns.push( partitions .pop() .ok_or_else(|| anyhow!("empty partition for {}th column", i))?, ); } par_destinations.push(PandasPartitionDestination::new(c, columns, schema)); } // We need to reverse the par_destinations because partitions are poped reversely par_destinations.into_iter().rev().collect() } fn schema(&self) -> &[PandasTypeSystem] { static EMPTY_SCHEMA: Vec<PandasTypeSystem> = vec![]; self.schema.as_ref().unwrap_or(EMPTY_SCHEMA.as_ref()) } } pub struct PandasPartitionDestination<'a> { nrows: usize, columns: Vec<Box<dyn PandasColumnObject + 'a>>, schema: &'a [PandasTypeSystem], seq: usize, } impl<'a> PandasPartitionDestination<'a> { fn new( nrows: usize, columns: Vec<Box<dyn PandasColumnObject + 'a>>, schema: &'a [PandasTypeSystem], ) -> Self { Self { nrows, columns, schema, seq: 0, } } fn loc(&mut self) -> (usize, usize) { let (row, col) = (self.seq / self.ncols(), self.seq % self.ncols()); self.seq += 1; (row, col) } } impl<'a> DestinationPartition<'a> for PandasPartitionDestination<'a> { type TypeSystem = PandasTypeSystem; fn nrows(&self) -> usize { self.nrows } fn ncols(&self) -> usize { self.schema.len() } fn finalize(&mut self) -> Result<()> { for col in &mut self.columns { col.finalize()?; } Ok(()) } } impl<'a, T> Consume<T> for PandasPartitionDestination<'a> where T: HasPandasColumn + TypeAssoc<PandasTypeSystem> + std::fmt::Debug, { fn consume(&mut self, value: T) -> Result<()> { let (_, col) = self.loc(); self.schema[col].check::<T>()?; // How do we check type id for borrowed types? // assert!(self.columns[col].typecheck(TypeId::of::<T>())); let (column, _): (&mut T::PandasColumn<'a>, *const ()) = unsafe { transmute(&*self.columns[col]) }; column.write(value) } } /// call python code to construct the dataframe and expose its buffers #[throws(ConnectorAgentError)] fn create_dataframe<'a, S: AsRef<str>>( py: Python<'a>, names: &[S], schema: &[PandasTypeSystem], nrows: usize, ) -> (&'a PyAny, &'a PyList, &'a PyList) { let names: Vec<_> = names.into_iter().map(|s| s.as_ref()).collect(); debug!("names: {:?}", names); debug!("schema: {:?}", schema); let mut schema_dict: HashMap<PandasTypeSystem, Vec<usize>> = HashMap::new(); schema.iter().enumerate().for_each(|(idx, &dt)| { let indices = schema_dict.entry(dt).or_insert(vec![]); indices.push(idx); }); debug!("schema_dict: {:?}", schema_dict); let mut blocks_code = vec![]; schema_dict .iter() .for_each(|(&dt, indices)| { if dt.is_extension() { // each extension block only contains one column for idx in indices { blocks_code.push(format!( "pd.core.internals.ExtensionBlock(pd.array(np.empty([{}], dtype='{}'), dtype='{}'), placement={}, ndim=2)", nrows, dt.npdtype(), dt.dtype(), idx, )); } } else { blocks_code.push(format!( "pd.core.internals.{}(np.empty([{}, {}], dtype='{}'), placement={:?}, ndim=2)", dt.block_name(), indices.len(), nrows, dt.npdtype(), indices, )); } }); // https://github.com/pandas-dev/pandas/blob/master/pandas/core/internals/managers.py // Suppose we want to find the array corresponding to our i'th column.
// blknos[i] identifies the block from self.blocks that contains this column. // blklocs[i] identifies the column of interest within // self.blocks[self.blknos[i]]
random_line_split
destination.rs
, } } pub fn result(self) -> Option<&'a PyAny> { self.dataframe } } impl<'a> Destination for PandasDestination<'a> { const DATA_ORDERS: &'static [DataOrder] = &[DataOrder::RowMajor]; type TypeSystem = PandasTypeSystem; type Partition<'b> = PandasPartitionDestination<'b>; #[throws(ConnectorAgentError)] fn allocate<S: AsRef<str>>( &mut self, nrows: usize, names: &[S], schema: &[PandasTypeSystem], data_order: DataOrder, ) { if !matches!(data_order, DataOrder::RowMajor) { throw!(ConnectorAgentError::UnsupportedDataOrder(data_order)) } if matches!(self.nrows, Some(_)) { throw!(ConnectorAgentError::DuplicatedAllocation); } let (df, buffers, index) = create_dataframe(self.py, names, schema, nrows)?; debug!("DataFrame created"); // get index for each column: (index of block, index of column within the block) let mut column_buffer_index: Vec<(usize, usize)> = Vec::with_capacity(index.len()); index.iter().try_for_each(|tuple| -> Result<()> { column_buffer_index.push(tuple.extract().map_err(|e| { anyhow!("cannot extract index tuple for `column_buffer_index` {}", e) })?); Ok(()) })?; let nbuffers = buffers.len(); // buffer_column_index[i][j] = the column id of the j-th row (pandas buffer stores columns row-wise) in the i-th buffer. let mut buffer_column_index = vec![vec![]; nbuffers]; let mut column_buffer_index_cid: Vec<_> = column_buffer_index.iter().enumerate().collect(); column_buffer_index_cid.sort_by_key(|(_, blk)| *blk); for (cid, &(blkno, _)) in column_buffer_index_cid { buffer_column_index[blkno].push(cid); } self.nrows = Some(nrows); self.schema = Some(schema.to_vec()); self.buffers = Some(buffers); self.buffer_column_index = Some(buffer_column_index); self.dataframe = Some(df); } #[throws(ConnectorAgentError)] fn partition(&mut self, counts: &[usize]) -> Vec<Self::Partition<'_>> { assert_eq!( counts.iter().sum::<usize>(), self.nrows .ok_or_else(|| ConnectorAgentError::DestinationNotAllocated)?, "counts: {} != nrows: {:?}", counts.iter().sum::<usize>(), self.nrows ); let buffers = self.buffers.ok_or_else(|| anyhow!("got None buffers"))?; let schema = self .schema .as_ref() .ok_or_else(|| anyhow!("got None schema"))?; let buffer_column_index = self .buffer_column_index .as_ref() .ok_or_else(|| anyhow!("got None buffer_column_index"))?; let mut partitioned_columns: Vec<Vec<Box<dyn PandasColumnObject>>> = (0..schema.len()).map(|_| vec![]).collect(); for (buf, cids) in buffers.iter().zip_eq(buffer_column_index) { for &cid in cids { match schema[cid] { PandasTypeSystem::F64(_) => { let fblock = Float64Block::extract(buf).map_err(|e| anyhow!(e))?; let fcols = fblock.split()?; for (&cid, fcol) in cids.iter().zip_eq(fcols) { partitioned_columns[cid] = fcol .partition(&counts) .into_iter() .map(|c| Box::new(c) as _) .collect() } } PandasTypeSystem::I64(_) => { let ublock = Int64Block::extract(buf).map_err(|e| anyhow!(e))?; let ucols = ublock.split()?; for (&cid, ucol) in cids.iter().zip_eq(ucols) { partitioned_columns[cid] = ucol .partition(&counts) .into_iter() .map(|c| Box::new(c) as _) .collect() } } PandasTypeSystem::Bool(_) => { let bblock = BooleanBlock::extract(buf).map_err(|e| anyhow!(e))?; let bcols = bblock.split()?; for (&cid, bcol) in cids.iter().zip_eq(bcols) { partitioned_columns[cid] = bcol .partition(&counts) .into_iter() .map(|c| Box::new(c) as _) .collect() } } PandasTypeSystem::String(_) | PandasTypeSystem::BoxStr(_) | PandasTypeSystem::Str(_) | PandasTypeSystem::Char(_) => { let block = StringBlock::extract(buf).map_err(|e| anyhow!(e))?; let cols = block.split()?; for (&cid, col) in cids.iter().zip_eq(cols) { partitioned_columns[cid] = col .partition(&counts) .into_iter() .map(|c| Box::new(c) as _) .collect() } } PandasTypeSystem::Bytes(_) => { let block = BytesBlock::extract(buf).map_err(|e| anyhow!(e))?; let cols = block.split()?; for (&cid, col) in cids.iter().zip_eq(cols) { partitioned_columns[cid] = col .partition(&counts) .into_iter() .map(|c| Box::new(c) as _) .collect() } } PandasTypeSystem::DateTime(_) => { let block = DateTimeBlock::extract(buf).map_err(|e| anyhow!(e))?; let cols = block.split()?; for (&cid, col) in cids.iter().zip_eq(cols) { partitioned_columns[cid] = col .partition(&counts) .into_iter() .map(|c| Box::new(c) as _) .collect() } } } } } let mut par_destinations = vec![]; for &c in counts.into_iter().rev() { let mut columns = Vec::with_capacity(partitioned_columns.len()); for (i, partitions) in partitioned_columns.iter_mut().enumerate() { columns.push( partitions .pop() .ok_or_else(|| anyhow!("empty partition for {}th column", i))?, ); } par_destinations.push(PandasPartitionDestination::new(c, columns, schema)); } // We need to reverse the par_destinations because partitions are poped reversely par_destinations.into_iter().rev().collect() } fn
(&self) -> &[PandasTypeSystem] { static EMPTY_SCHEMA: Vec<PandasTypeSystem> = vec![]; self.schema.as_ref().unwrap_or(EMPTY_SCHEMA.as_ref()) } } pub struct PandasPartitionDestination<'a> { nrows: usize, columns: Vec<Box<dyn PandasColumnObject + 'a>>, schema: &'a [PandasTypeSystem], seq: usize, } impl<'a> PandasPartitionDestination<'a> { fn new( nrows: usize, columns: Vec<Box<dyn PandasColumnObject + 'a>>, schema: &'a [PandasTypeSystem], ) -> Self { Self { nrows, columns, schema, seq: 0, } } fn loc(&mut self) -> (usize, usize) { let (row, col) = (self.seq / self.ncols(), self.seq % self.ncols()); self.seq += 1; (row, col) } } impl<'a> DestinationPartition<'a> for PandasPartitionDestination<'a> { type TypeSystem = PandasTypeSystem; fn nrows(&self) -> usize { self.nrows } fn ncols(&self) -> usize { self.schema.len() } fn finalize(&mut self) -> Result<()> { for col in &mut self.columns { col.finalize()?; } Ok(()) } } impl<'a, T> Consume<T> for PandasPartitionDestination<'a> where T: HasPandasColumn + TypeAssoc<PandasTypeSystem> + std::fmt::Debug, { fn consume(&mut self, value: T) -> Result<()> { let (_, col) = self.loc(); self.schema[col].check::<T>()?; // How do we check type id for borrowed types? // assert!(self.columns[col].typecheck(TypeId::of::<T>())); let (column, _): (&mut T::PandasColumn<'a>, *const ()) = unsafe { transmute(&*self.columns[col]) }; column.write(value) } } /// call python code to construct the dataframe and expose its buffers #[throws(ConnectorAgentError)] fn create_dataframe<'a, S: AsRef<str>>( py: Python<'a>, names: &[S], schema: &[PandasTypeSystem], nrows: usize, ) -> (&'a PyAny, &'a PyList, &'a PyList) { let names: Vec
schema
identifier_name
destination.rs
, } } pub fn result(self) -> Option<&'a PyAny> { self.dataframe } } impl<'a> Destination for PandasDestination<'a> { const DATA_ORDERS: &'static [DataOrder] = &[DataOrder::RowMajor]; type TypeSystem = PandasTypeSystem; type Partition<'b> = PandasPartitionDestination<'b>; #[throws(ConnectorAgentError)] fn allocate<S: AsRef<str>>( &mut self, nrows: usize, names: &[S], schema: &[PandasTypeSystem], data_order: DataOrder, ) { if !matches!(data_order, DataOrder::RowMajor) { throw!(ConnectorAgentError::UnsupportedDataOrder(data_order)) } if matches!(self.nrows, Some(_)) { throw!(ConnectorAgentError::DuplicatedAllocation); } let (df, buffers, index) = create_dataframe(self.py, names, schema, nrows)?; debug!("DataFrame created"); // get index for each column: (index of block, index of column within the block) let mut column_buffer_index: Vec<(usize, usize)> = Vec::with_capacity(index.len()); index.iter().try_for_each(|tuple| -> Result<()> { column_buffer_index.push(tuple.extract().map_err(|e| { anyhow!("cannot extract index tuple for `column_buffer_index` {}", e) })?); Ok(()) })?; let nbuffers = buffers.len(); // buffer_column_index[i][j] = the column id of the j-th row (pandas buffer stores columns row-wise) in the i-th buffer. let mut buffer_column_index = vec![vec![]; nbuffers]; let mut column_buffer_index_cid: Vec<_> = column_buffer_index.iter().enumerate().collect(); column_buffer_index_cid.sort_by_key(|(_, blk)| *blk); for (cid, &(blkno, _)) in column_buffer_index_cid { buffer_column_index[blkno].push(cid); } self.nrows = Some(nrows); self.schema = Some(schema.to_vec()); self.buffers = Some(buffers); self.buffer_column_index = Some(buffer_column_index); self.dataframe = Some(df); } #[throws(ConnectorAgentError)] fn partition(&mut self, counts: &[usize]) -> Vec<Self::Partition<'_>> { assert_eq!( counts.iter().sum::<usize>(), self.nrows .ok_or_else(|| ConnectorAgentError::DestinationNotAllocated)?, "counts: {} != nrows: {:?}", counts.iter().sum::<usize>(), self.nrows ); let buffers = self.buffers.ok_or_else(|| anyhow!("got None buffers"))?; let schema = self .schema .as_ref() .ok_or_else(|| anyhow!("got None schema"))?; let buffer_column_index = self .buffer_column_index .as_ref() .ok_or_else(|| anyhow!("got None buffer_column_index"))?; let mut partitioned_columns: Vec<Vec<Box<dyn PandasColumnObject>>> = (0..schema.len()).map(|_| vec![]).collect(); for (buf, cids) in buffers.iter().zip_eq(buffer_column_index) { for &cid in cids { match schema[cid] { PandasTypeSystem::F64(_) => { let fblock = Float64Block::extract(buf).map_err(|e| anyhow!(e))?; let fcols = fblock.split()?; for (&cid, fcol) in cids.iter().zip_eq(fcols) { partitioned_columns[cid] = fcol .partition(&counts) .into_iter() .map(|c| Box::new(c) as _) .collect() } } PandasTypeSystem::I64(_) => { let ublock = Int64Block::extract(buf).map_err(|e| anyhow!(e))?; let ucols = ublock.split()?; for (&cid, ucol) in cids.iter().zip_eq(ucols) { partitioned_columns[cid] = ucol .partition(&counts) .into_iter() .map(|c| Box::new(c) as _) .collect() } } PandasTypeSystem::Bool(_) => { let bblock = BooleanBlock::extract(buf).map_err(|e| anyhow!(e))?; let bcols = bblock.split()?; for (&cid, bcol) in cids.iter().zip_eq(bcols) { partitioned_columns[cid] = bcol .partition(&counts) .into_iter() .map(|c| Box::new(c) as _) .collect() } } PandasTypeSystem::String(_) | PandasTypeSystem::BoxStr(_) | PandasTypeSystem::Str(_) | PandasTypeSystem::Char(_) => { let block = StringBlock::extract(buf).map_err(|e| anyhow!(e))?; let cols = block.split()?; for (&cid, col) in cids.iter().zip_eq(cols) { partitioned_columns[cid] = col .partition(&counts) .into_iter() .map(|c| Box::new(c) as _) .collect() } } PandasTypeSystem::Bytes(_) => { let block = BytesBlock::extract(buf).map_err(|e| anyhow!(e))?; let cols = block.split()?; for (&cid, col) in cids.iter().zip_eq(cols) { partitioned_columns[cid] = col .partition(&counts) .into_iter() .map(|c| Box::new(c) as _) .collect() } } PandasTypeSystem::DateTime(_) => { let block = DateTimeBlock::extract(buf).map_err(|e| anyhow!(e))?; let cols = block.split()?; for (&cid, col) in cids.iter().zip_eq(cols) { partitioned_columns[cid] = col .partition(&counts) .into_iter() .map(|c| Box::new(c) as _) .collect() } } } } } let mut par_destinations = vec![]; for &c in counts.into_iter().rev() { let mut columns = Vec::with_capacity(partitioned_columns.len()); for (i, partitions) in partitioned_columns.iter_mut().enumerate() { columns.push( partitions .pop() .ok_or_else(|| anyhow!("empty partition for {}th column", i))?, ); } par_destinations.push(PandasPartitionDestination::new(c, columns, schema)); } // We need to reverse the par_destinations because partitions are poped reversely par_destinations.into_iter().rev().collect() } fn schema(&self) -> &[PandasTypeSystem]
} pub struct PandasPartitionDestination<'a> { nrows: usize, columns: Vec<Box<dyn PandasColumnObject + 'a>>, schema: &'a [PandasTypeSystem], seq: usize, } impl<'a> PandasPartitionDestination<'a> { fn new( nrows: usize, columns: Vec<Box<dyn PandasColumnObject + 'a>>, schema: &'a [PandasTypeSystem], ) -> Self { Self { nrows, columns, schema, seq: 0, } } fn loc(&mut self) -> (usize, usize) { let (row, col) = (self.seq / self.ncols(), self.seq % self.ncols()); self.seq += 1; (row, col) } } impl<'a> DestinationPartition<'a> for PandasPartitionDestination<'a> { type TypeSystem = PandasTypeSystem; fn nrows(&self) -> usize { self.nrows } fn ncols(&self) -> usize { self.schema.len() } fn finalize(&mut self) -> Result<()> { for col in &mut self.columns { col.finalize()?; } Ok(()) } } impl<'a, T> Consume<T> for PandasPartitionDestination<'a> where T: HasPandasColumn + TypeAssoc<PandasTypeSystem> + std::fmt::Debug, { fn consume(&mut self, value: T) -> Result<()> { let (_, col) = self.loc(); self.schema[col].check::<T>()?; // How do we check type id for borrowed types? // assert!(self.columns[col].typecheck(TypeId::of::<T>())); let (column, _): (&mut T::PandasColumn<'a>, *const ()) = unsafe { transmute(&*self.columns[col]) }; column.write(value) } } /// call python code to construct the dataframe and expose its buffers #[throws(ConnectorAgentError)] fn create_dataframe<'a, S: AsRef<str>>( py: Python<'a>, names: &[S], schema: &[PandasTypeSystem], nrows: usize, ) -> (&'a PyAny, &'a PyList, &'a PyList) { let names:
{ static EMPTY_SCHEMA: Vec<PandasTypeSystem> = vec![]; self.schema.as_ref().unwrap_or(EMPTY_SCHEMA.as_ref()) }
identifier_body
towrap.go
64(0) return CRConfigHistoryThreadsafe{hist: &hist, m: &sync.RWMutex{}, limit: &limit, length: &length, pos: &pos} } // Add adds the given stat to the history. Does not add new additions with the // same remote address and CRConfig Date as the previous. func (h CRConfigHistoryThreadsafe) Add(i *CRConfigStat) { h.m.Lock() defer h.m.Unlock() if *h.length != 0 { last := (*h.hist)[(*h.pos-1)%*h.limit] datesEqual := (i.Stats.DateUnixSeconds == nil && last.Stats.DateUnixSeconds == nil) || (i.Stats.DateUnixSeconds != nil && last.Stats.DateUnixSeconds != nil && *i.Stats.DateUnixSeconds == *last.Stats.DateUnixSeconds) cdnsEqual := (i.Stats.CDNName == nil && last.Stats.CDNName == nil) || (i.Stats.CDNName != nil && last.Stats.CDNName != nil && *i.Stats.CDNName == *last.Stats.CDNName) reqAddrsEqual := i.ReqAddr == last.ReqAddr if reqAddrsEqual && datesEqual && cdnsEqual { return } } (*h.hist)[*h.pos] = *i *h.pos = (*h.pos + 1) % *h.limit if *h.length < *h.limit { *h.length++ } } // Get retrieves the stored history of CRConfigStat entries. func (h CRConfigHistoryThreadsafe) Get() []CRConfigStat { h.m.RLock() defer h.m.RUnlock() if *h.length < *h.limit { return CopyCRConfigStat((*h.hist)[:*h.length]) } newStats := make([]CRConfigStat, *h.limit) copy(newStats, (*h.hist)[*h.pos:]) copy(newStats[*h.length-*h.pos:], (*h.hist)[:*h.pos]) return newStats } // Len gives the number of currently stored items in the buffer. // // An uninitialized buffer has zero length. func (h CRConfigHistoryThreadsafe) Len() uint64 { if h.length == nil { return 0 } return *h.length } // TrafficOpsSessionThreadsafe provides access to the Traffic Ops client safe // for multiple goroutines. This fulfills the ITrafficOpsSession interface. type TrafficOpsSessionThreadsafe struct { session **client.Session // pointer-to-pointer, because we're given a pointer from the Traffic Ops package, and we don't want to copy it. legacySession **legacyClient.Session m *sync.Mutex lastCRConfig ByteMapCache crConfigHist CRConfigHistoryThreadsafe CRConfigBackupFile string TMConfigBackupFile string } // NewTrafficOpsSessionThreadsafe returns a new threadsafe // TrafficOpsSessionThreadsafe wrapping the given `Session`. func NewTrafficOpsSessionThreadsafe(s *client.Session, ls *legacyClient.Session, histLimit uint64, cfg config.Config) TrafficOpsSessionThreadsafe { return TrafficOpsSessionThreadsafe{ CRConfigBackupFile: cfg.CRConfigBackupFile, crConfigHist: NewCRConfigHistoryThreadsafe(histLimit), lastCRConfig: NewByteMapCache(), m: &sync.Mutex{}, session: &s, legacySession: &ls, TMConfigBackupFile: cfg.TMConfigBackupFile, } } // Initialized tells whether or not the TrafficOpsSessionThreadsafe has been // properly initialized with non-nil sessions. func (s TrafficOpsSessionThreadsafe) Initialized() bool { return s.session != nil && *s.session != nil && s.legacySession != nil && *s.legacySession != nil } // Update updates the TrafficOpsSessionThreadsafe's connection information with // the provided information. It's safe for calling by multiple goroutines, being // aware that they will race. func (s *TrafficOpsSessionThreadsafe) Update( url string, username string, password string, insecure bool, userAgent string, useCache bool, timeout time.Duration, ) error { if s == nil { return errors.New("cannot update nil session") } s.m.Lock() defer s.m.Unlock() // always set unauthenticated sessions first which can eventually authenticate themselves when attempting requests if err := s.setSession(url, username, password, insecure, userAgent, useCache, timeout); err != nil { return err } if err := s.setLegacySession(url, username, password, insecure, userAgent, useCache, timeout); err != nil { return err } session, _, err := client.LoginWithAgent(url, username, password, insecure, userAgent, useCache, timeout) if err != nil { log.Errorf("logging in using up-to-date client: %v", err) legacySession, _, err := legacyClient.LoginWithAgent(url, username, password, insecure, userAgent, useCache, timeout) if err != nil || legacySession == nil { err = fmt.Errorf("logging in using legacy client: %v", err) return err } *s.legacySession = legacySession } else { *s.session = session } return nil } // setSession sets the session for the up-to-date client without logging in. func (s *TrafficOpsSessionThreadsafe) setSession(url, username, password string, insecure bool, userAgent string, useCache bool, timeout time.Duration) error { options := cookiejar.Options{ PublicSuffixList: publicsuffix.List, } jar, err := cookiejar.New(&options) if err != nil { return err } to := client.NewSession(username, password, url, userAgent, &http.Client{ Timeout: timeout, Transport: &http.Transport{ TLSClientConfig: &tls.Config{InsecureSkipVerify: insecure}, }, Jar: jar, }, useCache) *s.session = to return nil } // setSession sets the session for the legacy client without logging in. func (s *TrafficOpsSessionThreadsafe) setLegacySession(url, username, password string, insecure bool, userAgent string, useCache bool, timeout time.Duration) error { options := cookiejar.Options{ PublicSuffixList: publicsuffix.List, } jar, err := cookiejar.New(&options) if err != nil { return err } to := legacyClient.NewSession(username, password, url, userAgent, &http.Client{ Timeout: timeout, Transport: &http.Transport{ TLSClientConfig: &tls.Config{InsecureSkipVerify: insecure}, }, Jar: jar, }, useCache) *s.legacySession = to return nil } // getThreadsafeSession is used internally to get a copy of the session pointer, // or nil if it doesn't exist. This should not be used outside // TrafficOpsSessionThreadsafe, and never stored, because part of the purpose of // rafficOpsSessionThreadsafe is to store a pointer to the Session pointer, so // it can be updated by one goroutine and immediately used by another. This // should only be called immediately before using the session, since someone // else may update it concurrently. func (s TrafficOpsSessionThreadsafe) get() *client.Session { s.m.Lock() defer s.m.Unlock() if s.session == nil || *s.session == nil { return nil } return *s.session } func (s TrafficOpsSessionThreadsafe) getLegacy() *legacyClient.Session { s.m.Lock() defer s.m.Unlock() if s.legacySession == nil || *s.legacySession == nil { return nil } return *s.legacySession } // CRConfigHistory gets all of the stored, historical data about CRConfig // Snapshots' Stats sections. func (s TrafficOpsSessionThreadsafe) CRConfigHistory() []CRConfigStat
// CRConfigValid checks if the passed tc.CRConfig structure is valid, and // ensures that it is from the same CDN as the last CRConfig Snapshot, as well // as that it is newer than the last CRConfig Snapshot. func (s *TrafficOpsSessionThreadsafe) CRConfigValid(crc *tc.CRConfig, cdn string) error { if crc == nil { return errors.New("CRConfig is nil") } if crc.Stats.CDNName == nil { return errors.New("CRConfig.Stats.CDN missing") } if crc.Stats.DateUnixSeconds == nil { return errors.New("CRConfig.Stats.Date missing") } // Note this intentionally takes intended CDN, rather than trusting // crc.Stats lastCrc, lastCrcTime, lastCrcStats := s.lastCRConfig.Get(cdn) if lastCrc == nil { return nil } if lastCrcStats.DateUnixSeconds == nil { log.Warnln("TrafficOpsSessionThreadsafe.CRConfigValid returning no error, but last CRConfig Date was missing!") return nil } if lastCrcStats.CDNName == nil { log.Warnln("TrafficOpsSessionThreadsafe.CRConfigValid returning no error, but last CRConfig CDN was missing!") return nil } if *lastCrcStats.CDNName != *crc.Stats.CDNName { return errors.New("CRConfig.Stats.CDN " + *crc.Stats.C
{ return s.crConfigHist.Get() }
identifier_body
towrap.go
properly initialized with non-nil sessions. func (s TrafficOpsSessionThreadsafe) Initialized() bool { return s.session != nil && *s.session != nil && s.legacySession != nil && *s.legacySession != nil } // Update updates the TrafficOpsSessionThreadsafe's connection information with // the provided information. It's safe for calling by multiple goroutines, being // aware that they will race. func (s *TrafficOpsSessionThreadsafe) Update( url string, username string, password string, insecure bool, userAgent string, useCache bool, timeout time.Duration, ) error { if s == nil { return errors.New("cannot update nil session") } s.m.Lock() defer s.m.Unlock() // always set unauthenticated sessions first which can eventually authenticate themselves when attempting requests if err := s.setSession(url, username, password, insecure, userAgent, useCache, timeout); err != nil { return err } if err := s.setLegacySession(url, username, password, insecure, userAgent, useCache, timeout); err != nil { return err } session, _, err := client.LoginWithAgent(url, username, password, insecure, userAgent, useCache, timeout) if err != nil { log.Errorf("logging in using up-to-date client: %v", err) legacySession, _, err := legacyClient.LoginWithAgent(url, username, password, insecure, userAgent, useCache, timeout) if err != nil || legacySession == nil { err = fmt.Errorf("logging in using legacy client: %v", err) return err } *s.legacySession = legacySession } else { *s.session = session } return nil } // setSession sets the session for the up-to-date client without logging in. func (s *TrafficOpsSessionThreadsafe) setSession(url, username, password string, insecure bool, userAgent string, useCache bool, timeout time.Duration) error { options := cookiejar.Options{ PublicSuffixList: publicsuffix.List, } jar, err := cookiejar.New(&options) if err != nil { return err } to := client.NewSession(username, password, url, userAgent, &http.Client{ Timeout: timeout, Transport: &http.Transport{ TLSClientConfig: &tls.Config{InsecureSkipVerify: insecure}, }, Jar: jar, }, useCache) *s.session = to return nil } // setSession sets the session for the legacy client without logging in. func (s *TrafficOpsSessionThreadsafe) setLegacySession(url, username, password string, insecure bool, userAgent string, useCache bool, timeout time.Duration) error { options := cookiejar.Options{ PublicSuffixList: publicsuffix.List, } jar, err := cookiejar.New(&options) if err != nil { return err } to := legacyClient.NewSession(username, password, url, userAgent, &http.Client{ Timeout: timeout, Transport: &http.Transport{ TLSClientConfig: &tls.Config{InsecureSkipVerify: insecure}, }, Jar: jar, }, useCache) *s.legacySession = to return nil } // getThreadsafeSession is used internally to get a copy of the session pointer, // or nil if it doesn't exist. This should not be used outside // TrafficOpsSessionThreadsafe, and never stored, because part of the purpose of // rafficOpsSessionThreadsafe is to store a pointer to the Session pointer, so // it can be updated by one goroutine and immediately used by another. This // should only be called immediately before using the session, since someone // else may update it concurrently. func (s TrafficOpsSessionThreadsafe) get() *client.Session { s.m.Lock() defer s.m.Unlock() if s.session == nil || *s.session == nil { return nil } return *s.session } func (s TrafficOpsSessionThreadsafe) getLegacy() *legacyClient.Session { s.m.Lock() defer s.m.Unlock() if s.legacySession == nil || *s.legacySession == nil { return nil } return *s.legacySession } // CRConfigHistory gets all of the stored, historical data about CRConfig // Snapshots' Stats sections. func (s TrafficOpsSessionThreadsafe) CRConfigHistory() []CRConfigStat { return s.crConfigHist.Get() } // CRConfigValid checks if the passed tc.CRConfig structure is valid, and // ensures that it is from the same CDN as the last CRConfig Snapshot, as well // as that it is newer than the last CRConfig Snapshot. func (s *TrafficOpsSessionThreadsafe) CRConfigValid(crc *tc.CRConfig, cdn string) error { if crc == nil { return errors.New("CRConfig is nil") } if crc.Stats.CDNName == nil { return errors.New("CRConfig.Stats.CDN missing") } if crc.Stats.DateUnixSeconds == nil { return errors.New("CRConfig.Stats.Date missing") } // Note this intentionally takes intended CDN, rather than trusting // crc.Stats lastCrc, lastCrcTime, lastCrcStats := s.lastCRConfig.Get(cdn) if lastCrc == nil { return nil } if lastCrcStats.DateUnixSeconds == nil { log.Warnln("TrafficOpsSessionThreadsafe.CRConfigValid returning no error, but last CRConfig Date was missing!") return nil } if lastCrcStats.CDNName == nil { log.Warnln("TrafficOpsSessionThreadsafe.CRConfigValid returning no error, but last CRConfig CDN was missing!") return nil } if *lastCrcStats.CDNName != *crc.Stats.CDNName { return errors.New("CRConfig.Stats.CDN " + *crc.Stats.CDNName + " different than last received CRConfig.Stats.CDNName " + *lastCrcStats.CDNName + " received at " + lastCrcTime.Format(time.RFC3339Nano)) } if *lastCrcStats.DateUnixSeconds > *crc.Stats.DateUnixSeconds { return errors.New("CRConfig.Stats.Date " + strconv.FormatInt(*crc.Stats.DateUnixSeconds, 10) + " older than last received CRConfig.Stats.Date " + strconv.FormatInt(*lastCrcStats.DateUnixSeconds, 10) + " received at " + lastCrcTime.Format(time.RFC3339Nano)) } return nil } // CRConfigRaw returns the CRConfig from the Traffic Ops. This is safe for // multiple goroutines. func (s TrafficOpsSessionThreadsafe) CRConfigRaw(cdn string) ([]byte, error) { var remoteAddr string var err error var crConfig *tc.CRConfig var configBytes []byte json := jsoniter.ConfigFastest ss := s.get() if ss == nil { return nil, ErrNilSession } response, reqInf, err := ss.GetCRConfig(cdn, client.RequestOptions{}) if reqInf.RemoteAddr != nil { remoteAddr = reqInf.RemoteAddr.String() } if err != nil { log.Warnln("getting CRConfig from Traffic Ops using up-to-date client: " + err.Error() + ". Retrying with legacy client") ls := s.getLegacy() if ls == nil { return nil, ErrNilSession } configBytes, reqInf, err = ls.GetCRConfig(cdn) if reqInf.RemoteAddr != nil { remoteAddr = reqInf.RemoteAddr.String() } if err != nil { log.Errorln("getting CRConfig from Traffic Ops using legacy client: " + err.Error() + ". Checking for backup") } } else { crConfig = &response.Response configBytes, err = json.Marshal(crConfig) if err != nil { crConfig = nil log.Warnln("failed to marshal CRConfig using up-to-date client: " + err.Error()) } } if err == nil { log.Infoln("successfully got CRConfig from Traffic Ops. Writing to backup file") if wErr := ioutil.WriteFile(s.CRConfigBackupFile, configBytes, 0644); wErr != nil { log.Errorf("failed to write CRConfig backup file: %v", wErr) } } else { if s.BackupFileExists() { log.Errorln("using backup file for CRConfig snapshot due to error fetching CRConfig snapshot from Traffic Ops: " + err.Error()) configBytes, err = ioutil.ReadFile(s.CRConfigBackupFile) if err != nil { return nil, fmt.Errorf("reading CRConfig backup file: %v", err) } remoteAddr = localHostIP err = nil } else { return nil, fmt.Errorf("failed to get CRConfig from Traffic Ops (%v), and there is no backup file", err) } } hist := &CRConfigStat{ Err: err, ReqAddr: remoteAddr, ReqTime: time.Now(), Stats: tc.CRConfigStats{}, } defer s.crConfigHist.Add(hist)
if crConfig == nil { if err = json.Unmarshal(configBytes, crConfig); err != nil { err = errors.New("invalid JSON: " + err.Error()) hist.Err = err
random_line_split
towrap.go
64(0) return CRConfigHistoryThreadsafe{hist: &hist, m: &sync.RWMutex{}, limit: &limit, length: &length, pos: &pos} } // Add adds the given stat to the history. Does not add new additions with the // same remote address and CRConfig Date as the previous. func (h CRConfigHistoryThreadsafe) Add(i *CRConfigStat) { h.m.Lock() defer h.m.Unlock() if *h.length != 0 { last := (*h.hist)[(*h.pos-1)%*h.limit] datesEqual := (i.Stats.DateUnixSeconds == nil && last.Stats.DateUnixSeconds == nil) || (i.Stats.DateUnixSeconds != nil && last.Stats.DateUnixSeconds != nil && *i.Stats.DateUnixSeconds == *last.Stats.DateUnixSeconds) cdnsEqual := (i.Stats.CDNName == nil && last.Stats.CDNName == nil) || (i.Stats.CDNName != nil && last.Stats.CDNName != nil && *i.Stats.CDNName == *last.Stats.CDNName) reqAddrsEqual := i.ReqAddr == last.ReqAddr if reqAddrsEqual && datesEqual && cdnsEqual
} (*h.hist)[*h.pos] = *i *h.pos = (*h.pos + 1) % *h.limit if *h.length < *h.limit { *h.length++ } } // Get retrieves the stored history of CRConfigStat entries. func (h CRConfigHistoryThreadsafe) Get() []CRConfigStat { h.m.RLock() defer h.m.RUnlock() if *h.length < *h.limit { return CopyCRConfigStat((*h.hist)[:*h.length]) } newStats := make([]CRConfigStat, *h.limit) copy(newStats, (*h.hist)[*h.pos:]) copy(newStats[*h.length-*h.pos:], (*h.hist)[:*h.pos]) return newStats } // Len gives the number of currently stored items in the buffer. // // An uninitialized buffer has zero length. func (h CRConfigHistoryThreadsafe) Len() uint64 { if h.length == nil { return 0 } return *h.length } // TrafficOpsSessionThreadsafe provides access to the Traffic Ops client safe // for multiple goroutines. This fulfills the ITrafficOpsSession interface. type TrafficOpsSessionThreadsafe struct { session **client.Session // pointer-to-pointer, because we're given a pointer from the Traffic Ops package, and we don't want to copy it. legacySession **legacyClient.Session m *sync.Mutex lastCRConfig ByteMapCache crConfigHist CRConfigHistoryThreadsafe CRConfigBackupFile string TMConfigBackupFile string } // NewTrafficOpsSessionThreadsafe returns a new threadsafe // TrafficOpsSessionThreadsafe wrapping the given `Session`. func NewTrafficOpsSessionThreadsafe(s *client.Session, ls *legacyClient.Session, histLimit uint64, cfg config.Config) TrafficOpsSessionThreadsafe { return TrafficOpsSessionThreadsafe{ CRConfigBackupFile: cfg.CRConfigBackupFile, crConfigHist: NewCRConfigHistoryThreadsafe(histLimit), lastCRConfig: NewByteMapCache(), m: &sync.Mutex{}, session: &s, legacySession: &ls, TMConfigBackupFile: cfg.TMConfigBackupFile, } } // Initialized tells whether or not the TrafficOpsSessionThreadsafe has been // properly initialized with non-nil sessions. func (s TrafficOpsSessionThreadsafe) Initialized() bool { return s.session != nil && *s.session != nil && s.legacySession != nil && *s.legacySession != nil } // Update updates the TrafficOpsSessionThreadsafe's connection information with // the provided information. It's safe for calling by multiple goroutines, being // aware that they will race. func (s *TrafficOpsSessionThreadsafe) Update( url string, username string, password string, insecure bool, userAgent string, useCache bool, timeout time.Duration, ) error { if s == nil { return errors.New("cannot update nil session") } s.m.Lock() defer s.m.Unlock() // always set unauthenticated sessions first which can eventually authenticate themselves when attempting requests if err := s.setSession(url, username, password, insecure, userAgent, useCache, timeout); err != nil { return err } if err := s.setLegacySession(url, username, password, insecure, userAgent, useCache, timeout); err != nil { return err } session, _, err := client.LoginWithAgent(url, username, password, insecure, userAgent, useCache, timeout) if err != nil { log.Errorf("logging in using up-to-date client: %v", err) legacySession, _, err := legacyClient.LoginWithAgent(url, username, password, insecure, userAgent, useCache, timeout) if err != nil || legacySession == nil { err = fmt.Errorf("logging in using legacy client: %v", err) return err } *s.legacySession = legacySession } else { *s.session = session } return nil } // setSession sets the session for the up-to-date client without logging in. func (s *TrafficOpsSessionThreadsafe) setSession(url, username, password string, insecure bool, userAgent string, useCache bool, timeout time.Duration) error { options := cookiejar.Options{ PublicSuffixList: publicsuffix.List, } jar, err := cookiejar.New(&options) if err != nil { return err } to := client.NewSession(username, password, url, userAgent, &http.Client{ Timeout: timeout, Transport: &http.Transport{ TLSClientConfig: &tls.Config{InsecureSkipVerify: insecure}, }, Jar: jar, }, useCache) *s.session = to return nil } // setSession sets the session for the legacy client without logging in. func (s *TrafficOpsSessionThreadsafe) setLegacySession(url, username, password string, insecure bool, userAgent string, useCache bool, timeout time.Duration) error { options := cookiejar.Options{ PublicSuffixList: publicsuffix.List, } jar, err := cookiejar.New(&options) if err != nil { return err } to := legacyClient.NewSession(username, password, url, userAgent, &http.Client{ Timeout: timeout, Transport: &http.Transport{ TLSClientConfig: &tls.Config{InsecureSkipVerify: insecure}, }, Jar: jar, }, useCache) *s.legacySession = to return nil } // getThreadsafeSession is used internally to get a copy of the session pointer, // or nil if it doesn't exist. This should not be used outside // TrafficOpsSessionThreadsafe, and never stored, because part of the purpose of // rafficOpsSessionThreadsafe is to store a pointer to the Session pointer, so // it can be updated by one goroutine and immediately used by another. This // should only be called immediately before using the session, since someone // else may update it concurrently. func (s TrafficOpsSessionThreadsafe) get() *client.Session { s.m.Lock() defer s.m.Unlock() if s.session == nil || *s.session == nil { return nil } return *s.session } func (s TrafficOpsSessionThreadsafe) getLegacy() *legacyClient.Session { s.m.Lock() defer s.m.Unlock() if s.legacySession == nil || *s.legacySession == nil { return nil } return *s.legacySession } // CRConfigHistory gets all of the stored, historical data about CRConfig // Snapshots' Stats sections. func (s TrafficOpsSessionThreadsafe) CRConfigHistory() []CRConfigStat { return s.crConfigHist.Get() } // CRConfigValid checks if the passed tc.CRConfig structure is valid, and // ensures that it is from the same CDN as the last CRConfig Snapshot, as well // as that it is newer than the last CRConfig Snapshot. func (s *TrafficOpsSessionThreadsafe) CRConfigValid(crc *tc.CRConfig, cdn string) error { if crc == nil { return errors.New("CRConfig is nil") } if crc.Stats.CDNName == nil { return errors.New("CRConfig.Stats.CDN missing") } if crc.Stats.DateUnixSeconds == nil { return errors.New("CRConfig.Stats.Date missing") } // Note this intentionally takes intended CDN, rather than trusting // crc.Stats lastCrc, lastCrcTime, lastCrcStats := s.lastCRConfig.Get(cdn) if lastCrc == nil { return nil } if lastCrcStats.DateUnixSeconds == nil { log.Warnln("TrafficOpsSessionThreadsafe.CRConfigValid returning no error, but last CRConfig Date was missing!") return nil } if lastCrcStats.CDNName == nil { log.Warnln("TrafficOpsSessionThreadsafe.CRConfigValid returning no error, but last CRConfig CDN was missing!") return nil } if *lastCrcStats.CDNName != *crc.Stats.CDNName { return errors.New("CRConfig.Stats.CDN " + *crc.Stats.C
{ return }
conditional_block
towrap.go
64(0) return CRConfigHistoryThreadsafe{hist: &hist, m: &sync.RWMutex{}, limit: &limit, length: &length, pos: &pos} } // Add adds the given stat to the history. Does not add new additions with the // same remote address and CRConfig Date as the previous. func (h CRConfigHistoryThreadsafe) Add(i *CRConfigStat) { h.m.Lock() defer h.m.Unlock() if *h.length != 0 { last := (*h.hist)[(*h.pos-1)%*h.limit] datesEqual := (i.Stats.DateUnixSeconds == nil && last.Stats.DateUnixSeconds == nil) || (i.Stats.DateUnixSeconds != nil && last.Stats.DateUnixSeconds != nil && *i.Stats.DateUnixSeconds == *last.Stats.DateUnixSeconds) cdnsEqual := (i.Stats.CDNName == nil && last.Stats.CDNName == nil) || (i.Stats.CDNName != nil && last.Stats.CDNName != nil && *i.Stats.CDNName == *last.Stats.CDNName) reqAddrsEqual := i.ReqAddr == last.ReqAddr if reqAddrsEqual && datesEqual && cdnsEqual { return } } (*h.hist)[*h.pos] = *i *h.pos = (*h.pos + 1) % *h.limit if *h.length < *h.limit { *h.length++ } } // Get retrieves the stored history of CRConfigStat entries. func (h CRConfigHistoryThreadsafe) Get() []CRConfigStat { h.m.RLock() defer h.m.RUnlock() if *h.length < *h.limit { return CopyCRConfigStat((*h.hist)[:*h.length]) } newStats := make([]CRConfigStat, *h.limit) copy(newStats, (*h.hist)[*h.pos:]) copy(newStats[*h.length-*h.pos:], (*h.hist)[:*h.pos]) return newStats } // Len gives the number of currently stored items in the buffer. // // An uninitialized buffer has zero length. func (h CRConfigHistoryThreadsafe) Len() uint64 { if h.length == nil { return 0 } return *h.length } // TrafficOpsSessionThreadsafe provides access to the Traffic Ops client safe // for multiple goroutines. This fulfills the ITrafficOpsSession interface. type TrafficOpsSessionThreadsafe struct { session **client.Session // pointer-to-pointer, because we're given a pointer from the Traffic Ops package, and we don't want to copy it. legacySession **legacyClient.Session m *sync.Mutex lastCRConfig ByteMapCache crConfigHist CRConfigHistoryThreadsafe CRConfigBackupFile string TMConfigBackupFile string } // NewTrafficOpsSessionThreadsafe returns a new threadsafe // TrafficOpsSessionThreadsafe wrapping the given `Session`. func NewTrafficOpsSessionThreadsafe(s *client.Session, ls *legacyClient.Session, histLimit uint64, cfg config.Config) TrafficOpsSessionThreadsafe { return TrafficOpsSessionThreadsafe{ CRConfigBackupFile: cfg.CRConfigBackupFile, crConfigHist: NewCRConfigHistoryThreadsafe(histLimit), lastCRConfig: NewByteMapCache(), m: &sync.Mutex{}, session: &s, legacySession: &ls, TMConfigBackupFile: cfg.TMConfigBackupFile, } } // Initialized tells whether or not the TrafficOpsSessionThreadsafe has been // properly initialized with non-nil sessions. func (s TrafficOpsSessionThreadsafe) Initialized() bool { return s.session != nil && *s.session != nil && s.legacySession != nil && *s.legacySession != nil } // Update updates the TrafficOpsSessionThreadsafe's connection information with // the provided information. It's safe for calling by multiple goroutines, being // aware that they will race. func (s *TrafficOpsSessionThreadsafe) Update( url string, username string, password string, insecure bool, userAgent string, useCache bool, timeout time.Duration, ) error { if s == nil { return errors.New("cannot update nil session") } s.m.Lock() defer s.m.Unlock() // always set unauthenticated sessions first which can eventually authenticate themselves when attempting requests if err := s.setSession(url, username, password, insecure, userAgent, useCache, timeout); err != nil { return err } if err := s.setLegacySession(url, username, password, insecure, userAgent, useCache, timeout); err != nil { return err } session, _, err := client.LoginWithAgent(url, username, password, insecure, userAgent, useCache, timeout) if err != nil { log.Errorf("logging in using up-to-date client: %v", err) legacySession, _, err := legacyClient.LoginWithAgent(url, username, password, insecure, userAgent, useCache, timeout) if err != nil || legacySession == nil { err = fmt.Errorf("logging in using legacy client: %v", err) return err } *s.legacySession = legacySession } else { *s.session = session } return nil } // setSession sets the session for the up-to-date client without logging in. func (s *TrafficOpsSessionThreadsafe) setSession(url, username, password string, insecure bool, userAgent string, useCache bool, timeout time.Duration) error { options := cookiejar.Options{ PublicSuffixList: publicsuffix.List, } jar, err := cookiejar.New(&options) if err != nil { return err } to := client.NewSession(username, password, url, userAgent, &http.Client{ Timeout: timeout, Transport: &http.Transport{ TLSClientConfig: &tls.Config{InsecureSkipVerify: insecure}, }, Jar: jar, }, useCache) *s.session = to return nil } // setSession sets the session for the legacy client without logging in. func (s *TrafficOpsSessionThreadsafe) setLegacySession(url, username, password string, insecure bool, userAgent string, useCache bool, timeout time.Duration) error { options := cookiejar.Options{ PublicSuffixList: publicsuffix.List, } jar, err := cookiejar.New(&options) if err != nil { return err } to := legacyClient.NewSession(username, password, url, userAgent, &http.Client{ Timeout: timeout, Transport: &http.Transport{ TLSClientConfig: &tls.Config{InsecureSkipVerify: insecure}, }, Jar: jar, }, useCache) *s.legacySession = to return nil } // getThreadsafeSession is used internally to get a copy of the session pointer, // or nil if it doesn't exist. This should not be used outside // TrafficOpsSessionThreadsafe, and never stored, because part of the purpose of // rafficOpsSessionThreadsafe is to store a pointer to the Session pointer, so // it can be updated by one goroutine and immediately used by another. This // should only be called immediately before using the session, since someone // else may update it concurrently. func (s TrafficOpsSessionThreadsafe) get() *client.Session { s.m.Lock() defer s.m.Unlock() if s.session == nil || *s.session == nil { return nil } return *s.session } func (s TrafficOpsSessionThreadsafe) getLegacy() *legacyClient.Session { s.m.Lock() defer s.m.Unlock() if s.legacySession == nil || *s.legacySession == nil { return nil } return *s.legacySession } // CRConfigHistory gets all of the stored, historical data about CRConfig // Snapshots' Stats sections. func (s TrafficOpsSessionThreadsafe) CRConfigHistory() []CRConfigStat { return s.crConfigHist.Get() } // CRConfigValid checks if the passed tc.CRConfig structure is valid, and // ensures that it is from the same CDN as the last CRConfig Snapshot, as well // as that it is newer than the last CRConfig Snapshot. func (s *TrafficOpsSessionThreadsafe)
(crc *tc.CRConfig, cdn string) error { if crc == nil { return errors.New("CRConfig is nil") } if crc.Stats.CDNName == nil { return errors.New("CRConfig.Stats.CDN missing") } if crc.Stats.DateUnixSeconds == nil { return errors.New("CRConfig.Stats.Date missing") } // Note this intentionally takes intended CDN, rather than trusting // crc.Stats lastCrc, lastCrcTime, lastCrcStats := s.lastCRConfig.Get(cdn) if lastCrc == nil { return nil } if lastCrcStats.DateUnixSeconds == nil { log.Warnln("TrafficOpsSessionThreadsafe.CRConfigValid returning no error, but last CRConfig Date was missing!") return nil } if lastCrcStats.CDNName == nil { log.Warnln("TrafficOpsSessionThreadsafe.CRConfigValid returning no error, but last CRConfig CDN was missing!") return nil } if *lastCrcStats.CDNName != *crc.Stats.CDNName { return errors.New("CRConfig.Stats.CDN " + *crc.Stats.CDN
CRConfigValid
identifier_name
main.rs
Vec::new(); //Checks if previous char was a | operator. //If so, save the current character as a transition or cycle character //Also fixes any previous transition state if previous_char == '|' { for (n, a) in dfa.alphabet.iter().enumerate() { if *a == c { dfa.transitions[0][n] = next_state; } } let j = Transitions{chars: c, state: next_state}; transitions.push(j); } //Same as above, just with the * operator. if previous_char == '*' { let j = Transitions{chars: c, state: next_state}; transitions.push(j); } //Operator '|': Implemented - single and multiple | operators are working //Multiple types of symbols are untested and could produce varying results //Checks if character is | operator. If so, save the final state as an accept state, reset //current state back to 1, and set previous_char as | if c == '|' { let final_bar_state = dfa.transitions.len()+1; let mut final_bar_state_count: Vec<usize> = Vec::new(); dfa.accept.push(final_bar_state); for _a in dfa.alphabet.iter() { final_bar_state_count.push(final_bar_state); } dfa.transitions.push(final_bar_state_count); current_state = 1; previous_char = '|'; } //Operator '+': Implemented - single works, multiple is funky, almost working //Removes the previous transition matrix to remake it with updated states //Fix to the multiple + operators I believe is using a for loop to go through the entire transitions vec // but I have ran out of time to get that working. else if c == '+' { dfa.transitions.remove(dfa.transitions.len()-1); next_state -= 1; current_state -= 1; for a in dfa.alphabet.iter() { if a == &previous_char { states.push(next_state); } else { if *a == transitions[0].chars { states.push(transitions[0].state); } else { states.push(1); } } } dfa.transitions.push(states); next_state += 1; current_state += 1; } //Operator '*': Implemented - Single and multiple * operators are working. Something funky happens with the more characters // added into the regex, especially after a *. Not time to check it. Very close to getting this part fixed, most of it works //Similar to + operator, remove previous transition to replace it with new one. // Step back 2 states for next and current to allow for proper transition. Push necessary states. // Potential fix is similar to + operator with iterating over transitions instead of just checking index 0. //At the end, add 2 to current state to get back, and set previous_char as * else if c == '*' { dfa.transitions.remove(dfa.transitions.len()-1); let mut pushed_forward = false; next_state -= 2; current_state -= 2; for a in dfa.alphabet.iter() { if a == &previous_char { next_state += 1; states.push(next_state); } else if *a == 'Σ' { states.push(1); } else { if *a == transitions[0].chars { states.push(transitions[0].state); } else if !pushed_forward { next_state += 1; states.push(next_state); pushed_forward = true; } else { states.push(1); } } } dfa.transitions.push(states); current_state += 2; previous_char = '*'; } //All word character symbols: Implemented //Allows for any character that is in the language to be added in, checks if there is a transition/cycle //to be made, set the state as that before pushing. If it is not a transition, push to state 1 //if sigma symbol, push to state 1 else if c != 'Σ' { for a in dfa.alphabet.iter() { let mut was_transition = false; if c == *a { states.push(next_state); } else { for i in 0..transitions.len() { if *a == transitions[i].chars { states.push(transitions[i].state); was_transition = true; } } if was_transition == false { if previous_char == '*' && *a != 'Σ' { states.push(1); previous_char = c; } else { states.push(1); } } } } if previous_char != '|' { dfa.transitions.push(states); } next_state += 1; current_state += 1; previous_char = c; } } //Go back through and fix any transitions that weren't marked properly // (i.e. | transitions to state 2 from state 4 if applicable) for i in 0..dfa.transitions.len() { for n in 0..dfa.transitions[i].len() { if n < dfa.transitions[i].len() - 1 && dfa.transitions[i][n] == 1 { for c in 0..transitions.len() { if dfa.alphabet[n] == transitions[c].chars { dfa.transitions[i][n] = transitions[c].state; } } } } } //Set final state as a cycle for transition matrix. If 3 states, push [3,3,3] let mut final_state_count: Vec<usize> = Vec::new(); for _alphabet in dfa.alphabet.iter() { final_state_count.push(final_state); } dfa.transitions.push(final_state_count); dfa } } // ********************************************************************* // Implement the methods of the DFA structure impl StateGraph<> { /// Create a state graph from a DFA structure fn new_from_dfa(dfa: &DFA) -> Box<StateGraph> { // Create an empty graph object let mut graph = Box::new(StateGraph{alphabet: dfa.alphabet.clone(), start_state: dfa.start - 1, states: vec!() }); // Look through the transition table building state objects for row in dfa.transitions.iter() { let mut v = Box::new(State{accept_state: false, transitions: vec!()}); for col in row { v.transitions.push(col-1); } graph.states.push(v); } // Set the accept states for astate in dfa.accept.iter() { graph.states[*astate - 1].accept_state = true; } graph } /// Execute the graph on a sentence /// Return Err if a character not in the alphabet is encountered /// Return Ok and a bool indicating accept (true) or reject (false) fn test_sentence(&self, sentence: &str) -> Result<bool, String> { let mut state = self.start_state; //Full alphabet to test against for sigma character let full_alphabet: Vec<char> = "abcdefghijklmnopqrstuvwxyz0123456789 ".chars().collect(); for ch in sentence.chars() { //Check if character is a word character. Accept it if it is and change it to the 'Σ' symbol for matching purposes let mut c = ch; if !self.alphabet.contains(&c) && full_alphabet.contains(&c) { c = 'Σ'; } let state_no = match self.alphabet.iter().position(|v| *v == ch || *v == c) { Some(t) => t, None => return Err(format!("Character <{}> does not have a transition", ch)) }; print!("δ(q{}, {}) → ", state+1, ch); state = self.states[state].transitions[state_no]; println!("(q{})", state+1); } Ok(self.states[state].accept_state) } fn write_graphviz(&self) { println!("digraph {{"); println!("\trankdir=LR;"); println!("\tnode [shape=point]; start;"); for (n, state) in self.states.iter().enumerate() { if state.accept_state { println!("\tnode [shape=doublecircle]; q{};", n+1); } } println!("\tnode [shape=circle];"); println!("\tstart -> q{}", self.start_state+1); for (n, state) in self.states.iter().enumerate() { for (i, ch) in self.alphabet.iter().enumerate() { println!("\tq{} -> q{} [label=\"{}\"]", n+1, state.transitions[i] + 1, ch); } } println!("}}"); } fn process(&
self) {
identifier_name
main.rs
transitions transitions: Vec<usize> } struct Transitions { chars: char, state: usize } fn main() { //Get and validate the RegEx on the command line let regex = get_regex(std::env::args()); let dfa = DFA::new_from_regex(&regex); //Create the dfa structure based on in RegEx entered from the command line let state_graph = StateGraph::new_from_dfa(&dfa); //eprintln!("{:?}", state_graph); state_graph.write_graphviz(); // Process through the input until end of file (cntl-z) is encountered state_graph.process(); } // ********************************************************************* /// Return the RegEx passed as the first parameter fn get_regex(args: std::env::Args) -> String { // Get the arguments as a vector let args: Vec<String> = args.collect(); // Make sure only one argument was passed if args.len() != 2 { writeln!(std::io::stderr(), "Usage: cargo run 'regex'") .unwrap(); std::process::exit(1); } args[1].to_string() } // ********************************************************************* /// Implement the methods of the DFA structure impl DFA { //Create and return a DFA on the heap //Generate the DFA from the given regex fn new_from_regex(regex: &str) -> Box<DFA> { //Setup the regex as the language / alphabet of the dfa //Remove any duplicate word characters let mut l = regex.replace("|", ""); l = l.replace("+", ""); l = l.replace("*", ""); //Creates a language Vec<char> without the operators in it and pushing the sigma symbol for alphabet purposes let mut language: Vec<char> = l.chars().collect(); language.sort(); language.dedup(); language.push('Σ'); let final_state = l.len()+1; //Create a near blank dfa object, with 1 being start state, accept state being the final state // which is calculated based on the length of the regex length + 1 let mut dfa = Box::new(DFA{alphabet: language, start: 1, accept: [final_state].to_vec(), transitions: vec![] }); //Set current and next state to traverse through the graph as we create the transition matrix. let mut current_state = 1; let mut next_state = 2; //Create the Transitions Struct to save any transitions characters. These are characters that would // need to be cycled back to. First character and second state will always start this off. let mut transitions: Vec<Transitions> = Vec::new(); let t = Transitions{chars: regex.chars().next().unwrap(), state: 2}; transitions.push(t); //Create a previous_char character for | and * operators let mut previous_char = regex.chars().next().unwrap(); //Traverse through the regex string, reading characters and deciding what to do depending on the character. for c in regex.chars() { let mut states: Vec<usize> = Vec::new(); //Checks if previous char was a | operator. //If so, save the current character as a transition or cycle character //Also fixes any previous transition state if previous_char == '|' { for (n, a) in dfa.alphabet.iter().enumerate() { if *a == c { dfa.transitions[0][n] = next_state; } } let j = Transitions{chars: c, state: next_state}; transitions.push(j); } //Same as above, just with the * operator. if previous_char == '*' { let j = Transitions{chars: c, state: next_state}; transitions.push(j); } //Operator '|': Implemented - single and multiple | operators are working //Multiple types of symbols are untested and could produce varying results //Checks if character is | operator. If so, save the final state as an accept state, reset //current state back to 1, and set previous_char as | if c == '|' { let final_bar_state = dfa.transitions.len()+1; let mut final_bar_state_count: Vec<usize> = Vec::new(); dfa.accept.push(final_bar_state); for _a in dfa.alphabet.iter() { final_bar_state_count.push(final_bar_state); } dfa.transitions.push(final_bar_state_count); current_state = 1; previous_char = '|'; } //Operator '+': Implemented - single works, multiple is funky, almost working //Removes the previous transition matrix to remake it with updated states //Fix to the multiple + operators I believe is using a for loop to go through the entire transitions vec // but I have ran out of time to get that working. else if c == '+' { dfa.transitions.remove(dfa.transitions.len()-1); next_state -= 1; current_state -= 1; for a in dfa.alphabet.iter() { if a == &previous_char { states.push(next_state); } else { if *a == transitions[0].chars { states.push(transitions[0].state); } else { states.push(1); } } } dfa.transitions.push(states); next_state += 1; current_state += 1; } //Operator '*': Implemented - Single and multiple * operators are working. Something funky happens with the more characters // added into the regex, especially after a *. Not time to check it. Very close to getting this part fixed, most of it works //Similar to + operator, remove previous transition to replace it with new one. // Step back 2 states for next and current to allow for proper transition. Push necessary states.
//At the end, add 2 to current state to get back, and set previous_char as * else if c == '*' { dfa.transitions.remove(dfa.transitions.len()-1); let mut pushed_forward = false; next_state -= 2; current_state -= 2; for a in dfa.alphabet.iter() { if a == &previous_char { next_state += 1; states.push(next_state); } else if *a == 'Σ' { states.push(1); } else { if *a == transitions[0].chars { states.push(transitions[0].state); } else if !pushed_forward { next_state += 1; states.push(next_state); pushed_forward = true; } else { states.push(1); } } } dfa.transitions.push(states); current_state += 2; previous_char = '*'; } //All word character symbols: Implemented //Allows for any character that is in the language to be added in, checks if there is a transition/cycle //to be made, set the state as that before pushing. If it is not a transition, push to state 1 //if sigma symbol, push to state 1 else if c != 'Σ' { for a in dfa.alphabet.iter() { let mut was_transition = false; if c == *a { states.push(next_state); } else { for i in 0..transitions.len() { if *a == transitions[i].chars { states.push(transitions[i].state); was_transition = true; } } if was_transition == false { if previous_char == '*' && *a != 'Σ' { states.push(1); previous_char = c; } else { states.push(1); } } } } if previous_char != '|' { dfa.transitions.push(states); } next_state += 1; current_state += 1; previous_char = c; } } //Go back through and fix any transitions that weren't marked properly // (i.e. | transitions to state 2 from state 4 if applicable) for i in 0..dfa.transitions.len() { for n in 0..dfa.transitions[i].len() { if n < dfa.transitions[i].len() - 1 && dfa.transitions[i][n] == 1 { for c in 0..transitions.len() { if dfa.alphabet[n] == transitions[c].chars { dfa.transitions[i][n] = transitions[c].state; } } } } } //Set final state as a cycle for transition matrix. If 3 states, push [3,3,3] let mut final_state_count: Vec<usize> = Vec::new(); for _alphabet in dfa.alphabet.iter() { final_state_count.push(final_state); }
// Potential fix is similar to + operator with iterating over transitions instead of just checking index 0.
random_line_split
main.rs
transitions transitions: Vec<usize> } struct Transitions { chars: char, state: usize } fn main()
// ********************************************************************* /// Return the RegEx passed as the first parameter fn get_regex(args: std::env::Args) -> String { // Get the arguments as a vector let args: Vec<String> = args.collect(); // Make sure only one argument was passed if args.len() != 2 { writeln!(std::io::stderr(), "Usage: cargo run 'regex'") .unwrap(); std::process::exit(1); } args[1].to_string() } // ********************************************************************* /// Implement the methods of the DFA structure impl DFA { //Create and return a DFA on the heap //Generate the DFA from the given regex fn new_from_regex(regex: &str) -> Box<DFA> { //Setup the regex as the language / alphabet of the dfa //Remove any duplicate word characters let mut l = regex.replace("|", ""); l = l.replace("+", ""); l = l.replace("*", ""); //Creates a language Vec<char> without the operators in it and pushing the sigma symbol for alphabet purposes let mut language: Vec<char> = l.chars().collect(); language.sort(); language.dedup(); language.push('Σ'); let final_state = l.len()+1; //Create a near blank dfa object, with 1 being start state, accept state being the final state // which is calculated based on the length of the regex length + 1 let mut dfa = Box::new(DFA{alphabet: language, start: 1, accept: [final_state].to_vec(), transitions: vec![] }); //Set current and next state to traverse through the graph as we create the transition matrix. let mut current_state = 1; let mut next_state = 2; //Create the Transitions Struct to save any transitions characters. These are characters that would // need to be cycled back to. First character and second state will always start this off. let mut transitions: Vec<Transitions> = Vec::new(); let t = Transitions{chars: regex.chars().next().unwrap(), state: 2}; transitions.push(t); //Create a previous_char character for | and * operators let mut previous_char = regex.chars().next().unwrap(); //Traverse through the regex string, reading characters and deciding what to do depending on the character. for c in regex.chars() { let mut states: Vec<usize> = Vec::new(); //Checks if previous char was a | operator. //If so, save the current character as a transition or cycle character //Also fixes any previous transition state if previous_char == '|' { for (n, a) in dfa.alphabet.iter().enumerate() { if *a == c { dfa.transitions[0][n] = next_state; } } let j = Transitions{chars: c, state: next_state}; transitions.push(j); } //Same as above, just with the * operator. if previous_char == '*' { let j = Transitions{chars: c, state: next_state}; transitions.push(j); } //Operator '|': Implemented - single and multiple | operators are working //Multiple types of symbols are untested and could produce varying results //Checks if character is | operator. If so, save the final state as an accept state, reset //current state back to 1, and set previous_char as | if c == '|' { let final_bar_state = dfa.transitions.len()+1; let mut final_bar_state_count: Vec<usize> = Vec::new(); dfa.accept.push(final_bar_state); for _a in dfa.alphabet.iter() { final_bar_state_count.push(final_bar_state); } dfa.transitions.push(final_bar_state_count); current_state = 1; previous_char = '|'; } //Operator '+': Implemented - single works, multiple is funky, almost working //Removes the previous transition matrix to remake it with updated states //Fix to the multiple + operators I believe is using a for loop to go through the entire transitions vec // but I have ran out of time to get that working. else if c == '+' { dfa.transitions.remove(dfa.transitions.len()-1); next_state -= 1; current_state -= 1; for a in dfa.alphabet.iter() { if a == &previous_char { states.push(next_state); } else { if *a == transitions[0].chars { states.push(transitions[0].state); } else { states.push(1); } } } dfa.transitions.push(states); next_state += 1; current_state += 1; } //Operator '*': Implemented - Single and multiple * operators are working. Something funky happens with the more characters // added into the regex, especially after a *. Not time to check it. Very close to getting this part fixed, most of it works //Similar to + operator, remove previous transition to replace it with new one. // Step back 2 states for next and current to allow for proper transition. Push necessary states. // Potential fix is similar to + operator with iterating over transitions instead of just checking index 0. //At the end, add 2 to current state to get back, and set previous_char as * else if c == '*' { dfa.transitions.remove(dfa.transitions.len()-1); let mut pushed_forward = false; next_state -= 2; current_state -= 2; for a in dfa.alphabet.iter() { if a == &previous_char { next_state += 1; states.push(next_state); } else if *a == 'Σ' { states.push(1); } else { if *a == transitions[0].chars { states.push(transitions[0].state); } else if !pushed_forward { next_state += 1; states.push(next_state); pushed_forward = true; } else { states.push(1); } } } dfa.transitions.push(states); current_state += 2; previous_char = '*'; } //All word character symbols: Implemented //Allows for any character that is in the language to be added in, checks if there is a transition/cycle //to be made, set the state as that before pushing. If it is not a transition, push to state 1 //if sigma symbol, push to state 1 else if c != 'Σ' { for a in dfa.alphabet.iter() { let mut was_transition = false; if c == *a { states.push(next_state); } else { for i in 0..transitions.len() { if *a == transitions[i].chars { states.push(transitions[i].state); was_transition = true; } } if was_transition == false { if previous_char == '*' && *a != 'Σ' { states.push(1); previous_char = c; } else { states.push(1); } } } } if previous_char != '|' { dfa.transitions.push(states); } next_state += 1; current_state += 1; previous_char = c; } } //Go back through and fix any transitions that weren't marked properly // (i.e. | transitions to state 2 from state 4 if applicable) for i in 0..dfa.transitions.len() { for n in 0..dfa.transitions[i].len() { if n < dfa.transitions[i].len() - 1 && dfa.transitions[i][n] == 1 { for c in 0..transitions.len() { if dfa.alphabet[n] == transitions[c].chars { dfa.transitions[i][n] = transitions[c].state; } } } } } //Set final state as a cycle for transition matrix. If 3 states, push [3,3,3] let mut final_state_count: Vec<usize> = Vec::new(); for _alphabet in dfa.alphabet.iter() { final_state_count.push(final_state);
{ //Get and validate the RegEx on the command line let regex = get_regex(std::env::args()); let dfa = DFA::new_from_regex(&regex); //Create the dfa structure based on in RegEx entered from the command line let state_graph = StateGraph::new_from_dfa(&dfa); //eprintln!("{:?}", state_graph); state_graph.write_graphviz(); // Process through the input until end of file (cntl-z) is encountered state_graph.process(); }
identifier_body
cblmg_cs_supplierCoachGeocoder.js
0) { nlapiSelectLineItem('addressbook', shipLine); oShipAdrText = nlapiGetCurrentLineItemValue('addressbook', 'addrtext'); //oShipAdrText = nlapiGetLineItemValue('addressbook', 'addrtext', shipLine)?nlapiGetLineItemValue('addressbook', 'addrtext', shipLine):''; } //Changing it so that we ONLY execute drawGoogleMap when it's handled by userinterface if (paramGeoCodeFormIds.contains(nlapiGetFieldValue('customform')) && nlapiGetContext().getExecutionContext() == 'userinterface') { canGeoCode = true; drawGoogleMapByGeoCode(); } } function supplierValidateLine(type) { if (canGeoCode && type == 'addressbook' && nlapiGetCurrentLineItemValue(type, 'defaultshipping')=='T') { var nShipAdrText = nlapiGetCurrentLineItemValue('addressbook', 'addrtext')?nlapiGetCurrentLineItemValue('addressbook', 'addrtext'):''; //alert(oShipAdrText +' // '+nShipAdrText); if (oShipAdrText != nShipAdrText) { reGeoCode = true; nlapiSetFieldValue('custentity_cbl_shipadr_lat','',false,true); nlapiSetFieldValue('custentity_cbl_shipadr_lng','',false,true); nlapiSetFieldValue('custentity_cbl_shipadr_geodetail', '',false,true); } } return true; } /** * The recordType (internal id) corresponds to the "Applied To" record in your script deployment. * @appliedtorecord recordType * * @returns {Boolean} True to continue save, false to abort save */ function supplierSaveRecord(){ //Attempt to geocode ONLY on User Interface if (canGeoCode && nlapiGetContext().getExecutionContext() == 'userinterface') { var shipLine = nlapiFindLineItemValue('addressbook', 'defaultshipping', 'T'); if (shipLine >= 1 && (!nlapiGetFieldValue('custentity_cbl_shipadr_lat') || !nlapiGetFieldValue('custentity_cbl_shipadr_lng')) && paramGeoCodeSupplierCategoryIds.contains(nlapiGetFieldValue('category'))) { alert('Please Geocode Shipping Address'); return false; } } return true; } function supplierFldChanged(type, name, linenum) { if (!canGeoCode) { return; } //If both Lat/Lng values are set, display the Google Map map_canvas //ONLY Redraw IF Both Lat/Lng values are provided if ((name=='custentity_cbl_shipadr_lat' || name=='custentity_cbl_shipadr_lng') && nlapiGetFieldValue('custentity_cbl_shipadr_lat') && nlapiGetFieldValue('custentity_cbl_shipadr_lng')) { drawGoogleMapByGeoCode(); } } /** * ONLY on booking record to fire suitelet that looks up matching clients * @returns {Boolean} */ function radiusCoachLookup() { if (!nlapiGetFieldValue('custentity_cbl_shipadr_lat') && !nlapiGetFieldValue('custentity_cbl_shipadr_lng')) { alert('Please Geocode Booking Location Address under Location Subtab'); return false; } //8/5/2014 - Client requested following changes: // - Subsidiary search // - 100 within UK; 1000 outside UK // - Selected coach pin // - Buyer // - var selectedCoachId = nlapiGetFieldValue('custentity_bo_coach'); var bookingSubsId = nlapiGetFieldValue('subsidiary'); var bookingBuyer = nlapiGetFieldText('custentity_bo_buyer'); var bookingCourse = nlapiGetFieldText('custentity_bo_course'); var bookingItem = nlapiGetFieldText('custentity_bo_item'); var bookingDate = nlapiGetFieldValue('enddate'); var bookingTime = nlapiGetFieldValue('custentity_bo_eventtime'); var selectedCoachText = nlapiGetFieldText('custentity_bo_coach'); var bookingClientText = nlapiGetFieldText('parent'); //customscript_cbl_sl_radiuscoachsearch //customdeploy_cbl_sl_radiuscoachsearchd var radiusSlUrl = nlapiResolveURL('SUITELET', 'customscript_cbl_sl_radiuscoachsearch', 'customdeploy_cbl_sl_radiuscoachsearchd', 'VIEW')+ '&booklat='+nlapiGetFieldValue('custentity_cbl_shipadr_lat')+'&booklng='+nlapiGetFieldValue('custentity_cbl_shipadr_lng')+ '&bookcountryid='+nlapiGetFieldValue('custentity_bo_eventcountry')+'&bookcountry='+nlapiGetFieldText('custentity_bo_eventcountry')+ '&selectedcoach='+selectedCoachId+'&booksubsid='+bookingSubsId+'&buyer='+encodeURIComponent(bookingBuyer)+ '&bookdatetime='+encodeURIComponent(bookingDate+' '+bookingTime)+'&course='+encodeURIComponent(bookingCourse)+ '&item='+encodeURIComponent(bookingItem)+'&client='+bookingClientText+'&selectedcoachtext='+selectedCoachText+ '&entityid='+nlapiGetFieldValue('entityid')+'&bookid='+nlapiGetRecordId()+'&bookingtype='+nlapiGetFieldValue('jobtype'); window.open(radiusSlUrl,'Radius_Search','width=1200,height=700,resizable=yes,scrollbars=yes'); } function setValueFromRadiusLookup(_id) { nlapiSetFieldValue('custentity_bo_coach', _id, true, true); } function drawGoogleMapByGeoCode() { //Make sure map_canvas div element is present if (!document.getElementById('map_canvas')) { return; } if (canGeoCode && nlapiGetFieldValue('custentity_cbl_shipadr_lat') && nlapiGetFieldValue('custentity_cbl_shipadr_lng')) { try { var nsmap = null; var latlng = new google.maps.LatLng(nlapiGetFieldValue('custentity_cbl_shipadr_lat'), nlapiGetFieldValue('custentity_cbl_shipadr_lng')); var moption = { center:latlng, zoom:15, mapTypeId: google.maps.MapTypeId.ROADMAP }; nsmap = new google.maps.Map(document.getElementById('map_canvas'),moption); //Drops Lead Pin on the map new google.maps.Marker({ position: latlng, map:nsmap, animation: google.maps.Animation.DROP, title:"Geocode Location", icon: PinImageUrl }); /** google.maps.event.addListener(nsmap, 'zoom_changed', function() { setTimeout(zipLabelToggle, 2000); }); */ } catch(e) { alert('Error Drawing Map: '+e.toString()); } document.getElementById('map_canvas').style.display = 'block'; } else { //Hide The Mini Map document.getElementById('map_canvas').style.display = 'none'; } } function geoCodeShipAddress() { //grab default shipping address on the record var shipLine = nlapiFindLineItemValue('addressbook', 'defaultshipping', 'T'); var fldschecked = false; var geoAddressText = ''; for (var g=0; g < geoflds.length; g++) { if (document.getElementById(geoflds[g]) && document.getElementById(geoflds[g]).checked) { fldschecked=true; if (nlapiGetRecordType() == 'job') { //for Booking record. country and state drop down is custom record. check for existance of value instead of text. //TEXT value will be used for geo coding if (geoflds[g]=='geocountry') { geoAddressText += nlapiGetFieldValue(document.getElementById(geoflds[g]).value)?nlapiGetFieldText(document.getElementById(geoflds[g]).value):''+' '; } else if (geoflds[g]=='geocountry') { var stateVal = nlapiGetFieldValue(document.getElementById(geoflds[g]).value)?nlapiGetFieldText(document.getElementById(geoflds[g]).value):''; if (stateVal)
} else { geoAddressText += nlapiGetFieldValue(document.getElementById(geoflds[g]).value)?nlapiGetFieldValue(document.getElementById(geoflds[g]).value):''+' '; } } else { //2014v2 Update - Must use nlapiGetCurrentLineItemValue nlapiSelectLineItem('addressbook', shipLine); if (geoflds[g]=='geocountry') { geoAddressText += nlapiGetCurrentLineItemText('addressbook',document.getElementById(geoflds[g]).value)?nlapiGetCurrentLineItemText('addressbook',document.getElementById(geoflds[g]).value):''+' '; } else { geoAddressText += nlapiGetCurrentLineItemValue('addressbook',document.getElementById(geoflds
{ stateVal = strGlobalReplace(stateVal, 'CA - ', ''); stateVal = strGlobalReplace(stateVal, 'US - ', ''); geoAddressText += stateVal+' '; }
conditional_block
cblmg_cs_supplierCoachGeocoder.js
0) { nlapiSelectLineItem('addressbook', shipLine); oShipAdrText = nlapiGetCurrentLineItemValue('addressbook', 'addrtext'); //oShipAdrText = nlapiGetLineItemValue('addressbook', 'addrtext', shipLine)?nlapiGetLineItemValue('addressbook', 'addrtext', shipLine):''; } //Changing it so that we ONLY execute drawGoogleMap when it's handled by userinterface if (paramGeoCodeFormIds.contains(nlapiGetFieldValue('customform')) && nlapiGetContext().getExecutionContext() == 'userinterface') { canGeoCode = true; drawGoogleMapByGeoCode(); } } function supplierValidateLine(type) { if (canGeoCode && type == 'addressbook' && nlapiGetCurrentLineItemValue(type, 'defaultshipping')=='T') { var nShipAdrText = nlapiGetCurrentLineItemValue('addressbook', 'addrtext')?nlapiGetCurrentLineItemValue('addressbook', 'addrtext'):''; //alert(oShipAdrText +' // '+nShipAdrText); if (oShipAdrText != nShipAdrText) { reGeoCode = true; nlapiSetFieldValue('custentity_cbl_shipadr_lat','',false,true); nlapiSetFieldValue('custentity_cbl_shipadr_lng','',false,true); nlapiSetFieldValue('custentity_cbl_shipadr_geodetail', '',false,true); } } return true; } /** * The recordType (internal id) corresponds to the "Applied To" record in your script deployment. * @appliedtorecord recordType * * @returns {Boolean} True to continue save, false to abort save */ function supplierSaveRecord(){ //Attempt to geocode ONLY on User Interface if (canGeoCode && nlapiGetContext().getExecutionContext() == 'userinterface') { var shipLine = nlapiFindLineItemValue('addressbook', 'defaultshipping', 'T'); if (shipLine >= 1 && (!nlapiGetFieldValue('custentity_cbl_shipadr_lat') || !nlapiGetFieldValue('custentity_cbl_shipadr_lng')) && paramGeoCodeSupplierCategoryIds.contains(nlapiGetFieldValue('category'))) { alert('Please Geocode Shipping Address'); return false; } } return true; } function
(type, name, linenum) { if (!canGeoCode) { return; } //If both Lat/Lng values are set, display the Google Map map_canvas //ONLY Redraw IF Both Lat/Lng values are provided if ((name=='custentity_cbl_shipadr_lat' || name=='custentity_cbl_shipadr_lng') && nlapiGetFieldValue('custentity_cbl_shipadr_lat') && nlapiGetFieldValue('custentity_cbl_shipadr_lng')) { drawGoogleMapByGeoCode(); } } /** * ONLY on booking record to fire suitelet that looks up matching clients * @returns {Boolean} */ function radiusCoachLookup() { if (!nlapiGetFieldValue('custentity_cbl_shipadr_lat') && !nlapiGetFieldValue('custentity_cbl_shipadr_lng')) { alert('Please Geocode Booking Location Address under Location Subtab'); return false; } //8/5/2014 - Client requested following changes: // - Subsidiary search // - 100 within UK; 1000 outside UK // - Selected coach pin // - Buyer // - var selectedCoachId = nlapiGetFieldValue('custentity_bo_coach'); var bookingSubsId = nlapiGetFieldValue('subsidiary'); var bookingBuyer = nlapiGetFieldText('custentity_bo_buyer'); var bookingCourse = nlapiGetFieldText('custentity_bo_course'); var bookingItem = nlapiGetFieldText('custentity_bo_item'); var bookingDate = nlapiGetFieldValue('enddate'); var bookingTime = nlapiGetFieldValue('custentity_bo_eventtime'); var selectedCoachText = nlapiGetFieldText('custentity_bo_coach'); var bookingClientText = nlapiGetFieldText('parent'); //customscript_cbl_sl_radiuscoachsearch //customdeploy_cbl_sl_radiuscoachsearchd var radiusSlUrl = nlapiResolveURL('SUITELET', 'customscript_cbl_sl_radiuscoachsearch', 'customdeploy_cbl_sl_radiuscoachsearchd', 'VIEW')+ '&booklat='+nlapiGetFieldValue('custentity_cbl_shipadr_lat')+'&booklng='+nlapiGetFieldValue('custentity_cbl_shipadr_lng')+ '&bookcountryid='+nlapiGetFieldValue('custentity_bo_eventcountry')+'&bookcountry='+nlapiGetFieldText('custentity_bo_eventcountry')+ '&selectedcoach='+selectedCoachId+'&booksubsid='+bookingSubsId+'&buyer='+encodeURIComponent(bookingBuyer)+ '&bookdatetime='+encodeURIComponent(bookingDate+' '+bookingTime)+'&course='+encodeURIComponent(bookingCourse)+ '&item='+encodeURIComponent(bookingItem)+'&client='+bookingClientText+'&selectedcoachtext='+selectedCoachText+ '&entityid='+nlapiGetFieldValue('entityid')+'&bookid='+nlapiGetRecordId()+'&bookingtype='+nlapiGetFieldValue('jobtype'); window.open(radiusSlUrl,'Radius_Search','width=1200,height=700,resizable=yes,scrollbars=yes'); } function setValueFromRadiusLookup(_id) { nlapiSetFieldValue('custentity_bo_coach', _id, true, true); } function drawGoogleMapByGeoCode() { //Make sure map_canvas div element is present if (!document.getElementById('map_canvas')) { return; } if (canGeoCode && nlapiGetFieldValue('custentity_cbl_shipadr_lat') && nlapiGetFieldValue('custentity_cbl_shipadr_lng')) { try { var nsmap = null; var latlng = new google.maps.LatLng(nlapiGetFieldValue('custentity_cbl_shipadr_lat'), nlapiGetFieldValue('custentity_cbl_shipadr_lng')); var moption = { center:latlng, zoom:15, mapTypeId: google.maps.MapTypeId.ROADMAP }; nsmap = new google.maps.Map(document.getElementById('map_canvas'),moption); //Drops Lead Pin on the map new google.maps.Marker({ position: latlng, map:nsmap, animation: google.maps.Animation.DROP, title:"Geocode Location", icon: PinImageUrl }); /** google.maps.event.addListener(nsmap, 'zoom_changed', function() { setTimeout(zipLabelToggle, 2000); }); */ } catch(e) { alert('Error Drawing Map: '+e.toString()); } document.getElementById('map_canvas').style.display = 'block'; } else { //Hide The Mini Map document.getElementById('map_canvas').style.display = 'none'; } } function geoCodeShipAddress() { //grab default shipping address on the record var shipLine = nlapiFindLineItemValue('addressbook', 'defaultshipping', 'T'); var fldschecked = false; var geoAddressText = ''; for (var g=0; g < geoflds.length; g++) { if (document.getElementById(geoflds[g]) && document.getElementById(geoflds[g]).checked) { fldschecked=true; if (nlapiGetRecordType() == 'job') { //for Booking record. country and state drop down is custom record. check for existance of value instead of text. //TEXT value will be used for geo coding if (geoflds[g]=='geocountry') { geoAddressText += nlapiGetFieldValue(document.getElementById(geoflds[g]).value)?nlapiGetFieldText(document.getElementById(geoflds[g]).value):''+' '; } else if (geoflds[g]=='geocountry') { var stateVal = nlapiGetFieldValue(document.getElementById(geoflds[g]).value)?nlapiGetFieldText(document.getElementById(geoflds[g]).value):''; if (stateVal) { stateVal = strGlobalReplace(stateVal, 'CA - ', ''); stateVal = strGlobalReplace(stateVal, 'US - ', ''); geoAddressText += stateVal+' '; } } else { geoAddressText += nlapiGetFieldValue(document.getElementById(geoflds[g]).value)?nlapiGetFieldValue(document.getElementById(geoflds[g]).value):''+' '; } } else { //2014v2 Update - Must use nlapiGetCurrentLineItemValue nlapiSelectLineItem('addressbook', shipLine); if (geoflds[g]=='geocountry') { geoAddressText += nlapiGetCurrentLineItemText('addressbook',document.getElementById(geoflds[g]).value)?nlapiGetCurrentLineItemText('addressbook',document.getElementById(geoflds[g]).value):''+' '; } else { geoAddressText += nlapiGetCurrentLineItemValue('addressbook',document.getElementById(geoflds[g
supplierFldChanged
identifier_name
cblmg_cs_supplierCoachGeocoder.js
> 0) { nlapiSelectLineItem('addressbook', shipLine); oShipAdrText = nlapiGetCurrentLineItemValue('addressbook', 'addrtext'); //oShipAdrText = nlapiGetLineItemValue('addressbook', 'addrtext', shipLine)?nlapiGetLineItemValue('addressbook', 'addrtext', shipLine):''; } //Changing it so that we ONLY execute drawGoogleMap when it's handled by userinterface if (paramGeoCodeFormIds.contains(nlapiGetFieldValue('customform')) && nlapiGetContext().getExecutionContext() == 'userinterface') { canGeoCode = true; drawGoogleMapByGeoCode(); } } function supplierValidateLine(type) { if (canGeoCode && type == 'addressbook' && nlapiGetCurrentLineItemValue(type, 'defaultshipping')=='T') { var nShipAdrText = nlapiGetCurrentLineItemValue('addressbook', 'addrtext')?nlapiGetCurrentLineItemValue('addressbook', 'addrtext'):''; //alert(oShipAdrText +' // '+nShipAdrText); if (oShipAdrText != nShipAdrText) { reGeoCode = true; nlapiSetFieldValue('custentity_cbl_shipadr_lat','',false,true); nlapiSetFieldValue('custentity_cbl_shipadr_lng','',false,true); nlapiSetFieldValue('custentity_cbl_shipadr_geodetail', '',false,true); } } return true; } /** * The recordType (internal id) corresponds to the "Applied To" record in your script deployment. * @appliedtorecord recordType * * @returns {Boolean} True to continue save, false to abort save */ function supplierSaveRecord(){ //Attempt to geocode ONLY on User Interface if (canGeoCode && nlapiGetContext().getExecutionContext() == 'userinterface') { var shipLine = nlapiFindLineItemValue('addressbook', 'defaultshipping', 'T'); if (shipLine >= 1 && (!nlapiGetFieldValue('custentity_cbl_shipadr_lat') || !nlapiGetFieldValue('custentity_cbl_shipadr_lng')) && paramGeoCodeSupplierCategoryIds.contains(nlapiGetFieldValue('category'))) { alert('Please Geocode Shipping Address'); return false; } } return true; } function supplierFldChanged(type, name, linenum) { if (!canGeoCode) { return; } //If both Lat/Lng values are set, display the Google Map map_canvas //ONLY Redraw IF Both Lat/Lng values are provided if ((name=='custentity_cbl_shipadr_lat' || name=='custentity_cbl_shipadr_lng') && nlapiGetFieldValue('custentity_cbl_shipadr_lat') && nlapiGetFieldValue('custentity_cbl_shipadr_lng')) { drawGoogleMapByGeoCode(); } } /** * ONLY on booking record to fire suitelet that looks up matching clients * @returns {Boolean} */ function radiusCoachLookup() { if (!nlapiGetFieldValue('custentity_cbl_shipadr_lat') && !nlapiGetFieldValue('custentity_cbl_shipadr_lng')) { alert('Please Geocode Booking Location Address under Location Subtab'); return false; } //8/5/2014 - Client requested following changes: // - Subsidiary search // - 100 within UK; 1000 outside UK // - Selected coach pin // - Buyer // - var selectedCoachId = nlapiGetFieldValue('custentity_bo_coach'); var bookingSubsId = nlapiGetFieldValue('subsidiary'); var bookingBuyer = nlapiGetFieldText('custentity_bo_buyer'); var bookingCourse = nlapiGetFieldText('custentity_bo_course'); var bookingItem = nlapiGetFieldText('custentity_bo_item'); var bookingDate = nlapiGetFieldValue('enddate'); var bookingTime = nlapiGetFieldValue('custentity_bo_eventtime'); var selectedCoachText = nlapiGetFieldText('custentity_bo_coach'); var bookingClientText = nlapiGetFieldText('parent'); //customscript_cbl_sl_radiuscoachsearch //customdeploy_cbl_sl_radiuscoachsearchd var radiusSlUrl = nlapiResolveURL('SUITELET', 'customscript_cbl_sl_radiuscoachsearch', 'customdeploy_cbl_sl_radiuscoachsearchd', 'VIEW')+ '&booklat='+nlapiGetFieldValue('custentity_cbl_shipadr_lat')+'&booklng='+nlapiGetFieldValue('custentity_cbl_shipadr_lng')+ '&bookcountryid='+nlapiGetFieldValue('custentity_bo_eventcountry')+'&bookcountry='+nlapiGetFieldText('custentity_bo_eventcountry')+ '&selectedcoach='+selectedCoachId+'&booksubsid='+bookingSubsId+'&buyer='+encodeURIComponent(bookingBuyer)+
window.open(radiusSlUrl,'Radius_Search','width=1200,height=700,resizable=yes,scrollbars=yes'); } function setValueFromRadiusLookup(_id) { nlapiSetFieldValue('custentity_bo_coach', _id, true, true); } function drawGoogleMapByGeoCode() { //Make sure map_canvas div element is present if (!document.getElementById('map_canvas')) { return; } if (canGeoCode && nlapiGetFieldValue('custentity_cbl_shipadr_lat') && nlapiGetFieldValue('custentity_cbl_shipadr_lng')) { try { var nsmap = null; var latlng = new google.maps.LatLng(nlapiGetFieldValue('custentity_cbl_shipadr_lat'), nlapiGetFieldValue('custentity_cbl_shipadr_lng')); var moption = { center:latlng, zoom:15, mapTypeId: google.maps.MapTypeId.ROADMAP }; nsmap = new google.maps.Map(document.getElementById('map_canvas'),moption); //Drops Lead Pin on the map new google.maps.Marker({ position: latlng, map:nsmap, animation: google.maps.Animation.DROP, title:"Geocode Location", icon: PinImageUrl }); /** google.maps.event.addListener(nsmap, 'zoom_changed', function() { setTimeout(zipLabelToggle, 2000); }); */ } catch(e) { alert('Error Drawing Map: '+e.toString()); } document.getElementById('map_canvas').style.display = 'block'; } else { //Hide The Mini Map document.getElementById('map_canvas').style.display = 'none'; } } function geoCodeShipAddress() { //grab default shipping address on the record var shipLine = nlapiFindLineItemValue('addressbook', 'defaultshipping', 'T'); var fldschecked = false; var geoAddressText = ''; for (var g=0; g < geoflds.length; g++) { if (document.getElementById(geoflds[g]) && document.getElementById(geoflds[g]).checked) { fldschecked=true; if (nlapiGetRecordType() == 'job') { //for Booking record. country and state drop down is custom record. check for existance of value instead of text. //TEXT value will be used for geo coding if (geoflds[g]=='geocountry') { geoAddressText += nlapiGetFieldValue(document.getElementById(geoflds[g]).value)?nlapiGetFieldText(document.getElementById(geoflds[g]).value):''+' '; } else if (geoflds[g]=='geocountry') { var stateVal = nlapiGetFieldValue(document.getElementById(geoflds[g]).value)?nlapiGetFieldText(document.getElementById(geoflds[g]).value):''; if (stateVal) { stateVal = strGlobalReplace(stateVal, 'CA - ', ''); stateVal = strGlobalReplace(stateVal, 'US - ', ''); geoAddressText += stateVal+' '; } } else { geoAddressText += nlapiGetFieldValue(document.getElementById(geoflds[g]).value)?nlapiGetFieldValue(document.getElementById(geoflds[g]).value):''+' '; } } else { //2014v2 Update - Must use nlapiGetCurrentLineItemValue nlapiSelectLineItem('addressbook', shipLine); if (geoflds[g]=='geocountry') { geoAddressText += nlapiGetCurrentLineItemText('addressbook',document.getElementById(geoflds[g]).value)?nlapiGetCurrentLineItemText('addressbook',document.getElementById(geoflds[g]).value):''+' '; } else { geoAddressText += nlapiGetCurrentLineItemValue('addressbook',document.getElementById(geoflds[g
'&bookdatetime='+encodeURIComponent(bookingDate+' '+bookingTime)+'&course='+encodeURIComponent(bookingCourse)+ '&item='+encodeURIComponent(bookingItem)+'&client='+bookingClientText+'&selectedcoachtext='+selectedCoachText+ '&entityid='+nlapiGetFieldValue('entityid')+'&bookid='+nlapiGetRecordId()+'&bookingtype='+nlapiGetFieldValue('jobtype');
random_line_split
cblmg_cs_supplierCoachGeocoder.js
0) { nlapiSelectLineItem('addressbook', shipLine); oShipAdrText = nlapiGetCurrentLineItemValue('addressbook', 'addrtext'); //oShipAdrText = nlapiGetLineItemValue('addressbook', 'addrtext', shipLine)?nlapiGetLineItemValue('addressbook', 'addrtext', shipLine):''; } //Changing it so that we ONLY execute drawGoogleMap when it's handled by userinterface if (paramGeoCodeFormIds.contains(nlapiGetFieldValue('customform')) && nlapiGetContext().getExecutionContext() == 'userinterface') { canGeoCode = true; drawGoogleMapByGeoCode(); } } function supplierValidateLine(type)
/** * The recordType (internal id) corresponds to the "Applied To" record in your script deployment. * @appliedtorecord recordType * * @returns {Boolean} True to continue save, false to abort save */ function supplierSaveRecord(){ //Attempt to geocode ONLY on User Interface if (canGeoCode && nlapiGetContext().getExecutionContext() == 'userinterface') { var shipLine = nlapiFindLineItemValue('addressbook', 'defaultshipping', 'T'); if (shipLine >= 1 && (!nlapiGetFieldValue('custentity_cbl_shipadr_lat') || !nlapiGetFieldValue('custentity_cbl_shipadr_lng')) && paramGeoCodeSupplierCategoryIds.contains(nlapiGetFieldValue('category'))) { alert('Please Geocode Shipping Address'); return false; } } return true; } function supplierFldChanged(type, name, linenum) { if (!canGeoCode) { return; } //If both Lat/Lng values are set, display the Google Map map_canvas //ONLY Redraw IF Both Lat/Lng values are provided if ((name=='custentity_cbl_shipadr_lat' || name=='custentity_cbl_shipadr_lng') && nlapiGetFieldValue('custentity_cbl_shipadr_lat') && nlapiGetFieldValue('custentity_cbl_shipadr_lng')) { drawGoogleMapByGeoCode(); } } /** * ONLY on booking record to fire suitelet that looks up matching clients * @returns {Boolean} */ function radiusCoachLookup() { if (!nlapiGetFieldValue('custentity_cbl_shipadr_lat') && !nlapiGetFieldValue('custentity_cbl_shipadr_lng')) { alert('Please Geocode Booking Location Address under Location Subtab'); return false; } //8/5/2014 - Client requested following changes: // - Subsidiary search // - 100 within UK; 1000 outside UK // - Selected coach pin // - Buyer // - var selectedCoachId = nlapiGetFieldValue('custentity_bo_coach'); var bookingSubsId = nlapiGetFieldValue('subsidiary'); var bookingBuyer = nlapiGetFieldText('custentity_bo_buyer'); var bookingCourse = nlapiGetFieldText('custentity_bo_course'); var bookingItem = nlapiGetFieldText('custentity_bo_item'); var bookingDate = nlapiGetFieldValue('enddate'); var bookingTime = nlapiGetFieldValue('custentity_bo_eventtime'); var selectedCoachText = nlapiGetFieldText('custentity_bo_coach'); var bookingClientText = nlapiGetFieldText('parent'); //customscript_cbl_sl_radiuscoachsearch //customdeploy_cbl_sl_radiuscoachsearchd var radiusSlUrl = nlapiResolveURL('SUITELET', 'customscript_cbl_sl_radiuscoachsearch', 'customdeploy_cbl_sl_radiuscoachsearchd', 'VIEW')+ '&booklat='+nlapiGetFieldValue('custentity_cbl_shipadr_lat')+'&booklng='+nlapiGetFieldValue('custentity_cbl_shipadr_lng')+ '&bookcountryid='+nlapiGetFieldValue('custentity_bo_eventcountry')+'&bookcountry='+nlapiGetFieldText('custentity_bo_eventcountry')+ '&selectedcoach='+selectedCoachId+'&booksubsid='+bookingSubsId+'&buyer='+encodeURIComponent(bookingBuyer)+ '&bookdatetime='+encodeURIComponent(bookingDate+' '+bookingTime)+'&course='+encodeURIComponent(bookingCourse)+ '&item='+encodeURIComponent(bookingItem)+'&client='+bookingClientText+'&selectedcoachtext='+selectedCoachText+ '&entityid='+nlapiGetFieldValue('entityid')+'&bookid='+nlapiGetRecordId()+'&bookingtype='+nlapiGetFieldValue('jobtype'); window.open(radiusSlUrl,'Radius_Search','width=1200,height=700,resizable=yes,scrollbars=yes'); } function setValueFromRadiusLookup(_id) { nlapiSetFieldValue('custentity_bo_coach', _id, true, true); } function drawGoogleMapByGeoCode() { //Make sure map_canvas div element is present if (!document.getElementById('map_canvas')) { return; } if (canGeoCode && nlapiGetFieldValue('custentity_cbl_shipadr_lat') && nlapiGetFieldValue('custentity_cbl_shipadr_lng')) { try { var nsmap = null; var latlng = new google.maps.LatLng(nlapiGetFieldValue('custentity_cbl_shipadr_lat'), nlapiGetFieldValue('custentity_cbl_shipadr_lng')); var moption = { center:latlng, zoom:15, mapTypeId: google.maps.MapTypeId.ROADMAP }; nsmap = new google.maps.Map(document.getElementById('map_canvas'),moption); //Drops Lead Pin on the map new google.maps.Marker({ position: latlng, map:nsmap, animation: google.maps.Animation.DROP, title:"Geocode Location", icon: PinImageUrl }); /** google.maps.event.addListener(nsmap, 'zoom_changed', function() { setTimeout(zipLabelToggle, 2000); }); */ } catch(e) { alert('Error Drawing Map: '+e.toString()); } document.getElementById('map_canvas').style.display = 'block'; } else { //Hide The Mini Map document.getElementById('map_canvas').style.display = 'none'; } } function geoCodeShipAddress() { //grab default shipping address on the record var shipLine = nlapiFindLineItemValue('addressbook', 'defaultshipping', 'T'); var fldschecked = false; var geoAddressText = ''; for (var g=0; g < geoflds.length; g++) { if (document.getElementById(geoflds[g]) && document.getElementById(geoflds[g]).checked) { fldschecked=true; if (nlapiGetRecordType() == 'job') { //for Booking record. country and state drop down is custom record. check for existance of value instead of text. //TEXT value will be used for geo coding if (geoflds[g]=='geocountry') { geoAddressText += nlapiGetFieldValue(document.getElementById(geoflds[g]).value)?nlapiGetFieldText(document.getElementById(geoflds[g]).value):''+' '; } else if (geoflds[g]=='geocountry') { var stateVal = nlapiGetFieldValue(document.getElementById(geoflds[g]).value)?nlapiGetFieldText(document.getElementById(geoflds[g]).value):''; if (stateVal) { stateVal = strGlobalReplace(stateVal, 'CA - ', ''); stateVal = strGlobalReplace(stateVal, 'US - ', ''); geoAddressText += stateVal+' '; } } else { geoAddressText += nlapiGetFieldValue(document.getElementById(geoflds[g]).value)?nlapiGetFieldValue(document.getElementById(geoflds[g]).value):''+' '; } } else { //2014v2 Update - Must use nlapiGetCurrentLineItemValue nlapiSelectLineItem('addressbook', shipLine); if (geoflds[g]=='geocountry') { geoAddressText += nlapiGetCurrentLineItemText('addressbook',document.getElementById(geoflds[g]).value)?nlapiGetCurrentLineItemText('addressbook',document.getElementById(geoflds[g]).value):''+' '; } else { geoAddressText += nlapiGetCurrentLineItemValue('addressbook',document.getElementById(geoflds
{ if (canGeoCode && type == 'addressbook' && nlapiGetCurrentLineItemValue(type, 'defaultshipping')=='T') { var nShipAdrText = nlapiGetCurrentLineItemValue('addressbook', 'addrtext')?nlapiGetCurrentLineItemValue('addressbook', 'addrtext'):''; //alert(oShipAdrText +' // '+nShipAdrText); if (oShipAdrText != nShipAdrText) { reGeoCode = true; nlapiSetFieldValue('custentity_cbl_shipadr_lat','',false,true); nlapiSetFieldValue('custentity_cbl_shipadr_lng','',false,true); nlapiSetFieldValue('custentity_cbl_shipadr_geodetail', '',false,true); } } return true; }
identifier_body
i2c.rs
= if speed <= Frequency::KHz(100) { clocks.apb1.mhz() + 1 } else { ((clocks.apb.mhz() * 300) / 1000) + 1 }; // Configure correct rise times new.write_bits(8, 0, trise, 6); // I2C clock control calculation // If in slow mode if speed <= Frequency::KHz(100) { let ccr = match clocks.apb.hz() / (speed.hz() * 2) { 0...3 => 4, n => n, }; // Set clock to standard mode with appropiate parameters for selected speed new.clear(7, 15) .clear(7, 14) .write_bits(7, 0, ccr, 12); } else { // Fast mode // Defaults for now to 2:1 duty cycle if true { let ccr = match clocks.apb1.hz() / (speed.hz() * 3) { 0 => 1, n => n, }; new.set(7, 15) .clear(7, 14) .write_bits(7, 0, ccr, 12); } else { // 16:9 duty cycle let ccr = match clocks.apb1.hz() / (speed.hz() * 25) { 0 => 1, n => n, }; new.set(7, 15) .set(7, 14) .write_bits(7, 0, ccr, 12); } } new.set(0, 0); Ok( new ) } /// Stop the peripheral and release the pins pub fn free(&mut self) -> (Pin, Pin) { self.clear(0, 0); self.pins } } /* impl I2c { /// Scans for devices and returns all the addresses it found connected pub fn scan(&mut self) -> Vec<u8> { let mut addresses = Vec::new(); let mut void = &[0]; for i in 0..128 { match self.read(i, void) { Ok(()) => addresses.push(i), _ => (), } } addresses } } */ impl Read for I2c { type Error = I2CError; /// Read bytes into buffer /// This function is based on MASTER mode /// WARNING! /// `unsafe` function (but now marked as such). This function may leave the sender hanging /// if the sender sends more bytes than what the buffer can hold. /// This is due to no STOP signal being sent back. fn read(&mut self, addr: u8, buffer: &mut [u8]) -> Result<(), I2CError> { let last = buffer.len() - 1; // Send start condition and ACK bit self.start() .ack(); // Wait until START condition is generated while !self.is_set(5, 0) {} // Wait until all devices are listening to us (bus is free) while !self.is_set(6, 0) && !self.is_set(6, 1) {} // Set up current address to talk to self.write_data(((addr as u32) << 1) + 1); // wait until address was sent while !self.is_set(5, 1) {} // Clear condition by reading SR2 let _ = self.block[6].read(); // Store bytes for i in 0..last { buffer[i] = self.recv_byte()?; } self.nack() .stop(); // Read last byte buffer[last] = self.recv_byte()?; Ok(()) } } impl Write for I2c { type Error = I2CError; /// Send a buffer of bytes fn write(&mut self, addr: u8, bytes: &[u8]) -> Result<(), I2CError> { // Send START condition self.start(); // Wait until START condition is generated while !self.is_set(5, 0) {} // Wait until all devices are listening to us (bus is free) while !self.is_set(6, 0) && !self.is_set(6, 1) {} // Set up current address to talk to self.write_data((addr as u32) << 1); // wait until address was sent while !self.is_set(5, 1) {} // Clear condition by reading SR2 // let _ = ptr::read_volatile(self as u32 + 0x18); let _ = self.block[6].read(); // Send the bytes for b in bytes { self.send_byte(*b)?; } Ok(()) } } impl WriteRead for I2c { type Error = I2CError; /// Writes some bytes then reads some bytes fn write_read(&mut self, addr: u8, bytes: &[u8], buffer: &mut [u8]) -> Result<(), I2CError> { self.write(addr, bytes)?; self.read(addr, buffer) } } impl I2c { /// Sends a byte pub fn send_byte(&mut self, byte: u8) -> Result<&mut Self, I2CError> { // Wait until TX buffer is empty while !self.is_raised(I2CFlags::TxEmpty) {} self.write_data(byte as u32); while { if self.is_raised(I2CFlags::ACKFailure) { return Err(I2CError::NACK); } !self.is_raised(I2CFlags::TransferComplete) } {} Ok( self ) } /// Receive a byte pub fn recv_byte(&self) -> Result<u8, I2CError> { while !self.is_raised(I2CFlags::RxNotEmpty) {} Ok( self.read_data() ) } } impl I2c { /// Enable the sending of ACK signal after byte transfer pub fn ack(&mut self) -> &mut Self { self.set(0, 10) } /// Disable the sending of ACK signal (effectively sending a NACK) after byte transfer pub fn nack(&mut self) -> &mut Self { self.clear(0, 10) } /// Stop generation /// 0: No stop generation /// 1: Slave Mode - Release the SCL and SDA lines after current byte transfer /// Master Mode - Stop generation after the current byte transfer or current Start condition is sent pub fn stop(&mut self) -> &mut Self { self.set(0, 9) } /// Start generation /// 0: No start generation /// 1: Slave Mode - Start generation when bus id free /// Master Mode - Repeated start generation pub fn start(&mut self) -> &mut Self { self.set(0, 8) } /// Enable/Disable peripheral pub fn state(&mut self, s: bool) -> &mut Self { match s { true => self.set(0, 0), _ => self.clear(0, 0), } } /// If enabled the next byte will received in shift register pub fn receive_in_shift(&mut self) -> &mut Self { self.set(0, 11) } /// Starts Packet Error Checking (PEC) for the next transfer pub fn start_pec(&mut self) -> &mut Self { self.set(0, 12) } /// Resets the peripheral pub fn reset(&mut self) -> &mut Self { // TODO : check lines are free self.stop() .set(0, 15) } /// Sets the frequency of the transfer pub fn set_frequency(&mut self, f: Frequency) -> Result<&mut Self, I2CError> { match f.mhz() { 2...50 => Ok( self.write_bits(1, 0, f.mhz() as u32, 6) ), _ => Err(I2CError::InvalidBusSpeed), } } /// Indicate this is the last trasnfer pub fn last_transfer(&mut self) -> &mut Self { self.set(1, 12) } /// Enable/Disable interrupt pub fn int_state(&mut self, s: bool, int: I2CInterrupt) -> &mut Self { let offsets = int.offsets(); match s { true => self.set(offsets.0, offsets.1), _ => self.clear(offsets.0, offsets.1), } } /// Sets the addressing mode between 7-bit and 10-bit pub fn
address_mode
identifier_name
i2c.rs
back. fn read(&mut self, addr: u8, buffer: &mut [u8]) -> Result<(), I2CError> { let last = buffer.len() - 1; // Send start condition and ACK bit self.start() .ack(); // Wait until START condition is generated while !self.is_set(5, 0) {} // Wait until all devices are listening to us (bus is free) while !self.is_set(6, 0) && !self.is_set(6, 1) {} // Set up current address to talk to self.write_data(((addr as u32) << 1) + 1); // wait until address was sent while !self.is_set(5, 1) {} // Clear condition by reading SR2 let _ = self.block[6].read(); // Store bytes for i in 0..last { buffer[i] = self.recv_byte()?; } self.nack() .stop(); // Read last byte buffer[last] = self.recv_byte()?; Ok(()) } } impl Write for I2c { type Error = I2CError; /// Send a buffer of bytes fn write(&mut self, addr: u8, bytes: &[u8]) -> Result<(), I2CError> { // Send START condition self.start(); // Wait until START condition is generated while !self.is_set(5, 0) {} // Wait until all devices are listening to us (bus is free) while !self.is_set(6, 0) && !self.is_set(6, 1) {} // Set up current address to talk to self.write_data((addr as u32) << 1); // wait until address was sent while !self.is_set(5, 1) {} // Clear condition by reading SR2 // let _ = ptr::read_volatile(self as u32 + 0x18); let _ = self.block[6].read(); // Send the bytes for b in bytes { self.send_byte(*b)?; } Ok(()) } } impl WriteRead for I2c { type Error = I2CError; /// Writes some bytes then reads some bytes fn write_read(&mut self, addr: u8, bytes: &[u8], buffer: &mut [u8]) -> Result<(), I2CError> { self.write(addr, bytes)?; self.read(addr, buffer) } } impl I2c { /// Sends a byte pub fn send_byte(&mut self, byte: u8) -> Result<&mut Self, I2CError> { // Wait until TX buffer is empty while !self.is_raised(I2CFlags::TxEmpty) {} self.write_data(byte as u32); while { if self.is_raised(I2CFlags::ACKFailure) { return Err(I2CError::NACK); } !self.is_raised(I2CFlags::TransferComplete) } {} Ok( self ) } /// Receive a byte pub fn recv_byte(&self) -> Result<u8, I2CError> { while !self.is_raised(I2CFlags::RxNotEmpty) {} Ok( self.read_data() ) } } impl I2c { /// Enable the sending of ACK signal after byte transfer pub fn ack(&mut self) -> &mut Self { self.set(0, 10) } /// Disable the sending of ACK signal (effectively sending a NACK) after byte transfer pub fn nack(&mut self) -> &mut Self { self.clear(0, 10) } /// Stop generation /// 0: No stop generation /// 1: Slave Mode - Release the SCL and SDA lines after current byte transfer /// Master Mode - Stop generation after the current byte transfer or current Start condition is sent pub fn stop(&mut self) -> &mut Self { self.set(0, 9) } /// Start generation /// 0: No start generation /// 1: Slave Mode - Start generation when bus id free /// Master Mode - Repeated start generation pub fn start(&mut self) -> &mut Self { self.set(0, 8) } /// Enable/Disable peripheral pub fn state(&mut self, s: bool) -> &mut Self { match s { true => self.set(0, 0), _ => self.clear(0, 0), } } /// If enabled the next byte will received in shift register pub fn receive_in_shift(&mut self) -> &mut Self { self.set(0, 11) } /// Starts Packet Error Checking (PEC) for the next transfer pub fn start_pec(&mut self) -> &mut Self { self.set(0, 12) } /// Resets the peripheral pub fn reset(&mut self) -> &mut Self { // TODO : check lines are free self.stop() .set(0, 15) } /// Sets the frequency of the transfer pub fn set_frequency(&mut self, f: Frequency) -> Result<&mut Self, I2CError> { match f.mhz() { 2...50 => Ok( self.write_bits(1, 0, f.mhz() as u32, 6) ), _ => Err(I2CError::InvalidBusSpeed), } } /// Indicate this is the last trasnfer pub fn last_transfer(&mut self) -> &mut Self { self.set(1, 12) } /// Enable/Disable interrupt pub fn int_state(&mut self, s: bool, int: I2CInterrupt) -> &mut Self { let offsets = int.offsets(); match s { true => self.set(offsets.0, offsets.1), _ => self.clear(offsets.0, offsets.1), } } /// Sets the addressing mode between 7-bit and 10-bit pub fn address_mode(&mut self, a: I2CBitMode) -> &mut Self { match a { I2CBitMode::Bit7 => self.clear(2, 15), _ => self.set(2, 15), } } /// Writes the interface address 1 /// To be set **after** the interface bit size is set (7-bit or 10-bit) pub fn set_address_1(&mut self, addr: u32) -> &mut Self { match self.is_set(2, 15) { true => self.write_bits(2, 0, addr, 10), _ => self.write_bits(2, 1, addr, 7), } } /// Writes the interface address 2 /// Returns an error if not in 7 bit mode pub fn set_address_2(&mut self, addr: u32) -> Result<&mut Self, I2CError> { match self.is_set(2, 15) { true => Err( I2CError::Address2NotAllowed), _ => Ok( self.write_bits(3, 1, addr, 7) ), } } /// Enable/Disable dual addressing mode pub fn dual_address_state(&mut self, s: bool) -> &mut Self { match s { true => self.set(3, 0), _ => self.clear(3, 0), } } /// Read received byte pub fn read_data(&self) -> u8 { self.block[4].read() as u8 } /// Write data to be transmitted pub fn write_data(&mut self, data: u32) -> &mut Self { self.write_bits(4, 0, data, 8) } /// Returns true if the flag is raised pub fn is_raised(&self, f: I2CFlags) -> bool { let offsets = f.offsets(); self.is_set( offsets.0, offsets.1 ) } /// Returns true if the device is master pub fn is_master(&self) -> bool { self.is_set(6, 0) } /// Returns true if the bus is busy pub fn is_bus_busy(&self) -> bool { self.is_set(6, 1) } /// Returns true if the TRA bit is set pub fn is_tra_set(&self) -> bool { self.is_set(6, 2) } /// Returns which Dual Address has matched pub fn which_addr(&self) -> DualAddress { match self.is_set(6, 7) { true => DualAddress::Addr2, _ => DualAddress::Addr1, } } /// Returns the PEC register pub fn pec(&self) -> u32
{ (self.block[6].read() >> 8) & 0b1111_1111 }
identifier_body
i2c.rs
2CError::Other), }; let new = I2c { block: &mut *(address as *mut _), pins, }; let (sda, scl) = pins; // TODO : Change to each board sda.altfn(4) .speed(HIGH); scl.altfn(4) .speed(HIGH); rcc.peripheral_state(true, i2cid); rcc.reset_peripheral(i2cid); // TODO set up RCC clocks // Disable he peripheral // by clearing PE bit in CR1 new.clear(0, 0); // Calculate settings for I2C speed modes // If the user used the RCC given clocks, APB clock is legal // Configure bus frequency into I2C peripheral new.write_bits(1, 0, clocks.apb1.mhz(), 6); let trise = if speed <= Frequency::KHz(100) { clocks.apb1.mhz() + 1 } else { ((clocks.apb.mhz() * 300) / 1000) + 1 }; // Configure correct rise times new.write_bits(8, 0, trise, 6); // I2C clock control calculation // If in slow mode if speed <= Frequency::KHz(100) { let ccr = match clocks.apb.hz() / (speed.hz() * 2) { 0...3 => 4, n => n, }; // Set clock to standard mode with appropiate parameters for selected speed new.clear(7, 15) .clear(7, 14) .write_bits(7, 0, ccr, 12); } else { // Fast mode // Defaults for now to 2:1 duty cycle if true { let ccr = match clocks.apb1.hz() / (speed.hz() * 3) { 0 => 1, n => n, }; new.set(7, 15) .clear(7, 14) .write_bits(7, 0, ccr, 12); } else { // 16:9 duty cycle let ccr = match clocks.apb1.hz() / (speed.hz() * 25) { 0 => 1, n => n, }; new.set(7, 15) .set(7, 14) .write_bits(7, 0, ccr, 12); } } new.set(0, 0); Ok( new ) } /// Stop the peripheral and release the pins pub fn free(&mut self) -> (Pin, Pin) { self.clear(0, 0); self.pins } } /* impl I2c { /// Scans for devices and returns all the addresses it found connected pub fn scan(&mut self) -> Vec<u8> { let mut addresses = Vec::new(); let mut void = &[0]; for i in 0..128 { match self.read(i, void) { Ok(()) => addresses.push(i), _ => (), } } addresses } } */ impl Read for I2c { type Error = I2CError; /// Read bytes into buffer /// This function is based on MASTER mode /// WARNING! /// `unsafe` function (but now marked as such). This function may leave the sender hanging /// if the sender sends more bytes than what the buffer can hold. /// This is due to no STOP signal being sent back. fn read(&mut self, addr: u8, buffer: &mut [u8]) -> Result<(), I2CError> { let last = buffer.len() - 1; // Send start condition and ACK bit self.start() .ack(); // Wait until START condition is generated while !self.is_set(5, 0) {} // Wait until all devices are listening to us (bus is free) while !self.is_set(6, 0) && !self.is_set(6, 1) {} // Set up current address to talk to self.write_data(((addr as u32) << 1) + 1); // wait until address was sent
// Clear condition by reading SR2 let _ = self.block[6].read(); // Store bytes for i in 0..last { buffer[i] = self.recv_byte()?; } self.nack() .stop(); // Read last byte buffer[last] = self.recv_byte()?; Ok(()) } } impl Write for I2c { type Error = I2CError; /// Send a buffer of bytes fn write(&mut self, addr: u8, bytes: &[u8]) -> Result<(), I2CError> { // Send START condition self.start(); // Wait until START condition is generated while !self.is_set(5, 0) {} // Wait until all devices are listening to us (bus is free) while !self.is_set(6, 0) && !self.is_set(6, 1) {} // Set up current address to talk to self.write_data((addr as u32) << 1); // wait until address was sent while !self.is_set(5, 1) {} // Clear condition by reading SR2 // let _ = ptr::read_volatile(self as u32 + 0x18); let _ = self.block[6].read(); // Send the bytes for b in bytes { self.send_byte(*b)?; } Ok(()) } } impl WriteRead for I2c { type Error = I2CError; /// Writes some bytes then reads some bytes fn write_read(&mut self, addr: u8, bytes: &[u8], buffer: &mut [u8]) -> Result<(), I2CError> { self.write(addr, bytes)?; self.read(addr, buffer) } } impl I2c { /// Sends a byte pub fn send_byte(&mut self, byte: u8) -> Result<&mut Self, I2CError> { // Wait until TX buffer is empty while !self.is_raised(I2CFlags::TxEmpty) {} self.write_data(byte as u32); while { if self.is_raised(I2CFlags::ACKFailure) { return Err(I2CError::NACK); } !self.is_raised(I2CFlags::TransferComplete) } {} Ok( self ) } /// Receive a byte pub fn recv_byte(&self) -> Result<u8, I2CError> { while !self.is_raised(I2CFlags::RxNotEmpty) {} Ok( self.read_data() ) } } impl I2c { /// Enable the sending of ACK signal after byte transfer pub fn ack(&mut self) -> &mut Self { self.set(0, 10) } /// Disable the sending of ACK signal (effectively sending a NACK) after byte transfer pub fn nack(&mut self) -> &mut Self { self.clear(0, 10) } /// Stop generation /// 0: No stop generation /// 1: Slave Mode - Release the SCL and SDA lines after current byte transfer /// Master Mode - Stop generation after the current byte transfer or current Start condition is sent pub fn stop(&mut self) -> &mut Self { self.set(0, 9) } /// Start generation /// 0: No start generation /// 1: Slave Mode - Start generation when bus id free /// Master Mode - Repeated start generation pub fn start(&mut self) -> &mut Self { self.set(0, 8) } /// Enable/Disable peripheral pub fn state(&mut self, s: bool) -> &mut Self { match s { true => self.set(0, 0), _ => self.clear(0, 0), } } /// If enabled the next byte will received in shift register pub fn receive_in_shift(&mut self) -> &mut Self { self.set(0, 11) } /// Starts Packet Error Checking (PEC) for the next transfer pub fn start_pec(&mut self) -> &mut Self { self.set(0, 12) } /// Resets the peripheral pub fn reset(&mut self) -> &mut Self { // TODO : check lines are free self.stop() .set(0, 15) } /// Sets the frequency of the transfer pub fn set_frequency(&mut self
while !self.is_set(5, 1) {}
random_line_split
bot.go
Path { path = append(path, game.GetIndex(node.X, node.Y)) } return path, nil } func GetBestMove(game *gioframework.Game) (bestFrom int, bestTo int) { defer func() { if r := recover(); r != nil { log.Printf("ERROR, GetBestMove recovered from panic: %v", r) fmt.Println(errors.Wrap(r, 2).ErrorStack()) bestFrom = -1 bestTo = -1 } }() bestFrom = -1 bestTo = -1 bestTotalScore := -10. var bestScores map[string]float64 myGeneral := game.Generals[game.PlayerIndex] enemyCOM := getEnemyCenterOfMass(game) /// First check for attacking new empty or enemy tiles for from, fromTile := range game.GameMap { if fromTile.Faction != game.PlayerIndex || fromTile.Armies < 2 { continue } for to, toTile := range game.GameMap { if toTile.Faction < TileEmpty || toTile.Faction == game.PlayerIndex { continue } if game.ImpossibleTiles[to] { continue } isEmpty := toTile.Faction == TileEmpty isEnemy := IsEnemy(game, toTile) isGeneral := toTile.Type == gioframework.General isCity := toTile.Type == gioframework.City outnumber := float64(fromTile.Armies - toTile.Armies) dist := getHeuristicPathDistance(game, from, to) distFromGen := getHeuristicPathDistance(game, myGeneral, to) center := game.GetIndex(game.Width/2, game.Height/2) distCenter := getHeuristicPathDistance(game, center, to) centerness := 1. - distCenter/float64(game.Width) // This is the vector pointing towards the enemy enemyVector := [2]int{ game.GetRow(enemyCOM) - game.GetRow(myGeneral), game.GetCol(enemyCOM) - game.GetCol(myGeneral), } // The vector showing the proposed move moveVector := [2]int{ game.GetRow(to) - game.GetRow(from), game.GetCol(to) - game.GetCol(from), } neighbors := game.GetNeighborhood(to, false) numAlliedNeighbors := 0 for _, neighbor := range neighbors { if !IsEnemy(game, game.GameMap[neighbor]) { numAlliedNeighbors += 1 } } if isCity && outnumber < 2 && !isEnemy { // Never attack a neutral city and lose continue } scores := make(map[string]float64) scores["outnumber score"] = Truncate(outnumber/200, 0., 0.25) * Btof(isEnemy) scores["outnumbered penalty"] = -0.2 * Btof(outnumber < 2) scores["general threat score"] = (0.25 * math.Pow(distFromGen, -1.0)) * Truncate(float64(toTile.Armies)/10, 0., 1.0) * Btof(isEnemy) scores["dist penalty"] = Truncate(-0.5*dist/30, -0.3, 0) scores["dist gt army penalty"] = -0.2 * Btof(fromTile.Armies < int(dist)) scores["is enemy score"] = 0.05 * Btof(isEnemy) scores["close city score"] = 0.35 * Btof(isCity && outnumber >= 2) * math.Pow(distFromGen, -0.5) scores["enemy city score"] = 0.2 * Btof(isCity && isEnemy) scores["enemy gen score"] = 0.15 * Btof(isGeneral) * Btof(isEnemy) scores["empty score"] = 0.08 * Btof(isEmpty) // Generally a good strategy to take the center of the board scores["centerness score"] = 0.02 * centerness // You should move towards enemy's main base, not random little // patches of enemy. This prevents the bot from "cleaning up" // irrelevant squares. This could be improved by making the vectors // normalized and having the score gradually increase as you point // towards the enemy scores["towards enemy score"] = 0.03 * Btof(dotProduct(enemyVector, moveVector) > 1) // Instead of attacking all the tiles on the enemy's border it is // typically better to make a deep drive into enemy land scores["deep drive score"] = 0.04 * Btof(numAlliedNeighbors < 2) totalScore := 0. for _, score := range scores { totalScore += score } //log.Printf("============Considering move %v->%v, got score: %v\n", from, to, totalScore) //logSortedScores(scores) if totalScore > bestTotalScore { bestScores = scores bestTotalScore = totalScore bestFrom = from bestTo = to } } } logSortedScores(bestScores) log.Printf("Attack score: %.2f", bestTotalScore) log.Printf("From:%v To:%v", game.GetCoordString(bestFrom), game.GetCoordString(bestTo)) log.Println("--------") /////////////// Then check for consolidation ////////////////////////////// //consolScore := getConsolidationScore(game) // It's a good idea to consolidate armies right after the armies regenerate. // armyCycle shows the amount of the cycle that's passed. [0, 1] armyCycle := float64(game.TurnCount % 50) / 50 consolScore := 0.6 * math.Pow(1-armyCycle, 6) - 0.2 log.Printf("Consolidation score:%.2f", consolScore) tiles := getTilesSortedOnArmies(game) if len(tiles) > 10 && consolScore > bestTotalScore { largestTile := tiles[0] for _, tile := range tiles[:5] { log.Printf("Army ranked: %v", game.GameMap[tile].Armies) } highestAvArmy := 0. for _, from := range tiles[1:] { if game.GameMap[from].Armies < 2 { continue } // Warning: this path could cut through enemy territory! Keep an // eye for this path_, _ := GetShortestPath(game, from, largestTile) // TODO: handle err //TODO: this throws an error it seems! largestTile == from ??!! armies := 0 for _, i := range path_[:len(path_)-1] { armies += game.GameMap[i].Armies } av_army := float64(armies) / float64(len(path_)-1) if av_army > highestAvArmy { highestAvArmy = av_army bestFrom = from } } bestTo = largestTile // We want to move towards the enemy. Reverse if that's not the // case if enemyCOM >= 0 { fromDistEnemy := getHeuristicPathDistance(game, enemyCOM, bestFrom) toDistEnemy := getHeuristicPathDistance(game, enemyCOM, bestTo) if fromDistEnemy < toDistEnemy { log.Println("Switching direction of consolidation") bestFrom, bestTo = bestTo, bestFrom } } log.Printf("Consolidating, with average army: %v", highestAvArmy) } // Trash talk toTile := game.GameMap[bestTo] if toTile.Type == gioframework.City && IsEnemy(game, toTile) { game.SendChat("Sorry, I'm gonna need that citadel") } return bestFrom, bestTo } func IsEnemy(game *gioframework.Game, tile gioframework.Cell) bool { if len(game.Teams) == 0 { // This means we're playing 1v1 or FFA return tile.Faction != game.PlayerIndex && tile.Faction >= 0 } else { myTeam := game.Teams[game.PlayerIndex] return tile.Faction >= 0 && game.Teams[tile.Faction] != myTeam } } func logSortedScores(scores map[string]float64) { keys := make([]string, len(scores)) i := 0 for k := range scores { keys[i] = k i++ } sort.Strings(keys) for _, k := range keys { log.Printf("%20v: %.3f\n", k, scores[k]) } } func Truncate(val, min, max float64) float64
{ return math.Min(math.Max(val, min), max) }
identifier_body
bot.go
ies) game.Attack(path[i], path[i+1], false) } } log.Printf("Replay available at: http://bot.generals.io/replays/%v", game.ReplayID) time.Sleep(10*time.Second) } } func setupLogging() { logDir := "log" _ = os.Mkdir(logDir, os.ModePerm) rand.Seed(time.Now().UTC().UnixNano()) logFilename := path.Join(logDir, "log_"+strconv.Itoa(rand.Intn(10000))) logFile, err := os.OpenFile(logFilename, os.O_CREATE|os.O_APPEND|os.O_RDWR, 0666) check(err) mw := io.MultiWriter(os.Stdout, logFile) log.SetOutput(mw) } func logTurnData(g *gioframework.Game) { log.Println("------------------------------------------") log.Printf("Turn: %v (UI%v)", g.TurnCount, float64(g.TurnCount)/2.) var msgs []string for i, s := range g.Scores { msg := fmt.Sprintf("%10v: Tiles: %v, Army: %v", g.Usernames[i], s.Tiles, s.Armies) msgs = append(msgs, msg) } sort.Strings(msgs) for _, msg := range msgs { log.Println(msg) } if g.TurnCount < 10 { log.Printf("My General at: %v", g.GetCoordString(g.Generals[g.PlayerIndex])) } log.Println("------------------------------------------") } func check(err error) { if err != nil { panic(err) } } func min(a, b int) int { if a < b { return a } return b } func
(b bool) int { if b { return 1 } return 0 } func Btof(b bool) float64 { if b { return 1. } return 0. } func getHeuristicPathDistance(game *gioframework.Game, from, to int) float64 { /* Would have preferred to use A* to get actual path distance, but that's prohibitvely expensive. (I need to calculate this many times per turn) */ baseDistance := game.GetDistance(from, to) tilesInSquare := getTilesInSquare(game, from, to) numObstacles := 0. for _, tile := range tilesInSquare { numObstacles += Btof(!game.Walkable(tile)) } total_area := len(tilesInSquare) obstacleRatio := numObstacles / float64(total_area) // Not sure this is the best heuristic, but it's simple, so I'll use it for // now hDist := float64(baseDistance) * (1. + 2.0*obstacleRatio) //log.Printf("hDist from %v to %v is: %v", game.GetCoordString(from), game.GetCoordString(to), hDist) //log.Println("tilesInSquare: ") //for _, i := range tilesInSquare { // log.Println(game.GetCoordString(i)) //} //log.Println("baseDistance:", baseDistance) //log.Println("obstacleRatio:", obstacleRatio) return hDist } func getTilesInSquare(game *gioframework.Game, i, j int) []int { // Gets index of all tiles in a square defined by two diagonally opposed // corners rowI := game.GetRow(i) colI := game.GetCol(i) rowJ := game.GetRow(j) colJ := game.GetCol(j) rowLimits := []int{rowI, rowJ} colLimits := []int{colI, colJ} sort.Ints(rowLimits) sort.Ints(colLimits) var tiles []int for row := rowLimits[0]; row < rowLimits[1]+1; row++ { for col := colLimits[0]; col < colLimits[1]+1; col++ { tiles = append(tiles, game.GetIndex(row, col)) } } return tiles } type AstarError struct { From, To string } func (e AstarError) Error() string { return fmt.Sprintf("Astar error with from:%v to:%v", e.From, e.To) } func GetShortestPath(game *gioframework.Game, from, to int) (path []int, err error) { // pathfinding.Astar has no error handling, so we catch its panics defer func() { if r := recover(); r != nil { log.Printf("ERROR, GetShortestPath recovered from panic: %v\n", r) path = []int{} err = AstarError{ game.GetCoordString(from), game.GetCoordString(to), } } }() // TODO: if from and to are the same, just erturn an err. map_data := *pathfinding.NewMapData(game.Height, game.Width) for row := 0; row < game.Height; row++ { for col := 0; col < game.Width; col++ { i := game.GetIndex(row, col) tile := game.GameMap[i] // We don't want to accidentally attack cities on route to // somewhere else. Note: if it is the final destination, it'll be // changed not_my_city := tile.Type == gioframework.City && tile.Faction != game.PlayerIndex map_data[row][col] = Btoi(!game.Walkable(i) || not_my_city) } } map_data[game.GetRow(from)][game.GetCol(from)] = pathfinding.START map_data[game.GetRow(to)][game.GetCol(to)] = pathfinding.STOP graph := pathfinding.NewGraph(&map_data) nodesPath := pathfinding.Astar(graph) path = []int{} for _, node := range nodesPath { path = append(path, game.GetIndex(node.X, node.Y)) } return path, nil } func GetBestMove(game *gioframework.Game) (bestFrom int, bestTo int) { defer func() { if r := recover(); r != nil { log.Printf("ERROR, GetBestMove recovered from panic: %v", r) fmt.Println(errors.Wrap(r, 2).ErrorStack()) bestFrom = -1 bestTo = -1 } }() bestFrom = -1 bestTo = -1 bestTotalScore := -10. var bestScores map[string]float64 myGeneral := game.Generals[game.PlayerIndex] enemyCOM := getEnemyCenterOfMass(game) /// First check for attacking new empty or enemy tiles for from, fromTile := range game.GameMap { if fromTile.Faction != game.PlayerIndex || fromTile.Armies < 2 { continue } for to, toTile := range game.GameMap { if toTile.Faction < TileEmpty || toTile.Faction == game.PlayerIndex { continue } if game.ImpossibleTiles[to] { continue } isEmpty := toTile.Faction == TileEmpty isEnemy := IsEnemy(game, toTile) isGeneral := toTile.Type == gioframework.General isCity := toTile.Type == gioframework.City outnumber := float64(fromTile.Armies - toTile.Armies) dist := getHeuristicPathDistance(game, from, to) distFromGen := getHeuristicPathDistance(game, myGeneral, to) center := game.GetIndex(game.Width/2, game.Height/2) distCenter := getHeuristicPathDistance(game, center, to) centerness := 1. - distCenter/float64(game.Width) // This is the vector pointing towards the enemy enemyVector := [2]int{ game.GetRow(enemyCOM) - game.GetRow(myGeneral), game.GetCol(enemyCOM) - game.GetCol(myGeneral), } // The vector showing the proposed move moveVector := [2]int{ game.GetRow(to) - game.GetRow(from), game.GetCol(to) - game.GetCol(from), } neighbors := game.GetNeighborhood(to, false) numAlliedNeighbors := 0 for _, neighbor := range neighbors { if !IsEnemy(game, game.GameMap[neighbor]) { numAlliedNeighbors += 1 } } if isCity && outnumber < 2 && !isEnemy { // Never attack a neutral city and lose continue } scores := make(map[string]float64) scores["outnumber score"] = Truncate(outnumber/200, 0., 0.25) * Btof(isEnemy) scores["outnumbered penalty"] = -0.2 * Btof(outnumber < 2) scores["general threat score"] = (0.25 * math.Pow(distFromGen, -1.0)) * Truncate(float64(toTile.Armies)/10, 0., 1.0) * Btof(isEnemy) scores["dist penalty"] = Truncate(-0.5*dist/30, -0.3, 0) scores["dist gt army penalty"] = -0.2 * Btof(fromTile
Btoi
identifier_name
bot.go
ies) game.Attack(path[i], path[i+1], false) } } log.Printf("Replay available at: http://bot.generals.io/replays/%v", game.ReplayID) time.Sleep(10*time.Second) } } func setupLogging() { logDir := "log" _ = os.Mkdir(logDir, os.ModePerm) rand.Seed(time.Now().UTC().UnixNano()) logFilename := path.Join(logDir, "log_"+strconv.Itoa(rand.Intn(10000))) logFile, err := os.OpenFile(logFilename, os.O_CREATE|os.O_APPEND|os.O_RDWR, 0666) check(err) mw := io.MultiWriter(os.Stdout, logFile) log.SetOutput(mw) } func logTurnData(g *gioframework.Game) { log.Println("------------------------------------------") log.Printf("Turn: %v (UI%v)", g.TurnCount, float64(g.TurnCount)/2.) var msgs []string for i, s := range g.Scores { msg := fmt.Sprintf("%10v: Tiles: %v, Army: %v", g.Usernames[i], s.Tiles, s.Armies) msgs = append(msgs, msg) } sort.Strings(msgs) for _, msg := range msgs { log.Println(msg) } if g.TurnCount < 10 { log.Printf("My General at: %v", g.GetCoordString(g.Generals[g.PlayerIndex])) } log.Println("------------------------------------------") } func check(err error) { if err != nil { panic(err) } } func min(a, b int) int { if a < b { return a } return b } func Btoi(b bool) int { if b { return 1 } return 0 } func Btof(b bool) float64 { if b { return 1. } return 0. } func getHeuristicPathDistance(game *gioframework.Game, from, to int) float64 { /* Would have preferred to use A* to get actual path distance, but that's prohibitvely expensive. (I need to calculate this many times per turn) */ baseDistance := game.GetDistance(from, to) tilesInSquare := getTilesInSquare(game, from, to) numObstacles := 0. for _, tile := range tilesInSquare { numObstacles += Btof(!game.Walkable(tile)) } total_area := len(tilesInSquare) obstacleRatio := numObstacles / float64(total_area) // Not sure this is the best heuristic, but it's simple, so I'll use it for // now hDist := float64(baseDistance) * (1. + 2.0*obstacleRatio) //log.Printf("hDist from %v to %v is: %v", game.GetCoordString(from), game.GetCoordString(to), hDist) //log.Println("tilesInSquare: ") //for _, i := range tilesInSquare { // log.Println(game.GetCoordString(i)) //} //log.Println("baseDistance:", baseDistance) //log.Println("obstacleRatio:", obstacleRatio) return hDist } func getTilesInSquare(game *gioframework.Game, i, j int) []int { // Gets index of all tiles in a square defined by two diagonally opposed // corners rowI := game.GetRow(i) colI := game.GetCol(i) rowJ := game.GetRow(j) colJ := game.GetCol(j) rowLimits := []int{rowI, rowJ} colLimits := []int{colI, colJ} sort.Ints(rowLimits) sort.Ints(colLimits) var tiles []int for row := rowLimits[0]; row < rowLimits[1]+1; row++ { for col := colLimits[0]; col < colLimits[1]+1; col++ { tiles = append(tiles, game.GetIndex(row, col)) } } return tiles } type AstarError struct { From, To string } func (e AstarError) Error() string { return fmt.Sprintf("Astar error with from:%v to:%v", e.From, e.To) } func GetShortestPath(game *gioframework.Game, from, to int) (path []int, err error) { // pathfinding.Astar has no error handling, so we catch its panics defer func() { if r := recover(); r != nil { log.Printf("ERROR, GetShortestPath recovered from panic: %v\n", r) path = []int{} err = AstarError{ game.GetCoordString(from), game.GetCoordString(to), } } }() // TODO: if from and to are the same, just erturn an err. map_data := *pathfinding.NewMapData(game.Height, game.Width) for row := 0; row < game.Height; row++ { for col := 0; col < game.Width; col++ { i := game.GetIndex(row, col) tile := game.GameMap[i] // We don't want to accidentally attack cities on route to // somewhere else. Note: if it is the final destination, it'll be // changed not_my_city := tile.Type == gioframework.City && tile.Faction != game.PlayerIndex map_data[row][col] = Btoi(!game.Walkable(i) || not_my_city) } } map_data[game.GetRow(from)][game.GetCol(from)] = pathfinding.START map_data[game.GetRow(to)][game.GetCol(to)] = pathfinding.STOP graph := pathfinding.NewGraph(&map_data) nodesPath := pathfinding.Astar(graph) path = []int{} for _, node := range nodesPath { path = append(path, game.GetIndex(node.X, node.Y)) } return path, nil } func GetBestMove(game *gioframework.Game) (bestFrom int, bestTo int) { defer func() { if r := recover(); r != nil { log.Printf("ERROR, GetBestMove recovered from panic: %v", r) fmt.Println(errors.Wrap(r, 2).ErrorStack()) bestFrom = -1 bestTo = -1 } }() bestFrom = -1 bestTo = -1 bestTotalScore := -10. var bestScores map[string]float64 myGeneral := game.Generals[game.PlayerIndex] enemyCOM := getEnemyCenterOfMass(game) /// First check for attacking new empty or enemy tiles for from, fromTile := range game.GameMap { if fromTile.Faction != game.PlayerIndex || fromTile.Armies < 2 { continue } for to, toTile := range game.GameMap { if toTile.Faction < TileEmpty || toTile.Faction == game.PlayerIndex { continue } if game.ImpossibleTiles[to] { continue } isEmpty := toTile.Faction == TileEmpty isEnemy := IsEnemy(game, toTile) isGeneral := toTile.Type == gioframework.General isCity := toTile.Type == gioframework.City outnumber := float64(fromTile.Armies - toTile.Armies) dist := getHeuristicPathDistance(game, from, to) distFromGen := getHeuristicPathDistance(game, myGeneral, to) center := game.GetIndex(game.Width/2, game.Height/2) distCenter := getHeuristicPathDistance(game, center, to) centerness := 1. - distCenter/float64(game.Width) // This is the vector pointing towards the enemy enemyVector := [2]int{ game.GetRow(enemyCOM) - game.GetRow(myGeneral), game.GetCol(enemyCOM) - game.GetCol(myGeneral), } // The vector showing the proposed move moveVector := [2]int{ game.GetRow(to) - game.GetRow(from), game.GetCol(to) - game.GetCol(from), } neighbors := game.GetNeighborhood(to, false) numAlliedNeighbors := 0 for _, neighbor := range neighbors { if !IsEnemy(game, game.GameMap[neighbor]) { numAlliedNeighbors += 1 } } if isCity && outnumber < 2 && !isEnemy { // Never attack a neutral city and lose continue } scores := make(map[string]float64)
scores["outnumbered penalty"] = -0.2 * Btof(outnumber < 2) scores["general threat score"] = (0.25 * math.Pow(distFromGen, -1.0)) * Truncate(float64(toTile.Armies)/10, 0., 1.0) * Btof(isEnemy) scores["dist penalty"] = Truncate(-0.5*dist/30, -0.3, 0) scores["dist gt army penalty"] = -0.2 * Btof(fromTile.A
scores["outnumber score"] = Truncate(outnumber/200, 0., 0.25) * Btof(isEnemy)
random_line_split
bot.go
on route to // somewhere else. Note: if it is the final destination, it'll be // changed not_my_city := tile.Type == gioframework.City && tile.Faction != game.PlayerIndex map_data[row][col] = Btoi(!game.Walkable(i) || not_my_city) } } map_data[game.GetRow(from)][game.GetCol(from)] = pathfinding.START map_data[game.GetRow(to)][game.GetCol(to)] = pathfinding.STOP graph := pathfinding.NewGraph(&map_data) nodesPath := pathfinding.Astar(graph) path = []int{} for _, node := range nodesPath { path = append(path, game.GetIndex(node.X, node.Y)) } return path, nil } func GetBestMove(game *gioframework.Game) (bestFrom int, bestTo int) { defer func() { if r := recover(); r != nil { log.Printf("ERROR, GetBestMove recovered from panic: %v", r) fmt.Println(errors.Wrap(r, 2).ErrorStack()) bestFrom = -1 bestTo = -1 } }() bestFrom = -1 bestTo = -1 bestTotalScore := -10. var bestScores map[string]float64 myGeneral := game.Generals[game.PlayerIndex] enemyCOM := getEnemyCenterOfMass(game) /// First check for attacking new empty or enemy tiles for from, fromTile := range game.GameMap { if fromTile.Faction != game.PlayerIndex || fromTile.Armies < 2 { continue } for to, toTile := range game.GameMap { if toTile.Faction < TileEmpty || toTile.Faction == game.PlayerIndex { continue } if game.ImpossibleTiles[to] { continue } isEmpty := toTile.Faction == TileEmpty isEnemy := IsEnemy(game, toTile) isGeneral := toTile.Type == gioframework.General isCity := toTile.Type == gioframework.City outnumber := float64(fromTile.Armies - toTile.Armies) dist := getHeuristicPathDistance(game, from, to) distFromGen := getHeuristicPathDistance(game, myGeneral, to) center := game.GetIndex(game.Width/2, game.Height/2) distCenter := getHeuristicPathDistance(game, center, to) centerness := 1. - distCenter/float64(game.Width) // This is the vector pointing towards the enemy enemyVector := [2]int{ game.GetRow(enemyCOM) - game.GetRow(myGeneral), game.GetCol(enemyCOM) - game.GetCol(myGeneral), } // The vector showing the proposed move moveVector := [2]int{ game.GetRow(to) - game.GetRow(from), game.GetCol(to) - game.GetCol(from), } neighbors := game.GetNeighborhood(to, false) numAlliedNeighbors := 0 for _, neighbor := range neighbors { if !IsEnemy(game, game.GameMap[neighbor]) { numAlliedNeighbors += 1 } } if isCity && outnumber < 2 && !isEnemy { // Never attack a neutral city and lose continue } scores := make(map[string]float64) scores["outnumber score"] = Truncate(outnumber/200, 0., 0.25) * Btof(isEnemy) scores["outnumbered penalty"] = -0.2 * Btof(outnumber < 2) scores["general threat score"] = (0.25 * math.Pow(distFromGen, -1.0)) * Truncate(float64(toTile.Armies)/10, 0., 1.0) * Btof(isEnemy) scores["dist penalty"] = Truncate(-0.5*dist/30, -0.3, 0) scores["dist gt army penalty"] = -0.2 * Btof(fromTile.Armies < int(dist)) scores["is enemy score"] = 0.05 * Btof(isEnemy) scores["close city score"] = 0.35 * Btof(isCity && outnumber >= 2) * math.Pow(distFromGen, -0.5) scores["enemy city score"] = 0.2 * Btof(isCity && isEnemy) scores["enemy gen score"] = 0.15 * Btof(isGeneral) * Btof(isEnemy) scores["empty score"] = 0.08 * Btof(isEmpty) // Generally a good strategy to take the center of the board scores["centerness score"] = 0.02 * centerness // You should move towards enemy's main base, not random little // patches of enemy. This prevents the bot from "cleaning up" // irrelevant squares. This could be improved by making the vectors // normalized and having the score gradually increase as you point // towards the enemy scores["towards enemy score"] = 0.03 * Btof(dotProduct(enemyVector, moveVector) > 1) // Instead of attacking all the tiles on the enemy's border it is // typically better to make a deep drive into enemy land scores["deep drive score"] = 0.04 * Btof(numAlliedNeighbors < 2) totalScore := 0. for _, score := range scores { totalScore += score } //log.Printf("============Considering move %v->%v, got score: %v\n", from, to, totalScore) //logSortedScores(scores) if totalScore > bestTotalScore { bestScores = scores bestTotalScore = totalScore bestFrom = from bestTo = to } } } logSortedScores(bestScores) log.Printf("Attack score: %.2f", bestTotalScore) log.Printf("From:%v To:%v", game.GetCoordString(bestFrom), game.GetCoordString(bestTo)) log.Println("--------") /////////////// Then check for consolidation ////////////////////////////// //consolScore := getConsolidationScore(game) // It's a good idea to consolidate armies right after the armies regenerate. // armyCycle shows the amount of the cycle that's passed. [0, 1] armyCycle := float64(game.TurnCount % 50) / 50 consolScore := 0.6 * math.Pow(1-armyCycle, 6) - 0.2 log.Printf("Consolidation score:%.2f", consolScore) tiles := getTilesSortedOnArmies(game) if len(tiles) > 10 && consolScore > bestTotalScore { largestTile := tiles[0] for _, tile := range tiles[:5] { log.Printf("Army ranked: %v", game.GameMap[tile].Armies) } highestAvArmy := 0. for _, from := range tiles[1:] { if game.GameMap[from].Armies < 2 { continue } // Warning: this path could cut through enemy territory! Keep an // eye for this path_, _ := GetShortestPath(game, from, largestTile) // TODO: handle err //TODO: this throws an error it seems! largestTile == from ??!! armies := 0 for _, i := range path_[:len(path_)-1] { armies += game.GameMap[i].Armies } av_army := float64(armies) / float64(len(path_)-1) if av_army > highestAvArmy { highestAvArmy = av_army bestFrom = from } } bestTo = largestTile // We want to move towards the enemy. Reverse if that's not the // case if enemyCOM >= 0 { fromDistEnemy := getHeuristicPathDistance(game, enemyCOM, bestFrom) toDistEnemy := getHeuristicPathDistance(game, enemyCOM, bestTo) if fromDistEnemy < toDistEnemy { log.Println("Switching direction of consolidation") bestFrom, bestTo = bestTo, bestFrom } } log.Printf("Consolidating, with average army: %v", highestAvArmy) } // Trash talk toTile := game.GameMap[bestTo] if toTile.Type == gioframework.City && IsEnemy(game, toTile) { game.SendChat("Sorry, I'm gonna need that citadel") } return bestFrom, bestTo } func IsEnemy(game *gioframework.Game, tile gioframework.Cell) bool { if len(game.Teams) == 0
{ // This means we're playing 1v1 or FFA return tile.Faction != game.PlayerIndex && tile.Faction >= 0 }
conditional_block
data_window.go
// CurrentTiflashTableScanWithFastScanCount count the number of tiflash table scan and tiflash partition table scan which use fastscan CurrentTiflashTableScanWithFastScanCount atomic.Uint64 ) const ( // WindowSize determines how long some data is aggregated by. WindowSize = 1 * time.Hour // SubWindowSize determines how often data is rotated. SubWindowSize = 1 * time.Minute maxSubWindowLength = int(ReportInterval / SubWindowSize) // TODO: Ceiling? maxSubWindowLengthInWindow = int(WindowSize / SubWindowSize) // TODO: Ceiling? promReadTimeout = time.Second * 30 ) type windowData struct { BeginAt time.Time `json:"beginAt"` ExecuteCount uint64 `json:"executeCount"` TiFlashUsage tiFlashUsageData `json:"tiFlashUsage"` CoprCacheUsage coprCacheUsageData `json:"coprCacheUsage"` SQLUsage sqlUsageData `json:"SQLUsage"` BuiltinFunctionsUsage map[string]uint32 `json:"builtinFunctionsUsage"` } type sqlType map[string]uint64 type sqlUsageData struct { SQLTotal uint64 `json:"total"` SQLType sqlType `json:"type"` } type coprCacheUsageData struct { GTE0 uint64 `json:"gte0"` GTE1 uint64 `json:"gte1"` GTE10 uint64 `json:"gte10"` GTE20 uint64 `json:"gte20"` GTE40 uint64 `json:"gte40"` GTE80 uint64 `json:"gte80"` GTE100 uint64 `json:"gte100"` } type tiFlashUsageData struct { PushDown uint64 `json:"pushDown"` ExchangePushDown uint64 `json:"exchangePushDown"` TableScan uint64 `json:"tableScan"` TableScanWithFastScan uint64 `json:"tableScanWithFastScan"` } // builtinFunctionsUsageCollector collects builtin functions usage information and dump it into windowData. type builtinFunctionsUsageCollector struct { sync.Mutex // Should acquire lock to access this usageData BuiltinFunctionsUsage } // Merge BuiltinFunctionsUsage data func (b *builtinFunctionsUsageCollector) Collect(usageData BuiltinFunctionsUsage) { // TODO(leiysky): use multi-worker to collect the usage information so we can make this asynchronous b.Lock() defer b.Unlock() b.usageData.Merge(usageData) } // Dump BuiltinFunctionsUsage data func (b *builtinFunctionsUsageCollector) Dump() map[string]uint32 { b.Lock() ret := b.usageData b.usageData = make(map[string]uint32) b.Unlock() return ret } // BuiltinFunctionsUsage is a map from ScalarFuncSig_name(string) to usage count(uint32) type BuiltinFunctionsUsage map[string]uint32 // Inc will increase the usage count of scalar function by 1 func (b BuiltinFunctionsUsage) Inc(scalarFuncSigName string) { v, ok := b[scalarFuncSigName] if !ok { b[scalarFuncSigName] = 1 } else { b[scalarFuncSigName] = v + 1 } } // Merge BuiltinFunctionsUsage data func (b BuiltinFunctionsUsage) Merge(usageData BuiltinFunctionsUsage) { for k, v := range usageData { prev, ok := b[k] if !ok { b[k] = v } else { b[k] = prev + v } } } // GlobalBuiltinFunctionsUsage is used to collect builtin functions usage information var GlobalBuiltinFunctionsUsage = &builtinFunctionsUsageCollector{usageData: make(BuiltinFunctionsUsage)} var ( rotatedSubWindows []*windowData subWindowsLock = sync.RWMutex{} ) func getSQLSum(sqlTypeData *sqlType) uint64 { result := uint64(0) for _, v := range *sqlTypeData { result += v } return result } func readSQLMetric(timepoint time.Time, sqlResult *sqlUsageData) error { ctx := context.TODO() promQL := "avg(tidb_executor_statement_total{}) by (type)" result, err := querySQLMetric(ctx, timepoint, promQL) if err != nil { return err } analysisSQLUsage(result, sqlResult) return nil } func querySQLMetric(ctx context.Context, queryTime time.Time, promQL string) (result pmodel.Value, err error) { // Add retry to avoid network error. var prometheusAddr string for i := 0; i < 5; i++ { //TODO: the prometheus will be Integrated into the PD, then we need to query the prometheus in PD directly, which need change the quire API prometheusAddr, err = infosync.GetPrometheusAddr() if err == nil || err == infosync.ErrPrometheusAddrIsNotSet { break } time.Sleep(100 * time.Millisecond) } if err != nil { return nil, err } promClient, err := api.NewClient(api.Config{ Address: prometheusAddr, }) if err != nil { return nil, err } promQLAPI := promv1.NewAPI(promClient) ctx, cancel := context.WithTimeout(ctx, promReadTimeout) defer cancel() // Add retry to avoid network error. for i := 0; i < 5; i++ { result, _, err = promQLAPI.Query(ctx, promQL, queryTime) if err == nil { break } time.Sleep(100 * time.Millisecond) } return result, err } func analysisSQLUsage(promResult pmodel.Value, sqlResult *sqlUsageData) { if promResult == nil { return } if promResult.Type() == pmodel.ValVector { matrix := promResult.(pmodel.Vector) for _, m := range matrix { v := m.Value promLable := string(m.Metric[pmodel.LabelName("type")]) sqlResult.SQLType[promLable] = uint64(v) } } } // RotateSubWindow rotates the telemetry sub window. func RotateSubWindow() { thisSubWindow := windowData{ BeginAt: time.Now(), ExecuteCount: CurrentExecuteCount.Swap(0), TiFlashUsage: tiFlashUsageData{ PushDown: CurrentTiFlashPushDownCount.Swap(0), ExchangePushDown: CurrentTiFlashExchangePushDownCount.Swap(0), TableScan: CurrentTiflashTableScanCount.Swap(0), TableScanWithFastScan: CurrentTiflashTableScanWithFastScanCount.Swap(0), }, CoprCacheUsage: coprCacheUsageData{ GTE0: CurrentCoprCacheHitRatioGTE0Count.Swap(0), GTE1: CurrentCoprCacheHitRatioGTE1Count.Swap(0), GTE10: CurrentCoprCacheHitRatioGTE10Count.Swap(0), GTE20: CurrentCoprCacheHitRatioGTE20Count.Swap(0), GTE40: CurrentCoprCacheHitRatioGTE40Count.Swap(0), GTE80: CurrentCoprCacheHitRatioGTE80Count.Swap(0), GTE100: CurrentCoprCacheHitRatioGTE100Count.Swap(0), }, SQLUsage: sqlUsageData{ SQLTotal: 0, SQLType: make(sqlType), }, BuiltinFunctionsUsage: GlobalBuiltinFunctionsUsage.Dump(), } err := readSQLMetric(time.Now(), &thisSubWindow.SQLUsage) if err != nil { logutil.BgLogger().Info("Error exists when getting the SQL Metric.", zap.Error(err)) } thisSubWindow.SQLUsage.SQLTotal = getSQLSum(&thisSubWindow.SQLUsage.SQLType) subWindowsLock.Lock() rotatedSubWindows = append(rotatedSubWindows, &thisSubWindow) if len(rotatedSubWindows) > maxSubWindowLength { // Only retain last N sub windows, according to the report interval. rotatedSubWindows = rotatedSubWindows[len(rotatedSubWindows)-maxSubWindowLength:] } subWindowsLock.Unlock() } func calDeltaSQLTypeMap(cur sqlType, last sqlType) sqlType { deltaMap := make(sqlType) for key, value := range cur { deltaMap[key] = value - (last)[key] } return deltaMap } // getWindowData returns data aggregated by window size. func getWindowData() []*windowData { results := make([]*windowData, 0) subWindowsLock.RLock() i := 0 for i < len(rotatedSubWindows) { thisWindow := *rotatedSubWindows[i] var startWindow windowData
if i == 0 { startWindow = thisWindow } else { startWindow = *rotatedSubWindows[i-1] }
random_line_split
data_window.go
HitRatioGTE0Count atomic.Uint64 // CurrentCoprCacheHitRatioGTE1Count is CurrentCoprCacheHitRatioGTE1Count CurrentCoprCacheHitRatioGTE1Count atomic.Uint64 // CurrentCoprCacheHitRatioGTE10Count is CurrentCoprCacheHitRatioGTE10Count CurrentCoprCacheHitRatioGTE10Count atomic.Uint64 // CurrentCoprCacheHitRatioGTE20Count is CurrentCoprCacheHitRatioGTE20Count CurrentCoprCacheHitRatioGTE20Count atomic.Uint64 // CurrentCoprCacheHitRatioGTE40Count is CurrentCoprCacheHitRatioGTE40Count CurrentCoprCacheHitRatioGTE40Count atomic.Uint64 // CurrentCoprCacheHitRatioGTE80Count is CurrentCoprCacheHitRatioGTE80Count CurrentCoprCacheHitRatioGTE80Count atomic.Uint64 // CurrentCoprCacheHitRatioGTE100Count is CurrentCoprCacheHitRatioGTE100Count CurrentCoprCacheHitRatioGTE100Count atomic.Uint64 // CurrentTiflashTableScanCount count the number of tiflash table scan and tiflash partition table scan CurrentTiflashTableScanCount atomic.Uint64 // CurrentTiflashTableScanWithFastScanCount count the number of tiflash table scan and tiflash partition table scan which use fastscan CurrentTiflashTableScanWithFastScanCount atomic.Uint64 ) const ( // WindowSize determines how long some data is aggregated by. WindowSize = 1 * time.Hour // SubWindowSize determines how often data is rotated. SubWindowSize = 1 * time.Minute maxSubWindowLength = int(ReportInterval / SubWindowSize) // TODO: Ceiling? maxSubWindowLengthInWindow = int(WindowSize / SubWindowSize) // TODO: Ceiling? promReadTimeout = time.Second * 30 ) type windowData struct { BeginAt time.Time `json:"beginAt"` ExecuteCount uint64 `json:"executeCount"` TiFlashUsage tiFlashUsageData `json:"tiFlashUsage"` CoprCacheUsage coprCacheUsageData `json:"coprCacheUsage"` SQLUsage sqlUsageData `json:"SQLUsage"` BuiltinFunctionsUsage map[string]uint32 `json:"builtinFunctionsUsage"` } type sqlType map[string]uint64 type sqlUsageData struct { SQLTotal uint64 `json:"total"` SQLType sqlType `json:"type"` } type coprCacheUsageData struct { GTE0 uint64 `json:"gte0"` GTE1 uint64 `json:"gte1"` GTE10 uint64 `json:"gte10"` GTE20 uint64 `json:"gte20"` GTE40 uint64 `json:"gte40"` GTE80 uint64 `json:"gte80"` GTE100 uint64 `json:"gte100"` } type tiFlashUsageData struct { PushDown uint64 `json:"pushDown"` ExchangePushDown uint64 `json:"exchangePushDown"` TableScan uint64 `json:"tableScan"` TableScanWithFastScan uint64 `json:"tableScanWithFastScan"` } // builtinFunctionsUsageCollector collects builtin functions usage information and dump it into windowData. type builtinFunctionsUsageCollector struct { sync.Mutex // Should acquire lock to access this usageData BuiltinFunctionsUsage } // Merge BuiltinFunctionsUsage data func (b *builtinFunctionsUsageCollector) Collect(usageData BuiltinFunctionsUsage) { // TODO(leiysky): use multi-worker to collect the usage information so we can make this asynchronous b.Lock() defer b.Unlock() b.usageData.Merge(usageData) } // Dump BuiltinFunctionsUsage data func (b *builtinFunctionsUsageCollector) Dump() map[string]uint32 { b.Lock() ret := b.usageData b.usageData = make(map[string]uint32) b.Unlock() return ret } // BuiltinFunctionsUsage is a map from ScalarFuncSig_name(string) to usage count(uint32) type BuiltinFunctionsUsage map[string]uint32 // Inc will increase the usage count of scalar function by 1 func (b BuiltinFunctionsUsage) Inc(scalarFuncSigName string) { v, ok := b[scalarFuncSigName] if !ok { b[scalarFuncSigName] = 1 } else { b[scalarFuncSigName] = v + 1 } } // Merge BuiltinFunctionsUsage data func (b BuiltinFunctionsUsage) Merge(usageData BuiltinFunctionsUsage) { for k, v := range usageData { prev, ok := b[k] if !ok { b[k] = v } else { b[k] = prev + v } } } // GlobalBuiltinFunctionsUsage is used to collect builtin functions usage information var GlobalBuiltinFunctionsUsage = &builtinFunctionsUsageCollector{usageData: make(BuiltinFunctionsUsage)} var ( rotatedSubWindows []*windowData subWindowsLock = sync.RWMutex{} ) func getSQLSum(sqlTypeData *sqlType) uint64 { result := uint64(0) for _, v := range *sqlTypeData { result += v } return result } func readSQLMetric(timepoint time.Time, sqlResult *sqlUsageData) error { ctx := context.TODO() promQL := "avg(tidb_executor_statement_total{}) by (type)" result, err := querySQLMetric(ctx, timepoint, promQL) if err != nil { return err } analysisSQLUsage(result, sqlResult) return nil } func querySQLMetric(ctx context.Context, queryTime time.Time, promQL string) (result pmodel.Value, err error) { // Add retry to avoid network error. var prometheusAddr string for i := 0; i < 5; i++ { //TODO: the prometheus will be Integrated into the PD, then we need to query the prometheus in PD directly, which need change the quire API prometheusAddr, err = infosync.GetPrometheusAddr() if err == nil || err == infosync.ErrPrometheusAddrIsNotSet { break } time.Sleep(100 * time.Millisecond) } if err != nil { return nil, err } promClient, err := api.NewClient(api.Config{ Address: prometheusAddr, }) if err != nil { return nil, err } promQLAPI := promv1.NewAPI(promClient) ctx, cancel := context.WithTimeout(ctx, promReadTimeout) defer cancel() // Add retry to avoid network error. for i := 0; i < 5; i++
return result, err } func analysisSQLUsage(promResult pmodel.Value, sqlResult *sqlUsageData) { if promResult == nil { return } if promResult.Type() == pmodel.ValVector { matrix := promResult.(pmodel.Vector) for _, m := range matrix { v := m.Value promLable := string(m.Metric[pmodel.LabelName("type")]) sqlResult.SQLType[promLable] = uint64(v) } } } // RotateSubWindow rotates the telemetry sub window. func RotateSubWindow() { thisSubWindow := windowData{ BeginAt: time.Now(), ExecuteCount: CurrentExecuteCount.Swap(0), TiFlashUsage: tiFlashUsageData{ PushDown: CurrentTiFlashPushDownCount.Swap(0), ExchangePushDown: CurrentTiFlashExchangePushDownCount.Swap(0), TableScan: CurrentTiflashTableScanCount.Swap(0), TableScanWithFastScan: CurrentTiflashTableScanWithFastScanCount.Swap(0), }, CoprCacheUsage: coprCacheUsageData{ GTE0: CurrentCoprCacheHitRatioGTE0Count.Swap(0), GTE1: CurrentCoprCacheHitRatioGTE1Count.Swap(0), GTE10: CurrentCoprCacheHitRatioGTE10Count.Swap(0), GTE20: CurrentCoprCacheHitRatioGTE20Count.Swap(0), GTE40: CurrentCoprCacheHitRatioGTE40Count.Swap(0), GTE80: CurrentCoprCacheHitRatioGTE80Count.Swap(0), GTE100: CurrentCoprCacheHitRatioGTE100Count.Swap(0), }, SQLUsage: sqlUsageData{ SQLTotal: 0, SQLType: make(sqlType), }, BuiltinFunctions
{ result, _, err = promQLAPI.Query(ctx, promQL, queryTime) if err == nil { break } time.Sleep(100 * time.Millisecond) }
conditional_block
data_window.go
HitRatioGTE0Count atomic.Uint64 // CurrentCoprCacheHitRatioGTE1Count is CurrentCoprCacheHitRatioGTE1Count CurrentCoprCacheHitRatioGTE1Count atomic.Uint64 // CurrentCoprCacheHitRatioGTE10Count is CurrentCoprCacheHitRatioGTE10Count CurrentCoprCacheHitRatioGTE10Count atomic.Uint64 // CurrentCoprCacheHitRatioGTE20Count is CurrentCoprCacheHitRatioGTE20Count CurrentCoprCacheHitRatioGTE20Count atomic.Uint64 // CurrentCoprCacheHitRatioGTE40Count is CurrentCoprCacheHitRatioGTE40Count CurrentCoprCacheHitRatioGTE40Count atomic.Uint64 // CurrentCoprCacheHitRatioGTE80Count is CurrentCoprCacheHitRatioGTE80Count CurrentCoprCacheHitRatioGTE80Count atomic.Uint64 // CurrentCoprCacheHitRatioGTE100Count is CurrentCoprCacheHitRatioGTE100Count CurrentCoprCacheHitRatioGTE100Count atomic.Uint64 // CurrentTiflashTableScanCount count the number of tiflash table scan and tiflash partition table scan CurrentTiflashTableScanCount atomic.Uint64 // CurrentTiflashTableScanWithFastScanCount count the number of tiflash table scan and tiflash partition table scan which use fastscan CurrentTiflashTableScanWithFastScanCount atomic.Uint64 ) const ( // WindowSize determines how long some data is aggregated by. WindowSize = 1 * time.Hour // SubWindowSize determines how often data is rotated. SubWindowSize = 1 * time.Minute maxSubWindowLength = int(ReportInterval / SubWindowSize) // TODO: Ceiling? maxSubWindowLengthInWindow = int(WindowSize / SubWindowSize) // TODO: Ceiling? promReadTimeout = time.Second * 30 ) type windowData struct { BeginAt time.Time `json:"beginAt"` ExecuteCount uint64 `json:"executeCount"` TiFlashUsage tiFlashUsageData `json:"tiFlashUsage"` CoprCacheUsage coprCacheUsageData `json:"coprCacheUsage"` SQLUsage sqlUsageData `json:"SQLUsage"` BuiltinFunctionsUsage map[string]uint32 `json:"builtinFunctionsUsage"` } type sqlType map[string]uint64 type sqlUsageData struct { SQLTotal uint64 `json:"total"` SQLType sqlType `json:"type"` } type coprCacheUsageData struct { GTE0 uint64 `json:"gte0"` GTE1 uint64 `json:"gte1"` GTE10 uint64 `json:"gte10"` GTE20 uint64 `json:"gte20"` GTE40 uint64 `json:"gte40"` GTE80 uint64 `json:"gte80"` GTE100 uint64 `json:"gte100"` } type tiFlashUsageData struct { PushDown uint64 `json:"pushDown"` ExchangePushDown uint64 `json:"exchangePushDown"` TableScan uint64 `json:"tableScan"` TableScanWithFastScan uint64 `json:"tableScanWithFastScan"` } // builtinFunctionsUsageCollector collects builtin functions usage information and dump it into windowData. type builtinFunctionsUsageCollector struct { sync.Mutex // Should acquire lock to access this usageData BuiltinFunctionsUsage } // Merge BuiltinFunctionsUsage data func (b *builtinFunctionsUsageCollector) Collect(usageData BuiltinFunctionsUsage) { // TODO(leiysky): use multi-worker to collect the usage information so we can make this asynchronous b.Lock() defer b.Unlock() b.usageData.Merge(usageData) } // Dump BuiltinFunctionsUsage data func (b *builtinFunctionsUsageCollector) Dump() map[string]uint32 { b.Lock() ret := b.usageData b.usageData = make(map[string]uint32) b.Unlock() return ret } // BuiltinFunctionsUsage is a map from ScalarFuncSig_name(string) to usage count(uint32) type BuiltinFunctionsUsage map[string]uint32 // Inc will increase the usage count of scalar function by 1 func (b BuiltinFunctionsUsage) Inc(scalarFuncSigName string) { v, ok := b[scalarFuncSigName] if !ok { b[scalarFuncSigName] = 1 } else { b[scalarFuncSigName] = v + 1 } } // Merge BuiltinFunctionsUsage data func (b BuiltinFunctionsUsage) Merge(usageData BuiltinFunctionsUsage) { for k, v := range usageData { prev, ok := b[k] if !ok { b[k] = v } else { b[k] = prev + v } } } // GlobalBuiltinFunctionsUsage is used to collect builtin functions usage information var GlobalBuiltinFunctionsUsage = &builtinFunctionsUsageCollector{usageData: make(BuiltinFunctionsUsage)} var ( rotatedSubWindows []*windowData subWindowsLock = sync.RWMutex{} ) func getSQLSum(sqlTypeData *sqlType) uint64 { result := uint64(0) for _, v := range *sqlTypeData { result += v } return result } func readSQLMetric(timepoint time.Time, sqlResult *sqlUsageData) error
func querySQLMetric(ctx context.Context, queryTime time.Time, promQL string) (result pmodel.Value, err error) { // Add retry to avoid network error. var prometheusAddr string for i := 0; i < 5; i++ { //TODO: the prometheus will be Integrated into the PD, then we need to query the prometheus in PD directly, which need change the quire API prometheusAddr, err = infosync.GetPrometheusAddr() if err == nil || err == infosync.ErrPrometheusAddrIsNotSet { break } time.Sleep(100 * time.Millisecond) } if err != nil { return nil, err } promClient, err := api.NewClient(api.Config{ Address: prometheusAddr, }) if err != nil { return nil, err } promQLAPI := promv1.NewAPI(promClient) ctx, cancel := context.WithTimeout(ctx, promReadTimeout) defer cancel() // Add retry to avoid network error. for i := 0; i < 5; i++ { result, _, err = promQLAPI.Query(ctx, promQL, queryTime) if err == nil { break } time.Sleep(100 * time.Millisecond) } return result, err } func analysisSQLUsage(promResult pmodel.Value, sqlResult *sqlUsageData) { if promResult == nil { return } if promResult.Type() == pmodel.ValVector { matrix := promResult.(pmodel.Vector) for _, m := range matrix { v := m.Value promLable := string(m.Metric[pmodel.LabelName("type")]) sqlResult.SQLType[promLable] = uint64(v) } } } // RotateSubWindow rotates the telemetry sub window. func RotateSubWindow() { thisSubWindow := windowData{ BeginAt: time.Now(), ExecuteCount: CurrentExecuteCount.Swap(0), TiFlashUsage: tiFlashUsageData{ PushDown: CurrentTiFlashPushDownCount.Swap(0), ExchangePushDown: CurrentTiFlashExchangePushDownCount.Swap(0), TableScan: CurrentTiflashTableScanCount.Swap(0), TableScanWithFastScan: CurrentTiflashTableScanWithFastScanCount.Swap(0), }, CoprCacheUsage: coprCacheUsageData{ GTE0: CurrentCoprCacheHitRatioGTE0Count.Swap(0), GTE1: CurrentCoprCacheHitRatioGTE1Count.Swap(0), GTE10: CurrentCoprCacheHitRatioGTE10Count.Swap(0), GTE20: CurrentCoprCacheHitRatioGTE20Count.Swap(0), GTE40: CurrentCoprCacheHitRatioGTE40Count.Swap(0), GTE80: CurrentCoprCacheHitRatioGTE80Count.Swap(0), GTE100: CurrentCoprCacheHitRatioGTE100Count.Swap(0), }, SQLUsage: sqlUsageData{ SQLTotal: 0, SQLType: make(sqlType), }, BuiltinFunctions
{ ctx := context.TODO() promQL := "avg(tidb_executor_statement_total{}) by (type)" result, err := querySQLMetric(ctx, timepoint, promQL) if err != nil { return err } analysisSQLUsage(result, sqlResult) return nil }
identifier_body
data_window.go
CacheHitRatioGTE0Count atomic.Uint64 // CurrentCoprCacheHitRatioGTE1Count is CurrentCoprCacheHitRatioGTE1Count CurrentCoprCacheHitRatioGTE1Count atomic.Uint64 // CurrentCoprCacheHitRatioGTE10Count is CurrentCoprCacheHitRatioGTE10Count CurrentCoprCacheHitRatioGTE10Count atomic.Uint64 // CurrentCoprCacheHitRatioGTE20Count is CurrentCoprCacheHitRatioGTE20Count CurrentCoprCacheHitRatioGTE20Count atomic.Uint64 // CurrentCoprCacheHitRatioGTE40Count is CurrentCoprCacheHitRatioGTE40Count CurrentCoprCacheHitRatioGTE40Count atomic.Uint64 // CurrentCoprCacheHitRatioGTE80Count is CurrentCoprCacheHitRatioGTE80Count CurrentCoprCacheHitRatioGTE80Count atomic.Uint64 // CurrentCoprCacheHitRatioGTE100Count is CurrentCoprCacheHitRatioGTE100Count CurrentCoprCacheHitRatioGTE100Count atomic.Uint64 // CurrentTiflashTableScanCount count the number of tiflash table scan and tiflash partition table scan CurrentTiflashTableScanCount atomic.Uint64 // CurrentTiflashTableScanWithFastScanCount count the number of tiflash table scan and tiflash partition table scan which use fastscan CurrentTiflashTableScanWithFastScanCount atomic.Uint64 ) const ( // WindowSize determines how long some data is aggregated by. WindowSize = 1 * time.Hour // SubWindowSize determines how often data is rotated. SubWindowSize = 1 * time.Minute maxSubWindowLength = int(ReportInterval / SubWindowSize) // TODO: Ceiling? maxSubWindowLengthInWindow = int(WindowSize / SubWindowSize) // TODO: Ceiling? promReadTimeout = time.Second * 30 ) type windowData struct { BeginAt time.Time `json:"beginAt"` ExecuteCount uint64 `json:"executeCount"` TiFlashUsage tiFlashUsageData `json:"tiFlashUsage"` CoprCacheUsage coprCacheUsageData `json:"coprCacheUsage"` SQLUsage sqlUsageData `json:"SQLUsage"` BuiltinFunctionsUsage map[string]uint32 `json:"builtinFunctionsUsage"` } type sqlType map[string]uint64 type sqlUsageData struct { SQLTotal uint64 `json:"total"` SQLType sqlType `json:"type"` } type coprCacheUsageData struct { GTE0 uint64 `json:"gte0"` GTE1 uint64 `json:"gte1"` GTE10 uint64 `json:"gte10"` GTE20 uint64 `json:"gte20"` GTE40 uint64 `json:"gte40"` GTE80 uint64 `json:"gte80"` GTE100 uint64 `json:"gte100"` } type tiFlashUsageData struct { PushDown uint64 `json:"pushDown"` ExchangePushDown uint64 `json:"exchangePushDown"` TableScan uint64 `json:"tableScan"` TableScanWithFastScan uint64 `json:"tableScanWithFastScan"` } // builtinFunctionsUsageCollector collects builtin functions usage information and dump it into windowData. type builtinFunctionsUsageCollector struct { sync.Mutex // Should acquire lock to access this usageData BuiltinFunctionsUsage } // Merge BuiltinFunctionsUsage data func (b *builtinFunctionsUsageCollector) Collect(usageData BuiltinFunctionsUsage) { // TODO(leiysky): use multi-worker to collect the usage information so we can make this asynchronous b.Lock() defer b.Unlock() b.usageData.Merge(usageData) } // Dump BuiltinFunctionsUsage data func (b *builtinFunctionsUsageCollector) Dump() map[string]uint32 { b.Lock() ret := b.usageData b.usageData = make(map[string]uint32) b.Unlock() return ret } // BuiltinFunctionsUsage is a map from ScalarFuncSig_name(string) to usage count(uint32) type BuiltinFunctionsUsage map[string]uint32 // Inc will increase the usage count of scalar function by 1 func (b BuiltinFunctionsUsage) Inc(scalarFuncSigName string) { v, ok := b[scalarFuncSigName] if !ok { b[scalarFuncSigName] = 1 } else { b[scalarFuncSigName] = v + 1 } } // Merge BuiltinFunctionsUsage data func (b BuiltinFunctionsUsage) Merge(usageData BuiltinFunctionsUsage) { for k, v := range usageData { prev, ok := b[k] if !ok { b[k] = v } else { b[k] = prev + v } } } // GlobalBuiltinFunctionsUsage is used to collect builtin functions usage information var GlobalBuiltinFunctionsUsage = &builtinFunctionsUsageCollector{usageData: make(BuiltinFunctionsUsage)} var ( rotatedSubWindows []*windowData subWindowsLock = sync.RWMutex{} ) func getSQLSum(sqlTypeData *sqlType) uint64 { result := uint64(0) for _, v := range *sqlTypeData { result += v } return result } func readSQLMetric(timepoint time.Time, sqlResult *sqlUsageData) error { ctx := context.TODO() promQL := "avg(tidb_executor_statement_total{}) by (type)" result, err := querySQLMetric(ctx, timepoint, promQL) if err != nil { return err } analysisSQLUsage(result, sqlResult) return nil } func
(ctx context.Context, queryTime time.Time, promQL string) (result pmodel.Value, err error) { // Add retry to avoid network error. var prometheusAddr string for i := 0; i < 5; i++ { //TODO: the prometheus will be Integrated into the PD, then we need to query the prometheus in PD directly, which need change the quire API prometheusAddr, err = infosync.GetPrometheusAddr() if err == nil || err == infosync.ErrPrometheusAddrIsNotSet { break } time.Sleep(100 * time.Millisecond) } if err != nil { return nil, err } promClient, err := api.NewClient(api.Config{ Address: prometheusAddr, }) if err != nil { return nil, err } promQLAPI := promv1.NewAPI(promClient) ctx, cancel := context.WithTimeout(ctx, promReadTimeout) defer cancel() // Add retry to avoid network error. for i := 0; i < 5; i++ { result, _, err = promQLAPI.Query(ctx, promQL, queryTime) if err == nil { break } time.Sleep(100 * time.Millisecond) } return result, err } func analysisSQLUsage(promResult pmodel.Value, sqlResult *sqlUsageData) { if promResult == nil { return } if promResult.Type() == pmodel.ValVector { matrix := promResult.(pmodel.Vector) for _, m := range matrix { v := m.Value promLable := string(m.Metric[pmodel.LabelName("type")]) sqlResult.SQLType[promLable] = uint64(v) } } } // RotateSubWindow rotates the telemetry sub window. func RotateSubWindow() { thisSubWindow := windowData{ BeginAt: time.Now(), ExecuteCount: CurrentExecuteCount.Swap(0), TiFlashUsage: tiFlashUsageData{ PushDown: CurrentTiFlashPushDownCount.Swap(0), ExchangePushDown: CurrentTiFlashExchangePushDownCount.Swap(0), TableScan: CurrentTiflashTableScanCount.Swap(0), TableScanWithFastScan: CurrentTiflashTableScanWithFastScanCount.Swap(0), }, CoprCacheUsage: coprCacheUsageData{ GTE0: CurrentCoprCacheHitRatioGTE0Count.Swap(0), GTE1: CurrentCoprCacheHitRatioGTE1Count.Swap(0), GTE10: CurrentCoprCacheHitRatioGTE10Count.Swap(0), GTE20: CurrentCoprCacheHitRatioGTE20Count.Swap(0), GTE40: CurrentCoprCacheHitRatioGTE40Count.Swap(0), GTE80: CurrentCoprCacheHitRatioGTE80Count.Swap(0), GTE100: CurrentCoprCacheHitRatioGTE100Count.Swap(0), }, SQLUsage: sqlUsageData{ SQLTotal: 0, SQLType: make(sqlType), }, BuiltinFunctions
querySQLMetric
identifier_name
download.rs
) => { format_update!([green] &$before, &$after) }; (chapters, $before:expr => $after:expr) => { format_update!([blue] $before, $after) }; (words, $before:expr => $after:expr) => { format_update!([blue] $before, $after) }; (timestamp, $before:expr => $after:expr) => { format_update!([yellow] ReadableDate($before), ReadableDate($after)) }; (status, $before:expr => $after:expr) => { format_update!([yellow] $before, $after) }; ([$color:ident] $before:expr, $after:expr) => { format_args!( "{} {} {}", style($before).$color(), style("=>").cyan(), style($after).$color().bold() ) }; } macro_rules! info_story_checking { ($story:expr) => { info!("Checking for {} ...", format_story!($story)); }; } macro_rules! info_update { ([ignored] $story:expr, $on:ident, $before:expr => $after:expr) => { info_update!($story, $on, $before, $after, ". Ignoring") }; ($story:expr, $on:ident, $before:expr => $after:expr) => { info_update!($story, $on, $before, $after, "") }; ($story:expr, $on:ident, $before:expr, $after:expr, $extra:expr) => { info!( "{} has an update on {} ({}){}", format_story!($story), stringify!($on), format_update!($on, $before => $after), $extra ); }; } #[derive(Debug)] enum StoryDownload { Update(Id, Story), Forced(Id), } pub fn download( config: &Config, requester: &Requester, story_data: &mut StoryData, Download { force, prompt, ref ids, }: Download, ) -> Result<()> { let selected_ids: Vec<Id> = if ids.is_empty() { story_data.keys().cloned().collect() } else { story_data .keys() .filter(|id| ids.contains(id)) .cloned() .collect() }; let mut ignored_ids: HashSet<Id> = HashSet::with_capacity(selected_ids.len()); let mut printed = false; macro_rules! set_printed { () => { if !printed { printed = true; } }; } for (id, story) in story_data.iter().filter_map(|(id, story)| { if selected_ids.contains(id) { Some((*id, story)) } else { None } }) { if let StoryStatus::Incomplete = story.status { continue; } set_printed!(); let status_notice = format!( "{} has been marked as {} by the author", format_story!(story), format_status!(story) ); match prompt { Prompt::AssumeYes => { info!("{}. Checking for an update on it anyways.", status_notice); } Prompt::AssumeNo => { info!("{}. Skipping checking for an update on it.", status_notice); ignored_ids.insert(id); } Prompt::Ask => { let confirm = Confirm::new() .with_prompt(format!( "{}. Do you want to still check for an update on it?", status_notice )) .interact() .map_err(|err| { TrackerError::io(err) .context("failed to launch overwrite confirmation prompt") })?; if !confirm { ignored_ids.insert(id); } } } } if printed { separate!(); printed = false; } let mut updated_stories: HashMap<Id, Story> = HashMap::with_capacity(selected_ids.len()); let mut ids_to_download: HashSet<Id> = HashSet::with_capacity(selected_ids.len()); for (id, story) in story_data .iter() .filter(|(id, _)| selected_ids.contains(id) && !ignored_ids.contains(id)) .map(|(id, story)| (*id, story)) { info_story_checking!(story); let updated_story: Story = requester.get_story_response(id)?.into(); let title_changed = story.title != updated_story.title; let author_changed = story.author != updated_story.author; let status_changed = story.status != updated_story.status; let story_update = story.compare_to(&updated_story)?; if story_update.is_some() || title_changed || author_changed || status_changed { // If we are here, something will be printed to stderr. Be it by the specific cases // just below or by the resulting StoryUpdate comparison. set_printed!(); if title_changed || author_changed || status_changed { clear_last_lines!(); if title_changed { info!( "{} has changed its title to {}", format_story!(story), style(&updated_story.title).green().bold() ); } if author_changed { info!( "{} has changed its author ({})", format_story!(story), format_update!(author, story.author => updated_story.author) ); } if status_changed { info!( "{} has changed its status ({})", format_story!(story), format_update!(status, story.status => updated_story.status), ); } // Avoid this message from being repeated twice in verbose output. if verbose_disabled!() { info_story_checking!(story); } } updated_stories.insert(id, updated_story); } clear_last_lines!(); match story_update { Some(StoryUpdate::Chapters { before, after }) => { info_update!(story, chapters, before => after); } Some(StoryUpdate::Words { before, after }) if config.sensibility_level >= SensibilityLevel::IncludeWords => { info_update!(story, words, before => after); } Some(StoryUpdate::DateTime { before, after }) if config.sensibility_level == SensibilityLevel::Anything => { info_update!(story, timestamp, before => after); } Some(StoryUpdate::Words { before, after }) => { info_update!([ignored] story, words, before => after); continue; } Some(StoryUpdate::DateTime { before, after }) => { info_update!([ignored] story, timestamp, before => after); continue; } None => continue, }; ids_to_download.insert(id); } // Update stories with ignored updates. // This way if the downloads fail, these should be saved by the "emergency save". // // After this block, `updated_stories` should only contain stories whose IDs are in // `ids_to_download`. { let mut updated_ids = story_data .keys() .filter(|id| !ids_to_download.contains(id)) .filter_map(|id| updated_stories.remove_entry(id)) .collect::<Vec<(Id, Story)>>(); debug!("Ignored updates: {:?}", &updated_ids); for (id, story) in updated_ids.drain(..) { story_data.insert(id, story); } } if printed { separate!(); } if !force && ids_to_download.is_empty() { info!("There is nothing to download"); } else if force { progress_or_info!( "{}", style(format!( "Force downloading {}", if ids.is_empty() && ignored_ids.is_empty() { "every story on the tracking list" } else { "selected stories" } )) .bold(), ); separate!(); } let use_separator = config.exec.is_some() && !config.quiet; let delay = std::time::Duration::from_secs(config.download_delay); let mut stories_to_download: Vec<StoryDownload> = story_data .keys() // Only download the stories that: // (1) Whose IDs were given by the user if any. // (2) The user responded to its prompt with Y. .filter(|id| selected_ids.contains(id) && !ignored_ids.contains(id)) // Download all stories if the user forced it, otherwise only those who passed the update // sensibility test. .filter(|id| force || ids_to_download.contains(id)) .map(|id| match updated_stories.remove(id) { Some(story) => StoryDownload::Update(*id, story), None => StoryDownload::Forced(*id), }) .collect(); debug!("Stories to download: {:?}", &stories_to_download); for (is_first, story_download) in stories_to_download .drain(..) .enumerate() .map(|(index, story_download)| (index == 0, story_download)) { download_delay!(!is_first, use_separator, delay); match &story_download { StoryDownload::Update(_, story) => requester.download(story)?, // While this should be safe to unwrap, in the unlikely event that it panics the // "emergency save" would be skipped.
// So I throw in a `match` to "safely" unwrap it and throw a warning if it is not // present.
random_line_split
download.rs
args::{Download, Prompt}; use crate::readable::ReadableDate; use crate::Requester; macro_rules! format_update { (author, $before:expr => $after:expr) => { format_update!([green] &$before, &$after) }; (chapters, $before:expr => $after:expr) => { format_update!([blue] $before, $after) }; (words, $before:expr => $after:expr) => { format_update!([blue] $before, $after) }; (timestamp, $before:expr => $after:expr) => { format_update!([yellow] ReadableDate($before), ReadableDate($after)) }; (status, $before:expr => $after:expr) => { format_update!([yellow] $before, $after) }; ([$color:ident] $before:expr, $after:expr) => { format_args!( "{} {} {}", style($before).$color(), style("=>").cyan(), style($after).$color().bold() ) }; } macro_rules! info_story_checking { ($story:expr) => { info!("Checking for {} ...", format_story!($story)); }; } macro_rules! info_update { ([ignored] $story:expr, $on:ident, $before:expr => $after:expr) => { info_update!($story, $on, $before, $after, ". Ignoring") }; ($story:expr, $on:ident, $before:expr => $after:expr) => { info_update!($story, $on, $before, $after, "") }; ($story:expr, $on:ident, $before:expr, $after:expr, $extra:expr) => { info!( "{} has an update on {} ({}){}", format_story!($story), stringify!($on), format_update!($on, $before => $after), $extra ); }; } #[derive(Debug)] enum StoryDownload { Update(Id, Story), Forced(Id), } pub fn download( config: &Config, requester: &Requester, story_data: &mut StoryData, Download { force, prompt, ref ids, }: Download, ) -> Result<()> { let selected_ids: Vec<Id> = if ids.is_empty() { story_data.keys().cloned().collect() } else { story_data .keys() .filter(|id| ids.contains(id)) .cloned() .collect() }; let mut ignored_ids: HashSet<Id> = HashSet::with_capacity(selected_ids.len()); let mut printed = false; macro_rules! set_printed { () => { if !printed { printed = true; } }; } for (id, story) in story_data.iter().filter_map(|(id, story)| { if selected_ids.contains(id) { Some((*id, story)) } else { None } }) { if let StoryStatus::Incomplete = story.status
set_printed!(); let status_notice = format!( "{} has been marked as {} by the author", format_story!(story), format_status!(story) ); match prompt { Prompt::AssumeYes => { info!("{}. Checking for an update on it anyways.", status_notice); } Prompt::AssumeNo => { info!("{}. Skipping checking for an update on it.", status_notice); ignored_ids.insert(id); } Prompt::Ask => { let confirm = Confirm::new() .with_prompt(format!( "{}. Do you want to still check for an update on it?", status_notice )) .interact() .map_err(|err| { TrackerError::io(err) .context("failed to launch overwrite confirmation prompt") })?; if !confirm { ignored_ids.insert(id); } } } } if printed { separate!(); printed = false; } let mut updated_stories: HashMap<Id, Story> = HashMap::with_capacity(selected_ids.len()); let mut ids_to_download: HashSet<Id> = HashSet::with_capacity(selected_ids.len()); for (id, story) in story_data .iter() .filter(|(id, _)| selected_ids.contains(id) && !ignored_ids.contains(id)) .map(|(id, story)| (*id, story)) { info_story_checking!(story); let updated_story: Story = requester.get_story_response(id)?.into(); let title_changed = story.title != updated_story.title; let author_changed = story.author != updated_story.author; let status_changed = story.status != updated_story.status; let story_update = story.compare_to(&updated_story)?; if story_update.is_some() || title_changed || author_changed || status_changed { // If we are here, something will be printed to stderr. Be it by the specific cases // just below or by the resulting StoryUpdate comparison. set_printed!(); if title_changed || author_changed || status_changed { clear_last_lines!(); if title_changed { info!( "{} has changed its title to {}", format_story!(story), style(&updated_story.title).green().bold() ); } if author_changed { info!( "{} has changed its author ({})", format_story!(story), format_update!(author, story.author => updated_story.author) ); } if status_changed { info!( "{} has changed its status ({})", format_story!(story), format_update!(status, story.status => updated_story.status), ); } // Avoid this message from being repeated twice in verbose output. if verbose_disabled!() { info_story_checking!(story); } } updated_stories.insert(id, updated_story); } clear_last_lines!(); match story_update { Some(StoryUpdate::Chapters { before, after }) => { info_update!(story, chapters, before => after); } Some(StoryUpdate::Words { before, after }) if config.sensibility_level >= SensibilityLevel::IncludeWords => { info_update!(story, words, before => after); } Some(StoryUpdate::DateTime { before, after }) if config.sensibility_level == SensibilityLevel::Anything => { info_update!(story, timestamp, before => after); } Some(StoryUpdate::Words { before, after }) => { info_update!([ignored] story, words, before => after); continue; } Some(StoryUpdate::DateTime { before, after }) => { info_update!([ignored] story, timestamp, before => after); continue; } None => continue, }; ids_to_download.insert(id); } // Update stories with ignored updates. // This way if the downloads fail, these should be saved by the "emergency save". // // After this block, `updated_stories` should only contain stories whose IDs are in // `ids_to_download`. { let mut updated_ids = story_data .keys() .filter(|id| !ids_to_download.contains(id)) .filter_map(|id| updated_stories.remove_entry(id)) .collect::<Vec<(Id, Story)>>(); debug!("Ignored updates: {:?}", &updated_ids); for (id, story) in updated_ids.drain(..) { story_data.insert(id, story); } } if printed { separate!(); } if !force && ids_to_download.is_empty() { info!("There is nothing to download"); } else if force { progress_or_info!( "{}", style(format!( "Force downloading {}", if ids.is_empty() && ignored_ids.is_empty() { "every story on the tracking list" } else { "selected stories" } )) .bold(), ); separate!(); } let use_separator = config.exec.is_some() && !config.quiet; let delay = std::time::Duration::from_secs(config.download_delay); let mut stories_to_download: Vec<StoryDownload> = story_data .keys() // Only download the stories that: // (1) Whose IDs were given by the user if any. // (2) The user responded to its prompt with Y. .filter(|id| selected_ids.contains(id) && !ignored_ids.contains(id)) // Download all stories if the user forced it, otherwise only those who passed the update // sensibility test. .filter(|id| force || ids_to_download.contains(id)) .map(|id| match updated_stories.remove(id) { Some(story) => StoryDownload::Update(*id, story), None => StoryDownload::Forced(*id), }) .collect(); debug!("Stories to download: {:?}", &stories_to_download); for (is_first, story_download) in stories_to_download .drain(..) .enumerate() .map(|(index, story_download)| (index == 0, story_download)) { download_delay!(!is_first, use_separator, delay); match &story_download { StoryDownload::Update(_, story) => requester.download(story)?, // While this should be safe to unwrap, in the unlikely event that it panics the
{ continue; }
conditional_block
download.rs
args::{Download, Prompt}; use crate::readable::ReadableDate; use crate::Requester; macro_rules! format_update { (author, $before:expr => $after:expr) => { format_update!([green] &$before, &$after) }; (chapters, $before:expr => $after:expr) => { format_update!([blue] $before, $after) }; (words, $before:expr => $after:expr) => { format_update!([blue] $before, $after) }; (timestamp, $before:expr => $after:expr) => { format_update!([yellow] ReadableDate($before), ReadableDate($after)) }; (status, $before:expr => $after:expr) => { format_update!([yellow] $before, $after) }; ([$color:ident] $before:expr, $after:expr) => { format_args!( "{} {} {}", style($before).$color(), style("=>").cyan(), style($after).$color().bold() ) }; } macro_rules! info_story_checking { ($story:expr) => { info!("Checking for {} ...", format_story!($story)); }; } macro_rules! info_update { ([ignored] $story:expr, $on:ident, $before:expr => $after:expr) => { info_update!($story, $on, $before, $after, ". Ignoring") }; ($story:expr, $on:ident, $before:expr => $after:expr) => { info_update!($story, $on, $before, $after, "") }; ($story:expr, $on:ident, $before:expr, $after:expr, $extra:expr) => { info!( "{} has an update on {} ({}){}", format_story!($story), stringify!($on), format_update!($on, $before => $after), $extra ); }; } #[derive(Debug)] enum StoryDownload { Update(Id, Story), Forced(Id), } pub fn download( config: &Config, requester: &Requester, story_data: &mut StoryData, Download { force, prompt, ref ids, }: Download, ) -> Result<()>
} for (id, story) in story_data.iter().filter_map(|(id, story)| { if selected_ids.contains(id) { Some((*id, story)) } else { None } }) { if let StoryStatus::Incomplete = story.status { continue; } set_printed!(); let status_notice = format!( "{} has been marked as {} by the author", format_story!(story), format_status!(story) ); match prompt { Prompt::AssumeYes => { info!("{}. Checking for an update on it anyways.", status_notice); } Prompt::AssumeNo => { info!("{}. Skipping checking for an update on it.", status_notice); ignored_ids.insert(id); } Prompt::Ask => { let confirm = Confirm::new() .with_prompt(format!( "{}. Do you want to still check for an update on it?", status_notice )) .interact() .map_err(|err| { TrackerError::io(err) .context("failed to launch overwrite confirmation prompt") })?; if !confirm { ignored_ids.insert(id); } } } } if printed { separate!(); printed = false; } let mut updated_stories: HashMap<Id, Story> = HashMap::with_capacity(selected_ids.len()); let mut ids_to_download: HashSet<Id> = HashSet::with_capacity(selected_ids.len()); for (id, story) in story_data .iter() .filter(|(id, _)| selected_ids.contains(id) && !ignored_ids.contains(id)) .map(|(id, story)| (*id, story)) { info_story_checking!(story); let updated_story: Story = requester.get_story_response(id)?.into(); let title_changed = story.title != updated_story.title; let author_changed = story.author != updated_story.author; let status_changed = story.status != updated_story.status; let story_update = story.compare_to(&updated_story)?; if story_update.is_some() || title_changed || author_changed || status_changed { // If we are here, something will be printed to stderr. Be it by the specific cases // just below or by the resulting StoryUpdate comparison. set_printed!(); if title_changed || author_changed || status_changed { clear_last_lines!(); if title_changed { info!( "{} has changed its title to {}", format_story!(story), style(&updated_story.title).green().bold() ); } if author_changed { info!( "{} has changed its author ({})", format_story!(story), format_update!(author, story.author => updated_story.author) ); } if status_changed { info!( "{} has changed its status ({})", format_story!(story), format_update!(status, story.status => updated_story.status), ); } // Avoid this message from being repeated twice in verbose output. if verbose_disabled!() { info_story_checking!(story); } } updated_stories.insert(id, updated_story); } clear_last_lines!(); match story_update { Some(StoryUpdate::Chapters { before, after }) => { info_update!(story, chapters, before => after); } Some(StoryUpdate::Words { before, after }) if config.sensibility_level >= SensibilityLevel::IncludeWords => { info_update!(story, words, before => after); } Some(StoryUpdate::DateTime { before, after }) if config.sensibility_level == SensibilityLevel::Anything => { info_update!(story, timestamp, before => after); } Some(StoryUpdate::Words { before, after }) => { info_update!([ignored] story, words, before => after); continue; } Some(StoryUpdate::DateTime { before, after }) => { info_update!([ignored] story, timestamp, before => after); continue; } None => continue, }; ids_to_download.insert(id); } // Update stories with ignored updates. // This way if the downloads fail, these should be saved by the "emergency save". // // After this block, `updated_stories` should only contain stories whose IDs are in // `ids_to_download`. { let mut updated_ids = story_data .keys() .filter(|id| !ids_to_download.contains(id)) .filter_map(|id| updated_stories.remove_entry(id)) .collect::<Vec<(Id, Story)>>(); debug!("Ignored updates: {:?}", &updated_ids); for (id, story) in updated_ids.drain(..) { story_data.insert(id, story); } } if printed { separate!(); } if !force && ids_to_download.is_empty() { info!("There is nothing to download"); } else if force { progress_or_info!( "{}", style(format!( "Force downloading {}", if ids.is_empty() && ignored_ids.is_empty() { "every story on the tracking list" } else { "selected stories" } )) .bold(), ); separate!(); } let use_separator = config.exec.is_some() && !config.quiet; let delay = std::time::Duration::from_secs(config.download_delay); let mut stories_to_download: Vec<StoryDownload> = story_data .keys() // Only download the stories that: // (1) Whose IDs were given by the user if any. // (2) The user responded to its prompt with Y. .filter(|id| selected_ids.contains(id) && !ignored_ids.contains(id)) // Download all stories if the user forced it, otherwise only those who passed the update // sensibility test. .filter(|id| force || ids_to_download.contains(id)) .map(|id| match updated_stories.remove(id) { Some(story) => StoryDownload::Update(*id, story), None => StoryDownload::Forced(*id), }) .collect(); debug!("Stories to download: {:?}", &stories_to_download); for (is_first, story_download) in stories_to_download .drain(..) .enumerate() .map(|(index, story_download)| (index == 0, story_download)) { download_delay!(!is_first, use_separator, delay); match &story_download { StoryDownload::Update(_, story) => requester.download(story)?, // While this should be safe to unwrap, in the unlikely event that it panics the //
{ let selected_ids: Vec<Id> = if ids.is_empty() { story_data.keys().cloned().collect() } else { story_data .keys() .filter(|id| ids.contains(id)) .cloned() .collect() }; let mut ignored_ids: HashSet<Id> = HashSet::with_capacity(selected_ids.len()); let mut printed = false; macro_rules! set_printed { () => { if !printed { printed = true; } };
identifier_body
download.rs
args::{Download, Prompt}; use crate::readable::ReadableDate; use crate::Requester; macro_rules! format_update { (author, $before:expr => $after:expr) => { format_update!([green] &$before, &$after) }; (chapters, $before:expr => $after:expr) => { format_update!([blue] $before, $after) }; (words, $before:expr => $after:expr) => { format_update!([blue] $before, $after) }; (timestamp, $before:expr => $after:expr) => { format_update!([yellow] ReadableDate($before), ReadableDate($after)) }; (status, $before:expr => $after:expr) => { format_update!([yellow] $before, $after) }; ([$color:ident] $before:expr, $after:expr) => { format_args!( "{} {} {}", style($before).$color(), style("=>").cyan(), style($after).$color().bold() ) }; } macro_rules! info_story_checking { ($story:expr) => { info!("Checking for {} ...", format_story!($story)); }; } macro_rules! info_update { ([ignored] $story:expr, $on:ident, $before:expr => $after:expr) => { info_update!($story, $on, $before, $after, ". Ignoring") }; ($story:expr, $on:ident, $before:expr => $after:expr) => { info_update!($story, $on, $before, $after, "") }; ($story:expr, $on:ident, $before:expr, $after:expr, $extra:expr) => { info!( "{} has an update on {} ({}){}", format_story!($story), stringify!($on), format_update!($on, $before => $after), $extra ); }; } #[derive(Debug)] enum
{ Update(Id, Story), Forced(Id), } pub fn download( config: &Config, requester: &Requester, story_data: &mut StoryData, Download { force, prompt, ref ids, }: Download, ) -> Result<()> { let selected_ids: Vec<Id> = if ids.is_empty() { story_data.keys().cloned().collect() } else { story_data .keys() .filter(|id| ids.contains(id)) .cloned() .collect() }; let mut ignored_ids: HashSet<Id> = HashSet::with_capacity(selected_ids.len()); let mut printed = false; macro_rules! set_printed { () => { if !printed { printed = true; } }; } for (id, story) in story_data.iter().filter_map(|(id, story)| { if selected_ids.contains(id) { Some((*id, story)) } else { None } }) { if let StoryStatus::Incomplete = story.status { continue; } set_printed!(); let status_notice = format!( "{} has been marked as {} by the author", format_story!(story), format_status!(story) ); match prompt { Prompt::AssumeYes => { info!("{}. Checking for an update on it anyways.", status_notice); } Prompt::AssumeNo => { info!("{}. Skipping checking for an update on it.", status_notice); ignored_ids.insert(id); } Prompt::Ask => { let confirm = Confirm::new() .with_prompt(format!( "{}. Do you want to still check for an update on it?", status_notice )) .interact() .map_err(|err| { TrackerError::io(err) .context("failed to launch overwrite confirmation prompt") })?; if !confirm { ignored_ids.insert(id); } } } } if printed { separate!(); printed = false; } let mut updated_stories: HashMap<Id, Story> = HashMap::with_capacity(selected_ids.len()); let mut ids_to_download: HashSet<Id> = HashSet::with_capacity(selected_ids.len()); for (id, story) in story_data .iter() .filter(|(id, _)| selected_ids.contains(id) && !ignored_ids.contains(id)) .map(|(id, story)| (*id, story)) { info_story_checking!(story); let updated_story: Story = requester.get_story_response(id)?.into(); let title_changed = story.title != updated_story.title; let author_changed = story.author != updated_story.author; let status_changed = story.status != updated_story.status; let story_update = story.compare_to(&updated_story)?; if story_update.is_some() || title_changed || author_changed || status_changed { // If we are here, something will be printed to stderr. Be it by the specific cases // just below or by the resulting StoryUpdate comparison. set_printed!(); if title_changed || author_changed || status_changed { clear_last_lines!(); if title_changed { info!( "{} has changed its title to {}", format_story!(story), style(&updated_story.title).green().bold() ); } if author_changed { info!( "{} has changed its author ({})", format_story!(story), format_update!(author, story.author => updated_story.author) ); } if status_changed { info!( "{} has changed its status ({})", format_story!(story), format_update!(status, story.status => updated_story.status), ); } // Avoid this message from being repeated twice in verbose output. if verbose_disabled!() { info_story_checking!(story); } } updated_stories.insert(id, updated_story); } clear_last_lines!(); match story_update { Some(StoryUpdate::Chapters { before, after }) => { info_update!(story, chapters, before => after); } Some(StoryUpdate::Words { before, after }) if config.sensibility_level >= SensibilityLevel::IncludeWords => { info_update!(story, words, before => after); } Some(StoryUpdate::DateTime { before, after }) if config.sensibility_level == SensibilityLevel::Anything => { info_update!(story, timestamp, before => after); } Some(StoryUpdate::Words { before, after }) => { info_update!([ignored] story, words, before => after); continue; } Some(StoryUpdate::DateTime { before, after }) => { info_update!([ignored] story, timestamp, before => after); continue; } None => continue, }; ids_to_download.insert(id); } // Update stories with ignored updates. // This way if the downloads fail, these should be saved by the "emergency save". // // After this block, `updated_stories` should only contain stories whose IDs are in // `ids_to_download`. { let mut updated_ids = story_data .keys() .filter(|id| !ids_to_download.contains(id)) .filter_map(|id| updated_stories.remove_entry(id)) .collect::<Vec<(Id, Story)>>(); debug!("Ignored updates: {:?}", &updated_ids); for (id, story) in updated_ids.drain(..) { story_data.insert(id, story); } } if printed { separate!(); } if !force && ids_to_download.is_empty() { info!("There is nothing to download"); } else if force { progress_or_info!( "{}", style(format!( "Force downloading {}", if ids.is_empty() && ignored_ids.is_empty() { "every story on the tracking list" } else { "selected stories" } )) .bold(), ); separate!(); } let use_separator = config.exec.is_some() && !config.quiet; let delay = std::time::Duration::from_secs(config.download_delay); let mut stories_to_download: Vec<StoryDownload> = story_data .keys() // Only download the stories that: // (1) Whose IDs were given by the user if any. // (2) The user responded to its prompt with Y. .filter(|id| selected_ids.contains(id) && !ignored_ids.contains(id)) // Download all stories if the user forced it, otherwise only those who passed the update // sensibility test. .filter(|id| force || ids_to_download.contains(id)) .map(|id| match updated_stories.remove(id) { Some(story) => StoryDownload::Update(*id, story), None => StoryDownload::Forced(*id), }) .collect(); debug!("Stories to download: {:?}", &stories_to_download); for (is_first, story_download) in stories_to_download .drain(..) .enumerate() .map(|(index, story_download)| (index == 0, story_download)) { download_delay!(!is_first, use_separator, delay); match &story_download { StoryDownload::Update(_, story) => requester.download(story)?, // While this should be safe to unwrap, in the unlikely event that it panics the //
StoryDownload
identifier_name
asnd.rs
clusive. #[must_use] pub fn voice(mut self, voice: u32) -> Self { assert!(voice < 16, "Voice index {} is >= 16", voice); self.voice = voice; self } /// Format to use for this sound. #[must_use] pub fn format(mut self, format: VoiceFormat) -> Self { self.format = format; self } /// Frequency to use, in Hz. #[must_use] pub fn pitch(mut self, pitch: u32) -> Self { self.pitch = pitch; self } /// Delay to wait before playing, in milliseconds. #[must_use] pub fn delay(mut self, delay: u32) -> Self { self.delay = delay; self } /// Voice volume of the left channel. #[must_use] pub fn volume_left(mut self, volume_left: u8) -> Self { self.volume_left = volume_left; self } /// Voice volume of the right channel. #[must_use] pub fn volume_right(mut self, volume_right: u8) -> Self { self.volume_right = volume_right; self } /// Optional callback function to use. #[must_use] pub fn callback(mut self, callback: Option<unsafe extern "C" fn(i32)>) -> Self { self.callback = callback; self } } /// Source voice format. pub enum VoiceFormat { Mono8Bit, Mono16Bit, Mono16BitBe, Stereo8Bit, Stereo16Bit, Stereo16BitBe, Mono8BitU, Mono16BitLE, Stereo8BitU, Stereo16BitLe, } impl VoiceFormat { fn as_i32(&self) -> i32 { match self { VoiceFormat::Mono8Bit => 0, VoiceFormat::Mono16Bit => 1, VoiceFormat::Mono16BitBe => 1, VoiceFormat::Stereo8Bit => 2, VoiceFormat::Stereo16Bit => 3, VoiceFormat::Stereo16BitBe => 3, VoiceFormat::Mono8BitU => 4, VoiceFormat::Mono16BitLE => 5, VoiceFormat::Stereo8BitU => 6, VoiceFormat::Stereo16BitLe => 7, } } } /// Represents the asnd service. /// This service can only be created once! /// If you use `Asnd::init()`, you cannot do `Audio::init()`. /// Only one of them can be used at a time. pub struct Asnd; /// Implementation of the asnd service. impl Asnd { /// Initializes the asnd lib and fixes the hardware sample rate to 48000hz. pub fn init() -> Self { unsafe { ffi::ASND_Init(); } Self } /// De-initializes the asnd lib. This is also called when `Asnd` gets dropped. pub fn end() { unsafe { ffi::ASND_End(); } } /// Pauses if true and resumes if false. pub fn pause(should_pause: bool) { unsafe { ffi::ASND_Pause(should_pause as i32); } } /// Returns true if paused, false if not paused. pub fn is_paused() -> bool { unsafe { ffi::ASND_Is_Paused() > 0 } } /// Returns the global time in milliseconds. Time is updated from the IRQ. pub fn get_time() -> u32 { unsafe { ffi::ASND_GetTime() } } /// Returns the global sample counter. Can be used to implement timers with high precision. pub fn
() -> u32 { unsafe { ffi::ASND_GetSampleCounter() } } /// Returns the samples sent from the IRQ in one tick. pub fn get_samples_per_tick() -> u32 { unsafe { ffi::ASND_GetSamplesPerTick() } } /// Sets the global time, in milliseconds. pub fn set_time(time: u32) { unsafe { ffi::ASND_SetTime(time); } } /// Sets a global callback for general purposes. It is called by the IRQ. pub fn set_callback<F>(callback: Option<unsafe extern "C" fn()>) { unsafe { ffi::ASND_SetCallback(callback); } } /// Returs the current audio rate. Default is 48000hz. pub fn get_audio_rate() -> i32 { unsafe { ffi::ASND_GetAudioRate() } } /// Sets a PCM voice to play. This function stops one previous voice. Use /// `Asnd::status_voice()` to test status. The voices are played in 16-bit stereo, /// regardless of source format. The buffer MUST be aligned and padded to 32 bytes. pub fn set_voice(options: VoiceOptions, sound_buffer: &mut [u8]) -> Result<()> { Self::validate_buffer(sound_buffer); let err = unsafe { ffi::ASND_SetVoice( options.voice as i32, options.format.as_i32(), options.pitch as i32, options.delay as i32, sound_buffer.as_mut_ptr() as *mut _, sound_buffer.len() as i32, options.volume_left as i32, options.volume_right as i32, options.callback, ) }; if_not!(SND_OK => "Asnd::set_voice() failed with error {}!", err) } /// Sets a PCM voice to play infinitely. See `Asnd::set_voice()` as it is largely identical. /// The buffer MUST be aligned and padded to 32 bytes. pub fn set_infinite_voice(options: VoiceOptions, sound_buffer: &mut [u8]) -> Result<()> { Self::validate_buffer(sound_buffer); let err = unsafe { ffi::ASND_SetInfiniteVoice( options.voice as i32, options.format.as_i32(), options.pitch as i32, options.delay as i32, sound_buffer.as_mut_ptr() as *mut _, sound_buffer.len() as i32, options.volume_left as i32, options.volume_right as i32, ) }; if_not!(SND_OK => "Asnd::set_infinite_voice() failed with error {}", err) } /// Adds a PCM voice to play from the second buffer. Sound buffer must be 32-byte /// aligned and have same sample format as first buffer. This must only be called after /// `Asnd::set_voice()`, which must return `Ok()`. /// The buffer MUST be aligned and padded to 32 bytes. fn add_voice(voice: u32, sound_buffer: &mut [u8]) -> Result<()> { assert!(voice < 16, "Voice index {} is >= 16", voice); Self::validate_buffer(sound_buffer); let err = unsafe { ffi::ASND_AddVoice( voice as i32, sound_buffer.as_mut_ptr() as *mut _, sound_buffer.len() as i32, ) }; if_not!(SND_OK => "Asnd::add_voice() failed with error {}", err) } /// Stops the selected voice. If the voice is used in song mode, you need to /// assign the samples with `Asnd::set_song_sample_voice()`. pub fn stop_voice(voice: u32) -> Result<()> { assert!(voice < 16, "Voice index {} is >= 16", voice); let err = unsafe { ffi::ASND_StopVoice(voice as i32) }; if_not!(SND_OK => "Asnd::stop_voice() failed with error {}", err) } /// Pauses the selected voice. Can also be used to resume voice. pub fn pause_voice(voice: u32, pause: bool) -> Result<()> { assert!(voice < 16, "Voice index {} is >= 16", voice); let err = unsafe { ffi::ASND_PauseVoice(voice as i32, pause as i32) }; if_not!(SND_OK => "Asnd::pause_voice() failed with error {}", err) } /// Returns the state of the selected voice. pub fn status_voice(voice: u32) -> Result<()> { assert!(voice < 16, "Voice index {} is >= 16", voice); let err = unsafe { ffi::ASND_StatusVoice(voice as i32) }; if_not!(SND_WORKING => "Asnd::status_voice() failed with error {}", err) } /// Returns the first unused voice. Fails if no voices are available. pub fn get_first_unused_voice() -> Result<u32> { let err = unsafe { ffi::ASND_GetFirstUnusedVoice() }; match err { x if x < 16 => Ok(x as u32), _ => Err(OgcError::Audio(format!( "Asnd::get_first_unused_voice() failed with error {}",
get_sample_counter
identifier_name
asnd.rs
() -> Self { unsafe { ffi::ASND_Init(); } Self } /// De-initializes the asnd lib. This is also called when `Asnd` gets dropped. pub fn end() { unsafe { ffi::ASND_End(); } } /// Pauses if true and resumes if false. pub fn pause(should_pause: bool) { unsafe { ffi::ASND_Pause(should_pause as i32); } } /// Returns true if paused, false if not paused. pub fn is_paused() -> bool { unsafe { ffi::ASND_Is_Paused() > 0 } } /// Returns the global time in milliseconds. Time is updated from the IRQ. pub fn get_time() -> u32 { unsafe { ffi::ASND_GetTime() } } /// Returns the global sample counter. Can be used to implement timers with high precision. pub fn get_sample_counter() -> u32 { unsafe { ffi::ASND_GetSampleCounter() } } /// Returns the samples sent from the IRQ in one tick. pub fn get_samples_per_tick() -> u32 { unsafe { ffi::ASND_GetSamplesPerTick() } } /// Sets the global time, in milliseconds. pub fn set_time(time: u32) { unsafe { ffi::ASND_SetTime(time); } } /// Sets a global callback for general purposes. It is called by the IRQ. pub fn set_callback<F>(callback: Option<unsafe extern "C" fn()>) { unsafe { ffi::ASND_SetCallback(callback); } } /// Returs the current audio rate. Default is 48000hz. pub fn get_audio_rate() -> i32 { unsafe { ffi::ASND_GetAudioRate() } } /// Sets a PCM voice to play. This function stops one previous voice. Use /// `Asnd::status_voice()` to test status. The voices are played in 16-bit stereo, /// regardless of source format. The buffer MUST be aligned and padded to 32 bytes. pub fn set_voice(options: VoiceOptions, sound_buffer: &mut [u8]) -> Result<()> { Self::validate_buffer(sound_buffer); let err = unsafe { ffi::ASND_SetVoice( options.voice as i32, options.format.as_i32(), options.pitch as i32, options.delay as i32, sound_buffer.as_mut_ptr() as *mut _, sound_buffer.len() as i32, options.volume_left as i32, options.volume_right as i32, options.callback, ) }; if_not!(SND_OK => "Asnd::set_voice() failed with error {}!", err) } /// Sets a PCM voice to play infinitely. See `Asnd::set_voice()` as it is largely identical. /// The buffer MUST be aligned and padded to 32 bytes. pub fn set_infinite_voice(options: VoiceOptions, sound_buffer: &mut [u8]) -> Result<()> { Self::validate_buffer(sound_buffer); let err = unsafe { ffi::ASND_SetInfiniteVoice( options.voice as i32, options.format.as_i32(), options.pitch as i32, options.delay as i32, sound_buffer.as_mut_ptr() as *mut _, sound_buffer.len() as i32, options.volume_left as i32, options.volume_right as i32, ) }; if_not!(SND_OK => "Asnd::set_infinite_voice() failed with error {}", err) } /// Adds a PCM voice to play from the second buffer. Sound buffer must be 32-byte /// aligned and have same sample format as first buffer. This must only be called after /// `Asnd::set_voice()`, which must return `Ok()`. /// The buffer MUST be aligned and padded to 32 bytes. fn add_voice(voice: u32, sound_buffer: &mut [u8]) -> Result<()> { assert!(voice < 16, "Voice index {} is >= 16", voice); Self::validate_buffer(sound_buffer); let err = unsafe { ffi::ASND_AddVoice( voice as i32, sound_buffer.as_mut_ptr() as *mut _, sound_buffer.len() as i32, ) }; if_not!(SND_OK => "Asnd::add_voice() failed with error {}", err) } /// Stops the selected voice. If the voice is used in song mode, you need to /// assign the samples with `Asnd::set_song_sample_voice()`. pub fn stop_voice(voice: u32) -> Result<()> { assert!(voice < 16, "Voice index {} is >= 16", voice); let err = unsafe { ffi::ASND_StopVoice(voice as i32) }; if_not!(SND_OK => "Asnd::stop_voice() failed with error {}", err) } /// Pauses the selected voice. Can also be used to resume voice. pub fn pause_voice(voice: u32, pause: bool) -> Result<()> { assert!(voice < 16, "Voice index {} is >= 16", voice); let err = unsafe { ffi::ASND_PauseVoice(voice as i32, pause as i32) }; if_not!(SND_OK => "Asnd::pause_voice() failed with error {}", err) } /// Returns the state of the selected voice. pub fn status_voice(voice: u32) -> Result<()> { assert!(voice < 16, "Voice index {} is >= 16", voice); let err = unsafe { ffi::ASND_StatusVoice(voice as i32) }; if_not!(SND_WORKING => "Asnd::status_voice() failed with error {}", err) } /// Returns the first unused voice. Fails if no voices are available. pub fn get_first_unused_voice() -> Result<u32> { let err = unsafe { ffi::ASND_GetFirstUnusedVoice() }; match err { x if x < 16 => Ok(x as u32), _ => Err(OgcError::Audio(format!( "Asnd::get_first_unused_voice() failed with error {}", err ))), } } /// Changes the voice-pitch in real time. This function can be used to /// create audio effects such as Doppler effect simulation. pub fn change_pitch_voice(voice: u32, pitch: u32) -> Result<()> { assert!(voice < 16, "Voice index {} is >= 16", voice); let err = unsafe { ffi::ASND_ChangePitchVoice(voice as i32, pitch as i32) }; if_not!(SND_OK => "Asnd::change_pitch_voice() failed with error {}", err) } /// Changes the voice volume in real time. This function can be used to create /// audio effects like distance attenuation. pub fn change_volume_voice(voice: u32, volume_left: u8, volume_right: u8) -> Result<()> { assert!(voice < 16, "Voice index {} is >= 16", voice); let err = unsafe { ffi::ASND_ChangeVolumeVoice(voice as i32, volume_left as i32, volume_right as i32) }; if_not!(SND_OK => "Asnd::change_volume_voice() failed with error {}", err) } /// Returns the voice tick counter. This value represents the number of ticks /// since this voice started to play, sans delay time. If the lib is initialized with /// `INIT_RATE=48000`, a return value of 24000 is equal to 0.5 seconds. pub fn get_tick_counter_voice(voice: u32) -> u32 { assert!(voice < 16, "Voice index {} is >= 16", voice); unsafe { ffi::ASND_GetTickCounterVoice(voice as i32) } } /// Returns the voice playback time. This value represents the time in milliseconds /// since this voice started playing. pub fn get_timer_voice(voice: u32) -> u32 { assert!(voice < 16, "Voice index {} is >= 16", voice); unsafe { ffi::ASND_GetTimerVoice(voice as i32) } } /// Tests if a pointer is in use by a voice as a buffer. /// This must be the same pointer sent to `Asnd::add_voice()` or `Asnd::set_voice()`. /// Returns 0 if the pointer is unused. /// Returns 1 if the pointer is used as a buffer. /// Returns `ogc_sys::SND_INVALID` if invalid. pub fn test_pointer<T>(voice: u32, pointer: *mut T) -> i32
{ assert!(voice < 16, "Voice index {} is >= 16", voice); unsafe { ffi::ASND_TestPointer(voice as i32, pointer as *mut _) } }
identifier_body
asnd.rs
) { unsafe { ffi::ASND_SetTime(time); } } /// Sets a global callback for general purposes. It is called by the IRQ. pub fn set_callback<F>(callback: Option<unsafe extern "C" fn()>) { unsafe { ffi::ASND_SetCallback(callback); } } /// Returs the current audio rate. Default is 48000hz. pub fn get_audio_rate() -> i32 { unsafe { ffi::ASND_GetAudioRate() } } /// Sets a PCM voice to play. This function stops one previous voice. Use /// `Asnd::status_voice()` to test status. The voices are played in 16-bit stereo, /// regardless of source format. The buffer MUST be aligned and padded to 32 bytes. pub fn set_voice(options: VoiceOptions, sound_buffer: &mut [u8]) -> Result<()> { Self::validate_buffer(sound_buffer); let err = unsafe { ffi::ASND_SetVoice( options.voice as i32, options.format.as_i32(), options.pitch as i32, options.delay as i32, sound_buffer.as_mut_ptr() as *mut _, sound_buffer.len() as i32, options.volume_left as i32, options.volume_right as i32, options.callback, ) }; if_not!(SND_OK => "Asnd::set_voice() failed with error {}!", err) } /// Sets a PCM voice to play infinitely. See `Asnd::set_voice()` as it is largely identical. /// The buffer MUST be aligned and padded to 32 bytes. pub fn set_infinite_voice(options: VoiceOptions, sound_buffer: &mut [u8]) -> Result<()> { Self::validate_buffer(sound_buffer); let err = unsafe { ffi::ASND_SetInfiniteVoice( options.voice as i32, options.format.as_i32(), options.pitch as i32, options.delay as i32, sound_buffer.as_mut_ptr() as *mut _, sound_buffer.len() as i32, options.volume_left as i32, options.volume_right as i32, ) }; if_not!(SND_OK => "Asnd::set_infinite_voice() failed with error {}", err) } /// Adds a PCM voice to play from the second buffer. Sound buffer must be 32-byte /// aligned and have same sample format as first buffer. This must only be called after /// `Asnd::set_voice()`, which must return `Ok()`. /// The buffer MUST be aligned and padded to 32 bytes. fn add_voice(voice: u32, sound_buffer: &mut [u8]) -> Result<()> { assert!(voice < 16, "Voice index {} is >= 16", voice); Self::validate_buffer(sound_buffer); let err = unsafe { ffi::ASND_AddVoice( voice as i32, sound_buffer.as_mut_ptr() as *mut _, sound_buffer.len() as i32, ) }; if_not!(SND_OK => "Asnd::add_voice() failed with error {}", err) } /// Stops the selected voice. If the voice is used in song mode, you need to /// assign the samples with `Asnd::set_song_sample_voice()`. pub fn stop_voice(voice: u32) -> Result<()> { assert!(voice < 16, "Voice index {} is >= 16", voice); let err = unsafe { ffi::ASND_StopVoice(voice as i32) }; if_not!(SND_OK => "Asnd::stop_voice() failed with error {}", err) } /// Pauses the selected voice. Can also be used to resume voice. pub fn pause_voice(voice: u32, pause: bool) -> Result<()> { assert!(voice < 16, "Voice index {} is >= 16", voice); let err = unsafe { ffi::ASND_PauseVoice(voice as i32, pause as i32) }; if_not!(SND_OK => "Asnd::pause_voice() failed with error {}", err) } /// Returns the state of the selected voice. pub fn status_voice(voice: u32) -> Result<()> { assert!(voice < 16, "Voice index {} is >= 16", voice); let err = unsafe { ffi::ASND_StatusVoice(voice as i32) }; if_not!(SND_WORKING => "Asnd::status_voice() failed with error {}", err) } /// Returns the first unused voice. Fails if no voices are available. pub fn get_first_unused_voice() -> Result<u32> { let err = unsafe { ffi::ASND_GetFirstUnusedVoice() }; match err { x if x < 16 => Ok(x as u32), _ => Err(OgcError::Audio(format!( "Asnd::get_first_unused_voice() failed with error {}", err ))), } } /// Changes the voice-pitch in real time. This function can be used to /// create audio effects such as Doppler effect simulation. pub fn change_pitch_voice(voice: u32, pitch: u32) -> Result<()> { assert!(voice < 16, "Voice index {} is >= 16", voice); let err = unsafe { ffi::ASND_ChangePitchVoice(voice as i32, pitch as i32) }; if_not!(SND_OK => "Asnd::change_pitch_voice() failed with error {}", err) } /// Changes the voice volume in real time. This function can be used to create /// audio effects like distance attenuation. pub fn change_volume_voice(voice: u32, volume_left: u8, volume_right: u8) -> Result<()> { assert!(voice < 16, "Voice index {} is >= 16", voice); let err = unsafe { ffi::ASND_ChangeVolumeVoice(voice as i32, volume_left as i32, volume_right as i32) }; if_not!(SND_OK => "Asnd::change_volume_voice() failed with error {}", err) } /// Returns the voice tick counter. This value represents the number of ticks /// since this voice started to play, sans delay time. If the lib is initialized with /// `INIT_RATE=48000`, a return value of 24000 is equal to 0.5 seconds. pub fn get_tick_counter_voice(voice: u32) -> u32 { assert!(voice < 16, "Voice index {} is >= 16", voice); unsafe { ffi::ASND_GetTickCounterVoice(voice as i32) } } /// Returns the voice playback time. This value represents the time in milliseconds /// since this voice started playing. pub fn get_timer_voice(voice: u32) -> u32 { assert!(voice < 16, "Voice index {} is >= 16", voice); unsafe { ffi::ASND_GetTimerVoice(voice as i32) } } /// Tests if a pointer is in use by a voice as a buffer. /// This must be the same pointer sent to `Asnd::add_voice()` or `Asnd::set_voice()`. /// Returns 0 if the pointer is unused. /// Returns 1 if the pointer is used as a buffer. /// Returns `ogc_sys::SND_INVALID` if invalid. pub fn test_pointer<T>(voice: u32, pointer: *mut T) -> i32 { assert!(voice < 16, "Voice index {} is >= 16", voice); unsafe { ffi::ASND_TestPointer(voice as i32, pointer as *mut _) } } /// Tests to determine if the voice is ready to receive a new buffer sample /// with `Asnd::add_voice()`. Returns true if voice is ready. pub fn test_voice_buffer_ready(voice: u32) -> bool { assert!(voice < 16, "Voice index {} is >= 16", voice); unsafe { ffi::ASND_TestVoiceBufferReady(voice as i32) > 0 } } /// Returns the DSP usage, in percent `(0..=100)`. pub fn get_dsp_percent_use() -> u32 { unsafe { ffi::ASND_GetDSP_PercentUse() } } /// Returns DSP process time, in nano seconds. pub fn get_dsp_process_time() -> Duration { unsafe { Duration::from_nanos(ffi::ASND_GetDSP_ProcessTime().into()) } } fn validate_buffer(sound_buffer: &mut [u8]) { assert_eq!( 0, sound_buffer.as_ptr().align_offset(32), "Data is not aligned correctly." ); assert_eq!( 0, sound_buffer.len() % 32, "Data length is not a multiple of 32." ); } } impl Drop for Asnd { fn drop(&mut self) { Self::end(); }
}
random_line_split
dg.go
推广者-物料精选 ) * taobao.tbk.dg.optimus.material * @line https://open.taobao.com/api.htm?docId=33947&docType=2 */ func (d *dg) OptimusMaterial(adzoneId int64, others ...map[string]string) (res *OptimusMaterialResponse, err error) { params := make(map[string]string) if len(others) > 0 { params = others[0] } params["adzone_id"] = strconv.FormatInt(adzoneId, 10) _, err = d.Client.httpPost("taobao.tbk.dg.optimus.material", params, &res) return } type vegas struct { Client *Tbk } func (d *dg) Vegas() *vegas { return &vegas{Client: d.Client} } // 淘礼金创建配置 type TljConf struct { AdzoneId int64 `json:"adzone_id"` // 广告位id ItemId string `json:"item_id"` // 商品id TotalNum int64 `json:"total_num"` // 总体个数 Name string `json:"name"` // 淘礼金名称 UserTotalWinNumLimit int `json:"user_total_win_num_limit"` // 单用户累计中奖次数上限 SecuritySwitch bool `json:"security_switch"` // 安全开关 PerFace string `json:"per_face"` // 单个淘礼金面额 SendStartTime string `json:"send_start_time"` // 发放开始时间 SendEndTime string `json:"send_end_time"` // 发放截止时间 UseEndTime string `json:"use_end_time"` // 使用结束日期 UseEndTimeMode int `json:"use_end_time_mode"` // 结束日期模式,1:相对时间 2:绝对时间 UseStartTime string `json:"use_start_time"` // 开始时间 } // 淘礼金创建完成响应 type TljResponse struct { Response struct { Result struct { Model struct { RightsId string `json:"rights_id"` SendUrl string `json:"send_url"` VegasCode string `json:"vegas_code"` } `json:"model"` MsgCode string `json:"msg_code"` MsgInfo string `json:"msg_info"` Success bool `json:"success"` } `json:"result"` } `json:"tbk_dg_vegas_tlj_create_response"` } /** * (淘宝客-推广者-淘礼金创建) * taobao.tbk.dg.vegas.tlj.create * @line https://open.taobao.com/api.htm?docId=40173&docType=2 */ func (v *vegas) TljCreate(conf *TljConf) (res *TljResponse, err error) { _, err = v.Client.httpPost("taobao.tbk.dg.vegas.tlj.create", v.Client.Struct2MapString(conf), &res) return } type TljReportResponse struct { Response struct { Result struct { MsgCode string `json:"msg_code"` MsgInfo string `json:"msg_info"` Success bool `json:"success"` Model struct { UnfreezeAmount float64 `json:"unfreeze_amount"` UnfreezeNum int64 `json:"unfreeze_num"` RefundAmount float64 `json:"refund_amount"` RefundNum int64 `json:"refund_num"` AlipayAmount float64 `json:"alipay_amount"` UseAmount float64 `json:"use_amount"` UseNum int64 `json:"use_num"` WinAmount float64 `json:"win_amount"` WinNum int64 `json:"win_num"` PerCommissionAmount float64 `json:"per_commission_amount"` FpRefundAmount float64 `json:"fp_refund_amount"` FpRefundNum int64 `json:"fp_refund_num"` } `json:"model"` } `json:"result"` } `json:"tbk_dg_vegas_tlj_instance_report_response"` } /** * ( 淘宝客-推广者-淘礼金发放及使用报表 ) * taobao.tbk.dg.vegas.tlj.instance.report * @line https://open.taobao.com/api.htm?docId=43317&docType=2&scopeId=15029 */ func (v *vegas) TljInstanceReport(RightsId string) (res *TljReportResponse, err error) { _, err = v.Client.httpPost("taobao.tbk.dg.vegas.tlj.instance.report", map[string]string{ "rights_id": RightsId, }, &res) return } type LbTljConf struct { SecurityLevel int `json:"security_level"` // 安全等级 UseStartTime string `json:"use_start_time"` // 使用开始日期 UseEndTime string `json:"use_end_time"` // 使用结束日期 UseEndTimeMode int `json:"use_end_time_mode"` // 结束日期的模式 1:相对时间 2:绝对时间 AcceptStartTime string `json:"accept_start_time"` // 裂变任务领取开始时间 AcceptEndTime string `json:"accept_end_time"` // 裂变任务领取截止时间 RightsPerFace string `json:"rights_per_face"` // 单个淘礼金面额,支持两位小数,单位元 SecuritySwitch bool `json:"security_switch"` // 安全开关 true启用 false不启用 UserTotalWinNumLimit int `json:"user_total_win_num_limit"` // 单用户累计中奖次数上限 Name string `json:"name"` // 淘礼金名称 RightsNum int `json:"rights_num"` // 淘礼金总个数 ItemId string `json:"item_id"` // 商品ID CampaignType string `json:"campaign_type"` TaskRightNum int `json:"task_right_num"` // 裂变淘礼金总个数 TaskRightsPerFace string `json:"task_rights_per_face"` // 裂变单个淘礼金面额 InviteNum int `json:"invite_num"` // 裂变淘礼金邀请人数 InviteTimeLimit int `json:"invite_time_limit"` // 裂变淘礼金邀请时长,单位分钟,最大120分钟 AdzoneId int `json:"adzone_id"` // 推广位ID } type LbTljResponse struct { Response struct { Result struct { Model struct { RightsID string `json:"rights_id"` // 直接领取淘礼金ID 即小额淘礼金 SendURL string `json:"send_url"` // 发放地址 TaskRightsID string `json:"task_rights_id"` // 裂变淘礼金ID 即大额淘礼金 TaskID string `json:"task_id"` // 裂变任务ID } `json:"model"` MsgCode string `json:"msg_code"` MsgInfo string `json:"msg_info"` Success bool `json:"success"` } `json:"result"` } `json:"tbk_dg_vegas_lbtlj_create_response"` } /** * ( 淘宝客-推广者-裂变淘礼金创建 ) * taobao.tbk.dg.vegas.lbtlj.create * @line https://open.taobao.com/api.htm?docId=57710&docType=2&scopeId=23995 */ func (v *vegas) LbTljCreate(conf *LbTljConf) (res *LbTljResponse, err error) { _, err = v.Client.httpPost("taobao.tbk.dg.vegas.lbtlj.create", v.Client.Struct2MapString(conf), &res) return } type newUser struct { Client *Tbk } func (d *dg) NewUser() *newUser { return &newUser{Client: d.Client} } type OrderGetResponse struct { Response struct { Result struct { Data struct { Result []OrderGetData `json:"result"` PageNo int `json:"page_no"` PageSize int `json:"page_size"` HasNext bool `json:"has_next"` } `json:"data"` } `json:"result"` } `json:"tbk_dg_newuser_order_get_response"` } type OrderGetData struct { RegisterTime string `json:"register_time"` BindTime string `json:"bind_time"` BuyTime string `json:"buy_time"` Status int `json:"status"` Mobile string `json:"mobile"` OrderTkType int
`jso
identifier_name
dg.go
// 搜索响应数据结构体 type MaterialOptionalResponse struct { Response struct { TotalResults int `json:"total_results"` ResultList struct { MapData []MaterialOptionalData `json:"map_data"` } `json:"result_list"` } `json:"tbk_dg_material_optional_response"` } /** * (通用物料搜索API(导购)) * taobao.tbk.dg.material.optional * @line http://open.taobao.com/docs/api.htm?apiId=35896 */ func (d *dg) MaterialOptional(adzoneId int64, others ...map[string]string) (res *MaterialOptionalResponse, err error) { params := make(map[string]string) if len(others) > 0 { params = others[0] } params["adzone_id"] = strconv.FormatInt(adzoneId, 10) _, err = d.Client.httpPost("taobao.tbk.dg.material.optional", params, &res) return } type OptimusMaterialResponse struct { Response struct { ResultList struct { MapData []struct { MaterialOptionalData JuPlayEndTime string `json:"ju_play_end_time"` JuPlayStartTime string `json:"ju_play_start_time"` PlayInfo string `json:"play_info"` TmallPlayActivityEndTime int64 `json:"tmall_play_activity_end_time"` TmallPlayActivityStartTime int64 `json:"tmall_play_activity_start_time"` JuPreShowEndTime int64 `json:"ju_pre_show_end_time"` JuPreShowStartTime int64 `json:"ju_pre_show_start_time"` FavoritesInfo struct { TotalCount int `json:"total_count"` FavoritesList []FavoritesDetail `json:"favorites_list"` } `json:"favorites_info"` // 选品库信息 } `json:"map_data"` } `json:"result_list"` IsDefault string `json:"is_default"` TotalCount int `json:"total_count"` } `json:"tbk_dg_optimus_material_response"` } // 选品库详情 type FavoritesDetail struct { FavoritesId int64 `json:"favorites_id"` FavoritesTitle string `json:"favorites_title"` } /** * ( 淘宝客-推广者-物料精选 ) * taobao.tbk.dg.optimus.material * @line https://open.taobao.com/api.htm?docId=33947&docType=2 */ func (d *dg) OptimusMaterial(adzoneId int64, others ...map[string]string) (res *OptimusMaterialResponse, err error) { params := make(map[string]string) if len(others) > 0 { params = others[0] } params["adzone_id"] = strconv.FormatInt(adzoneId, 10) _, err = d.Client.httpPost("taobao.tbk.dg.optimus.material", params, &res) return } type vegas struct { Client *Tbk } func (d *dg) Vegas() *vegas { return &vegas{Client: d.Client} } // 淘礼金创建配置 type TljConf struct { AdzoneId int64 `json:"adzone_id"` // 广告位id ItemId string `json:"item_id"` // 商品id TotalNum int64 `json:"total_num"` // 总体个数 Name string `json:"name"` // 淘礼金名称 UserTotalWinNumLimit int `json:"user_total_win_num_limit"` // 单用户累计中奖次数上限 SecuritySwitch bool `json:"security_switch"` // 安全开关 PerFace string `json:"per_face"` // 单个淘礼金面额 SendStartTime string `json:"send_start_time"` // 发放开始时间 SendEndTime string `json:"send_end_time"` // 发放截止时间 UseEndTime string `json:"use_end_time"` // 使用结束日期 UseEndTimeMode int `json:"use_end_time_mode"` // 结束日期模式,1:相对时间 2:绝对时间 UseStartTime string `json:"use_start_time"` // 开始时间 } // 淘礼金创建完成响应 type TljResponse struct { Response struct { Result struct { Model struct { RightsId string `json:"rights_id"` SendUrl string `json:"send_url"` VegasCode string `json:"vegas_code"` } `json:"model"` MsgCode string `json:"msg_code"` MsgInfo string `json:"msg_info"` Success bool `json:"success"` } `json:"result"` } `json:"tbk_dg_vegas_tlj_create_response"` } /** * (淘宝客-推广者-淘礼金创建) * taobao.tbk.dg.vegas.tlj.create * @line https://open.taobao.com/api.htm?docId=40173&docType=2 */ func (v *vegas) TljCreate(conf *TljConf) (res *TljResponse, err error) { _, err = v.Client.httpPost("taobao.tbk.dg.vegas.tlj.create", v.Client.Struct2MapString(conf), &res) return } type TljReportResponse struct { Response struct { Result struct { MsgCode string `json:"msg_code"` MsgInfo string `json:"msg_info"` Success bool `json:"success"` Model struct { UnfreezeAmount float64 `json:"unfreeze_amount"` UnfreezeNum int64 `json:"unfreeze_num"` RefundAmount float64 `json:"refund_amount"` RefundNum int64 `json:"refund_num"` AlipayAmount float64 `json:"alipay_amount"` UseAmount float64 `json:"use_amount"` UseNum int64 `json:"use_num"` WinAmount float64 `json:"win_amount"` WinNum int64 `json:"win_num"` PerCommissionAmount float64 `json:"per_commission_amount"` FpRefundAmount float64 `json:"fp_refund_amount"` FpRefundNum int64 `json:"fp_refund_num"` } `json:"model"` } `json:"result"` } `json:"tbk_dg_vegas_tlj_instance_report_response"` } /** * ( 淘宝客-推广者-淘礼金发放及使用报表 ) * taobao.tbk.dg.vegas.tlj.instance.report * @line https://open.taobao.com/api.htm?docId=43317&docType=2&scopeId=15029 */ func (v *vegas) TljInstanceReport(RightsId string) (res *TljReportResponse, err error) { _, err = v.Client.httpPost("taobao.tbk.dg.vegas.tlj.instance.report", map[string]string{ "rights_id": RightsId, }, &res) return } type LbTljConf struct { SecurityLevel int `json:"security_level"` // 安全等级 UseStartTime string `json:"use_start_time"` // 使用开始日期 UseEndTime string `json:"use_end_time"` // 使用结束日期 UseEndTimeMode int `json:"use_end_time_mode"` // 结束日期的模式 1:相对时间 2:绝对时间 AcceptStartTime string `json:"accept_start_time"` // 裂变任务领取开始时间 AcceptEndTime string `json:"accept_end_time"` // 裂变任务领取截止时间 RightsPerFace string `json:"rights_per_face"` // 单个淘礼金面额,支持两位小数,单位元 SecuritySwitch bool `json:"security_switch"` // 安全开关 true启用 false不启用 UserTotalWinNumLimit int `json:"user_total_win_num_limit"` // 单用户累计中奖次数上限 Name string `json:"name"` // 淘礼金名称 RightsNum int `json:"rights_num"` // 淘礼金总个数 ItemId string `json:"item_id"` // 商品ID CampaignType string `json:"campaign_type"` TaskRightNum int `json:"task_right_num"` // 裂变淘礼金总个数 TaskRightsPerFace string `json:"task_rights_per_face"` // 裂变单个淘礼金面额 InviteNum int `json:"invite_num"` // 裂变淘礼金邀请人数 InviteTimeLimit int `json:"invite_time_limit"` // 裂变淘礼金邀请时长,单位分钟,最大120分钟 AdzoneId int `json:"adzone_id"`
{ return &dg{Client: t} }
identifier_body
dg.go
JuPlayEndTime string `json:"ju_play_end_time"` JuPlayStartTime string `json:"ju_play_start_time"` PlayInfo string `json:"play_info"` TmallPlayActivityEndTime int64 `json:"tmall_play_activity_end_time"` TmallPlayActivityStartTime int64 `json:"tmall_play_activity_start_time"` JuPreShowEndTime int64 `json:"ju_pre_show_end_time"` JuPreShowStartTime int64 `json:"ju_pre_show_start_time"` FavoritesInfo struct { TotalCount int `json:"total_count"` FavoritesList []FavoritesDetail `json:"favorites_list"` } `json:"favorites_info"` // 选品库信息 } `json:"map_data"` } `json:"result_list"` IsDefault string `json:"is_default"` TotalCount int `json:"total_count"` } `json:"tbk_dg_optimus_material_response"` } // 选品库详情 type FavoritesDetail struct { FavoritesId int64 `json:"favorites_id"` FavoritesTitle string `json:"favorites_title"` } /** * ( 淘宝客-推广者-物料精选 ) * taobao.tbk.dg.optimus.material * @line https://open.taobao.com/api.htm?docId=33947&docType=2 */ func (d *dg) OptimusMaterial(adzoneId int64, others ...map[string]string) (res *OptimusMaterialResponse, err error) { params := make(map[string]string) if len(others) > 0 { params = others[0] } params["adzone_id"] = strconv.FormatInt(adzoneId, 10) _, err = d.Client.httpPost("taobao.tbk.dg.optimus.material", params, &res) return } type vegas struct { Client *Tbk } func (d *dg) Vegas() *vegas { return &vegas{Client: d.Client} } // 淘礼金创建配置 type TljConf struct { AdzoneId int64 `json:"adzone_id"` // 广告位id ItemId string `json:"item_id"` // 商品id TotalNum int64 `json:"total_num"` // 总体个数 Name string `json:"name"` // 淘礼金名称 UserTotalWinNumLimit int `json:"user_total_win_num_limit"` // 单用户累计中奖次数上限 SecuritySwitch bool `json:"security_switch"` // 安全开关 PerFace string `json:"per_face"` // 单个淘礼金面额 SendStartTime string `json:"send_start_time"` // 发放开始时间 SendEndTime string `json:"send_end_time"` // 发放截止时间 UseEndTime string `json:"use_end_time"` // 使用结束日期 UseEndTimeMode int `json:"use_end_time_mode"` // 结束日期模式,1:相对时间 2:绝对时间 UseStartTime string `json:"use_start_time"` // 开始时间 } // 淘礼金创建完成响应 type TljResponse struct { Response struct { Result struct { Model struct { RightsId string `json:"rights_id"` SendUrl string `json:"send_url"` VegasCode string `json:"vegas_code"` } `json:"model"` MsgCode string `json:"msg_code"` MsgInfo string `json:"msg_info"` Success bool `json:"success"` } `json:"result"` } `json:"tbk_dg_vegas_tlj_create_response"` } /** * (淘宝客-推广者-淘礼金创建) * taobao.tbk.dg.vegas.tlj.create * @line https://open.taobao.com/api.htm?docId=40173&docType=2 */ func (v *vegas) TljCreate(conf *TljConf) (res *TljResponse, err error) { _, err = v.Client.httpPost("taobao.tbk.dg.vegas.tlj.create", v.Client.Struct2MapString(conf), &res) return } type TljReportResponse struct { Response struct { Result struct { MsgCode string `json:"msg_code"` MsgInfo string `json:"msg_info"` Success bool `json:"success"` Model struct { UnfreezeAmount float64 `json:"unfreeze_amount"` UnfreezeNum int64 `json:"unfreeze_num"` RefundAmount float64 `json:"refund_amount"` RefundNum int64 `json:"refund_num"` AlipayAmount float64 `json:"alipay_amount"` UseAmount float64 `json:"use_amount"` UseNum int64 `json:"use_num"` WinAmount float64 `json:"win_amount"` WinNum int64 `json:"win_num"` PerCommissionAmount float64 `json:"per_commission_amount"` FpRefundAmount float64 `json:"fp_refund_amount"` FpRefundNum int64 `json:"fp_refund_num"` } `json:"model"` } `json:"result"` } `json:"tbk_dg_vegas_tlj_instance_report_response"` } /** * ( 淘宝客-推广者-淘礼金发放及使用报表 ) * taobao.tbk.dg.vegas.tlj.instance.report * @line https://open.taobao.com/api.htm?docId=43317&docType=2&scopeId=15029 */ func (v *vegas) TljInstanceReport(RightsId string) (res *TljReportResponse, err error) { _, err = v.Client.httpPost("taobao.tbk.dg.vegas.tlj.instance.report", map[string]string{ "rights_id": RightsId, }, &res) return } type LbTljConf struct { SecurityLevel int `json:"security_level"` // 安全等级 UseStartTime string `json:"use_start_time"` // 使用开始日期 UseEndTime string `json:"use_end_time"` // 使用结束日期 UseEndTimeMode int `json:"use_end_time_mode"` // 结束日期的模式 1:相对时间 2:绝对时间 AcceptStartTime string `json:"accept_start_time"` // 裂变任务领取开始时间 AcceptEndTime string `json:"accept_end_time"` // 裂变任务领取截止时间 RightsPerFace string `json:"rights_per_face"` // 单个淘礼金面额,支持两位小数,单位元 SecuritySwitch bool `json:"security_switch"` // 安全开关 true启用 false不启用 UserTotalWinNumLimit int `json:"user_total_win_num_limit"` // 单用户累计中奖次数上限 Name string `json:"name"` // 淘礼金名称 RightsNum int `json:"rights_num"` // 淘礼金总个数 ItemId string `json:"item_id"` // 商品ID CampaignType string `json:"campaign_type"` TaskRightNum int `json:"task_right_num"` // 裂变淘礼金总个数 TaskRightsPerFace string `json:"task_rights_per_face"` // 裂变单个淘礼金面额 InviteNum int `json:"invite_num"` // 裂变淘礼金邀请人数 InviteTimeLimit int `json:"invite_time_limit"` // 裂变淘礼金邀请时长,单位分钟,最大120分钟 AdzoneId int `json:"adzone_id"` // 推广位ID } type LbTljResponse struct { Response struct { Result struct { Model struct { RightsID string `json:"rights_id"` // 直接领取淘礼金ID 即小额淘礼金 SendURL string `json:"send_url"` // 发放地址 TaskRightsID string `json:"task_rights_id"` // 裂变淘礼金ID 即大额淘礼金 TaskID string `json:"task_id"` // 裂变任务ID } `json:"model"` MsgCode string `json:"msg_code"` MsgInfo string `json:"msg_info"` Success bool `json:"success"` } `json:"result"` } `json:"tbk_dg_vegas_lbtlj_create_response"` } /** * ( 淘宝客-推广者-裂变淘礼金创建 ) * taobao.tbk.dg.vegas.lbtlj.create * @line https://
type OptimusMaterialResponse struct { Response struct { ResultList struct { MapData []struct { MaterialOptionalData
random_line_split
dg.go
TljInstanceReport(RightsId string) (res *TljReportResponse, err error) { _, err = v.Client.httpPost("taobao.tbk.dg.vegas.tlj.instance.report", map[string]string{ "rights_id": RightsId, }, &res) return } type LbTljConf struct { SecurityLevel int `json:"security_level"` // 安全等级 UseStartTime string `json:"use_start_time"` // 使用开始日期 UseEndTime string `json:"use_end_time"` // 使用结束日期 UseEndTimeMode int `json:"use_end_time_mode"` // 结束日期的模式 1:相对时间 2:绝对时间 AcceptStartTime string `json:"accept_start_time"` // 裂变任务领取开始时间 AcceptEndTime string `json:"accept_end_time"` // 裂变任务领取截止时间 RightsPerFace string `json:"rights_per_face"` // 单个淘礼金面额,支持两位小数,单位元 SecuritySwitch bool `json:"security_switch"` // 安全开关 true启用 false不启用 UserTotalWinNumLimit int `json:"user_total_win_num_limit"` // 单用户累计中奖次数上限 Name string `json:"name"` // 淘礼金名称 RightsNum int `json:"rights_num"` // 淘礼金总个数 ItemId string `json:"item_id"` // 商品ID CampaignType string `json:"campaign_type"` TaskRightNum int `json:"task_right_num"` // 裂变淘礼金总个数 TaskRightsPerFace string `json:"task_rights_per_face"` // 裂变单个淘礼金面额 InviteNum int `json:"invite_num"` // 裂变淘礼金邀请人数 InviteTimeLimit int `json:"invite_time_limit"` // 裂变淘礼金邀请时长,单位分钟,最大120分钟 AdzoneId int `json:"adzone_id"` // 推广位ID } type LbTljResponse struct { Response struct { Result struct { Model struct { RightsID string `json:"rights_id"` // 直接领取淘礼金ID 即小额淘礼金 SendURL string `json:"send_url"` // 发放地址 TaskRightsID string `json:"task_rights_id"` // 裂变淘礼金ID 即大额淘礼金 TaskID string `json:"task_id"` // 裂变任务ID } `json:"model"` MsgCode string `json:"msg_code"` MsgInfo string `json:"msg_info"` Success bool `json:"success"` } `json:"result"` } `json:"tbk_dg_vegas_lbtlj_create_response"` } /** * ( 淘宝客-推广者-裂变淘礼金创建 ) * taobao.tbk.dg.vegas.lbtlj.create * @line https://open.taobao.com/api.htm?docId=57710&docType=2&scopeId=23995 */ func (v *vegas) LbTljCreate(conf *LbTljConf) (res *LbTljResponse, err error) { _, err = v.Client.httpPost("taobao.tbk.dg.vegas.lbtlj.create", v.Client.Struct2MapString(conf), &res) return } type newUser struct { Client *Tbk } func (d *dg) NewUser() *newUser { return &newUser{Client: d.Client} } type OrderGetResponse struct { Response struct { Result struct { Data struct { Result []OrderGetData `json:"result"` PageNo int `json:"page_no"` PageSize int `json:"page_size"` HasNext bool `json:"has_next"` } `json:"data"` } `json:"result"` } `json:"tbk_dg_newuser_order_get_response"` } type OrderGetData struct { RegisterTime string `json:"register_time"` BindTime string `json:"bind_time"` BuyTime string `json:"buy_time"` Status int `json:"status"` Mobile string `json:"mobile"` OrderTkType int `json:"order_tk_type"` // 订单淘客类型:1.淘客订单 2.非淘客订单,仅淘宝天猫拉新适用 UnionId string `json:"union_id"` MemberId int `json:"member_id"` MemberNick string `json:"member_nick"` SiteId int64 `json:"site_id"` SiteName string `json:"site_name"` AdzoneId int64 `json:"adzone_id"` AdzoneName string `json:"adzone_name"` TbTradeParentId int64 `json:"tb_trade_parent_id"` AcceptTime string `json:"accept_time"` ReceiveTime string `json:"receive_time"` SuccessTime string `json:"success_time"` ActivityType string `json:"activity_type"` ActivityId string `json:"activity_id"` BizDate string `json:"biz_date"` BindCardTime string `json:"bind_card_time"` LoginTime string `json:"login_time"` IsCardSave int `json:"is_card_save"` // 银行卡绑定状态 1.绑定 0.未绑定 UseRightsTime string `json:"use_rights_time"` GetRightsTime string `json:"get_rights_time"` RelationId string `json:"relation_id"` Orders []OrderData `json:"orders"` } type OrderData struct { Commission string `json:"commission"` ConfirmReceiveTime string `json:"confirm_receive_time"` PayTime string `json:"pay_time"` OrderNo string `json:"order_no"` } /** * (淘宝客-推广者-新用户订单明细查询) * taobao.tbk.dg.newuser.order.get * @line https://open.taobao.com/api.htm?docId=33892&docType=2 */ func (n *newUser) OrderGet(activityId string, others ...map[string]string) (res *OrderGetResponse, err error) { params := make(map[string]string) if len(others) > 0 { params = others[0] } params["activity_id"] = activityId _, err = n.Client.httpPost("taobao.tbk.dg.newuser.order.get", params, &res) return } type OrderSumResponse struct { Response struct { Results struct { Data struct { PageNo int `json:"page_no"` PageSize int `json:"page_size"` HasNext bool `json:"has_next"` Results struct { Data []SumData `json:"data"` } `json:"results"` } `json:"data"` } `json:"results"` } `json:"tbk_dg_newuser_order_sum_response"` } type SumData struct { ActivityId string `json:"activity_id"` BizDate string `json:"biz_date"` RegUserCnt int64 `json:"reg_user_cnt"` LoginUserCnt int64 `json:"login_user_cnt"` AlipayUserCnt int64 `json:"alipay_user_cnt"` RcvValidUserCnt int64 `json:"rcv_valid_user_cnt"` RcvUserCnt int64 `json:"rcv_user_cnt"` AlipayUserCpaPreAmt string `json:"alipay_user_cpa_pre_amt"` BindBuyUserCpaPreAmt string `json:"bind_buy_user_cpa_pre_amt"` BindBuyValidUserCnt int64 `json:"bind_buy_valid_user_cnt"` BindCardValidUserCnt int64 `json:"bind_card_valid_user_cnt"` ReBuyValidUserCnt int64 `json:"re_buy_valid_user_cnt"` ValidNum int64 `json:"valid_num"` RelationId string `json:"relation_id"` } /** * ( 淘宝客-推广者-拉新活动对应数据查询 ) * taobao.tbk.dg.newuser.order.sum * @line https://open.taobao.com/api.htm?docId=36836&docType=2 */ func (n *newUser) OrderSum(activityId string, pageNo, pageSize int, others ...map[string]string) (res *OrderSumResponse, err error) { params := make(map[string]string) if len(others) > 0 { params = others[0] } params["activity_id"] = activityId params["page_no"] = strconv.Itoa(pageNo) params["page_size"] = strconv.Itoa(pageSize) _, err = n.Client.httpPost("taobao.tbk.dg.newuser.order.sum", params, &res) return }
conditional_block
path.rs
] pub fn get_file_name(full_path: &str) -> Option<&str> { match full_path.rfind('\\') { None => Some(full_path), // if no backslash, the whole string is the file name Some(idx) => if idx == full_path.chars().count() - 1 { None // last char is '\\', no file name } else { Some(&full_path[idx + 1..]) }, } } /// Extracts the full path, but the last part. /// /// # Examples /// /// ```no_run /// use winsafe::{self as w, prelude::*}; /// /// let p = w::path::get_path("C:\\Temp\\xx\\a.txt"); // C:\Temp\xx /// let q = w::path::get_path("C:\\Temp\\xx\\"); // C:\Temp\xx /// let r = w::path::get_path("C:\\Temp\\xx"); // C:\Temp" /// ``` #[must_use] pub fn get_path(full_path: &str) -> Option<&str> { full_path.rfind('\\') // if no backslash, the whole string is the file name, so no path .map(|idx| &full_path[0..idx]) } /// Tells whether the full path ends in one of the given extensions, /// case-insensitive. /// /// # Examples /// /// ```no_run /// use winsafe::{self as w, prelude::*}; /// /// println!("{}", /// w::path::has_extension("file.txt", &[".txt", ".bat"])); /// ``` #[must_use] pub fn has_extension(full_path: &str, extensions: &[impl AsRef<str>]) -> bool { let full_path_u = full_path.to_uppercase(); extensions.iter() .find(|ext| { let ext_u = ext.as_ref().to_uppercase(); full_path_u.ends_with(&ext_u) }) .is_some() } /// Returns true if the path is a directory. /// /// # Panics /// /// Panics if the path does not exist. #[must_use] pub fn is_directory(full_path: &str) -> bool { let flags = GetFileAttributes(full_path).unwrap(); flags.has(co::FILE_ATTRIBUTE::DIRECTORY) } /// Returns true if the path is hidden. /// /// # Panics /// /// Panics if the path does not exist. #[must_use] pub fn is_hidden(full_path: &str) -> bool { let flags = GetFileAttributes(full_path).unwrap(); flags.has(co::FILE_ATTRIBUTE::HIDDEN) } /// Replaces the extension by the given one. /// /// # Examples /// /// ```no_run /// use winsafe::{self as w, prelude::*}; /// /// let p = w::path::replace_extension( /// "C:\\Temp\\something.txt", ".sh"); // C:\Temp\something.sh /// ``` #[must_use] pub fn replace_extension(full_path: &str, new_extension: &str) -> String { if let Some(last) = full_path.chars().last() { if last == '\\' { // full_path is a directory, do nothing return rtrim_backslash(full_path).to_owned(); } } let new_has_dot = new_extension.chars().next() == Some('.'); match full_path.rfind('.') { None => format!("{}{}{}", // file name without extension, just append it full_path, if new_has_dot { "" } else { "." }, new_extension, ), Some(idx) => format!("{}{}{}", &full_path[0..idx], if new_has_dot { "" } else { "." }, new_extension, ), } } /// Replaces the file name by the given one. #[must_use] pub fn replace_file_name(full_path: &str, new_file: &str) -> String { match get_path(full_path) { None => new_file.to_owned(), Some(path) => format!("{}\\{}", path, new_file), } } /// Keeps the file name and replaces the path by the given one. /// /// # Examples /// /// ```no_run /// use winsafe::{self as w, prelude::*}; /// /// let p = w::path::replace_path( // C:\another\foo.txt /// "C:\\Temp\\foo.txt", /// "C:\\another", /// ); /// ``` #[must_use] pub fn replace_path(full_path: &str, new_path: &str) -> String { let file_name = get_file_name(full_path); format!("{}{}{}", rtrim_backslash(new_path), if file_name.is_some() { "\\" } else { "" }, file_name.unwrap_or("")) } /// Removes a trailing backslash, if any. /// /// # Examples /// /// ```no_run /// use winsafe::{self as w, prelude::*}; /// /// let p = w::path::rtrim_backslash("C:\\Temp\\"); // C:\Temp /// ``` #[must_use] pub fn rtrim_backslash(full_path: &str) -> &str { match full_path.chars().last() { None => full_path, // empty string Some(last_ch) => if last_ch == '\\' { let mut chars = full_path.chars(); chars.next_back(); // remove last char chars.as_str() } else { full_path // no trailing backslash }, } } /// Returns a `Vec` with each part of the full path. #[must_use] pub fn split_parts(full_path: &str) -> Vec<&str> { let no_bs = rtrim_backslash(full_path); no_bs.split('\\').collect() } //------------------------------------------------------------------------------ pub(in crate::kernel) struct DirListIter<'a> { dir_path: String, filter: Option<&'a str>, hfind: Option<FindCloseGuard>, wfd: WIN32_FIND_DATA, no_more: bool, } impl<'a> Iterator for DirListIter<'a> { type Item = SysResult<String>; fn next(&mut self) -> Option<Self::Item> { if self.no_more { return None; } let found = match &self.hfind { None => { // first pass let dir_final = match self.filter { None => format!("{}\\*", self.dir_path), Some(filter) => format!("{}\\{}", self.dir_path, filter), }; let found = match HFINDFILE::FindFirstFile(&dir_final, &mut self.wfd) { Err(e) => { self.no_more = true; // prevent further iterations return Some(Err(e)); }, Ok((hfind, found)) => { self.hfind = Some(hfind); // store our find handle found }, }; found }, Some(hfind) => { // subsequent passes match hfind.FindNextFile(&mut self.wfd) { Err(e) => { self.no_more = true; // prevent further iterations return Some(Err(e)); }, Ok(found) => found, } }, }; if found { let file_name = self.wfd.cFileName(); if file_name == "." || file_name == ".." { // skip these self.next() } else { Some(Ok(format!("{}\\{}", self.dir_path, self.wfd.cFileName()))) } } else { None } } } impl<'a> DirListIter<'a> { pub(in crate::kernel) fn new( dir_path: String, filter: Option<&'a str>, ) -> Self { Self { dir_path: rtrim_backslash(&dir_path).to_owned(), filter, hfind: None, wfd: WIN32_FIND_DATA::default(), no_more: false, } } } //------------------------------------------------------------------------------ pub(in crate::kernel) struct DirWalkIter<'a> { runner: DirListIter<'a>, subdir_runner: Option<Box<DirWalkIter<'a>>>, no_more: bool, } impl<'a> Iterator for DirWalkIter<'a> { type Item = SysResult<String>; fn next(&mut self) -> Option<Self::Item> { if self.no_more { return None; } match &mut self.subdir_runner { None => { let cur_file = self.runner.next(); match cur_file { None => None, Some(cur_file) => { match cur_file { Err(e) => { self.no_more = true; // prevent further iterations Some(Err(e)) }, Ok(cur_file) => { if is_directory(&cur_file) { self.subdir_runner = Some(Box::new(Self::new(cur_file))); // recursively self.next() } else { Some(Ok(cur_file)) } }, } }, } }, Some(subdir_runner) => { let inner_file = subdir_runner.next(); match inner_file { None => { // subdir_runner finished his work self.subdir_runner = None; self.next() }, Some(inner_file) => { Some(inner_file) }, }
}, } } }
random_line_split
path.rs
is a high-level abstraction over [`HFINDFILE`](crate::HFINDFILE) /// iteration functions. /// /// # Examples /// /// ```no_run /// use winsafe::{self as w, prelude::*}; /// /// // Ordinary for loop /// for file_path in w::path::dir_walk("C:\\Temp") { /// let file_path = file_path?; /// println!("{}", file_path); /// } /// /// // Closure with try_for_each /// w::path::dir_walk("C:\\Temp") /// .try_for_each(|file_path| { /// let file_path = file_path?; /// println!("{}", file_path); /// Ok(()) /// })?; /// /// // Collecting into a Vec /// let all = w::path::dir_walk("C:\\Temp") /// .collect::<w::SysResult<Vec<_>>>()?; /// /// // Transforming and collecting into a Vec /// let all = w::path::dir_walk("C:\\Temp") /// .map(|file_path| { /// let file_path = file_path?; /// Ok(format!("PATH: {}", file_path)) /// }) /// .collect::<w::SysResult<Vec<_>>>()?; /// # Ok::<_, winsafe::co::ERROR>(()) /// ``` #[must_use] pub fn dir_walk<'a>( dir_path: &'a str, ) -> impl Iterator<Item = SysResult<String>> + 'a { DirWalkIter::new(dir_path.to_owned()) } /// Returns the path of the current EXE file, without the EXE filename, and /// without a trailing backslash. /// /// In a debug build, the `target\debug` folders will be suppressed. #[cfg(debug_assertions)] #[must_use] pub fn exe_path() -> SysResult<String> { let dbg = HINSTANCE::NULL.GetModuleFileName()?; Ok( get_path( // target get_path( // debug get_path(&dbg).unwrap(), // exe name ).unwrap(), ).unwrap() .to_owned(), ) } /// Returns the path of the current EXE file, without the EXE filename, and /// without a trailing backslash. /// /// In a debug build, the `target\debug` folders will be suppressed. #[cfg(not(debug_assertions))] #[must_use] pub fn exe_path() -> SysResult<String> { Ok( get_path(&HINSTANCE::NULL.GetModuleFileName()?) .unwrap().to_owned(), ) } /// Returns true if the path exists. #[must_use] pub fn exists(full_path: &str) -> bool { GetFileAttributes(full_path).is_ok() } /// Extracts the file name from a full path, if any. /// /// # Examples /// /// ```no_run /// use winsafe::{self as w, prelude::*}; /// /// let f = w::path::get_file_name("C:\\Temp\\foo.txt"); // foo.txt /// ``` #[must_use] pub fn get_file_name(full_path: &str) -> Option<&str>
/// Extracts the full path, but the last part. /// /// # Examples /// /// ```no_run /// use winsafe::{self as w, prelude::*}; /// /// let p = w::path::get_path("C:\\Temp\\xx\\a.txt"); // C:\Temp\xx /// let q = w::path::get_path("C:\\Temp\\xx\\"); // C:\Temp\xx /// let r = w::path::get_path("C:\\Temp\\xx"); // C:\Temp" /// ``` #[must_use] pub fn get_path(full_path: &str) -> Option<&str> { full_path.rfind('\\') // if no backslash, the whole string is the file name, so no path .map(|idx| &full_path[0..idx]) } /// Tells whether the full path ends in one of the given extensions, /// case-insensitive. /// /// # Examples /// /// ```no_run /// use winsafe::{self as w, prelude::*}; /// /// println!("{}", /// w::path::has_extension("file.txt", &[".txt", ".bat"])); /// ``` #[must_use] pub fn has_extension(full_path: &str, extensions: &[impl AsRef<str>]) -> bool { let full_path_u = full_path.to_uppercase(); extensions.iter() .find(|ext| { let ext_u = ext.as_ref().to_uppercase(); full_path_u.ends_with(&ext_u) }) .is_some() } /// Returns true if the path is a directory. /// /// # Panics /// /// Panics if the path does not exist. #[must_use] pub fn is_directory(full_path: &str) -> bool { let flags = GetFileAttributes(full_path).unwrap(); flags.has(co::FILE_ATTRIBUTE::DIRECTORY) } /// Returns true if the path is hidden. /// /// # Panics /// /// Panics if the path does not exist. #[must_use] pub fn is_hidden(full_path: &str) -> bool { let flags = GetFileAttributes(full_path).unwrap(); flags.has(co::FILE_ATTRIBUTE::HIDDEN) } /// Replaces the extension by the given one. /// /// # Examples /// /// ```no_run /// use winsafe::{self as w, prelude::*}; /// /// let p = w::path::replace_extension( /// "C:\\Temp\\something.txt", ".sh"); // C:\Temp\something.sh /// ``` #[must_use] pub fn replace_extension(full_path: &str, new_extension: &str) -> String { if let Some(last) = full_path.chars().last() { if last == '\\' { // full_path is a directory, do nothing return rtrim_backslash(full_path).to_owned(); } } let new_has_dot = new_extension.chars().next() == Some('.'); match full_path.rfind('.') { None => format!("{}{}{}", // file name without extension, just append it full_path, if new_has_dot { "" } else { "." }, new_extension, ), Some(idx) => format!("{}{}{}", &full_path[0..idx], if new_has_dot { "" } else { "." }, new_extension, ), } } /// Replaces the file name by the given one. #[must_use] pub fn replace_file_name(full_path: &str, new_file: &str) -> String { match get_path(full_path) { None => new_file.to_owned(), Some(path) => format!("{}\\{}", path, new_file), } } /// Keeps the file name and replaces the path by the given one. /// /// # Examples /// /// ```no_run /// use winsafe::{self as w, prelude::*}; /// /// let p = w::path::replace_path( // C:\another\foo.txt /// "C:\\Temp\\foo.txt", /// "C:\\another", /// ); /// ``` #[must_use] pub fn replace_path(full_path: &str, new_path: &str) -> String { let file_name = get_file_name(full_path); format!("{}{}{}", rtrim_backslash(new_path), if file_name.is_some() { "\\" } else { "" }, file_name.unwrap_or("")) } /// Removes a trailing backslash, if any. /// /// # Examples /// /// ```no_run /// use winsafe::{self as w, prelude::*}; /// /// let p = w::path::rtrim_backslash("C:\\Temp\\"); // C:\Temp /// ``` #[must_use] pub fn rtrim_backslash(full_path: &str) -> &str { match full_path.chars().last() { None => full_path, // empty string Some(last_ch) => if last_ch == '\\' { let mut chars = full_path.chars(); chars.next_back(); // remove last char chars.as_str() } else { full_path // no trailing backslash }, } } /// Returns a `Vec` with each part of the full path. #[must_use] pub fn split_parts(full_path: &str) -> Vec<&str> { let no_bs = rtrim_backslash(full_path); no_bs.split('\\').collect() } //------------------------------------------------------------------------------ pub(in crate::kernel) struct DirListIter<'a> { dir_path: String, filter: Option<&'a str>, hfind: Option<FindCloseGuard>, wfd: WIN32_FIND_DATA, no_more: bool, } impl<'a> Iterator for DirListIter<'a> { type Item = SysResult<String>; fn next(&mut self) -> Option<Self::Item> { if self.no_more { return None; } let found = match &self.hfind { None => { // first pass let dir_final = match self.filter { None => format!("{}\\*", self.dir_path), Some(filter) => format!("{}\\{}", self.dir_path, filter), }; let found = match HFINDFILE::FindFirstFile
{ match full_path.rfind('\\') { None => Some(full_path), // if no backslash, the whole string is the file name Some(idx) => if idx == full_path.chars().count() - 1 { None // last char is '\\', no file name } else { Some(&full_path[idx + 1..]) }, } }
identifier_body
path.rs
get_path(&dbg).unwrap(), // exe name ).unwrap(), ).unwrap() .to_owned(), ) } /// Returns the path of the current EXE file, without the EXE filename, and /// without a trailing backslash. /// /// In a debug build, the `target\debug` folders will be suppressed. #[cfg(not(debug_assertions))] #[must_use] pub fn exe_path() -> SysResult<String> { Ok( get_path(&HINSTANCE::NULL.GetModuleFileName()?) .unwrap().to_owned(), ) } /// Returns true if the path exists. #[must_use] pub fn exists(full_path: &str) -> bool { GetFileAttributes(full_path).is_ok() } /// Extracts the file name from a full path, if any. /// /// # Examples /// /// ```no_run /// use winsafe::{self as w, prelude::*}; /// /// let f = w::path::get_file_name("C:\\Temp\\foo.txt"); // foo.txt /// ``` #[must_use] pub fn get_file_name(full_path: &str) -> Option<&str> { match full_path.rfind('\\') { None => Some(full_path), // if no backslash, the whole string is the file name Some(idx) => if idx == full_path.chars().count() - 1 { None // last char is '\\', no file name } else { Some(&full_path[idx + 1..]) }, } } /// Extracts the full path, but the last part. /// /// # Examples /// /// ```no_run /// use winsafe::{self as w, prelude::*}; /// /// let p = w::path::get_path("C:\\Temp\\xx\\a.txt"); // C:\Temp\xx /// let q = w::path::get_path("C:\\Temp\\xx\\"); // C:\Temp\xx /// let r = w::path::get_path("C:\\Temp\\xx"); // C:\Temp" /// ``` #[must_use] pub fn get_path(full_path: &str) -> Option<&str> { full_path.rfind('\\') // if no backslash, the whole string is the file name, so no path .map(|idx| &full_path[0..idx]) } /// Tells whether the full path ends in one of the given extensions, /// case-insensitive. /// /// # Examples /// /// ```no_run /// use winsafe::{self as w, prelude::*}; /// /// println!("{}", /// w::path::has_extension("file.txt", &[".txt", ".bat"])); /// ``` #[must_use] pub fn has_extension(full_path: &str, extensions: &[impl AsRef<str>]) -> bool { let full_path_u = full_path.to_uppercase(); extensions.iter() .find(|ext| { let ext_u = ext.as_ref().to_uppercase(); full_path_u.ends_with(&ext_u) }) .is_some() } /// Returns true if the path is a directory. /// /// # Panics /// /// Panics if the path does not exist. #[must_use] pub fn is_directory(full_path: &str) -> bool { let flags = GetFileAttributes(full_path).unwrap(); flags.has(co::FILE_ATTRIBUTE::DIRECTORY) } /// Returns true if the path is hidden. /// /// # Panics /// /// Panics if the path does not exist. #[must_use] pub fn is_hidden(full_path: &str) -> bool { let flags = GetFileAttributes(full_path).unwrap(); flags.has(co::FILE_ATTRIBUTE::HIDDEN) } /// Replaces the extension by the given one. /// /// # Examples /// /// ```no_run /// use winsafe::{self as w, prelude::*}; /// /// let p = w::path::replace_extension( /// "C:\\Temp\\something.txt", ".sh"); // C:\Temp\something.sh /// ``` #[must_use] pub fn replace_extension(full_path: &str, new_extension: &str) -> String { if let Some(last) = full_path.chars().last() { if last == '\\' { // full_path is a directory, do nothing return rtrim_backslash(full_path).to_owned(); } } let new_has_dot = new_extension.chars().next() == Some('.'); match full_path.rfind('.') { None => format!("{}{}{}", // file name without extension, just append it full_path, if new_has_dot { "" } else { "." }, new_extension, ), Some(idx) => format!("{}{}{}", &full_path[0..idx], if new_has_dot { "" } else { "." }, new_extension, ), } } /// Replaces the file name by the given one. #[must_use] pub fn replace_file_name(full_path: &str, new_file: &str) -> String { match get_path(full_path) { None => new_file.to_owned(), Some(path) => format!("{}\\{}", path, new_file), } } /// Keeps the file name and replaces the path by the given one. /// /// # Examples /// /// ```no_run /// use winsafe::{self as w, prelude::*}; /// /// let p = w::path::replace_path( // C:\another\foo.txt /// "C:\\Temp\\foo.txt", /// "C:\\another", /// ); /// ``` #[must_use] pub fn replace_path(full_path: &str, new_path: &str) -> String { let file_name = get_file_name(full_path); format!("{}{}{}", rtrim_backslash(new_path), if file_name.is_some() { "\\" } else { "" }, file_name.unwrap_or("")) } /// Removes a trailing backslash, if any. /// /// # Examples /// /// ```no_run /// use winsafe::{self as w, prelude::*}; /// /// let p = w::path::rtrim_backslash("C:\\Temp\\"); // C:\Temp /// ``` #[must_use] pub fn rtrim_backslash(full_path: &str) -> &str { match full_path.chars().last() { None => full_path, // empty string Some(last_ch) => if last_ch == '\\' { let mut chars = full_path.chars(); chars.next_back(); // remove last char chars.as_str() } else { full_path // no trailing backslash }, } } /// Returns a `Vec` with each part of the full path. #[must_use] pub fn split_parts(full_path: &str) -> Vec<&str> { let no_bs = rtrim_backslash(full_path); no_bs.split('\\').collect() } //------------------------------------------------------------------------------ pub(in crate::kernel) struct DirListIter<'a> { dir_path: String, filter: Option<&'a str>, hfind: Option<FindCloseGuard>, wfd: WIN32_FIND_DATA, no_more: bool, } impl<'a> Iterator for DirListIter<'a> { type Item = SysResult<String>; fn next(&mut self) -> Option<Self::Item> { if self.no_more { return None; } let found = match &self.hfind { None => { // first pass let dir_final = match self.filter { None => format!("{}\\*", self.dir_path), Some(filter) => format!("{}\\{}", self.dir_path, filter), }; let found = match HFINDFILE::FindFirstFile(&dir_final, &mut self.wfd) { Err(e) => { self.no_more = true; // prevent further iterations return Some(Err(e)); }, Ok((hfind, found)) => { self.hfind = Some(hfind); // store our find handle found }, }; found }, Some(hfind) => { // subsequent passes match hfind.FindNextFile(&mut self.wfd) { Err(e) => { self.no_more = true; // prevent further iterations return Some(Err(e)); }, Ok(found) => found, } }, }; if found { let file_name = self.wfd.cFileName(); if file_name == "." || file_name == ".." { // skip these self.next() } else { Some(Ok(format!("{}\\{}", self.dir_path, self.wfd.cFileName()))) } } else { None } } } impl<'a> DirListIter<'a> { pub(in crate::kernel) fn new( dir_path: String, filter: Option<&'a str>, ) -> Self { Self { dir_path: rtrim_backslash(&dir_path).to_owned(), filter, hfind: None, wfd: WIN32_FIND_DATA::default(), no_more: false, } } } //------------------------------------------------------------------------------ pub(in crate::kernel) struct DirWalkIter<'a> { runner: DirListIter<'a>, subdir_runner: Option<Box<DirWalkIter<'a>>>, no_more: bool, } impl<'a> Iterator for DirWalkIter<'a> { type Item = SysResult<String>; fn next(&mut self) -> Option<Self::Item> { if self.no_more
{ return None; }
conditional_block
path.rs
This is a high-level abstraction over [`HFINDFILE`](crate::HFINDFILE) /// iteration functions. /// /// # Examples /// /// ```no_run /// use winsafe::{self as w, prelude::*}; /// /// // Ordinary for loop /// for file_path in w::path::dir_walk("C:\\Temp") { /// let file_path = file_path?; /// println!("{}", file_path); /// } /// /// // Closure with try_for_each /// w::path::dir_walk("C:\\Temp") /// .try_for_each(|file_path| { /// let file_path = file_path?; /// println!("{}", file_path); /// Ok(()) /// })?; /// /// // Collecting into a Vec /// let all = w::path::dir_walk("C:\\Temp") /// .collect::<w::SysResult<Vec<_>>>()?; /// /// // Transforming and collecting into a Vec /// let all = w::path::dir_walk("C:\\Temp") /// .map(|file_path| { /// let file_path = file_path?; /// Ok(format!("PATH: {}", file_path)) /// }) /// .collect::<w::SysResult<Vec<_>>>()?; /// # Ok::<_, winsafe::co::ERROR>(()) /// ``` #[must_use] pub fn dir_walk<'a>( dir_path: &'a str, ) -> impl Iterator<Item = SysResult<String>> + 'a { DirWalkIter::new(dir_path.to_owned()) } /// Returns the path of the current EXE file, without the EXE filename, and /// without a trailing backslash. /// /// In a debug build, the `target\debug` folders will be suppressed. #[cfg(debug_assertions)] #[must_use] pub fn exe_path() -> SysResult<String> { let dbg = HINSTANCE::NULL.GetModuleFileName()?; Ok( get_path( // target get_path( // debug get_path(&dbg).unwrap(), // exe name ).unwrap(), ).unwrap() .to_owned(), ) } /// Returns the path of the current EXE file, without the EXE filename, and /// without a trailing backslash. /// /// In a debug build, the `target\debug` folders will be suppressed. #[cfg(not(debug_assertions))] #[must_use] pub fn exe_path() -> SysResult<String> { Ok( get_path(&HINSTANCE::NULL.GetModuleFileName()?) .unwrap().to_owned(), ) } /// Returns true if the path exists. #[must_use] pub fn exists(full_path: &str) -> bool { GetFileAttributes(full_path).is_ok() } /// Extracts the file name from a full path, if any. /// /// # Examples /// /// ```no_run /// use winsafe::{self as w, prelude::*}; /// /// let f = w::path::get_file_name("C:\\Temp\\foo.txt"); // foo.txt /// ``` #[must_use] pub fn get_file_name(full_path: &str) -> Option<&str> { match full_path.rfind('\\') { None => Some(full_path), // if no backslash, the whole string is the file name Some(idx) => if idx == full_path.chars().count() - 1 { None // last char is '\\', no file name } else { Some(&full_path[idx + 1..]) }, } } /// Extracts the full path, but the last part. /// /// # Examples /// /// ```no_run /// use winsafe::{self as w, prelude::*}; /// /// let p = w::path::get_path("C:\\Temp\\xx\\a.txt"); // C:\Temp\xx /// let q = w::path::get_path("C:\\Temp\\xx\\"); // C:\Temp\xx /// let r = w::path::get_path("C:\\Temp\\xx"); // C:\Temp" /// ``` #[must_use] pub fn get_path(full_path: &str) -> Option<&str> { full_path.rfind('\\') // if no backslash, the whole string is the file name, so no path .map(|idx| &full_path[0..idx]) } /// Tells whether the full path ends in one of the given extensions, /// case-insensitive. /// /// # Examples /// /// ```no_run /// use winsafe::{self as w, prelude::*}; /// /// println!("{}", /// w::path::has_extension("file.txt", &[".txt", ".bat"])); /// ``` #[must_use] pub fn has_extension(full_path: &str, extensions: &[impl AsRef<str>]) -> bool { let full_path_u = full_path.to_uppercase(); extensions.iter() .find(|ext| { let ext_u = ext.as_ref().to_uppercase(); full_path_u.ends_with(&ext_u) }) .is_some() } /// Returns true if the path is a directory. /// /// # Panics /// /// Panics if the path does not exist. #[must_use] pub fn is_directory(full_path: &str) -> bool { let flags = GetFileAttributes(full_path).unwrap(); flags.has(co::FILE_ATTRIBUTE::DIRECTORY) } /// Returns true if the path is hidden. /// /// # Panics /// /// Panics if the path does not exist. #[must_use] pub fn is_hidden(full_path: &str) -> bool { let flags = GetFileAttributes(full_path).unwrap(); flags.has(co::FILE_ATTRIBUTE::HIDDEN) } /// Replaces the extension by the given one. /// /// # Examples /// /// ```no_run /// use winsafe::{self as w, prelude::*}; /// /// let p = w::path::replace_extension( /// "C:\\Temp\\something.txt", ".sh"); // C:\Temp\something.sh /// ``` #[must_use] pub fn
(full_path: &str, new_extension: &str) -> String { if let Some(last) = full_path.chars().last() { if last == '\\' { // full_path is a directory, do nothing return rtrim_backslash(full_path).to_owned(); } } let new_has_dot = new_extension.chars().next() == Some('.'); match full_path.rfind('.') { None => format!("{}{}{}", // file name without extension, just append it full_path, if new_has_dot { "" } else { "." }, new_extension, ), Some(idx) => format!("{}{}{}", &full_path[0..idx], if new_has_dot { "" } else { "." }, new_extension, ), } } /// Replaces the file name by the given one. #[must_use] pub fn replace_file_name(full_path: &str, new_file: &str) -> String { match get_path(full_path) { None => new_file.to_owned(), Some(path) => format!("{}\\{}", path, new_file), } } /// Keeps the file name and replaces the path by the given one. /// /// # Examples /// /// ```no_run /// use winsafe::{self as w, prelude::*}; /// /// let p = w::path::replace_path( // C:\another\foo.txt /// "C:\\Temp\\foo.txt", /// "C:\\another", /// ); /// ``` #[must_use] pub fn replace_path(full_path: &str, new_path: &str) -> String { let file_name = get_file_name(full_path); format!("{}{}{}", rtrim_backslash(new_path), if file_name.is_some() { "\\" } else { "" }, file_name.unwrap_or("")) } /// Removes a trailing backslash, if any. /// /// # Examples /// /// ```no_run /// use winsafe::{self as w, prelude::*}; /// /// let p = w::path::rtrim_backslash("C:\\Temp\\"); // C:\Temp /// ``` #[must_use] pub fn rtrim_backslash(full_path: &str) -> &str { match full_path.chars().last() { None => full_path, // empty string Some(last_ch) => if last_ch == '\\' { let mut chars = full_path.chars(); chars.next_back(); // remove last char chars.as_str() } else { full_path // no trailing backslash }, } } /// Returns a `Vec` with each part of the full path. #[must_use] pub fn split_parts(full_path: &str) -> Vec<&str> { let no_bs = rtrim_backslash(full_path); no_bs.split('\\').collect() } //------------------------------------------------------------------------------ pub(in crate::kernel) struct DirListIter<'a> { dir_path: String, filter: Option<&'a str>, hfind: Option<FindCloseGuard>, wfd: WIN32_FIND_DATA, no_more: bool, } impl<'a> Iterator for DirListIter<'a> { type Item = SysResult<String>; fn next(&mut self) -> Option<Self::Item> { if self.no_more { return None; } let found = match &self.hfind { None => { // first pass let dir_final = match self.filter { None => format!("{}\\*", self.dir_path), Some(filter) => format!("{}\\{}", self.dir_path, filter), }; let found = match HFINDFILE::FindFirstFile
replace_extension
identifier_name
unitconverter.py
THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ class UnitNames(object): """ Unit Names is a namespace to hold units """ __slots__ = () # bits bits = "bits" kbits = "K" + bits kilobits = kbits mbits = "M" + bits megabits = mbits gbits = "G" + bits gigabits = gbits tbits = "T" + bits terabits = tbits pbits = "P" + bits petabits = pbits ebits = "E" + bits exabits = ebits zbits = "Z" + bits zettabits = zbits ybits = "Y" + bits yottabits = ybits # bytes bytes = "Bytes" kbytes = "K" + bytes kilobytes = kbytes mbytes = "M" + bytes megabytes = mbytes gbytes = "G" + bytes gigabytes = gbytes tbytes = "T" + bytes terabytes = tbytes pbytes = "P" + bytes petabytes = pbytes ebytes = "E" + bytes exabytes = ebytes zbytes = 'Z' + bytes zettabytes = zbytes ybytes = 'Y' + bytes yottabytes = ybytes class BinaryUnitNames(object): """ namespace for binary-unit names """ bits = UnitNames.bits bibits = 'bi' + bits kibibits = "ki" + bibits mebibits = 'me' + bibits gibibits = "gi" + bibits tebibits = "te" + bibits pebibits = "pe" + bibits exbibits = "ex" + bibits zebibits = "ze" + bibits yobibits = "yo" + bibits bytes = 'bytes' bibytes = 'bi' + bytes kibibytes = "ki" + bibytes mebibytes = "me" + bibytes gibibytes = 'gi' + bibytes tebibytes = 'te' + bibytes pebibytes = 'pe' + bibytes exbibytes = "ex" + bibytes zebibytes = "ze" + bibytes yobibytes = "yo" + bibytes # iperf base 2 iperf_bytes = UnitNames.bytes iperf_kibibytes = UnitNames.kbytes iperf_mebibytes = UnitNames.mbytes iperf_gibibytes = UnitNames.gbytes iperf_tebibytes = UnitNames.tbytes iperf_pebibytes = UnitNames.pbytes iperf_exbibytes = UnitNames.ebytes iperf_zebibytes = UnitNames.zbytes iperf_yobibytes = UnitNames.ybytes # end BinaryUnitNames IDENTITY = 1 ONE = 1.0 BYTE = 8 TO_BYTE = ONE/BYTE class BaseConverter(dict): """ A creator of unit-conversion dictionaries """ def __init__(self, to_units, kilo_prefix): """ base_converter constructor :param: - `to_units`: a list of the units to covert to (has to be half to-bits, half to-bytes) - `kilo_prefix`: kilo multiplier matching type of units """ self.to_units = to_units self.kilo_prefix = kilo_prefix self._prefix_conversions = None self._bits_to_bytes = None self._bytes_to_bits = None # split the to_units list for later self.bit_conversions = self.byte_conversions = len(to_units)//2 self.bit_units = to_units[:self.bit_conversions] self.byte_units = to_units[self.byte_conversions:] return @property def prefix_conversions(self): """ List of lists of prefix conversions """ if self._prefix_conversions is None: # start with list that assumes value has no prefix # this list is for 'bits' or 'bytes' # the values will be 1, 1/kilo, 1/mega, etc. start_list = [self.kilo_prefix**(-power) for power in range(self.bit_conversions)] self._prefix_conversions = self.conversions(conversion_factor=1, start_list=start_list) return self._prefix_conversions @property def bits_to_bytes(self): """ List of conversions for bits to bytes """ if self._bits_to_bytes is None: self._bits_to_bytes = self.conversions(conversion_factor=TO_BYTE) return self._bits_to_bytes @property def bytes_to_bits(self): """ list of conversions for bytes to bits """ if self._bytes_to_bits is None: self._bytes_to_bits = self.conversions(conversion_factor=BYTE) return self._bytes_to_bits def conversions(self, conversion_factor, start_list=None): """ Creates the converter-lists :param: - `conversion_factor`: multiplier for values (8 or 1/8, or 1) - `start_list`: if given, use to start the conversion-list :return: list of conversion_lists """ if start_list is None: # assume that prefix_conversions exists (not safe, but...) start_list = self.prefix_conversions[0] # start with byte_factor times the base conversions (1, 1/kilo, etc.) converter_list = [[conversion_factor * conversion for conversion in start_list]] for previous in range(self.bit_conversions - 1): # 'pop' last item from previous list # and prepend one higher-power conversion next_conversions = ([self.kilo_prefix**(previous+1) * conversion_factor] + converter_list[previous][:-1]) converter_list.append(next_conversions) return converter_list def build_conversions(self): """ builds the dictionary """ # from bits to bits or bytes for index, units in enumerate(self.bit_units): self[units] = dict(list(zip(self.to_units, self.prefix_conversions[index] + self.bits_to_bytes[index]))) # from bytes to bits or bytes for index, units in enumerate(self.byte_units): self[units] = dict(list(zip(self.to_units, self.bytes_to_bits[index] + self.prefix_conversions[index]))) return # end class BaseConverter bit_units = [UnitNames.bits, UnitNames.kbits, UnitNames.mbits, UnitNames.gbits, UnitNames.terabits, UnitNames.petabits, UnitNames.exabits, UnitNames.zettabits, UnitNames.yottabits] byte_units = [UnitNames.bytes, UnitNames.kbytes, UnitNames.mbytes, UnitNames.gbytes, UnitNames.terabytes, UnitNames.petabytes, UnitNames.exabytes, UnitNames.zettabytes, UnitNames.yottabytes] decimal_to_units = bit_units + byte_units KILO = 10**3 class UnitConverter(BaseConverter): """ The UnitConverter makes conversions based on a base-10 system """ def __init__(self): super(UnitConverter, self).__init__(to_units=decimal_to_units, kilo_prefix=KILO) self.build_conversions() return # end class UnitConverter DecimalUnitConverter = UnitConverter to_bits = [BinaryUnitNames.bits, BinaryUnitNames.kibibits, BinaryUnitNames.mebibits, BinaryUnitNames.gibibits, BinaryUnitNames.tebibits, BinaryUnitNames.pebibits, BinaryUnitNames.exbibits, BinaryUnitNames.zebibits, BinaryUnitNames.yobibits] to_bytes = [BinaryUnitNames.bytes, BinaryUnitNames.kibibytes, BinaryUnitNames.mebibytes, BinaryUnitNames.gibibytes, BinaryUnitNames.tebibytes, BinaryUnitNames.pebibytes, BinaryUnitNames.exbibytes, BinaryUnitNames.zebibytes, BinaryUnitNames.yobibytes] binary_to_units = to_bits + to_bytes KIBI = 2**10 class
(BaseConverter): """ The BinaryUnitconverter is a conversion lookup table for binary data Usage:: converted = old * UnitConverter[old units][new units] Use class UnitNames to get valid unit names """ def __init__(self): super(BinaryUnitconverter, self).__init__(to_units=binary_to_units, kilo_prefix=KIBI) self.build
BinaryUnitconverter
identifier_name
unitconverter.py
THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ class UnitNames(object): """ Unit Names is a namespace to hold units """ __slots__ = () # bits bits = "bits" kbits = "K" + bits kilobits = kbits mbits = "M" + bits megabits = mbits gbits = "G" + bits gigabits = gbits tbits = "T" + bits terabits = tbits pbits = "P" + bits petabits = pbits ebits = "E" + bits exabits = ebits zbits = "Z" + bits zettabits = zbits ybits = "Y" + bits yottabits = ybits # bytes bytes = "Bytes" kbytes = "K" + bytes kilobytes = kbytes mbytes = "M" + bytes megabytes = mbytes gbytes = "G" + bytes gigabytes = gbytes tbytes = "T" + bytes terabytes = tbytes pbytes = "P" + bytes petabytes = pbytes ebytes = "E" + bytes exabytes = ebytes zbytes = 'Z' + bytes zettabytes = zbytes ybytes = 'Y' + bytes yottabytes = ybytes class BinaryUnitNames(object): """ namespace for binary-unit names """ bits = UnitNames.bits bibits = 'bi' + bits kibibits = "ki" + bibits mebibits = 'me' + bibits gibibits = "gi" + bibits tebibits = "te" + bibits pebibits = "pe" + bibits exbibits = "ex" + bibits zebibits = "ze" + bibits yobibits = "yo" + bibits bytes = 'bytes' bibytes = 'bi' + bytes kibibytes = "ki" + bibytes mebibytes = "me" + bibytes gibibytes = 'gi' + bibytes tebibytes = 'te' + bibytes pebibytes = 'pe' + bibytes exbibytes = "ex" + bibytes zebibytes = "ze" + bibytes yobibytes = "yo" + bibytes # iperf base 2 iperf_bytes = UnitNames.bytes iperf_kibibytes = UnitNames.kbytes iperf_mebibytes = UnitNames.mbytes iperf_gibibytes = UnitNames.gbytes iperf_tebibytes = UnitNames.tbytes iperf_pebibytes = UnitNames.pbytes iperf_exbibytes = UnitNames.ebytes iperf_zebibytes = UnitNames.zbytes iperf_yobibytes = UnitNames.ybytes # end BinaryUnitNames IDENTITY = 1 ONE = 1.0 BYTE = 8 TO_BYTE = ONE/BYTE class BaseConverter(dict): """ A creator of unit-conversion dictionaries """ def __init__(self, to_units, kilo_prefix): """ base_converter constructor :param: - `to_units`: a list of the units to covert to (has to be half to-bits, half to-bytes) - `kilo_prefix`: kilo multiplier matching type of units """ self.to_units = to_units self.kilo_prefix = kilo_prefix self._prefix_conversions = None self._bits_to_bytes = None self._bytes_to_bits = None # split the to_units list for later self.bit_conversions = self.byte_conversions = len(to_units)//2 self.bit_units = to_units[:self.bit_conversions] self.byte_units = to_units[self.byte_conversions:] return @property def prefix_conversions(self):
@property def bits_to_bytes(self): """ List of conversions for bits to bytes """ if self._bits_to_bytes is None: self._bits_to_bytes = self.conversions(conversion_factor=TO_BYTE) return self._bits_to_bytes @property def bytes_to_bits(self): """ list of conversions for bytes to bits """ if self._bytes_to_bits is None: self._bytes_to_bits = self.conversions(conversion_factor=BYTE) return self._bytes_to_bits def conversions(self, conversion_factor, start_list=None): """ Creates the converter-lists :param: - `conversion_factor`: multiplier for values (8 or 1/8, or 1) - `start_list`: if given, use to start the conversion-list :return: list of conversion_lists """ if start_list is None: # assume that prefix_conversions exists (not safe, but...) start_list = self.prefix_conversions[0] # start with byte_factor times the base conversions (1, 1/kilo, etc.) converter_list = [[conversion_factor * conversion for conversion in start_list]] for previous in range(self.bit_conversions - 1): # 'pop' last item from previous list # and prepend one higher-power conversion next_conversions = ([self.kilo_prefix**(previous+1) * conversion_factor] + converter_list[previous][:-1]) converter_list.append(next_conversions) return converter_list def build_conversions(self): """ builds the dictionary """ # from bits to bits or bytes for index, units in enumerate(self.bit_units): self[units] = dict(list(zip(self.to_units, self.prefix_conversions[index] + self.bits_to_bytes[index]))) # from bytes to bits or bytes for index, units in enumerate(self.byte_units): self[units] = dict(list(zip(self.to_units, self.bytes_to_bits[index] + self.prefix_conversions[index]))) return # end class BaseConverter bit_units = [UnitNames.bits, UnitNames.kbits, UnitNames.mbits, UnitNames.gbits, UnitNames.terabits, UnitNames.petabits, UnitNames.exabits, UnitNames.zettabits, UnitNames.yottabits] byte_units = [UnitNames.bytes, UnitNames.kbytes, UnitNames.mbytes, UnitNames.gbytes, UnitNames.terabytes, UnitNames.petabytes, UnitNames.exabytes, UnitNames.zettabytes, UnitNames.yottabytes] decimal_to_units = bit_units + byte_units KILO = 10**3 class UnitConverter(BaseConverter): """ The UnitConverter makes conversions based on a base-10 system """ def __init__(self): super(UnitConverter, self).__init__(to_units=decimal_to_units, kilo_prefix=KILO) self.build_conversions() return # end class UnitConverter DecimalUnitConverter = UnitConverter to_bits = [BinaryUnitNames.bits, BinaryUnitNames.kibibits, BinaryUnitNames.mebibits, BinaryUnitNames.gibibits, BinaryUnitNames.tebibits, BinaryUnitNames.pebibits, BinaryUnitNames.exbibits, BinaryUnitNames.zebibits, BinaryUnitNames.yobibits] to_bytes = [BinaryUnitNames.bytes, BinaryUnitNames.kibibytes, BinaryUnitNames.mebibytes, BinaryUnitNames.gibibytes, BinaryUnitNames.tebibytes, BinaryUnitNames.pebibytes, BinaryUnitNames.exbibytes, BinaryUnitNames.zebibytes, BinaryUnitNames.yobibytes] binary_to_units = to_bits + to_bytes KIBI = 2**10 class BinaryUnitconverter(BaseConverter): """ The BinaryUnitconverter is a conversion lookup table for binary data Usage:: converted = old * UnitConverter[old units][new units] Use class UnitNames to get valid unit names """ def __init__(self): super(BinaryUnitconverter, self).__init__(to_units=binary_to_units, kilo_prefix=KIBI) self.build_con
""" List of lists of prefix conversions """ if self._prefix_conversions is None: # start with list that assumes value has no prefix # this list is for 'bits' or 'bytes' # the values will be 1, 1/kilo, 1/mega, etc. start_list = [self.kilo_prefix**(-power) for power in range(self.bit_conversions)] self._prefix_conversions = self.conversions(conversion_factor=1, start_list=start_list) return self._prefix_conversions
identifier_body
unitconverter.py
THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ class UnitNames(object): """ Unit Names is a namespace to hold units """ __slots__ = () # bits bits = "bits" kbits = "K" + bits kilobits = kbits mbits = "M" + bits megabits = mbits gbits = "G" + bits gigabits = gbits tbits = "T" + bits terabits = tbits pbits = "P" + bits petabits = pbits ebits = "E" + bits exabits = ebits zbits = "Z" + bits zettabits = zbits ybits = "Y" + bits yottabits = ybits # bytes bytes = "Bytes" kbytes = "K" + bytes kilobytes = kbytes mbytes = "M" + bytes megabytes = mbytes gbytes = "G" + bytes gigabytes = gbytes tbytes = "T" + bytes terabytes = tbytes pbytes = "P" + bytes petabytes = pbytes ebytes = "E" + bytes exabytes = ebytes zbytes = 'Z' + bytes zettabytes = zbytes ybytes = 'Y' + bytes yottabytes = ybytes class BinaryUnitNames(object): """ namespace for binary-unit names """ bits = UnitNames.bits bibits = 'bi' + bits kibibits = "ki" + bibits mebibits = 'me' + bibits gibibits = "gi" + bibits tebibits = "te" + bibits pebibits = "pe" + bibits exbibits = "ex" + bibits zebibits = "ze" + bibits yobibits = "yo" + bibits bytes = 'bytes' bibytes = 'bi' + bytes kibibytes = "ki" + bibytes mebibytes = "me" + bibytes gibibytes = 'gi' + bibytes tebibytes = 'te' + bibytes pebibytes = 'pe' + bibytes exbibytes = "ex" + bibytes zebibytes = "ze" + bibytes yobibytes = "yo" + bibytes # iperf base 2 iperf_bytes = UnitNames.bytes iperf_kibibytes = UnitNames.kbytes iperf_mebibytes = UnitNames.mbytes iperf_gibibytes = UnitNames.gbytes iperf_tebibytes = UnitNames.tbytes iperf_pebibytes = UnitNames.pbytes iperf_exbibytes = UnitNames.ebytes iperf_zebibytes = UnitNames.zbytes iperf_yobibytes = UnitNames.ybytes # end BinaryUnitNames IDENTITY = 1 ONE = 1.0 BYTE = 8 TO_BYTE = ONE/BYTE class BaseConverter(dict): """ A creator of unit-conversion dictionaries """ def __init__(self, to_units, kilo_prefix): """ base_converter constructor :param: - `to_units`: a list of the units to covert to (has to be half to-bits, half to-bytes) - `kilo_prefix`: kilo multiplier matching type of units """ self.to_units = to_units self.kilo_prefix = kilo_prefix self._prefix_conversions = None self._bits_to_bytes = None self._bytes_to_bits = None # split the to_units list for later self.bit_conversions = self.byte_conversions = len(to_units)//2 self.bit_units = to_units[:self.bit_conversions] self.byte_units = to_units[self.byte_conversions:] return @property def prefix_conversions(self): """ List of lists of prefix conversions """ if self._prefix_conversions is None: # start with list that assumes value has no prefix # this list is for 'bits' or 'bytes' # the values will be 1, 1/kilo, 1/mega, etc. start_list = [self.kilo_prefix**(-power) for power in range(self.bit_conversions)] self._prefix_conversions = self.conversions(conversion_factor=1, start_list=start_list) return self._prefix_conversions @property def bits_to_bytes(self): """ List of conversions for bits to bytes """ if self._bits_to_bytes is None: self._bits_to_bytes = self.conversions(conversion_factor=TO_BYTE) return self._bits_to_bytes @property def bytes_to_bits(self): """ list of conversions for bytes to bits """ if self._bytes_to_bits is None: self._bytes_to_bits = self.conversions(conversion_factor=BYTE) return self._bytes_to_bits def conversions(self, conversion_factor, start_list=None): """ Creates the converter-lists :param: - `conversion_factor`: multiplier for values (8 or 1/8, or 1) - `start_list`: if given, use to start the conversion-list :return: list of conversion_lists """ if start_list is None: # assume that prefix_conversions exists (not safe, but...) start_list = self.prefix_conversions[0] # start with byte_factor times the base conversions (1, 1/kilo, etc.) converter_list = [[conversion_factor * conversion for conversion in start_list]] for previous in range(self.bit_conversions - 1): # 'pop' last item from previous list # and prepend one higher-power conversion next_conversions = ([self.kilo_prefix**(previous+1) * conversion_factor] + converter_list[previous][:-1]) converter_list.append(next_conversions) return converter_list def build_conversions(self): """ builds the dictionary """ # from bits to bits or bytes for index, units in enumerate(self.bit_units):
# from bytes to bits or bytes for index, units in enumerate(self.byte_units): self[units] = dict(list(zip(self.to_units, self.bytes_to_bits[index] + self.prefix_conversions[index]))) return # end class BaseConverter bit_units = [UnitNames.bits, UnitNames.kbits, UnitNames.mbits, UnitNames.gbits, UnitNames.terabits, UnitNames.petabits, UnitNames.exabits, UnitNames.zettabits, UnitNames.yottabits] byte_units = [UnitNames.bytes, UnitNames.kbytes, UnitNames.mbytes, UnitNames.gbytes, UnitNames.terabytes, UnitNames.petabytes, UnitNames.exabytes, UnitNames.zettabytes, UnitNames.yottabytes] decimal_to_units = bit_units + byte_units KILO = 10**3 class UnitConverter(BaseConverter): """ The UnitConverter makes conversions based on a base-10 system """ def __init__(self): super(UnitConverter, self).__init__(to_units=decimal_to_units, kilo_prefix=KILO) self.build_conversions() return # end class UnitConverter DecimalUnitConverter = UnitConverter to_bits = [BinaryUnitNames.bits, BinaryUnitNames.kibibits, BinaryUnitNames.mebibits, BinaryUnitNames.gibibits, BinaryUnitNames.tebibits, BinaryUnitNames.pebibits, BinaryUnitNames.exbibits, BinaryUnitNames.zebibits, BinaryUnitNames.yobibits] to_bytes = [BinaryUnitNames.bytes, BinaryUnitNames.kibibytes, BinaryUnitNames.mebibytes, BinaryUnitNames.gibibytes, BinaryUnitNames.tebibytes, BinaryUnitNames.pebibytes, BinaryUnitNames.exbibytes, BinaryUnitNames.zebibytes, BinaryUnitNames.yobibytes] binary_to_units = to_bits + to_bytes KIBI = 2**10 class BinaryUnitconverter(BaseConverter): """ The BinaryUnitconverter is a conversion lookup table for binary data Usage:: converted = old * UnitConverter[old units][new units] Use class UnitNames to get valid unit names """ def __init__(self): super(BinaryUnitconverter, self).__init__(to_units=binary_to_units, kilo_prefix=KIBI) self.build_con
self[units] = dict(list(zip(self.to_units, self.prefix_conversions[index] + self.bits_to_bytes[index])))
conditional_block
unitconverter.py
THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ class UnitNames(object): """ Unit Names is a namespace to hold units """ __slots__ = () # bits bits = "bits" kbits = "K" + bits kilobits = kbits mbits = "M" + bits megabits = mbits gbits = "G" + bits gigabits = gbits tbits = "T" + bits terabits = tbits pbits = "P" + bits petabits = pbits ebits = "E" + bits exabits = ebits zbits = "Z" + bits zettabits = zbits ybits = "Y" + bits yottabits = ybits # bytes bytes = "Bytes" kbytes = "K" + bytes kilobytes = kbytes mbytes = "M" + bytes megabytes = mbytes gbytes = "G" + bytes gigabytes = gbytes tbytes = "T" + bytes terabytes = tbytes pbytes = "P" + bytes petabytes = pbytes ebytes = "E" + bytes exabytes = ebytes zbytes = 'Z' + bytes zettabytes = zbytes ybytes = 'Y' + bytes yottabytes = ybytes class BinaryUnitNames(object): """ namespace for binary-unit names """ bits = UnitNames.bits bibits = 'bi' + bits kibibits = "ki" + bibits mebibits = 'me' + bibits gibibits = "gi" + bibits tebibits = "te" + bibits pebibits = "pe" + bibits exbibits = "ex" + bibits zebibits = "ze" + bibits yobibits = "yo" + bibits bytes = 'bytes' bibytes = 'bi' + bytes kibibytes = "ki" + bibytes mebibytes = "me" + bibytes gibibytes = 'gi' + bibytes tebibytes = 'te' + bibytes pebibytes = 'pe' + bibytes exbibytes = "ex" + bibytes zebibytes = "ze" + bibytes yobibytes = "yo" + bibytes # iperf base 2 iperf_bytes = UnitNames.bytes iperf_kibibytes = UnitNames.kbytes iperf_mebibytes = UnitNames.mbytes iperf_gibibytes = UnitNames.gbytes iperf_tebibytes = UnitNames.tbytes iperf_pebibytes = UnitNames.pbytes iperf_exbibytes = UnitNames.ebytes iperf_zebibytes = UnitNames.zbytes iperf_yobibytes = UnitNames.ybytes # end BinaryUnitNames IDENTITY = 1 ONE = 1.0 BYTE = 8 TO_BYTE = ONE/BYTE class BaseConverter(dict): """ A creator of unit-conversion dictionaries """ def __init__(self, to_units, kilo_prefix): """ base_converter constructor :param: - `to_units`: a list of the units to covert to (has to be half to-bits, half to-bytes) - `kilo_prefix`: kilo multiplier matching type of units """ self.to_units = to_units self.kilo_prefix = kilo_prefix self._prefix_conversions = None self._bits_to_bytes = None self._bytes_to_bits = None # split the to_units list for later self.bit_conversions = self.byte_conversions = len(to_units)//2 self.bit_units = to_units[:self.bit_conversions] self.byte_units = to_units[self.byte_conversions:] return @property def prefix_conversions(self): """ List of lists of prefix conversions """ if self._prefix_conversions is None: # start with list that assumes value has no prefix # this list is for 'bits' or 'bytes' # the values will be 1, 1/kilo, 1/mega, etc. start_list = [self.kilo_prefix**(-power) for power in range(self.bit_conversions)] self._prefix_conversions = self.conversions(conversion_factor=1, start_list=start_list) return self._prefix_conversions @property def bits_to_bytes(self): """ List of conversions for bits to bytes """ if self._bits_to_bytes is None: self._bits_to_bytes = self.conversions(conversion_factor=TO_BYTE) return self._bits_to_bytes @property def bytes_to_bits(self): """ list of conversions for bytes to bits """ if self._bytes_to_bits is None: self._bytes_to_bits = self.conversions(conversion_factor=BYTE) return self._bytes_to_bits def conversions(self, conversion_factor, start_list=None): """ Creates the converter-lists :param: - `conversion_factor`: multiplier for values (8 or 1/8, or 1) - `start_list`: if given, use to start the conversion-list :return: list of conversion_lists """ if start_list is None: # assume that prefix_conversions exists (not safe, but...) start_list = self.prefix_conversions[0] # start with byte_factor times the base conversions (1, 1/kilo, etc.) converter_list = [[conversion_factor * conversion for conversion in start_list]] for previous in range(self.bit_conversions - 1): # 'pop' last item from previous list # and prepend one higher-power conversion next_conversions = ([self.kilo_prefix**(previous+1) * conversion_factor] + converter_list[previous][:-1]) converter_list.append(next_conversions)
def build_conversions(self): """ builds the dictionary """ # from bits to bits or bytes for index, units in enumerate(self.bit_units): self[units] = dict(list(zip(self.to_units, self.prefix_conversions[index] + self.bits_to_bytes[index]))) # from bytes to bits or bytes for index, units in enumerate(self.byte_units): self[units] = dict(list(zip(self.to_units, self.bytes_to_bits[index] + self.prefix_conversions[index]))) return # end class BaseConverter bit_units = [UnitNames.bits, UnitNames.kbits, UnitNames.mbits, UnitNames.gbits, UnitNames.terabits, UnitNames.petabits, UnitNames.exabits, UnitNames.zettabits, UnitNames.yottabits] byte_units = [UnitNames.bytes, UnitNames.kbytes, UnitNames.mbytes, UnitNames.gbytes, UnitNames.terabytes, UnitNames.petabytes, UnitNames.exabytes, UnitNames.zettabytes, UnitNames.yottabytes] decimal_to_units = bit_units + byte_units KILO = 10**3 class UnitConverter(BaseConverter): """ The UnitConverter makes conversions based on a base-10 system """ def __init__(self): super(UnitConverter, self).__init__(to_units=decimal_to_units, kilo_prefix=KILO) self.build_conversions() return # end class UnitConverter DecimalUnitConverter = UnitConverter to_bits = [BinaryUnitNames.bits, BinaryUnitNames.kibibits, BinaryUnitNames.mebibits, BinaryUnitNames.gibibits, BinaryUnitNames.tebibits, BinaryUnitNames.pebibits, BinaryUnitNames.exbibits, BinaryUnitNames.zebibits, BinaryUnitNames.yobibits] to_bytes = [BinaryUnitNames.bytes, BinaryUnitNames.kibibytes, BinaryUnitNames.mebibytes, BinaryUnitNames.gibibytes, BinaryUnitNames.tebibytes, BinaryUnitNames.pebibytes, BinaryUnitNames.exbibytes, BinaryUnitNames.zebibytes, BinaryUnitNames.yobibytes] binary_to_units = to_bits + to_bytes KIBI = 2**10 class BinaryUnitconverter(BaseConverter): """ The BinaryUnitconverter is a conversion lookup table for binary data Usage:: converted = old * UnitConverter[old units][new units] Use class UnitNames to get valid unit names """ def __init__(self): super(BinaryUnitconverter, self).__init__(to_units=binary_to_units, kilo_prefix=KIBI) self.build_con
return converter_list
random_line_split
timeloop.py
, OR TORT # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. import functools import inspect import os import subprocess import sys import timeit import argparse import copy import re import libconf import yaml from common import * # Output file names. out_prefix = "timeloop-mapper." log_file_name = out_prefix + "log" stats_file_name = out_prefix + "stats.txt" xml_file_name = out_prefix + "map+stats.xml" map_txt_file_name = out_prefix + "map.txt" map_cfg_file_name = out_prefix + "map.cfg" map_cpp_file_name = out_prefix + "map.cpp" output_file_names = [log_file_name, stats_file_name, xml_file_name, map_txt_file_name, map_cfg_file_name, map_cpp_file_name] # dimension conversion that maps a WU problem to FW problem wu2fw = {'P': 'R', 'Q': 'S', 'R': 'P', 'S': 'Q', 'C': 'K', 'K': 'N', 'N': 'C'} def prod(l): return functools.reduce(lambda x, y: x * y, l) def rewrite_workload_bounds(src, dst, workload_bounds, model, layer, batchsize, dataflow, phase, terminate, threads, synthetic, sparsity, save, replication, array_width, glb_scaling, dense): # backward_padding w, h, c, n, k, s, r, wpad, hpad, wstride, hstride = workload_bounds n = batchsize q = int((w - s + 2 * wpad) / wstride) + 1 p = int((h - r + 2 * hpad) / hstride) + 1 wu_equiv = k != 'D' and phase == 'wu' env_list = {} if not wu_equiv: print('Workload Dimensions:') print(' W =', w) print(' H =', h) print(' C =', c) print(' K =', k) print(' S =', s) print(' R =', r)
print(' H-pad =', hpad) print(' W-stride =', wstride) print(' H-stride =', hstride) print() else: print('Equivalence Test: can we convert WU problem to FW and use cnn-layer.cfg? (at least in the dense case?)') print('Workload Dimensions:') print(' W =', w) print(' H =', h) print(f' C <- N {n}') print(f' K <- C {c}') print(f' S <- Q {q}') print(f' R <- P {p}') print(f' P <- R {r}') print(f' Q <- S {s}') print(f' N <- K {k}') print(' W-pad =', wpad) print(' H-pad =', hpad) print(' W-stride =', wstride) print(' H-stride =', hstride) print() env_list['TIMELOOP_EQUIVLENT_WU'] = 'True' with open(src, "r") as f: if "cfg" in src: config = libconf.load(f) elif "yaml" in src: config = yaml.load(f, Loader=yaml.SafeLoader) config['problem']['shape'] = shapes[phase] if wu_equiv: config['problem']['shape'] = shapes['fw'] if k == 'D': depthwise = True adapt_depthwise_config(config) else: depthwise = False config['problem']['shape'] += '.yaml' if wu_equiv: dataflow = convert_dataflow(dataflow) if phase == 'wu': remove_block_constraint(config) if depthwise: if dataflow == 'CK': dataflow = 'CN' dataflow = dataflow.replace('K', 'C') rewrite_dataflow(config, dataflow, replication, array_width) rewrite_mesh(config, array_width) if glb_scaling: rewrite_glb_size(config, array_width) if not wu_equiv: config['problem']['R'] = r config['problem']['S'] = s config['problem']['P'] = p config['problem']['Q'] = q config['problem']['C'] = c if not depthwise: config['problem']['K'] = k config['problem']['N'] = n else: config['problem']['R'] = p config['problem']['S'] = q config['problem']['P'] = r config['problem']['Q'] = s config['problem']['C'] = n config['problem']['K'] = c config['problem']['N'] = k config['problem']['Wstride'] = wstride config['problem']['Hstride'] = hstride config['problem']['Wdilation'] = 1 config['problem']['Hdilation'] = 1 config['mapper']['model-name'] = model config['mapper']['layer-name'] = layer if terminate is not None: config['mapper']['victory-condition'] = terminate if threads is not None: config['mapper']['num-threads'] = threads # rewrite synthetic mask configuration if not synthetic: try: config['mapper'].pop('mask-synthetic') except KeyError: pass else: config['mapper']['mask-synthetic'] = {} if sparsity is not None: config['mapper']['mask-synthetic']['target-sparsity'] = sparsity if save is not None: config['mapper']['mask-synthetic']['synthetic-mask-path'] = save if dense: opt_metrics = [] for opt in config['mapper']['optimization-metrics']: opt_metrics.append(opt.split('-')[-1]) config['mapper']['optimization-metrics'] = opt_metrics with open(dst, "w") as f: if "cfg" in src: f.write(libconf.dumps(config)) elif "yaml" in src: f.write(yaml.dump(config)) return env_list def convert_dataflow(dataflow): pre_convert_dataflow = copy.copy(dataflow) converted_dataflow = [] converted_dataflow.append(wu2fw[pre_convert_dataflow[0]]) converted_dataflow.append(wu2fw[pre_convert_dataflow[1]]) converted = '' converted = converted.join(converted_dataflow) print(f'convert from {dataflow} to {converted}') return converted def remove_block_constraint(config): # or possibily remove for constraint in config['mapspace']['constraints']: if constraint['type'] == 'temporal' and constraint['target'] == 'RegFile': try: constraint.pop('factors') except KeyError: pass def rewrite_dataflow(config, dataflow, replication, array_width): # loop through constaints, and make sure there is only 1 spatial type constraint # dingqing FIXME: not general for more spatial level architecture config num_spatial = 0 for constraint in config['mapspace']['constraints']: if num_spatial > 1: raise Exception("More than one spatial level! Check the config and the scripts.") if constraint['type'] == 'spatial': num_spatial += 1 # determine if it is possible to replicate possible2replicate = replication and (not config['problem'][dataflow[0]] > array_width / 2 or not config['problem'][dataflow[1]] > array_width / 2) print('possible2replicate?', possible2replicate) factors = constraint['factors'].split(' ') new_factor = [] for factor in factors: if factor[0] in dataflow: # look at problem size new_factor.append(factor[0] + f'{array_width}') elif not possible2replicate: new_factor.append(factor[0] + '1') constraint['factors'] = ' '.join(new_factor) # rewrite permutation # emmmm ugly non_spatial_dims = constraint['permutation'].replace(dataflow[0], '').replace(dataflow[1], '') constraint['permutation'] = dataflow[0] + non_spatial_dims + dataflow[1] def rewrite_mesh(config, array_width): # honestly, the structure is kinda unnatural... pe_subtree = config['architecture']['subtree'][0]['subtree'][0] # FIXME: this is not generic enough pe_name = pe_subtree['name'] num_pe_prev = re.findall(r'\d+', pe_name)[-1] num_pe_new = array_width * array_width - 1 pe_subtree['name'] = pe
print(' P =', p) print(' Q =', q) print(' N =', n) print(' W-pad =', wpad)
random_line_split
timeloop.py
, OR TORT # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. import functools import inspect import os import subprocess import sys import timeit import argparse import copy import re import libconf import yaml from common import * # Output file names. out_prefix = "timeloop-mapper." log_file_name = out_prefix + "log" stats_file_name = out_prefix + "stats.txt" xml_file_name = out_prefix + "map+stats.xml" map_txt_file_name = out_prefix + "map.txt" map_cfg_file_name = out_prefix + "map.cfg" map_cpp_file_name = out_prefix + "map.cpp" output_file_names = [log_file_name, stats_file_name, xml_file_name, map_txt_file_name, map_cfg_file_name, map_cpp_file_name] # dimension conversion that maps a WU problem to FW problem wu2fw = {'P': 'R', 'Q': 'S', 'R': 'P', 'S': 'Q', 'C': 'K', 'K': 'N', 'N': 'C'} def prod(l): return functools.reduce(lambda x, y: x * y, l) def rewrite_workload_bounds(src, dst, workload_bounds, model, layer, batchsize, dataflow, phase, terminate, threads, synthetic, sparsity, save, replication, array_width, glb_scaling, dense): # backward_padding w, h, c, n, k, s, r, wpad, hpad, wstride, hstride = workload_bounds n = batchsize q = int((w - s + 2 * wpad) / wstride) + 1 p = int((h - r + 2 * hpad) / hstride) + 1 wu_equiv = k != 'D' and phase == 'wu' env_list = {} if not wu_equiv: print('Workload Dimensions:') print(' W =', w) print(' H =', h) print(' C =', c) print(' K =', k) print(' S =', s) print(' R =', r) print(' P =', p) print(' Q =', q) print(' N =', n) print(' W-pad =', wpad) print(' H-pad =', hpad) print(' W-stride =', wstride) print(' H-stride =', hstride) print() else: print('Equivalence Test: can we convert WU problem to FW and use cnn-layer.cfg? (at least in the dense case?)') print('Workload Dimensions:') print(' W =', w) print(' H =', h) print(f' C <- N {n}') print(f' K <- C {c}') print(f' S <- Q {q}') print(f' R <- P {p}') print(f' P <- R {r}') print(f' Q <- S {s}') print(f' N <- K {k}') print(' W-pad =', wpad) print(' H-pad =', hpad) print(' W-stride =', wstride) print(' H-stride =', hstride) print() env_list['TIMELOOP_EQUIVLENT_WU'] = 'True' with open(src, "r") as f: if "cfg" in src: config = libconf.load(f) elif "yaml" in src: config = yaml.load(f, Loader=yaml.SafeLoader) config['problem']['shape'] = shapes[phase] if wu_equiv: config['problem']['shape'] = shapes['fw'] if k == 'D': depthwise = True adapt_depthwise_config(config) else: depthwise = False config['problem']['shape'] += '.yaml' if wu_equiv: dataflow = convert_dataflow(dataflow) if phase == 'wu': remove_block_constraint(config) if depthwise: if dataflow == 'CK': dataflow = 'CN' dataflow = dataflow.replace('K', 'C') rewrite_dataflow(config, dataflow, replication, array_width) rewrite_mesh(config, array_width) if glb_scaling: rewrite_glb_size(config, array_width) if not wu_equiv:
else: config['problem']['R'] = p config['problem']['S'] = q config['problem']['P'] = r config['problem']['Q'] = s config['problem']['C'] = n config['problem']['K'] = c config['problem']['N'] = k config['problem']['Wstride'] = wstride config['problem']['Hstride'] = hstride config['problem']['Wdilation'] = 1 config['problem']['Hdilation'] = 1 config['mapper']['model-name'] = model config['mapper']['layer-name'] = layer if terminate is not None: config['mapper']['victory-condition'] = terminate if threads is not None: config['mapper']['num-threads'] = threads # rewrite synthetic mask configuration if not synthetic: try: config['mapper'].pop('mask-synthetic') except KeyError: pass else: config['mapper']['mask-synthetic'] = {} if sparsity is not None: config['mapper']['mask-synthetic']['target-sparsity'] = sparsity if save is not None: config['mapper']['mask-synthetic']['synthetic-mask-path'] = save if dense: opt_metrics = [] for opt in config['mapper']['optimization-metrics']: opt_metrics.append(opt.split('-')[-1]) config['mapper']['optimization-metrics'] = opt_metrics with open(dst, "w") as f: if "cfg" in src: f.write(libconf.dumps(config)) elif "yaml" in src: f.write(yaml.dump(config)) return env_list def convert_dataflow(dataflow): pre_convert_dataflow = copy.copy(dataflow) converted_dataflow = [] converted_dataflow.append(wu2fw[pre_convert_dataflow[0]]) converted_dataflow.append(wu2fw[pre_convert_dataflow[1]]) converted = '' converted = converted.join(converted_dataflow) print(f'convert from {dataflow} to {converted}') return converted def remove_block_constraint(config): # or possibily remove for constraint in config['mapspace']['constraints']: if constraint['type'] == 'temporal' and constraint['target'] == 'RegFile': try: constraint.pop('factors') except KeyError: pass def rewrite_dataflow(config, dataflow, replication, array_width): # loop through constaints, and make sure there is only 1 spatial type constraint # dingqing FIXME: not general for more spatial level architecture config num_spatial = 0 for constraint in config['mapspace']['constraints']: if num_spatial > 1: raise Exception("More than one spatial level! Check the config and the scripts.") if constraint['type'] == 'spatial': num_spatial += 1 # determine if it is possible to replicate possible2replicate = replication and (not config['problem'][dataflow[0]] > array_width / 2 or not config['problem'][dataflow[1]] > array_width / 2) print('possible2replicate?', possible2replicate) factors = constraint['factors'].split(' ') new_factor = [] for factor in factors: if factor[0] in dataflow: # look at problem size new_factor.append(factor[0] + f'{array_width}') elif not possible2replicate: new_factor.append(factor[0] + '1') constraint['factors'] = ' '.join(new_factor) # rewrite permutation # emmmm ugly non_spatial_dims = constraint['permutation'].replace(dataflow[0], '').replace(dataflow[1], '') constraint['permutation'] = dataflow[0] + non_spatial_dims + dataflow[1] def rewrite_mesh(config, array_width): # honestly, the structure is kinda unnatural... pe_subtree = config['architecture']['subtree'][0]['subtree'][0] # FIXME: this is not generic enough pe_name = pe_subtree['name'] num_pe_prev = re.findall(r'\d+', pe_name)[-1] num_pe_new = array_width * array_width - 1 pe_subtree['name'] = pe
config['problem']['R'] = r config['problem']['S'] = s config['problem']['P'] = p config['problem']['Q'] = q config['problem']['C'] = c if not depthwise: config['problem']['K'] = k config['problem']['N'] = n
conditional_block
timeloop.py
=', hstride) print() else: print('Equivalence Test: can we convert WU problem to FW and use cnn-layer.cfg? (at least in the dense case?)') print('Workload Dimensions:') print(' W =', w) print(' H =', h) print(f' C <- N {n}') print(f' K <- C {c}') print(f' S <- Q {q}') print(f' R <- P {p}') print(f' P <- R {r}') print(f' Q <- S {s}') print(f' N <- K {k}') print(' W-pad =', wpad) print(' H-pad =', hpad) print(' W-stride =', wstride) print(' H-stride =', hstride) print() env_list['TIMELOOP_EQUIVLENT_WU'] = 'True' with open(src, "r") as f: if "cfg" in src: config = libconf.load(f) elif "yaml" in src: config = yaml.load(f, Loader=yaml.SafeLoader) config['problem']['shape'] = shapes[phase] if wu_equiv: config['problem']['shape'] = shapes['fw'] if k == 'D': depthwise = True adapt_depthwise_config(config) else: depthwise = False config['problem']['shape'] += '.yaml' if wu_equiv: dataflow = convert_dataflow(dataflow) if phase == 'wu': remove_block_constraint(config) if depthwise: if dataflow == 'CK': dataflow = 'CN' dataflow = dataflow.replace('K', 'C') rewrite_dataflow(config, dataflow, replication, array_width) rewrite_mesh(config, array_width) if glb_scaling: rewrite_glb_size(config, array_width) if not wu_equiv: config['problem']['R'] = r config['problem']['S'] = s config['problem']['P'] = p config['problem']['Q'] = q config['problem']['C'] = c if not depthwise: config['problem']['K'] = k config['problem']['N'] = n else: config['problem']['R'] = p config['problem']['S'] = q config['problem']['P'] = r config['problem']['Q'] = s config['problem']['C'] = n config['problem']['K'] = c config['problem']['N'] = k config['problem']['Wstride'] = wstride config['problem']['Hstride'] = hstride config['problem']['Wdilation'] = 1 config['problem']['Hdilation'] = 1 config['mapper']['model-name'] = model config['mapper']['layer-name'] = layer if terminate is not None: config['mapper']['victory-condition'] = terminate if threads is not None: config['mapper']['num-threads'] = threads # rewrite synthetic mask configuration if not synthetic: try: config['mapper'].pop('mask-synthetic') except KeyError: pass else: config['mapper']['mask-synthetic'] = {} if sparsity is not None: config['mapper']['mask-synthetic']['target-sparsity'] = sparsity if save is not None: config['mapper']['mask-synthetic']['synthetic-mask-path'] = save if dense: opt_metrics = [] for opt in config['mapper']['optimization-metrics']: opt_metrics.append(opt.split('-')[-1]) config['mapper']['optimization-metrics'] = opt_metrics with open(dst, "w") as f: if "cfg" in src: f.write(libconf.dumps(config)) elif "yaml" in src: f.write(yaml.dump(config)) return env_list def convert_dataflow(dataflow): pre_convert_dataflow = copy.copy(dataflow) converted_dataflow = [] converted_dataflow.append(wu2fw[pre_convert_dataflow[0]]) converted_dataflow.append(wu2fw[pre_convert_dataflow[1]]) converted = '' converted = converted.join(converted_dataflow) print(f'convert from {dataflow} to {converted}') return converted def remove_block_constraint(config): # or possibily remove for constraint in config['mapspace']['constraints']: if constraint['type'] == 'temporal' and constraint['target'] == 'RegFile': try: constraint.pop('factors') except KeyError: pass def rewrite_dataflow(config, dataflow, replication, array_width): # loop through constaints, and make sure there is only 1 spatial type constraint # dingqing FIXME: not general for more spatial level architecture config num_spatial = 0 for constraint in config['mapspace']['constraints']: if num_spatial > 1: raise Exception("More than one spatial level! Check the config and the scripts.") if constraint['type'] == 'spatial': num_spatial += 1 # determine if it is possible to replicate possible2replicate = replication and (not config['problem'][dataflow[0]] > array_width / 2 or not config['problem'][dataflow[1]] > array_width / 2) print('possible2replicate?', possible2replicate) factors = constraint['factors'].split(' ') new_factor = [] for factor in factors: if factor[0] in dataflow: # look at problem size new_factor.append(factor[0] + f'{array_width}') elif not possible2replicate: new_factor.append(factor[0] + '1') constraint['factors'] = ' '.join(new_factor) # rewrite permutation # emmmm ugly non_spatial_dims = constraint['permutation'].replace(dataflow[0], '').replace(dataflow[1], '') constraint['permutation'] = dataflow[0] + non_spatial_dims + dataflow[1] def rewrite_mesh(config, array_width): # honestly, the structure is kinda unnatural... pe_subtree = config['architecture']['subtree'][0]['subtree'][0] # FIXME: this is not generic enough pe_name = pe_subtree['name'] num_pe_prev = re.findall(r'\d+', pe_name)[-1] num_pe_new = array_width * array_width - 1 pe_subtree['name'] = pe_name.replace(num_pe_prev, f'{num_pe_new}') # iterate over RF and PE for component in pe_subtree['local']: component['attributes']['meshX'] = array_width def rewrite_glb_size(config, array_width): scaling_factor = array_width / 16 # honestly, the structure is kinda unnatural... sys_subtree = config['architecture']['subtree'][0] # FIXME: this is not generic enough for comp in sys_subtree['local']: if comp['name'] == 'GlobalBuffer': comp['attributes']['depth'] = int(comp['attributes']['depth'] * scaling_factor) comp['attributes']['n_banks'] = int(comp['attributes']['n_banks'] * scaling_factor) def adapt_depthwise_config(config): config['problem']['shape'] += '-depthwise.yaml' try: config['problem'].pop('K') except KeyError: pass for constraint in config['mapspace']['constraints']: if 'factors' in constraint: factors = constraint['factors'].split(' ') new_factor = [x for x in factors if x[0] != 'K'] constraint['factors'] = ' '.join(new_factor) if 'permutation' in constraint: constraint['permutation'] = ''.join([x for x in constraint['permutation'] if x != 'K']) def run_timeloop(dirname, configfile, logfile='timeloop.log', env_list={}, dense=False, dense_dirname='dense-timeloop'): configfile_path = os.path.join(dirname, os.path.basename(configfile)) logfile_path = os.path.join(dirname, logfile) print('Running timeloop to get mapping') def stmt():
with open(logfile_path, "w") as outfile: this_file_path = os.path.abspath(inspect.getfile(inspect.currentframe())) if not dense: timeloop_executable_location = os.path.join( os.path.dirname(this_file_path), '..', 'build', 'timeloop-mapper') else: timeloop_executable_location = os.path.join( os.path.dirname(this_file_path), '..', '..', dense_dirname, 'build', 'timeloop-mapper') status = subprocess.call([timeloop_executable_location, configfile_path], stdout=outfile, stderr=outfile, env=dict(os.environ, **env_list)) # status = subprocess.call([timeloop_executable_location, configfile_path, 'ERT.yaml'], stdout=outfile, stderr=outfile) if status != 0: subprocess.check_call(['cat', logfile_path]) print('Did you remember to build timeloop and set up your environment properly?') sys.exit(1)
identifier_body
timeloop.py
, OR TORT # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. import functools import inspect import os import subprocess import sys import timeit import argparse import copy import re import libconf import yaml from common import * # Output file names. out_prefix = "timeloop-mapper." log_file_name = out_prefix + "log" stats_file_name = out_prefix + "stats.txt" xml_file_name = out_prefix + "map+stats.xml" map_txt_file_name = out_prefix + "map.txt" map_cfg_file_name = out_prefix + "map.cfg" map_cpp_file_name = out_prefix + "map.cpp" output_file_names = [log_file_name, stats_file_name, xml_file_name, map_txt_file_name, map_cfg_file_name, map_cpp_file_name] # dimension conversion that maps a WU problem to FW problem wu2fw = {'P': 'R', 'Q': 'S', 'R': 'P', 'S': 'Q', 'C': 'K', 'K': 'N', 'N': 'C'} def
(l): return functools.reduce(lambda x, y: x * y, l) def rewrite_workload_bounds(src, dst, workload_bounds, model, layer, batchsize, dataflow, phase, terminate, threads, synthetic, sparsity, save, replication, array_width, glb_scaling, dense): # backward_padding w, h, c, n, k, s, r, wpad, hpad, wstride, hstride = workload_bounds n = batchsize q = int((w - s + 2 * wpad) / wstride) + 1 p = int((h - r + 2 * hpad) / hstride) + 1 wu_equiv = k != 'D' and phase == 'wu' env_list = {} if not wu_equiv: print('Workload Dimensions:') print(' W =', w) print(' H =', h) print(' C =', c) print(' K =', k) print(' S =', s) print(' R =', r) print(' P =', p) print(' Q =', q) print(' N =', n) print(' W-pad =', wpad) print(' H-pad =', hpad) print(' W-stride =', wstride) print(' H-stride =', hstride) print() else: print('Equivalence Test: can we convert WU problem to FW and use cnn-layer.cfg? (at least in the dense case?)') print('Workload Dimensions:') print(' W =', w) print(' H =', h) print(f' C <- N {n}') print(f' K <- C {c}') print(f' S <- Q {q}') print(f' R <- P {p}') print(f' P <- R {r}') print(f' Q <- S {s}') print(f' N <- K {k}') print(' W-pad =', wpad) print(' H-pad =', hpad) print(' W-stride =', wstride) print(' H-stride =', hstride) print() env_list['TIMELOOP_EQUIVLENT_WU'] = 'True' with open(src, "r") as f: if "cfg" in src: config = libconf.load(f) elif "yaml" in src: config = yaml.load(f, Loader=yaml.SafeLoader) config['problem']['shape'] = shapes[phase] if wu_equiv: config['problem']['shape'] = shapes['fw'] if k == 'D': depthwise = True adapt_depthwise_config(config) else: depthwise = False config['problem']['shape'] += '.yaml' if wu_equiv: dataflow = convert_dataflow(dataflow) if phase == 'wu': remove_block_constraint(config) if depthwise: if dataflow == 'CK': dataflow = 'CN' dataflow = dataflow.replace('K', 'C') rewrite_dataflow(config, dataflow, replication, array_width) rewrite_mesh(config, array_width) if glb_scaling: rewrite_glb_size(config, array_width) if not wu_equiv: config['problem']['R'] = r config['problem']['S'] = s config['problem']['P'] = p config['problem']['Q'] = q config['problem']['C'] = c if not depthwise: config['problem']['K'] = k config['problem']['N'] = n else: config['problem']['R'] = p config['problem']['S'] = q config['problem']['P'] = r config['problem']['Q'] = s config['problem']['C'] = n config['problem']['K'] = c config['problem']['N'] = k config['problem']['Wstride'] = wstride config['problem']['Hstride'] = hstride config['problem']['Wdilation'] = 1 config['problem']['Hdilation'] = 1 config['mapper']['model-name'] = model config['mapper']['layer-name'] = layer if terminate is not None: config['mapper']['victory-condition'] = terminate if threads is not None: config['mapper']['num-threads'] = threads # rewrite synthetic mask configuration if not synthetic: try: config['mapper'].pop('mask-synthetic') except KeyError: pass else: config['mapper']['mask-synthetic'] = {} if sparsity is not None: config['mapper']['mask-synthetic']['target-sparsity'] = sparsity if save is not None: config['mapper']['mask-synthetic']['synthetic-mask-path'] = save if dense: opt_metrics = [] for opt in config['mapper']['optimization-metrics']: opt_metrics.append(opt.split('-')[-1]) config['mapper']['optimization-metrics'] = opt_metrics with open(dst, "w") as f: if "cfg" in src: f.write(libconf.dumps(config)) elif "yaml" in src: f.write(yaml.dump(config)) return env_list def convert_dataflow(dataflow): pre_convert_dataflow = copy.copy(dataflow) converted_dataflow = [] converted_dataflow.append(wu2fw[pre_convert_dataflow[0]]) converted_dataflow.append(wu2fw[pre_convert_dataflow[1]]) converted = '' converted = converted.join(converted_dataflow) print(f'convert from {dataflow} to {converted}') return converted def remove_block_constraint(config): # or possibily remove for constraint in config['mapspace']['constraints']: if constraint['type'] == 'temporal' and constraint['target'] == 'RegFile': try: constraint.pop('factors') except KeyError: pass def rewrite_dataflow(config, dataflow, replication, array_width): # loop through constaints, and make sure there is only 1 spatial type constraint # dingqing FIXME: not general for more spatial level architecture config num_spatial = 0 for constraint in config['mapspace']['constraints']: if num_spatial > 1: raise Exception("More than one spatial level! Check the config and the scripts.") if constraint['type'] == 'spatial': num_spatial += 1 # determine if it is possible to replicate possible2replicate = replication and (not config['problem'][dataflow[0]] > array_width / 2 or not config['problem'][dataflow[1]] > array_width / 2) print('possible2replicate?', possible2replicate) factors = constraint['factors'].split(' ') new_factor = [] for factor in factors: if factor[0] in dataflow: # look at problem size new_factor.append(factor[0] + f'{array_width}') elif not possible2replicate: new_factor.append(factor[0] + '1') constraint['factors'] = ' '.join(new_factor) # rewrite permutation # emmmm ugly non_spatial_dims = constraint['permutation'].replace(dataflow[0], '').replace(dataflow[1], '') constraint['permutation'] = dataflow[0] + non_spatial_dims + dataflow[1] def rewrite_mesh(config, array_width): # honestly, the structure is kinda unnatural... pe_subtree = config['architecture']['subtree'][0]['subtree'][0] # FIXME: this is not generic enough pe_name = pe_subtree['name'] num_pe_prev = re.findall(r'\d+', pe_name)[-1] num_pe_new = array_width * array_width - 1 pe_subtree['name'] =
prod
identifier_name
ops.py
.geometry.Polygon g1 = geojson.loads(s) # covert to shapely.geometry.polygon.Polygon g2 = shape(g1) if feat['properties']['color'] == 'red': # red for the burnt region burnt_polys.append(g2) else: # for the building poly building_polys.append([g2, [feat['properties']['BuildingID'], 'yellow', False]]) # mark building polygons as 'yellow' for non-burnt for now return burnt_polys, building_polys def label_building_polys(burnt_polys, building_polys): """Labels the building polygons as ('blue', True) if the building is destroyed.""" for b in building_polys: for r in burnt_polys: if b[0].intersects(r): b[1] = [b[1][0], 'blue', True] # mark building polygon as 'blue' if found in burnt region continue def to_geojson_burnt(burnt_polys, building_polys): """Convert shapes into geojson with new labelled building footprints. """ results = ({ 'type': 'Feature', 'properties': {'color': 'red'}, 'geometry': geo.mapping(r)} for r in burnt_polys) list_results = list(results) # append the building footprints to geojson results_buildings = ({ 'type': 'Feature', 'properties': {'BuildingID': b[1][0], 'color': b[1][1]}, 'geometry': geo.mapping(b[0])} for b in building_polys) list_results_buildings = list(results_buildings) collection = { 'type': 'FeatureCollection', 'features': list_results + list_results_buildings} return collection def to_geojson_groundtruth(burnt_polys, data_labelled): """Convert shapes into geojson for the groundtruth.""" results = ({ 'type': 'Feature', 'properties': {'color': 'red'}, 'geometry': geo.mapping(r)} for r in burnt_polys) list_results = list(results) # append the building footprints to geojson results_buildings = ({ 'type': 'Feature', 'properties': {'BuildingID': b['properties']['BuildingID'], 'color': b['properties']['color'], 'Burnt_Label': b['properties']['Burnt_Label']}, 'geometry': b['geometry']} for b in data_labelled['features']) list_results_buildings = list(results_buildings) collection = { 'type': 'FeatureCollection', 'features': list_results + list_results_buildings} return collection def geojson_to_polygons_groundtruth(js_): """Convert geojson to polygons for the groundtruth map.""" burnt_polys = [] building_polys = [] for i, feat in enumerate(js_['features']): o = { "coordinates": feat['geometry']['coordinates'], "type": feat['geometry']['type'] } s = json.dumps(o) # convert to geojson.geometry.Polygon g1 = geojson.loads(s) # covert to shapely.geometry.polygon.Polygon g2 = shape(g1) if feat['properties']['color'] == 'red': # red for the burnt region burnt_polys.append(g2) else: # for the building poly if feat['properties']['Burnt_Label']: building_polys.append([g2, [feat['properties']['BuildingID'], 'blue', True]]) # mark building polygons as 'blue' for burnt for now else: building_polys.append([g2, [feat['properties']['BuildingID'], 'yellow', False]]) # mark building polygons as 'yellow' for non-burnt for now return burnt_polys, building_polys def accuracy_measures(predictions, trues): """Accuracy measures for the predictions of the method vs the groundtruth. Prints a confusion matrix, accuracy, misclassifcation rate, true positieve rate, false positive rate, specificity, precision, prevalence. Returns the accuracy score, precision score, and recall score.""" tn, fp, fn, tp = confusion_matrix(trues, predictions).ravel() print "\t(tn, fp, fn, tp) =", (tn, fp, fn, tp) # how often is classifier correct? print "\tAccuracy = {:.2%}".format(float(tp + tn) / len(trues)) # how often is it wrong? print "\tMisclassification Rate = {:.2%}".format(float(fp + fn) / len(trues)) # when actually yes, how often does it predict yes? print "\tTrue Positive Rate = {:.2%}".format(float(tp) / trues.count(True)) # when actually no, how often does it predict yes? print "\tFalse Positive Rate = {:.2%}".format(float(fp) / trues.count(False)) # when actually no, how often does it predict no? print "\tSpecificity = {:.2%}".format(float(tn) / trues.count(False)) # when it predicts yes, how often is it correct? print "\tPrecision = {:.2%}".format(float(tp) / predictions.count(True)) # how often does yes condition occur in our sample? print "\tPrevalence = {:.2%}\n".format(float(trues.count(True)) / len(trues)) # return accuracy, precision, and recall score return accuracy_score(trues, predictions), precision_score(trues, predictions, average='binary'), recall_score( trues, predictions, average='binary') def create_mask(predictions_2d, sizeX, sizeY, chip_shape): """Create a new binary mask of burn scar with the tiles.""" # reshape predictions_2d predictions_2d_res = np.array(predictions_2d) predictions_2d_res = predictions_2d_res.reshape(sizeX, sizeY) # create new mask of area of interest new_mask = np.zeros((chip_shape[1], chip_shape[2])) for x in range(0, chip_shape[1], 256): for y in range(0, chip_shape[2], 256): new_mask[x:x + 256, y:y + 256] = predictions_2d_res[x / 256][y / 256] return new_mask """Functions for plots.""" def folium_map(geojson_to_overlay, layer_name, location, style_function=None, tiles='Stamen Terrain', zoom_start=16, show_layer_control=True, width='100%', height='75%', attr=None, map_zoom=18, max_zoom=20, tms=False, zoom_beyond_max=None, base_tiles='OpenStreetMap', opacity=1): """Folium map with Geojson layer and TMS tiles layer. This function requires geojson_to_overlay (geojson), layer_name (String), and location (map center tuple). You can also set tiles to the TMS URL and control map zoom.""" m = folium.Map(location=location, zoom_start=zoom_start, width=width, height=height, max_zoom=map_zoom, tiles=base_tiles) tiles = folium.TileLayer(tiles=tiles, attr=attr, name=attr, max_zoom=max_zoom) if tms is True: options = json.loads(tiles.options) options.update({'tms': True}) tiles.options = json.dumps(options, sort_keys=True, indent=2) tiles._template = jinja2.Template(u""" {% macro script(this, kwargs) %} var {{this.get_name()}} = L.tileLayer( '{{this.tiles}}', {{ this.options }} ).addTo({{this._parent.get_name()}}); {% endmacro %} """) if zoom_beyond_max is not None: options = json.loads(tiles.options) options.update({'maxNativeZoom': zoom_beyond_max, 'maxZoom': max_zoom}) tiles.options = json.dumps(options, sort_keys=True, indent=2) tiles._template = jinja2.Template(u""" {% macro script(this, kwargs) %} var {{this.get_name()}} = L.tileLayer( '{{this.tiles}}', {{ this.options }} ).addTo({{this._parent.get_name()}}); {% endmacro %} """) if opacity < 1: options = json.loads(tiles.options) options.update({'opacity': opacity}) tiles.options = json.dumps(options, sort_keys=True, indent=2) tiles._template = jinja2.Template(u""" {% macro script(this, kwargs) %} var {{this.get_name()}} = L.tileLayer( '{{this.tiles}}', {{ this.options }} ).addTo({{this._parent.get_name()}}); {% endmacro %} """) tiles.add_to(m) if style_function is not None: gj = folium.GeoJson(geojson_to_overlay, overlay=True, name=layer_name, style_function=style_function) else: gj = folium.GeoJson(geojson_to_overlay, overlay=True, name=layer_name) gj.add_to(m) if show_layer_control is True: folium.LayerControl().add_to(m) return m def
plot_array
identifier_name
ops.py
feats = stack.ravel().reshape(stack.shape[0] * stack.shape[1], stack.shape[2]) return feats def calc_rsi(image): """Remote sensing indices for vegetation, built-up, and bare soil.""" # roll axes to conventional row,col,depth img = np.rollaxis(image, 0, 3) # bands: Coastal(0), Blue(1), Green(2), Yellow(3), Red(4), Red-edge(5), NIR1(6), NIR2(7)) Multispectral COAST = img[:, :, 0] B = img[:, :, 1] G = img[:, :, 2] Y = img[:, :, 3] R = img[:, :, 4] RE = img[:, :, 5] NIR1 = img[:, :, 6] NIR2 = img[:, :, 7] arvi = old_div((NIR1 - (R - (B - R))), (NIR1 + (R - (B - R)))) dd = (2 * NIR1 - R) - (G - B) gi2 = (B * -0.2848 + G * -0.2434 + R * -0.5436 + NIR1 * 0.7243 + NIR2 * 0.0840) * 5 gndvi = old_div((NIR1 - G), (NIR1 + G)) ndre = old_div((NIR1 - RE), (NIR1 + RE)) ndvi = old_div((NIR1 - R), (NIR1 + R)) ndvi35 = old_div((G - R), (G + R)) ndvi84 = old_div((NIR2 - Y), (NIR2 + Y)) nirry = old_div((NIR1), (R + Y)) normnir = old_div(NIR1, (NIR1 + R + G)) psri = old_div((R - B), RE) rey = old_div((RE - Y), (RE + Y)) rvi = old_div(NIR1, R) sa = old_div(((Y + R) * 0.35), 2) + old_div((0.7 * (NIR1 + NIR2)), 2) - 0.69 vi1 = old_div((10000 * NIR1), (RE) ** 2) vire = old_div(NIR1, RE) br = (old_div(R, B)) * (old_div(G, B)) * (old_div(RE, B)) * (old_div(NIR1, B)) gr = old_div(G, R) rr = (old_div(NIR1, R)) * (old_div(G, R)) * (old_div(NIR1, RE)) ###Built-Up indices wvbi = old_div((COAST - RE), (COAST + RE)) wvnhfd = old_div((RE - COAST), (RE + COAST)) ###SIs evi = old_div((2.5 * (NIR2 - R)), (NIR2 + 6 * R - 7.5 * B + 1)) L = 0.5 # some coefficient for Soil Adjusted Vegetation Index (SAVI) DO NOT INCLUDE IN FEATURES savi = old_div(((1 + L) * (NIR2 - R)), (NIR2 + R + L)) msavi = old_div((2 * NIR2 + 1 - ((2 * NIR2 + 1) ** 2 - 8 * (NIR2 - R)) ** 0.5), 2) bai = old_div(1.0, ((0.1 + R) ** 2 + 0.06 + NIR2)) rgi = old_div(R, G) bri = old_div(B, R) rsi = np.stack( [arvi, dd, gi2, gndvi, ndre, ndvi, ndvi35, ndvi84, nirry, normnir, psri, rey, rvi, sa, vi1, vire, br, gr, rr, wvbi, wvnhfd, evi, savi, msavi, bai, rgi, bri], axis=2) return rsi def power(image, kernel): """Normalize images for better comparison.""" image = old_div((image - image.mean()), image.std()) return np.sqrt(ndi.convolve(image, np.real(kernel), mode='wrap') ** 2 + ndi.convolve(image, np.imag(kernel), mode='wrap') ** 2) def calc_gabors(image, frequency=1, theta_vals=[0, 1, 2, 3]): """Calculate gabor.""" # convert to gray scale img = exposure.equalize_hist(color.rgb2gray(image.rgb(blm=True))) results_list = [] for theta in theta_vals: theta = theta / 4. * np.pi kernel = filters.gabor_kernel(frequency, theta=theta) # Save kernel and the power image for each image results_list.append(power(img, kernel)) gabors = np.rollaxis(np.dstack([results_list]), 0, 3) return gabors def get_link(model_url): """Fetch the RF model pickle file or the building footprints geojson.""" response = requests.get(model_url) return response.content #partials get_model = partial(get_link, model_url=RF_model_link) #gets RF model response content get_geojson = partial(get_link, model_url=buildings_geojson_link) #gets building geojson response content def reproject(geom, from_proj='EPSG:4326', to_proj='EPSG:26942'): """Project from ESPG:4326 to ESPG:26942.""" tfm = partial(pyproj.transform, pyproj.Proj(init=from_proj), pyproj.Proj(init=to_proj)) return ops.transform(tfm, geom) def km2_area(polygons): """Get area in km^2 after reprojection.""" reprojected_polygons = [reproject(p) for p in polygons] return ops.cascaded_union(reprojected_polygons).area * 1e-6 def clean(img): """Clean the binary image by removing small holes and objects.""" label_img = label(img, connectivity=2) props = sorted(regionprops(label_img), key=lambda x: x.area) clean = morphology.binary_closing(img) clean = morphology.remove_small_holes(clean) return morphology.remove_small_objects(clean, int(np.floor(props[-1].area) / 10), connectivity=2) def to_geojson(shapes, buildings): """Converts the shapes into geojson. This function will combine the burn scar region and buildings into geojson. Burn scar polygon in red, buildings polygon all in blue.""" #append burn scar region polygons to geojson if type(shapes) == list: results = ({ 'type': 'Feature', 'properties': {'raster_val': v, 'color': 'red'}, 'geometry': s.__geo_interface__} for i, (s, v) in enumerate(shapes)) else: results = ({ 'type': 'Feature', 'properties': {'raster_val': v, 'color': 'red'}, 'geometry': s} for i, (s, v) in enumerate(shapes)) list_results = list(results) # append the building footprints to geojson results_buildings = ({ 'type': 'Feature', 'properties': {'BuildingID': b['properties']['BuildingID'], 'color': 'blue'}, 'geometry': b['geometry']} for i, b in enumerate(buildings['features'])) list_results_buildings = list(results_buildings) collection = { 'type': 'FeatureCollection', 'features': list_results + list_results_buildings} return collection def geojson_to_polygons(js_): """Convert the geojson into Shapely Polygons. Keep burn scar polygons as red. Mark all building polygons labelled as ('yellow', False) and will be changed later.""" burnt_polys = [] building_polys = [] for i, feat in enumerate(js_['features']): o = { "coordinates": feat['geometry']['coordinates'], "type": feat['geometry']['type'] } s = json.dumps(o) # convert to geojson.geometry.Polygon g1 = geojson.loads(s) # covert to shapely.geometry.polygon.Polygon g2 = shape(g1) if feat['properties']['color'] == 'red': # red for the burnt region burnt_polys.append(g2) else: # for the building poly building_polys.append([g2, [feat['properties']['BuildingID'], 'yellow', False]]) # mark building polygons as 'yellow' for non-burnt for now return burnt_polys, building_polys def label_building_polys(burnt_polys, building_pol
stack = np.dstack([img, rsi])
conditional_block
ops.py
R + Y)) normnir = old_div(NIR1, (NIR1 + R + G)) psri = old_div((R - B), RE) rey = old_div((RE - Y), (RE + Y)) rvi = old_div(NIR1, R) sa = old_div(((Y + R) * 0.35), 2) + old_div((0.7 * (NIR1 + NIR2)), 2) - 0.69 vi1 = old_div((10000 * NIR1), (RE) ** 2) vire = old_div(NIR1, RE) br = (old_div(R, B)) * (old_div(G, B)) * (old_div(RE, B)) * (old_div(NIR1, B)) gr = old_div(G, R) rr = (old_div(NIR1, R)) * (old_div(G, R)) * (old_div(NIR1, RE)) ###Built-Up indices wvbi = old_div((COAST - RE), (COAST + RE)) wvnhfd = old_div((RE - COAST), (RE + COAST)) ###SIs evi = old_div((2.5 * (NIR2 - R)), (NIR2 + 6 * R - 7.5 * B + 1)) L = 0.5 # some coefficient for Soil Adjusted Vegetation Index (SAVI) DO NOT INCLUDE IN FEATURES savi = old_div(((1 + L) * (NIR2 - R)), (NIR2 + R + L)) msavi = old_div((2 * NIR2 + 1 - ((2 * NIR2 + 1) ** 2 - 8 * (NIR2 - R)) ** 0.5), 2) bai = old_div(1.0, ((0.1 + R) ** 2 + 0.06 + NIR2)) rgi = old_div(R, G) bri = old_div(B, R) rsi = np.stack( [arvi, dd, gi2, gndvi, ndre, ndvi, ndvi35, ndvi84, nirry, normnir, psri, rey, rvi, sa, vi1, vire, br, gr, rr, wvbi, wvnhfd, evi, savi, msavi, bai, rgi, bri], axis=2) return rsi def power(image, kernel): """Normalize images for better comparison.""" image = old_div((image - image.mean()), image.std()) return np.sqrt(ndi.convolve(image, np.real(kernel), mode='wrap') ** 2 + ndi.convolve(image, np.imag(kernel), mode='wrap') ** 2) def calc_gabors(image, frequency=1, theta_vals=[0, 1, 2, 3]): """Calculate gabor.""" # convert to gray scale img = exposure.equalize_hist(color.rgb2gray(image.rgb(blm=True))) results_list = [] for theta in theta_vals: theta = theta / 4. * np.pi kernel = filters.gabor_kernel(frequency, theta=theta) # Save kernel and the power image for each image results_list.append(power(img, kernel)) gabors = np.rollaxis(np.dstack([results_list]), 0, 3) return gabors def get_link(model_url): """Fetch the RF model pickle file or the building footprints geojson.""" response = requests.get(model_url) return response.content #partials get_model = partial(get_link, model_url=RF_model_link) #gets RF model response content get_geojson = partial(get_link, model_url=buildings_geojson_link) #gets building geojson response content def reproject(geom, from_proj='EPSG:4326', to_proj='EPSG:26942'): """Project from ESPG:4326 to ESPG:26942.""" tfm = partial(pyproj.transform, pyproj.Proj(init=from_proj), pyproj.Proj(init=to_proj)) return ops.transform(tfm, geom) def km2_area(polygons): """Get area in km^2 after reprojection.""" reprojected_polygons = [reproject(p) for p in polygons] return ops.cascaded_union(reprojected_polygons).area * 1e-6 def clean(img): """Clean the binary image by removing small holes and objects.""" label_img = label(img, connectivity=2) props = sorted(regionprops(label_img), key=lambda x: x.area) clean = morphology.binary_closing(img) clean = morphology.remove_small_holes(clean) return morphology.remove_small_objects(clean, int(np.floor(props[-1].area) / 10), connectivity=2) def to_geojson(shapes, buildings): """Converts the shapes into geojson. This function will combine the burn scar region and buildings into geojson. Burn scar polygon in red, buildings polygon all in blue.""" #append burn scar region polygons to geojson if type(shapes) == list: results = ({ 'type': 'Feature', 'properties': {'raster_val': v, 'color': 'red'}, 'geometry': s.__geo_interface__} for i, (s, v) in enumerate(shapes)) else: results = ({ 'type': 'Feature', 'properties': {'raster_val': v, 'color': 'red'}, 'geometry': s} for i, (s, v) in enumerate(shapes)) list_results = list(results) # append the building footprints to geojson results_buildings = ({ 'type': 'Feature', 'properties': {'BuildingID': b['properties']['BuildingID'], 'color': 'blue'}, 'geometry': b['geometry']} for i, b in enumerate(buildings['features'])) list_results_buildings = list(results_buildings) collection = { 'type': 'FeatureCollection', 'features': list_results + list_results_buildings} return collection def geojson_to_polygons(js_): """Convert the geojson into Shapely Polygons. Keep burn scar polygons as red. Mark all building polygons labelled as ('yellow', False) and will be changed later.""" burnt_polys = [] building_polys = [] for i, feat in enumerate(js_['features']): o = { "coordinates": feat['geometry']['coordinates'], "type": feat['geometry']['type'] } s = json.dumps(o) # convert to geojson.geometry.Polygon g1 = geojson.loads(s) # covert to shapely.geometry.polygon.Polygon g2 = shape(g1) if feat['properties']['color'] == 'red': # red for the burnt region burnt_polys.append(g2) else: # for the building poly building_polys.append([g2, [feat['properties']['BuildingID'], 'yellow', False]]) # mark building polygons as 'yellow' for non-burnt for now return burnt_polys, building_polys def label_building_polys(burnt_polys, building_polys): """Labels the building polygons as ('blue', True) if the building is destroyed.""" for b in building_polys: for r in burnt_polys: if b[0].intersects(r): b[1] = [b[1][0], 'blue', True] # mark building polygon as 'blue' if found in burnt region continue def to_geojson_burnt(burnt_polys, building_polys): """Convert shapes into geojson with new labelled building footprints. """ results = ({ 'type': 'Feature', 'properties': {'color': 'red'}, 'geometry': geo.mapping(r)} for r in burnt_polys) list_results = list(results) # append the building footprints to geojson results_buildings = ({ 'type': 'Feature', 'properties': {'BuildingID': b[1][0], 'color': b[1][1]}, 'geometry': geo.mapping(b[0])} for b in building_polys) list_results_buildings = list(results_buildings) collection = { 'type': 'FeatureCollection', 'features': list_results + list_results_buildings} return collection def to_geojson_groundtruth(burnt_polys, data_labelled):
"""Convert shapes into geojson for the groundtruth.""" results = ({ 'type': 'Feature', 'properties': {'color': 'red'}, 'geometry': geo.mapping(r)} for r in burnt_polys) list_results = list(results) # append the building footprints to geojson results_buildings = ({ 'type': 'Feature', 'properties': {'BuildingID': b['properties']['BuildingID'], 'color': b['properties']['color'], 'Burnt_Label': b['properties']['Burnt_Label']}, 'geometry': b['geometry']} for b in data_labelled['features']) list_results_buildings = list(results_buildings)
identifier_body
ops.py
- R), (NIR1 + R)) ndvi35 = old_div((G - R), (G + R)) ndvi84 = old_div((NIR2 - Y), (NIR2 + Y)) nirry = old_div((NIR1), (R + Y)) normnir = old_div(NIR1, (NIR1 + R + G)) psri = old_div((R - B), RE) rey = old_div((RE - Y), (RE + Y)) rvi = old_div(NIR1, R) sa = old_div(((Y + R) * 0.35), 2) + old_div((0.7 * (NIR1 + NIR2)), 2) - 0.69 vi1 = old_div((10000 * NIR1), (RE) ** 2) vire = old_div(NIR1, RE) br = (old_div(R, B)) * (old_div(G, B)) * (old_div(RE, B)) * (old_div(NIR1, B)) gr = old_div(G, R) rr = (old_div(NIR1, R)) * (old_div(G, R)) * (old_div(NIR1, RE)) ###Built-Up indices wvbi = old_div((COAST - RE), (COAST + RE)) wvnhfd = old_div((RE - COAST), (RE + COAST)) ###SIs evi = old_div((2.5 * (NIR2 - R)), (NIR2 + 6 * R - 7.5 * B + 1)) L = 0.5 # some coefficient for Soil Adjusted Vegetation Index (SAVI) DO NOT INCLUDE IN FEATURES savi = old_div(((1 + L) * (NIR2 - R)), (NIR2 + R + L)) msavi = old_div((2 * NIR2 + 1 - ((2 * NIR2 + 1) ** 2 - 8 * (NIR2 - R)) ** 0.5), 2) bai = old_div(1.0, ((0.1 + R) ** 2 + 0.06 + NIR2)) rgi = old_div(R, G) bri = old_div(B, R) rsi = np.stack( [arvi, dd, gi2, gndvi, ndre, ndvi, ndvi35, ndvi84, nirry, normnir, psri, rey, rvi, sa, vi1, vire, br, gr, rr, wvbi, wvnhfd, evi, savi, msavi, bai, rgi, bri], axis=2) return rsi def power(image, kernel): """Normalize images for better comparison.""" image = old_div((image - image.mean()), image.std()) return np.sqrt(ndi.convolve(image, np.real(kernel), mode='wrap') ** 2 + ndi.convolve(image, np.imag(kernel), mode='wrap') ** 2) def calc_gabors(image, frequency=1, theta_vals=[0, 1, 2, 3]): """Calculate gabor.""" # convert to gray scale img = exposure.equalize_hist(color.rgb2gray(image.rgb(blm=True))) results_list = [] for theta in theta_vals: theta = theta / 4. * np.pi kernel = filters.gabor_kernel(frequency, theta=theta) # Save kernel and the power image for each image results_list.append(power(img, kernel)) gabors = np.rollaxis(np.dstack([results_list]), 0, 3) return gabors def get_link(model_url): """Fetch the RF model pickle file or the building footprints geojson.""" response = requests.get(model_url)
return response.content #partials get_model = partial(get_link, model_url=RF_model_link) #gets RF model response content get_geojson = partial(get_link, model_url=buildings_geojson_link) #gets building geojson response content def reproject(geom, from_proj='EPSG:4326', to_proj='EPSG:26942'): """Project from ESPG:4326 to ESPG:26942.""" tfm = partial(pyproj.transform, pyproj.Proj(init=from_proj), pyproj.Proj(init=to_proj)) return ops.transform(tfm, geom) def km2_area(polygons): """Get area in km^2 after reprojection.""" reprojected_polygons = [reproject(p) for p in polygons] return ops.cascaded_union(reprojected_polygons).area * 1e-6 def clean(img): """Clean the binary image by removing small holes and objects.""" label_img = label(img, connectivity=2) props = sorted(regionprops(label_img), key=lambda x: x.area) clean = morphology.binary_closing(img) clean = morphology.remove_small_holes(clean) return morphology.remove_small_objects(clean, int(np.floor(props[-1].area) / 10), connectivity=2) def to_geojson(shapes, buildings): """Converts the shapes into geojson. This function will combine the burn scar region and buildings into geojson. Burn scar polygon in red, buildings polygon all in blue.""" #append burn scar region polygons to geojson if type(shapes) == list: results = ({ 'type': 'Feature', 'properties': {'raster_val': v, 'color': 'red'}, 'geometry': s.__geo_interface__} for i, (s, v) in enumerate(shapes)) else: results = ({ 'type': 'Feature', 'properties': {'raster_val': v, 'color': 'red'}, 'geometry': s} for i, (s, v) in enumerate(shapes)) list_results = list(results) # append the building footprints to geojson results_buildings = ({ 'type': 'Feature', 'properties': {'BuildingID': b['properties']['BuildingID'], 'color': 'blue'}, 'geometry': b['geometry']} for i, b in enumerate(buildings['features'])) list_results_buildings = list(results_buildings) collection = { 'type': 'FeatureCollection', 'features': list_results + list_results_buildings} return collection def geojson_to_polygons(js_): """Convert the geojson into Shapely Polygons. Keep burn scar polygons as red. Mark all building polygons labelled as ('yellow', False) and will be changed later.""" burnt_polys = [] building_polys = [] for i, feat in enumerate(js_['features']): o = { "coordinates": feat['geometry']['coordinates'], "type": feat['geometry']['type'] } s = json.dumps(o) # convert to geojson.geometry.Polygon g1 = geojson.loads(s) # covert to shapely.geometry.polygon.Polygon g2 = shape(g1) if feat['properties']['color'] == 'red': # red for the burnt region burnt_polys.append(g2) else: # for the building poly building_polys.append([g2, [feat['properties']['BuildingID'], 'yellow', False]]) # mark building polygons as 'yellow' for non-burnt for now return burnt_polys, building_polys def label_building_polys(burnt_polys, building_polys): """Labels the building polygons as ('blue', True) if the building is destroyed.""" for b in building_polys: for r in burnt_polys: if b[0].intersects(r): b[1] = [b[1][0], 'blue', True] # mark building polygon as 'blue' if found in burnt region continue def to_geojson_burnt(burnt_polys, building_polys): """Convert shapes into geojson with new labelled building footprints. """ results = ({ 'type': 'Feature', 'properties': {'color': 'red'}, 'geometry': geo.mapping(r)} for r in burnt_polys) list_results = list(results) # append the building footprints to geojson results_buildings = ({ 'type': 'Feature', 'properties': {'BuildingID': b[1][0], 'color': b[1][1]}, 'geometry': geo.mapping(b[0])} for b in building_polys) list_results_buildings = list(results_buildings) collection = { 'type': 'FeatureCollection', 'features': list_results + list_results_buildings} return collection def to_geojson_groundtruth(burnt_polys, data_labelled): """Convert shapes into geojson for the groundtruth.""" results = ({ 'type': 'Feature', 'properties': {'color': 'red'}, 'geometry': geo.mapping(r)} for r in burnt_polys) list_results = list(results) # append the building footprints to geojson results_buildings = ({ 'type': 'Feature', 'properties': {'
random_line_split
dwi_corr_util.py
class MRdegibbsOutputSpec(TraitedSpec): out_file = File(desc = "the output unringed DWI image", exists = True) class MRdegibbs(CommandLine): """Use MRTrix3 degibbs command for removing the gibbs ringing artefact. For more information, see <https://mrtrix.readthedocs.io/en/latest/reference/commands/mrdegibbs.html> """ _cmd = '/a/software/mrtrix/3.0-rc2/ubuntu-xenial-amd64/bin/mrdegibbs' input_spec = MRdegibbsInputSpec output_spec = MRdegibbsOutputSpec # Wrap FSL eddy (copy from nipype interface) class EddyInputSpec(CommandLineInputSpec): in_file = File( exists=True, mandatory=True, argstr='--imain=%s', desc=('File containing all the images to estimate ' 'distortions for')) in_mask = File( exists=True, mandatory=True, argstr='--mask=%s', desc='Mask to indicate brain') in_index = File( exists=True, mandatory=True, argstr='--index=%s', desc=('File containing indices for all volumes in --imain ' 'into --acqp and --topup')) in_acqp = File( exists=True, mandatory=True, argstr='--acqp=%s', desc='File containing acquisition parameters') in_bvec = File( exists=True, mandatory=True, argstr='--bvecs=%s', desc=('File containing the b-vectors for all volumes in ' '--imain')) in_bval = File( exists=True, mandatory=True, argstr='--bvals=%s', desc=('File containing the b-values for all volumes in ' '--imain')) out_base = traits.Str( 'eddy_corrected', argstr='--out=%s', usedefault=True, desc=('basename for output (warped) image')) session = File( exists=True, argstr='--session=%s', desc=('File containing session indices for all volumes in ' '--imain')) in_topup_fieldcoef = File( exists=True, argstr="--topup=%s", requires=['in_topup_movpar'], desc=('topup file containing the field ' 'coefficients')) in_topup_movpar = File( exists=True, requires=['in_topup_fieldcoef'], desc='topup movpar.txt file') flm = traits.Enum( 'linear', 'quadratic', 'cubic', argstr='--flm=%s', desc='First level EC model') slm = traits.Enum( 'none', 'linear', 'quadratic', argstr='--slm=%s', desc='Second level EC model') fep = traits.Bool( False, argstr='--fep', desc='Fill empty planes in x- or y-directions') interp = traits.Enum( 'spline', 'trilinear', argstr='--interp=%s', desc='Interpolation model for estimation step') nvoxhp = traits.Int( 1000, usedefault=True, argstr='--nvoxhp=%s', desc=('# of voxels used to estimate the ' 'hyperparameters')) fudge_factor = traits.Float( 10.0, usedefault=True, argstr='--ff=%s', desc=('Fudge factor for hyperparameter ' 'error variance')) dont_sep_offs_move = traits.Bool( False, argstr='--dont_sep_offs_move', desc=('Do NOT attempt to separate ' 'field offset from subject ' 'movement')) dont_peas = traits.Bool( False, argstr='--dont_peas', desc="Do NOT perform a post-eddy alignment of " "shells") fwhm = traits.Float( desc=('FWHM for conditioning filter when estimating ' 'the parameters'), argstr='--fwhm=%s') niter = traits.Int(5, usedefault=True, argstr='--niter=%s', desc='Number of iterations') method = traits.Enum( 'jac', 'lsr', argstr='--resamp=%s', desc=('Final resampling method (jacobian/least ' 'squares)')) repol = traits.Bool( False, argstr='--repol', desc='Detect and replace outlier slices') num_threads = traits.Int( 1, usedefault=True, nohash=True, desc="Number of openmp threads to use") is_shelled = traits.Bool( False, argstr='--data_is_shelled', desc="Override internal check to ensure that " "date are acquired on a set of b-value " "shells") field = traits.Str( argstr='--field=%s', desc="NonTOPUP fieldmap scaled in Hz - filename has " "to be provided without an extension. TOPUP is " "strongly recommended") field_mat = File( exists=True, argstr='--field_mat=%s', desc="Matrix that specifies the relative locations of " "the field specified by --field and first volume " "in file --imain") use_cuda = traits.Bool(False, desc="Run eddy using cuda gpu") class EddyOutputSpec(TraitedSpec): out_corrected = File( exists=True, desc='4D image file containing all the corrected volumes') out_parameter = File( exists=True, desc=('text file with parameters definining the field and' 'movement for each scan')) out_rotated_bvecs = File( exists=True, desc='File containing rotated b-values for all volumes') out_movement_rms = File( exists=True, desc='Summary of the "total movement" in each volume') out_restricted_movement_rms = File( exists=True, desc=('Summary of the "total movement" in each volume ' 'disregarding translation in the PE direction')) out_shell_alignment_parameters = File( exists=True, desc=('File containing rigid body movement parameters ' 'between the different shells as estimated by a ' 'post-hoc mutual information based registration')) out_outlier_report = File( exists=True, desc=('Text-file with a plain language report on what ' 'outlier slices eddy has found')) class Eddy(CommandLine): """ Interface for FSL eddy, a tool for estimating and correcting eddy currents induced distortions. `User guide <http://fsl.fmrib.ox.ac.uk/fsl/fslwiki/Eddy/UsersGuide>`_ and `more info regarding acqp file <http://fsl.fmrib.ox.ac.uk/fsl/fslwiki/eddy/Faq#How_do_I_know_what_to_put_into_my_--acqp_file>`_. Examples -------- >>> from nipype.interfaces.fsl import Eddy >>> eddy = Eddy() >>> eddy.inputs.in_file = 'epi.nii' >>> eddy.inputs.in_mask = 'epi_mask.nii' >>> eddy.inputs.in_index = 'epi_index.txt' >>> eddy.inputs.in_acqp = 'epi_acqp.txt' >>> eddy.inputs.in_bvec = 'bvecs.scheme' >>> eddy.inputs.in_bval = 'bvals.scheme' >>> eddy.inputs.use_cuda = True >>> eddy.cmdline # doctest: +ELLIPSIS 'eddy_cuda --ff=10.0 --acqp=epi_acqp.txt --bvals=bvals.scheme \ --bvecs=bvecs.scheme --imain=epi.nii --index=epi_index.txt \ --mask=epi_mask.nii --niter=5 --nvoxhp=1000 --out=.../eddy_corrected' >>> eddy.inputs.use_cuda = False >>> eddy.cmdline # doctest: +ELLIPSIS 'eddy_openmp --ff=10.0 --acqp=epi_acqp.txt --bvals=bvals.scheme \ --bvecs=bvecs.scheme --imain=epi.nii --index=epi_index.txt \ --mask=epi_mask.nii --niter=5 --nvoxhp=1000 --out=.../eddy_corrected' >>> res = eddy.run() # doctest: +SKIP """ _cmd = '/data/pt_life_dti/scripts/life2018/eddy_openmp-5.0.11' input_spec = EddyInputSpec output_spec = EddyOutputSpec _num_threads = 1 def __init__(self, **inputs):
super(Eddy, self).__init__(**inputs) self.inputs.on_trait_change(self._num_threads_update, 'num_threads') if not isdefined(self.inputs.num_threads): self.inputs.num_threads = self._num_threads else: self._num_threads_update() self.inputs.on_trait_change(self._use_cuda, 'use_cuda') if isdefined(self.inputs.use_cuda): self._use_cuda()
identifier_body
dwi_corr_util.py
linear', 'quadratic', argstr='--slm=%s', desc='Second level EC model') fep = traits.Bool( False, argstr='--fep', desc='Fill empty planes in x- or y-directions') interp = traits.Enum( 'spline', 'trilinear', argstr='--interp=%s', desc='Interpolation model for estimation step') nvoxhp = traits.Int( 1000, usedefault=True, argstr='--nvoxhp=%s', desc=('# of voxels used to estimate the ' 'hyperparameters')) fudge_factor = traits.Float( 10.0, usedefault=True, argstr='--ff=%s', desc=('Fudge factor for hyperparameter ' 'error variance')) dont_sep_offs_move = traits.Bool( False, argstr='--dont_sep_offs_move', desc=('Do NOT attempt to separate ' 'field offset from subject ' 'movement')) dont_peas = traits.Bool( False, argstr='--dont_peas', desc="Do NOT perform a post-eddy alignment of " "shells") fwhm = traits.Float( desc=('FWHM for conditioning filter when estimating ' 'the parameters'), argstr='--fwhm=%s') niter = traits.Int(5, usedefault=True, argstr='--niter=%s', desc='Number of iterations') method = traits.Enum( 'jac', 'lsr', argstr='--resamp=%s', desc=('Final resampling method (jacobian/least ' 'squares)')) repol = traits.Bool( False, argstr='--repol', desc='Detect and replace outlier slices') num_threads = traits.Int( 1, usedefault=True, nohash=True, desc="Number of openmp threads to use") is_shelled = traits.Bool( False, argstr='--data_is_shelled', desc="Override internal check to ensure that " "date are acquired on a set of b-value " "shells") field = traits.Str( argstr='--field=%s', desc="NonTOPUP fieldmap scaled in Hz - filename has " "to be provided without an extension. TOPUP is " "strongly recommended") field_mat = File( exists=True, argstr='--field_mat=%s', desc="Matrix that specifies the relative locations of " "the field specified by --field and first volume " "in file --imain") use_cuda = traits.Bool(False, desc="Run eddy using cuda gpu") class EddyOutputSpec(TraitedSpec): out_corrected = File( exists=True, desc='4D image file containing all the corrected volumes') out_parameter = File( exists=True, desc=('text file with parameters definining the field and' 'movement for each scan')) out_rotated_bvecs = File( exists=True, desc='File containing rotated b-values for all volumes') out_movement_rms = File( exists=True, desc='Summary of the "total movement" in each volume') out_restricted_movement_rms = File( exists=True, desc=('Summary of the "total movement" in each volume ' 'disregarding translation in the PE direction')) out_shell_alignment_parameters = File( exists=True, desc=('File containing rigid body movement parameters ' 'between the different shells as estimated by a ' 'post-hoc mutual information based registration')) out_outlier_report = File( exists=True, desc=('Text-file with a plain language report on what ' 'outlier slices eddy has found')) class Eddy(CommandLine): """ Interface for FSL eddy, a tool for estimating and correcting eddy currents induced distortions. `User guide <http://fsl.fmrib.ox.ac.uk/fsl/fslwiki/Eddy/UsersGuide>`_ and `more info regarding acqp file <http://fsl.fmrib.ox.ac.uk/fsl/fslwiki/eddy/Faq#How_do_I_know_what_to_put_into_my_--acqp_file>`_. Examples -------- >>> from nipype.interfaces.fsl import Eddy >>> eddy = Eddy() >>> eddy.inputs.in_file = 'epi.nii' >>> eddy.inputs.in_mask = 'epi_mask.nii' >>> eddy.inputs.in_index = 'epi_index.txt' >>> eddy.inputs.in_acqp = 'epi_acqp.txt' >>> eddy.inputs.in_bvec = 'bvecs.scheme' >>> eddy.inputs.in_bval = 'bvals.scheme' >>> eddy.inputs.use_cuda = True >>> eddy.cmdline # doctest: +ELLIPSIS 'eddy_cuda --ff=10.0 --acqp=epi_acqp.txt --bvals=bvals.scheme \ --bvecs=bvecs.scheme --imain=epi.nii --index=epi_index.txt \ --mask=epi_mask.nii --niter=5 --nvoxhp=1000 --out=.../eddy_corrected' >>> eddy.inputs.use_cuda = False >>> eddy.cmdline # doctest: +ELLIPSIS 'eddy_openmp --ff=10.0 --acqp=epi_acqp.txt --bvals=bvals.scheme \ --bvecs=bvecs.scheme --imain=epi.nii --index=epi_index.txt \ --mask=epi_mask.nii --niter=5 --nvoxhp=1000 --out=.../eddy_corrected' >>> res = eddy.run() # doctest: +SKIP """ _cmd = '/data/pt_life_dti/scripts/life2018/eddy_openmp-5.0.11' input_spec = EddyInputSpec output_spec = EddyOutputSpec _num_threads = 1 def __init__(self, **inputs): super(Eddy, self).__init__(**inputs) self.inputs.on_trait_change(self._num_threads_update, 'num_threads') if not isdefined(self.inputs.num_threads): self.inputs.num_threads = self._num_threads else: self._num_threads_update() self.inputs.on_trait_change(self._use_cuda, 'use_cuda') if isdefined(self.inputs.use_cuda): self._use_cuda() def _num_threads_update(self): self._num_threads = self.inputs.num_threads if not isdefined(self.inputs.num_threads): if 'OMP_NUM_THREADS' in self.inputs.environ: del self.inputs.environ['OMP_NUM_THREADS'] else: self.inputs.environ['OMP_NUM_THREADS'] = str( self.inputs.num_threads) def _use_cuda(self): self._cmd = 'eddy_cuda' if self.inputs.use_cuda else 'eddy_openmp' def _run_interface(self, runtime): # If 'eddy_openmp' is missing, use 'eddy' FSLDIR = os.getenv('FSLDIR', '') cmd = self._cmd if all((FSLDIR != '', cmd == 'eddy_openmp', not os.path.exists(os.path.join(FSLDIR, 'bin', cmd)))): self._cmd = 'eddy' runtime = super(Eddy, self)._run_interface(runtime) # Restore command to avoid side-effects self._cmd = cmd return runtime def _format_arg(self, name, spec, value): if name == 'in_topup_fieldcoef': return spec.argstr % value.split('_fieldcoef')[0] if name == 'out_base': return spec.argstr % os.path.abspath(value) return super(Eddy, self)._format_arg(name, spec, value) def _list_outputs(self): outputs = self.output_spec().get() outputs['out_corrected'] = os.path.abspath( '%s.nii.gz' % self.inputs.out_base) outputs['out_parameter'] = os.path.abspath( '%s.eddy_parameters' % self.inputs.out_base) # File generation might depend on the version of EDDY out_rotated_bvecs = os.path.abspath( '%s.eddy_rotated_bvecs' % self.inputs.out_base) out_movement_rms = os.path.abspath( '%s.eddy_movement_rms' % self.inputs.out_base) out_restricted_movement_rms = os.path.abspath( '%s.eddy_restricted_movement_rms' % self.inputs.out_base) out_shell_alignment_parameters = os.path.abspath( '%s.eddy_post_eddy_shell_alignment_parameters' % self.inputs.out_base) out_outlier_report = os.path.abspath( '%s.eddy_outlier_report' % self.inputs.out_base) if os.path.exists(out_rotated_bvecs): outputs['out_rotated_bvecs'] = out_rotated_bvecs if os.path.exists(out_movement_rms): outputs['out_movement_rms'] = out_movement_rms if os.path.exists(out_restricted_movement_rms): outputs['out_restricted_movement_rms'] = \ out_restricted_movement_rms if os.path.exists(out_shell_alignment_parameters):
outputs['out_shell_alignment_parameters'] = \ out_shell_alignment_parameters
conditional_block
dwi_corr_util.py
exists = True) class MRdegibbs(CommandLine): """Use MRTrix3 degibbs command for removing the gibbs ringing artefact. For more information, see <https://mrtrix.readthedocs.io/en/latest/reference/commands/mrdegibbs.html> """ _cmd = '/a/software/mrtrix/3.0-rc2/ubuntu-xenial-amd64/bin/mrdegibbs' input_spec = MRdegibbsInputSpec output_spec = MRdegibbsOutputSpec # Wrap FSL eddy (copy from nipype interface) class EddyInputSpec(CommandLineInputSpec): in_file = File( exists=True, mandatory=True, argstr='--imain=%s', desc=('File containing all the images to estimate ' 'distortions for')) in_mask = File( exists=True, mandatory=True, argstr='--mask=%s', desc='Mask to indicate brain') in_index = File( exists=True, mandatory=True, argstr='--index=%s', desc=('File containing indices for all volumes in --imain ' 'into --acqp and --topup')) in_acqp = File( exists=True, mandatory=True, argstr='--acqp=%s', desc='File containing acquisition parameters') in_bvec = File( exists=True, mandatory=True, argstr='--bvecs=%s', desc=('File containing the b-vectors for all volumes in ' '--imain')) in_bval = File( exists=True, mandatory=True, argstr='--bvals=%s', desc=('File containing the b-values for all volumes in ' '--imain')) out_base = traits.Str( 'eddy_corrected', argstr='--out=%s', usedefault=True, desc=('basename for output (warped) image')) session = File( exists=True, argstr='--session=%s', desc=('File containing session indices for all volumes in ' '--imain')) in_topup_fieldcoef = File( exists=True, argstr="--topup=%s", requires=['in_topup_movpar'], desc=('topup file containing the field ' 'coefficients')) in_topup_movpar = File( exists=True, requires=['in_topup_fieldcoef'], desc='topup movpar.txt file') flm = traits.Enum( 'linear', 'quadratic', 'cubic', argstr='--flm=%s', desc='First level EC model') slm = traits.Enum( 'none', 'linear', 'quadratic', argstr='--slm=%s', desc='Second level EC model') fep = traits.Bool( False, argstr='--fep', desc='Fill empty planes in x- or y-directions') interp = traits.Enum( 'spline', 'trilinear', argstr='--interp=%s', desc='Interpolation model for estimation step') nvoxhp = traits.Int( 1000, usedefault=True, argstr='--nvoxhp=%s', desc=('# of voxels used to estimate the ' 'hyperparameters')) fudge_factor = traits.Float( 10.0, usedefault=True, argstr='--ff=%s', desc=('Fudge factor for hyperparameter ' 'error variance')) dont_sep_offs_move = traits.Bool( False, argstr='--dont_sep_offs_move', desc=('Do NOT attempt to separate ' 'field offset from subject ' 'movement')) dont_peas = traits.Bool( False, argstr='--dont_peas', desc="Do NOT perform a post-eddy alignment of " "shells") fwhm = traits.Float( desc=('FWHM for conditioning filter when estimating ' 'the parameters'), argstr='--fwhm=%s') niter = traits.Int(5, usedefault=True, argstr='--niter=%s', desc='Number of iterations') method = traits.Enum( 'jac', 'lsr', argstr='--resamp=%s', desc=('Final resampling method (jacobian/least ' 'squares)')) repol = traits.Bool( False, argstr='--repol', desc='Detect and replace outlier slices') num_threads = traits.Int( 1, usedefault=True, nohash=True, desc="Number of openmp threads to use") is_shelled = traits.Bool( False, argstr='--data_is_shelled', desc="Override internal check to ensure that " "date are acquired on a set of b-value " "shells") field = traits.Str( argstr='--field=%s', desc="NonTOPUP fieldmap scaled in Hz - filename has " "to be provided without an extension. TOPUP is " "strongly recommended") field_mat = File( exists=True, argstr='--field_mat=%s', desc="Matrix that specifies the relative locations of " "the field specified by --field and first volume " "in file --imain") use_cuda = traits.Bool(False, desc="Run eddy using cuda gpu") class EddyOutputSpec(TraitedSpec): out_corrected = File( exists=True, desc='4D image file containing all the corrected volumes') out_parameter = File( exists=True, desc=('text file with parameters definining the field and' 'movement for each scan')) out_rotated_bvecs = File( exists=True, desc='File containing rotated b-values for all volumes') out_movement_rms = File( exists=True, desc='Summary of the "total movement" in each volume') out_restricted_movement_rms = File( exists=True, desc=('Summary of the "total movement" in each volume ' 'disregarding translation in the PE direction')) out_shell_alignment_parameters = File( exists=True, desc=('File containing rigid body movement parameters ' 'between the different shells as estimated by a ' 'post-hoc mutual information based registration')) out_outlier_report = File( exists=True, desc=('Text-file with a plain language report on what ' 'outlier slices eddy has found')) class Eddy(CommandLine): """ Interface for FSL eddy, a tool for estimating and correcting eddy currents induced distortions. `User guide <http://fsl.fmrib.ox.ac.uk/fsl/fslwiki/Eddy/UsersGuide>`_ and `more info regarding acqp file <http://fsl.fmrib.ox.ac.uk/fsl/fslwiki/eddy/Faq#How_do_I_know_what_to_put_into_my_--acqp_file>`_. Examples -------- >>> from nipype.interfaces.fsl import Eddy >>> eddy = Eddy() >>> eddy.inputs.in_file = 'epi.nii' >>> eddy.inputs.in_mask = 'epi_mask.nii' >>> eddy.inputs.in_index = 'epi_index.txt' >>> eddy.inputs.in_acqp = 'epi_acqp.txt' >>> eddy.inputs.in_bvec = 'bvecs.scheme' >>> eddy.inputs.in_bval = 'bvals.scheme' >>> eddy.inputs.use_cuda = True >>> eddy.cmdline # doctest: +ELLIPSIS 'eddy_cuda --ff=10.0 --acqp=epi_acqp.txt --bvals=bvals.scheme \ --bvecs=bvecs.scheme --imain=epi.nii --index=epi_index.txt \ --mask=epi_mask.nii --niter=5 --nvoxhp=1000 --out=.../eddy_corrected' >>> eddy.inputs.use_cuda = False >>> eddy.cmdline # doctest: +ELLIPSIS 'eddy_openmp --ff=10.0 --acqp=epi_acqp.txt --bvals=bvals.scheme \ --bvecs=bvecs.scheme --imain=epi.nii --index=epi_index.txt \ --mask=epi_mask.nii --niter=5 --nvoxhp=1000 --out=.../eddy_corrected' >>> res = eddy.run() # doctest: +SKIP """ _cmd = '/data/pt_life_dti/scripts/life2018/eddy_openmp-5.0.11' input_spec = EddyInputSpec output_spec = EddyOutputSpec _num_threads = 1 def __init__(self, **inputs): super(Eddy, self).__init__(**inputs) self.inputs.on_trait_change(self._num_threads_update, 'num_threads') if not isdefined(self.inputs.num_threads): self.inputs.num_threads = self._num_threads else: self._num_threads_update() self.inputs.on_trait_change(self._use_cuda, 'use_cuda') if isdefined(self.inputs.use_cuda): self._use_cuda() def _num_threads_update(self):
self._num_threads = self.inputs.num_threads if not isdefined(self.inputs.num_threads):
random_line_split
dwi_corr_util.py
Spec(TraitedSpec): out_file = File(desc = "the output unringed DWI image", exists = True) class MRdegibbs(CommandLine): """Use MRTrix3 degibbs command for removing the gibbs ringing artefact. For more information, see <https://mrtrix.readthedocs.io/en/latest/reference/commands/mrdegibbs.html> """ _cmd = '/a/software/mrtrix/3.0-rc2/ubuntu-xenial-amd64/bin/mrdegibbs' input_spec = MRdegibbsInputSpec output_spec = MRdegibbsOutputSpec # Wrap FSL eddy (copy from nipype interface) class EddyInputSpec(CommandLineInputSpec): in_file = File( exists=True, mandatory=True, argstr='--imain=%s', desc=('File containing all the images to estimate ' 'distortions for')) in_mask = File( exists=True, mandatory=True, argstr='--mask=%s', desc='Mask to indicate brain') in_index = File( exists=True, mandatory=True, argstr='--index=%s', desc=('File containing indices for all volumes in --imain ' 'into --acqp and --topup')) in_acqp = File( exists=True, mandatory=True, argstr='--acqp=%s', desc='File containing acquisition parameters') in_bvec = File( exists=True, mandatory=True, argstr='--bvecs=%s', desc=('File containing the b-vectors for all volumes in ' '--imain')) in_bval = File( exists=True, mandatory=True, argstr='--bvals=%s', desc=('File containing the b-values for all volumes in ' '--imain')) out_base = traits.Str( 'eddy_corrected', argstr='--out=%s', usedefault=True, desc=('basename for output (warped) image')) session = File( exists=True, argstr='--session=%s', desc=('File containing session indices for all volumes in ' '--imain')) in_topup_fieldcoef = File( exists=True, argstr="--topup=%s", requires=['in_topup_movpar'], desc=('topup file containing the field ' 'coefficients')) in_topup_movpar = File( exists=True, requires=['in_topup_fieldcoef'], desc='topup movpar.txt file') flm = traits.Enum( 'linear', 'quadratic', 'cubic', argstr='--flm=%s', desc='First level EC model') slm = traits.Enum( 'none', 'linear', 'quadratic', argstr='--slm=%s', desc='Second level EC model') fep = traits.Bool( False, argstr='--fep', desc='Fill empty planes in x- or y-directions') interp = traits.Enum( 'spline', 'trilinear', argstr='--interp=%s', desc='Interpolation model for estimation step') nvoxhp = traits.Int( 1000, usedefault=True, argstr='--nvoxhp=%s', desc=('# of voxels used to estimate the ' 'hyperparameters')) fudge_factor = traits.Float( 10.0, usedefault=True, argstr='--ff=%s', desc=('Fudge factor for hyperparameter ' 'error variance')) dont_sep_offs_move = traits.Bool( False, argstr='--dont_sep_offs_move', desc=('Do NOT attempt to separate ' 'field offset from subject ' 'movement')) dont_peas = traits.Bool( False, argstr='--dont_peas', desc="Do NOT perform a post-eddy alignment of " "shells") fwhm = traits.Float( desc=('FWHM for conditioning filter when estimating ' 'the parameters'), argstr='--fwhm=%s') niter = traits.Int(5, usedefault=True, argstr='--niter=%s', desc='Number of iterations') method = traits.Enum( 'jac', 'lsr', argstr='--resamp=%s', desc=('Final resampling method (jacobian/least ' 'squares)')) repol = traits.Bool( False, argstr='--repol', desc='Detect and replace outlier slices') num_threads = traits.Int( 1, usedefault=True, nohash=True, desc="Number of openmp threads to use") is_shelled = traits.Bool( False, argstr='--data_is_shelled', desc="Override internal check to ensure that " "date are acquired on a set of b-value " "shells") field = traits.Str( argstr='--field=%s', desc="NonTOPUP fieldmap scaled in Hz - filename has " "to be provided without an extension. TOPUP is " "strongly recommended") field_mat = File( exists=True, argstr='--field_mat=%s', desc="Matrix that specifies the relative locations of " "the field specified by --field and first volume " "in file --imain") use_cuda = traits.Bool(False, desc="Run eddy using cuda gpu") class EddyOutputSpec(TraitedSpec): out_corrected = File( exists=True, desc='4D image file containing all the corrected volumes') out_parameter = File( exists=True, desc=('text file with parameters definining the field and' 'movement for each scan')) out_rotated_bvecs = File( exists=True, desc='File containing rotated b-values for all volumes') out_movement_rms = File( exists=True, desc='Summary of the "total movement" in each volume') out_restricted_movement_rms = File( exists=True, desc=('Summary of the "total movement" in each volume ' 'disregarding translation in the PE direction')) out_shell_alignment_parameters = File( exists=True, desc=('File containing rigid body movement parameters ' 'between the different shells as estimated by a ' 'post-hoc mutual information based registration')) out_outlier_report = File( exists=True, desc=('Text-file with a plain language report on what ' 'outlier slices eddy has found')) class Eddy(CommandLine): """ Interface for FSL eddy, a tool for estimating and correcting eddy currents induced distortions. `User guide <http://fsl.fmrib.ox.ac.uk/fsl/fslwiki/Eddy/UsersGuide>`_ and `more info regarding acqp file <http://fsl.fmrib.ox.ac.uk/fsl/fslwiki/eddy/Faq#How_do_I_know_what_to_put_into_my_--acqp_file>`_. Examples -------- >>> from nipype.interfaces.fsl import Eddy >>> eddy = Eddy() >>> eddy.inputs.in_file = 'epi.nii' >>> eddy.inputs.in_mask = 'epi_mask.nii' >>> eddy.inputs.in_index = 'epi_index.txt' >>> eddy.inputs.in_acqp = 'epi_acqp.txt' >>> eddy.inputs.in_bvec = 'bvecs.scheme' >>> eddy.inputs.in_bval = 'bvals.scheme' >>> eddy.inputs.use_cuda = True >>> eddy.cmdline # doctest: +ELLIPSIS 'eddy_cuda --ff=10.0 --acqp=epi_acqp.txt --bvals=bvals.scheme \ --bvecs=bvecs.scheme --imain=epi.nii --index=epi_index.txt \ --mask=epi_mask.nii --niter=5 --nvoxhp=1000 --out=.../eddy_corrected' >>> eddy.inputs.use_cuda = False >>> eddy.cmdline # doctest: +ELLIPSIS 'eddy_openmp --ff=10.0 --acqp=epi_acqp.txt --bvals=bvals.scheme \ --bvecs=bvecs.scheme --imain=epi.nii --index=epi_index.txt \ --mask=epi_mask.nii --niter=5 --nvoxhp=1000 --out=.../eddy_corrected' >>> res = eddy.run() # doctest: +SKIP """ _cmd = '/data/pt_life_dti/scripts/life2018/eddy_openmp-5.0.11' input_spec = EddyInputSpec output_spec = EddyOutputSpec _num_threads = 1 def __init__(self, **inputs): super(Eddy, self).__init__(**inputs) self.inputs.on_trait_change(self._num_threads_update, 'num_threads') if not isdefined(self.inputs.num_threads): self.inputs.num_threads = self._num_threads else: self._num_threads_update() self.inputs.on_trait_change(self._use_cuda, 'use_cuda') if isdefined(self.inputs.use_cuda): self._use_cuda() def
_num_threads_update
identifier_name
lib.rs
values and //! intuitive syntax. //! //! - `local-offset` (_implicitly enables `std`_) //! //! This feature enables a number of methods that allow obtaining the system's //! UTC offset. //! //! - `large-dates` //! //! By default, only years within the ±9999 range (inclusive) are supported. //! If you need support for years outside this range, consider enabling this //! feature; the supported range will be increased to ±999,999. //! //! Note that enabling this feature has some costs, as it means forgoing some //! optimizations. Ambiguities may be introduced when parsing that would not //! otherwise exist. //! //! - `serde` //! //! Enables [serde](https://docs.rs/serde) support for all types. //! //! - `rand` //! //! Enables [rand](https://docs.rs/rand) support for all types. //! //! - `quickcheck` (_implicitly enables `rand`_) //! //! Enables [quickcheck](https://docs.rs/quickcheck) support for all types except [`Instant`]. #![cfg_attr(__time_03_docs, feature(doc_cfg))] #![cfg_attr(__time_03_docs, deny(broken_intra_doc_links))] #![cfg_attr(not(feature = "std"), no_std)] #![deny( anonymous_parameters, clippy::all, const_err, illegal_floating_point_literal_pattern, late_bound_lifetime_arguments, path_statements, patterns_in_fns_without_body, rust_2018_idioms, trivial_casts, trivial_numeric_casts, unreachable_pub, unsafe_code, unused_extern_crates )] #![warn( clippy::dbg_macro, clippy::decimal_literal_representation, clippy::get_unwrap, clippy::missing_docs_in_private_items, clippy::nursery, clippy::pedantic, clippy::print_stdout, clippy::todo, clippy::unimplemented, clippy::unwrap_used, clippy::use_debug, missing_copy_implementations, missing_debug_implementations, unused_qualifications, variant_size_differences )] #![allow( clippy::cast_lossless, clippy::cast_possible_truncation, clippy::cast_possible_wrap, clippy::cast_precision_loss, clippy::cast_sign_loss, clippy::enum_glob_use, clippy::map_err_ignore, clippy::missing_errors_doc, clippy::must_use_candidate, clippy::redundant_pub_crate, clippy::wildcard_imports )] #![doc(html_favicon_url = "https://avatars0.githubusercontent.com/u/55999857")] #![doc(html_logo_url = "https://avatars0.githubusercontent.com/u/55999857")] #![doc(test(attr(deny(warnings))))] #[cfg(feature = "alloc")] extern crate alloc; /// Returns `Err(error::ComponentRange)` if the value is not in range. macro_rules! ensure_value_in_range { ($value:ident in $start:expr => $end:expr) => {{ #![allow(trivial_numeric_casts, unused_comparisons)] if $value < $start || $value > $end { return Err(crate::error::ComponentRange { name: stringify!($value), minimum: $start as _, maximum: $end as _, value: $value as _, conditional_range: false, }); } }}; ($value:ident conditionally in $start:expr => $end:expr) => {{ #![allow(trivial_numeric_casts, unused_comparisons)] if $value < $start || $value > $end { return Err(crate::error::ComponentRange { name: stringify!($value), minimum: $start as _, maximum: $end as _, value: $value as _, conditional_range: true, }); } }}; } /// Try to unwrap an expression, returning if not possible. /// /// This is similar to the `?` operator, but does not perform `.into()`. Because /// of this, it is usable in `const` contexts.
Err(error) => return Err(error), } }; } /// Try to unwrap an expression, returning if not possible. /// /// This is similar to the `?` operator, but is usable in `const` contexts. macro_rules! const_try_opt { ($e:expr) => { match $e { Some(value) => value, None => return None, } }; } /// The [`Date`] struct and its associated `impl`s. mod date; /// The [`Duration`] struct and its associated `impl`s. mod duration; /// Various error types returned by methods in the time crate. pub mod error; /// Extension traits. pub mod ext; pub mod format_description; mod formatting; mod hack; /// The [`Instant`] struct and its associated `impl`s. #[cfg(feature = "std")] #[cfg_attr(__time_03_docs, doc(cfg(feature = "std")))] mod instant; /// The [`OffsetDateTime`] struct and its associated `impl`s. mod offset_date_time; /// The [`PrimitiveDateTime`] struct and its associated `impl`s. mod primitive_date_time; #[cfg(feature = "quickcheck")] #[cfg_attr(__time_03_docs, doc(cfg(feature = "quickcheck")))] mod quickcheck; #[cfg(feature = "rand")] #[cfg_attr(__time_03_docs, doc(cfg(feature = "rand")))] mod rand; #[cfg(feature = "serde")] #[cfg_attr(__time_03_docs, doc(cfg(feature = "serde")))] #[allow(missing_copy_implementations, missing_debug_implementations)] pub mod serde; /// The [`Time`] struct and its associated `impl`s. mod time; /// The [`UtcOffset`] struct and its associated `impl`s. mod utc_offset; pub mod util; /// Days of the week. mod weekday; /// Macros to construct statically known values. #[cfg(feature = "macros")] #[cfg_attr(__time_03_docs, doc(cfg(feature = "macros")))] pub mod macros { /// Construct a [`Date`](crate::Date) with a statically known value. /// /// The resulting expression can be used in `const` or `static` declarations. /// /// Three formats are supported: year-week-weekday, year-ordinal, and /// year-month-day. /// /// ```rust /// # use time::{Date, Weekday::*}; /// # use time_macros::date; /// assert_eq!( /// date!("2020-W01-3"), /// Date::from_iso_week_date(2020, 1, Wednesday)? /// ); /// assert_eq!(date!("2020-001"), Date::from_ordinal_date(2020, 1)?); /// assert_eq!(date!("2020-01-01"), Date::from_calendar_date(2020, 1, 1)?); /// # Ok::<_, time::Error>(()) /// ``` pub use time_macros::date; /// Construct a [`PrimitiveDateTime`] or [`OffsetDateTime`] with a /// statically known value. /// /// The resulting expression can be used in `const` or `static` declarations. /// /// The syntax accepted by this macro is the same as [`date!`] and /// [`time!`], with an optional [`offset!`], all space-separated. If an /// [`offset!`] is provided, the resulting value will be an /// [`OffsetDateTime`]; otherwise it will be a [`PrimitiveDateTime`]. /// /// [`date!`]: crate::macros::date /// [`time!`]: crate::macros::time /// [`offset!`]: crate::macros::offset /// [`OffsetDateTime`]: crate::OffsetDateTime /// [`PrimitiveDateTime`]: crate::PrimitiveDateTime pub use time_macros::datetime; /// Construct a [`UtcOffset`](crate::UtcOffset) with a statically known value. /// /// The resulting expression can be used in `const` or `static` declarations. /// /// A sign and the hour must be provided; minutes and seconds default to zero. /// `UTC` (both uppercase and lowercase) is also allowed. /// /// ```rust /// # use time::UtcOffset; /// # use time_macros::offset; /// assert_eq!(offset!("UTC"), UtcOffset::from_hms(0, 0, 0)?); /// assert_eq!(offset!("utc"), UtcOffset::from_hms(0, 0, 0)?); /// assert_eq!(offset!("+0"), UtcOffset::from_hms(0, 0, 0)?); /// assert_eq!(offset!("+1"), UtcOffset::from_hms(1, 0, 0)?); /// assert_eq!(offset!("-1"), UtcOffset::from_hms(-1, 0, 0)?); /// assert_eq!(offset!("+1:30"), UtcOffset::from_hms(1, 30, 0)?); /// assert_eq!(offset!("-1:30
macro_rules! const_try { ($e:expr) => { match $e { Ok(value) => value,
random_line_split
durconn.go
stanOptPingInterval int stanOptPingMaxOut int stanOptPubAckWait time.Duration connectCb func(stan.Conn) disconnectCb func(stan.Conn) subscribeCb func(sc stan.Conn, spec MsgSpec) wg sync.WaitGroup // wait for go routines connectMu sync.Mutex // at most on connect can be run at any time mu sync.RWMutex // to protect mutable fields // Mutable fields. closed bool subs map[[2]string]*subscription // (subject, queue) -> subscriptioin sc stan.Conn // nil if DurConn has not connected or is reconnecting scStaleCh chan struct{} // pair with sc, it will closed when sc is stale } type subscription struct { spec MsgSpec queue string handler MsgHandler stanOptions []stan.SubscriptionOption decoder npenc.Decoder } // DurConnOption is option in creating DurConn. type DurConnOption func(*DurConn) error // SubOption is option in subscription. type SubOption func(*subscription) error // NewDurConn creates a new DurConn. `nc` must have MaxReconnect < 0 // (e.g. never give up trying to reconnect). // `clusterID` is the nats-streaming-server's cluster id. func NewDurConn(nc *nats.Conn, clusterID string, opts ...DurConnOption) (durConn *DurConn, err error) { if nc.Opts.MaxReconnect >= 0 { return nil, ErrNCMaxReconnect } dc := &DurConn{ nc: nc, clusterID: clusterID, ctx: context.Background(), subjectPrefix: DefaultSubjectPrefix, runner: limitedrunner.Must(), logger: logr.Nop, reconnectWait: DefaultReconnectWait, subRetryWait: DefaultSubRetryWait, stanOptPingInterval: DefaultStanPingInterval, stanOptPingMaxOut: DefaultStanPingMaxOut, stanOptPubAckWait: DefaultStanPubAckWait, connectCb: func(_ stan.Conn) {}, disconnectCb: func(_ stan.Conn) {}, subscribeCb: func(_ stan.Conn, _ MsgSpec) {}, subs: make(map[[2]string]*subscription), } defer func() { if err != nil { dc.runner.Close() } }() for _, opt := range opts { if err = opt(dc); err != nil { return nil, err } } dc.goConnect(false) return dc, nil } // NewPublisher creates a publisher using specified encoder. func (dc *DurConn) NewPublisher(encoder npenc.Encoder) MsgAsyncPublisherFunc { return func(ctx context.Context, spec MsgSpec, msg interface{}, cb func(error)) error { return dc.publishAsync(ctx, spec, msg, encoder, cb) } } // NewSubscriber creates a subscriber using specified decoder. func (dc *DurConn) NewSubscriber(decoder npenc.Decoder) MsgSubscriberFunc { return func(spec MsgSpec, queue string, handler MsgHandler, opts ...interface{}) error { sub := &subscription{ spec: spec, queue: queue, handler: handler, stanOptions: []stan.SubscriptionOption{}, decoder: decoder, } for _, opt := range opts { option, ok := opt.(SubOption) if !ok { return fmt.Errorf("Expect SubOption but got %v", opt) } if err := option(sub); err != nil { return err } } return dc.subscribeOne(sub) } } func (dc *DurConn) goConnect(wait bool) { dc.wg.Add(1) go func() { defer dc.wg.Done() dc.connect(wait) }() } func (dc *DurConn) connect(wait bool) { if wait
dc.connectMu.Lock() defer dc.connectMu.Unlock() // Reset connection: release old connection { dc.mu.Lock() if dc.closed { dc.mu.Unlock() dc.logger.Info("closed when reseting connection") return } sc := dc.sc scStaleCh := dc.scStaleCh dc.sc = nil dc.scStaleCh = nil dc.mu.Unlock() if sc != nil { sc.Close() close(scStaleCh) } } // Connect var sc stan.Conn { opts := []stan.Option{ stan.Pings(dc.stanOptPingInterval, dc.stanOptPingMaxOut), stan.PubAckWait(dc.stanOptPubAckWait), stan.NatsConn(dc.nc), // NOTE: ConnectionLostHandler is used to be notified if the Streaming connection // is closed due to unexpected errors. // The callback will not be invoked on normal Conn.Close(). stan.SetConnectionLostHandler(func(sc stan.Conn, err error) { dc.logger.Error(err, "connection lost") dc.disconnectCb(sc) // reconnect after a while dc.goConnect(true) }), } // NOTE: Use a UUID-like id as client id sine we only use durable queue subscription. // See: https://groups.google.com/d/msg/natsio/SkWAdSU1AgU/tCX9f3ONBQAJ var err error sc, err = stan.Connect(dc.clusterID, xid.New().String(), opts...) if err != nil { // reconnect immediately. dc.logger.Error(err, "connect failed") dc.goConnect(true) return } dc.logger.Info("connect successfully") dc.connectCb(sc) } // Update new connection var subs []*subscription scStaleCh := make(chan struct{}) { dc.mu.Lock() if dc.closed { dc.mu.Unlock() sc.Close() close(scStaleCh) dc.logger.Info("closed when updating connection") return } dc.sc = sc dc.scStaleCh = scStaleCh for _, sub := range dc.subs { subs = append(subs, sub) } dc.mu.Unlock() } // Re-subscribe dc.goSubscribeAll(subs, sc, scStaleCh) } func (dc *DurConn) publishAsync(ctx context.Context, spec MsgSpec, msg interface{}, encoder npenc.Encoder, cb func(error)) error { if err := AssertMsgType(spec, msg); err != nil { return err } m := &nppbmsg.MessageWithMD{ MetaData: nppbmd.NewMetaData(npmd.MDFromOutgoingContext(ctx)), } if err := encoder.EncodeData(msg, &m.MsgFormat, &m.MsgBytes); err != nil { return err } mData, err := proto.Marshal(m) if err != nil { return err } dc.mu.RLock() closed := dc.closed sc := dc.sc dc.mu.RUnlock() if closed { return ErrClosed } if sc == nil { return ErrNotConnected } // Publish. // TODO: sc.PublishAsync maybe block in some rare condition: // see https://github.com/nats-io/stan.go/issues/210 _, err = sc.PublishAsync( subjectFormat(dc.subjectPrefix, spec.SubjectName()), mData, func(_ string, err error) { cb(err) }, ) return err } func (dc *DurConn) subscribeOne(sub *subscription) error { key := [2]string{sub.spec.SubjectName(), sub.queue} dc.mu.Lock() defer dc.mu.Unlock() if dc.closed { return ErrClosed } if _, ok := dc.subs[key]; ok { return ErrDupSubscription } // subscribe if sc is not nil if dc.sc != nil { if err := dc.subscribe(sub, dc.sc); err != nil { return err } } dc.subs[key] = sub return nil } func (dc *DurConn) goSubscribeAll(subs []*subscription, sc stan.Conn, scStaleCh chan struct{}) { dc.wg.Add(1) go func() { defer dc.wg.Done() dc.subscribeAll(subs, sc, scStaleCh) }() } // NOTE: subscribe until all success or scStaleCh closed. func (dc *DurConn) subscribeAll(subs []*subscription, sc stan.Conn, scStaleCh chan struct{}) { success := make([]bool, len(subs)) for { n := 0 for i, sub := range subs { if success[i] { // Already success. n++ continue } if err := dc.subscribe(sub, sc); err != nil { continue } success[i] = true n++ select { case <-scStaleCh: dc.logger.Info("subscribe stale") return default: } } if n >= len(subs) { // All success. return } select { case <-scStaleCh: dc.logger.Info("subscribe stale during retry wait") return
{ time.Sleep(dc.reconnectWait) }
conditional_block
durconn.go
.Duration stanOptPingInterval int stanOptPingMaxOut int stanOptPubAckWait time.Duration connectCb func(stan.Conn) disconnectCb func(stan.Conn) subscribeCb func(sc stan.Conn, spec MsgSpec) wg sync.WaitGroup // wait for go routines connectMu sync.Mutex // at most on connect can be run at any time mu sync.RWMutex // to protect mutable fields // Mutable fields. closed bool subs map[[2]string]*subscription // (subject, queue) -> subscriptioin sc stan.Conn // nil if DurConn has not connected or is reconnecting scStaleCh chan struct{} // pair with sc, it will closed when sc is stale } type subscription struct { spec MsgSpec queue string handler MsgHandler stanOptions []stan.SubscriptionOption decoder npenc.Decoder } // DurConnOption is option in creating DurConn. type DurConnOption func(*DurConn) error // SubOption is option in subscription. type SubOption func(*subscription) error // NewDurConn creates a new DurConn. `nc` must have MaxReconnect < 0 // (e.g. never give up trying to reconnect). // `clusterID` is the nats-streaming-server's cluster id. func NewDurConn(nc *nats.Conn, clusterID string, opts ...DurConnOption) (durConn *DurConn, err error) { if nc.Opts.MaxReconnect >= 0 { return nil, ErrNCMaxReconnect } dc := &DurConn{ nc: nc, clusterID: clusterID, ctx: context.Background(), subjectPrefix: DefaultSubjectPrefix, runner: limitedrunner.Must(), logger: logr.Nop, reconnectWait: DefaultReconnectWait, subRetryWait: DefaultSubRetryWait, stanOptPingInterval: DefaultStanPingInterval,
subs: make(map[[2]string]*subscription), } defer func() { if err != nil { dc.runner.Close() } }() for _, opt := range opts { if err = opt(dc); err != nil { return nil, err } } dc.goConnect(false) return dc, nil } // NewPublisher creates a publisher using specified encoder. func (dc *DurConn) NewPublisher(encoder npenc.Encoder) MsgAsyncPublisherFunc { return func(ctx context.Context, spec MsgSpec, msg interface{}, cb func(error)) error { return dc.publishAsync(ctx, spec, msg, encoder, cb) } } // NewSubscriber creates a subscriber using specified decoder. func (dc *DurConn) NewSubscriber(decoder npenc.Decoder) MsgSubscriberFunc { return func(spec MsgSpec, queue string, handler MsgHandler, opts ...interface{}) error { sub := &subscription{ spec: spec, queue: queue, handler: handler, stanOptions: []stan.SubscriptionOption{}, decoder: decoder, } for _, opt := range opts { option, ok := opt.(SubOption) if !ok { return fmt.Errorf("Expect SubOption but got %v", opt) } if err := option(sub); err != nil { return err } } return dc.subscribeOne(sub) } } func (dc *DurConn) goConnect(wait bool) { dc.wg.Add(1) go func() { defer dc.wg.Done() dc.connect(wait) }() } func (dc *DurConn) connect(wait bool) { if wait { time.Sleep(dc.reconnectWait) } dc.connectMu.Lock() defer dc.connectMu.Unlock() // Reset connection: release old connection { dc.mu.Lock() if dc.closed { dc.mu.Unlock() dc.logger.Info("closed when reseting connection") return } sc := dc.sc scStaleCh := dc.scStaleCh dc.sc = nil dc.scStaleCh = nil dc.mu.Unlock() if sc != nil { sc.Close() close(scStaleCh) } } // Connect var sc stan.Conn { opts := []stan.Option{ stan.Pings(dc.stanOptPingInterval, dc.stanOptPingMaxOut), stan.PubAckWait(dc.stanOptPubAckWait), stan.NatsConn(dc.nc), // NOTE: ConnectionLostHandler is used to be notified if the Streaming connection // is closed due to unexpected errors. // The callback will not be invoked on normal Conn.Close(). stan.SetConnectionLostHandler(func(sc stan.Conn, err error) { dc.logger.Error(err, "connection lost") dc.disconnectCb(sc) // reconnect after a while dc.goConnect(true) }), } // NOTE: Use a UUID-like id as client id sine we only use durable queue subscription. // See: https://groups.google.com/d/msg/natsio/SkWAdSU1AgU/tCX9f3ONBQAJ var err error sc, err = stan.Connect(dc.clusterID, xid.New().String(), opts...) if err != nil { // reconnect immediately. dc.logger.Error(err, "connect failed") dc.goConnect(true) return } dc.logger.Info("connect successfully") dc.connectCb(sc) } // Update new connection var subs []*subscription scStaleCh := make(chan struct{}) { dc.mu.Lock() if dc.closed { dc.mu.Unlock() sc.Close() close(scStaleCh) dc.logger.Info("closed when updating connection") return } dc.sc = sc dc.scStaleCh = scStaleCh for _, sub := range dc.subs { subs = append(subs, sub) } dc.mu.Unlock() } // Re-subscribe dc.goSubscribeAll(subs, sc, scStaleCh) } func (dc *DurConn) publishAsync(ctx context.Context, spec MsgSpec, msg interface{}, encoder npenc.Encoder, cb func(error)) error { if err := AssertMsgType(spec, msg); err != nil { return err } m := &nppbmsg.MessageWithMD{ MetaData: nppbmd.NewMetaData(npmd.MDFromOutgoingContext(ctx)), } if err := encoder.EncodeData(msg, &m.MsgFormat, &m.MsgBytes); err != nil { return err } mData, err := proto.Marshal(m) if err != nil { return err } dc.mu.RLock() closed := dc.closed sc := dc.sc dc.mu.RUnlock() if closed { return ErrClosed } if sc == nil { return ErrNotConnected } // Publish. // TODO: sc.PublishAsync maybe block in some rare condition: // see https://github.com/nats-io/stan.go/issues/210 _, err = sc.PublishAsync( subjectFormat(dc.subjectPrefix, spec.SubjectName()), mData, func(_ string, err error) { cb(err) }, ) return err } func (dc *DurConn) subscribeOne(sub *subscription) error { key := [2]string{sub.spec.SubjectName(), sub.queue} dc.mu.Lock() defer dc.mu.Unlock() if dc.closed { return ErrClosed } if _, ok := dc.subs[key]; ok { return ErrDupSubscription } // subscribe if sc is not nil if dc.sc != nil { if err := dc.subscribe(sub, dc.sc); err != nil { return err } } dc.subs[key] = sub return nil } func (dc *DurConn) goSubscribeAll(subs []*subscription, sc stan.Conn, scStaleCh chan struct{}) { dc.wg.Add(1) go func() { defer dc.wg.Done() dc.subscribeAll(subs, sc, scStaleCh) }() } // NOTE: subscribe until all success or scStaleCh closed. func (dc *DurConn) subscribeAll(subs []*subscription, sc stan.Conn, scStaleCh chan struct{}) { success := make([]bool, len(subs)) for { n := 0 for i, sub := range subs { if success[i] { // Already success. n++ continue } if err := dc.subscribe(sub, sc); err != nil { continue } success[i] = true n++ select { case <-scStaleCh: dc.logger.Info("subscribe stale") return default: } } if n >= len(subs) { // All success. return } select { case <-scStaleCh: dc.logger.Info("subscribe stale during retry wait") return case
stanOptPingMaxOut: DefaultStanPingMaxOut, stanOptPubAckWait: DefaultStanPubAckWait, connectCb: func(_ stan.Conn) {}, disconnectCb: func(_ stan.Conn) {}, subscribeCb: func(_ stan.Conn, _ MsgSpec) {},
random_line_split
durconn.go
spec, msg, encoder, cb) } } // NewSubscriber creates a subscriber using specified decoder. func (dc *DurConn) NewSubscriber(decoder npenc.Decoder) MsgSubscriberFunc { return func(spec MsgSpec, queue string, handler MsgHandler, opts ...interface{}) error { sub := &subscription{ spec: spec, queue: queue, handler: handler, stanOptions: []stan.SubscriptionOption{}, decoder: decoder, } for _, opt := range opts { option, ok := opt.(SubOption) if !ok { return fmt.Errorf("Expect SubOption but got %v", opt) } if err := option(sub); err != nil { return err } } return dc.subscribeOne(sub) } } func (dc *DurConn) goConnect(wait bool) { dc.wg.Add(1) go func() { defer dc.wg.Done() dc.connect(wait) }() } func (dc *DurConn) connect(wait bool) { if wait { time.Sleep(dc.reconnectWait) } dc.connectMu.Lock() defer dc.connectMu.Unlock() // Reset connection: release old connection { dc.mu.Lock() if dc.closed { dc.mu.Unlock() dc.logger.Info("closed when reseting connection") return } sc := dc.sc scStaleCh := dc.scStaleCh dc.sc = nil dc.scStaleCh = nil dc.mu.Unlock() if sc != nil { sc.Close() close(scStaleCh) } } // Connect var sc stan.Conn { opts := []stan.Option{ stan.Pings(dc.stanOptPingInterval, dc.stanOptPingMaxOut), stan.PubAckWait(dc.stanOptPubAckWait), stan.NatsConn(dc.nc), // NOTE: ConnectionLostHandler is used to be notified if the Streaming connection // is closed due to unexpected errors. // The callback will not be invoked on normal Conn.Close(). stan.SetConnectionLostHandler(func(sc stan.Conn, err error) { dc.logger.Error(err, "connection lost") dc.disconnectCb(sc) // reconnect after a while dc.goConnect(true) }), } // NOTE: Use a UUID-like id as client id sine we only use durable queue subscription. // See: https://groups.google.com/d/msg/natsio/SkWAdSU1AgU/tCX9f3ONBQAJ var err error sc, err = stan.Connect(dc.clusterID, xid.New().String(), opts...) if err != nil { // reconnect immediately. dc.logger.Error(err, "connect failed") dc.goConnect(true) return } dc.logger.Info("connect successfully") dc.connectCb(sc) } // Update new connection var subs []*subscription scStaleCh := make(chan struct{}) { dc.mu.Lock() if dc.closed { dc.mu.Unlock() sc.Close() close(scStaleCh) dc.logger.Info("closed when updating connection") return } dc.sc = sc dc.scStaleCh = scStaleCh for _, sub := range dc.subs { subs = append(subs, sub) } dc.mu.Unlock() } // Re-subscribe dc.goSubscribeAll(subs, sc, scStaleCh) } func (dc *DurConn) publishAsync(ctx context.Context, spec MsgSpec, msg interface{}, encoder npenc.Encoder, cb func(error)) error { if err := AssertMsgType(spec, msg); err != nil { return err } m := &nppbmsg.MessageWithMD{ MetaData: nppbmd.NewMetaData(npmd.MDFromOutgoingContext(ctx)), } if err := encoder.EncodeData(msg, &m.MsgFormat, &m.MsgBytes); err != nil { return err } mData, err := proto.Marshal(m) if err != nil { return err } dc.mu.RLock() closed := dc.closed sc := dc.sc dc.mu.RUnlock() if closed { return ErrClosed } if sc == nil { return ErrNotConnected } // Publish. // TODO: sc.PublishAsync maybe block in some rare condition: // see https://github.com/nats-io/stan.go/issues/210 _, err = sc.PublishAsync( subjectFormat(dc.subjectPrefix, spec.SubjectName()), mData, func(_ string, err error) { cb(err) }, ) return err } func (dc *DurConn) subscribeOne(sub *subscription) error { key := [2]string{sub.spec.SubjectName(), sub.queue} dc.mu.Lock() defer dc.mu.Unlock() if dc.closed { return ErrClosed } if _, ok := dc.subs[key]; ok { return ErrDupSubscription } // subscribe if sc is not nil if dc.sc != nil { if err := dc.subscribe(sub, dc.sc); err != nil { return err } } dc.subs[key] = sub return nil } func (dc *DurConn) goSubscribeAll(subs []*subscription, sc stan.Conn, scStaleCh chan struct{}) { dc.wg.Add(1) go func() { defer dc.wg.Done() dc.subscribeAll(subs, sc, scStaleCh) }() } // NOTE: subscribe until all success or scStaleCh closed. func (dc *DurConn) subscribeAll(subs []*subscription, sc stan.Conn, scStaleCh chan struct{}) { success := make([]bool, len(subs)) for { n := 0 for i, sub := range subs { if success[i] { // Already success. n++ continue } if err := dc.subscribe(sub, sc); err != nil { continue } success[i] = true n++ select { case <-scStaleCh: dc.logger.Info("subscribe stale") return default: } } if n >= len(subs) { // All success. return } select { case <-scStaleCh: dc.logger.Info("subscribe stale during retry wait") return case <-time.After(dc.subRetryWait): } } } func (dc *DurConn) subscribe(sub *subscription, sc stan.Conn) error { fullSubject := subjectFormat(dc.subjectPrefix, sub.spec.SubjectName()) opts := []stan.SubscriptionOption{} opts = append(opts, sub.stanOptions...) opts = append(opts, stan.SetManualAckMode()) // Use manual ack mode. opts = append(opts, stan.DurableName(sub.queue)) // Queue as durable name. _, err := sc.QueueSubscribe(fullSubject, sub.queue, dc.msgHandler(sub), opts...) if err != nil { dc.logger.Error(err, "subscribe error", "subject", sub.spec.SubjectName(), "queue", sub.queue) return err } dc.logger.Info("subscribe successfully", "subject", sub.spec.SubjectName(), "queue", sub.queue) dc.subscribeCb(sc, sub.spec) return nil } func (dc *DurConn) msgHandler(sub *subscription) stan.MsgHandler { logger := dc.logger.WithValues("subject", sub.spec.SubjectName(), "queue", sub.queue) return func(stanMsg *stan.Msg) { if err := dc.runner.Submit(func() { defer func() { if e := recover(); e != nil { err, ok := e.(error) if !ok { err = fmt.Errorf("%+v", e) } logger.Error(err, "handler panic") } }() m := &nppbmsg.MessageWithMD{} if err := proto.Unmarshal(stanMsg.Data, m); err != nil { logger.Error(err, "unmarshal msg error", "data", stanMsg.Data) return } msg := sub.spec.NewMsg() if err := sub.decoder.DecodeData(m.MsgFormat, m.MsgBytes, msg); err != nil { logger.Error(err, "decode msg error") return } ctx := dc.ctx if len(m.MetaData) != 0 { ctx = npmd.NewIncomingContextWithMD(ctx, nppbmd.MetaData(m.MetaData)) } if err := sub.handler(ctx, msg); err != nil { // NOTE: do not print handle's error log. Let the handler do it itself. return } // Ack if no error. stanMsg.Ack() return }); err != nil { logger.Error(err, "submit task error") } } } // Close shutdowns the DurConn: closes handler runner and disconnects from nats-streaming-server. func (dc *DurConn) Close()
{ dc.mu.Lock() if dc.closed { dc.mu.Unlock() return } sc := dc.sc scStaleCh := dc.scStaleCh dc.sc = nil dc.scStaleCh = nil dc.closed = true dc.mu.Unlock() if sc != nil { sc.Close() close(scStaleCh) } dc.runner.Close() dc.wg.Wait()
identifier_body
durconn.go
stanOptPingInterval int stanOptPingMaxOut int stanOptPubAckWait time.Duration connectCb func(stan.Conn) disconnectCb func(stan.Conn) subscribeCb func(sc stan.Conn, spec MsgSpec) wg sync.WaitGroup // wait for go routines connectMu sync.Mutex // at most on connect can be run at any time mu sync.RWMutex // to protect mutable fields // Mutable fields. closed bool subs map[[2]string]*subscription // (subject, queue) -> subscriptioin sc stan.Conn // nil if DurConn has not connected or is reconnecting scStaleCh chan struct{} // pair with sc, it will closed when sc is stale } type subscription struct { spec MsgSpec queue string handler MsgHandler stanOptions []stan.SubscriptionOption decoder npenc.Decoder } // DurConnOption is option in creating DurConn. type DurConnOption func(*DurConn) error // SubOption is option in subscription. type SubOption func(*subscription) error // NewDurConn creates a new DurConn. `nc` must have MaxReconnect < 0 // (e.g. never give up trying to reconnect). // `clusterID` is the nats-streaming-server's cluster id. func NewDurConn(nc *nats.Conn, clusterID string, opts ...DurConnOption) (durConn *DurConn, err error) { if nc.Opts.MaxReconnect >= 0 { return nil, ErrNCMaxReconnect } dc := &DurConn{ nc: nc, clusterID: clusterID, ctx: context.Background(), subjectPrefix: DefaultSubjectPrefix, runner: limitedrunner.Must(), logger: logr.Nop, reconnectWait: DefaultReconnectWait, subRetryWait: DefaultSubRetryWait, stanOptPingInterval: DefaultStanPingInterval, stanOptPingMaxOut: DefaultStanPingMaxOut, stanOptPubAckWait: DefaultStanPubAckWait, connectCb: func(_ stan.Conn) {}, disconnectCb: func(_ stan.Conn) {}, subscribeCb: func(_ stan.Conn, _ MsgSpec) {}, subs: make(map[[2]string]*subscription), } defer func() { if err != nil { dc.runner.Close() } }() for _, opt := range opts { if err = opt(dc); err != nil { return nil, err } } dc.goConnect(false) return dc, nil } // NewPublisher creates a publisher using specified encoder. func (dc *DurConn) NewPublisher(encoder npenc.Encoder) MsgAsyncPublisherFunc { return func(ctx context.Context, spec MsgSpec, msg interface{}, cb func(error)) error { return dc.publishAsync(ctx, spec, msg, encoder, cb) } } // NewSubscriber creates a subscriber using specified decoder. func (dc *DurConn) NewSubscriber(decoder npenc.Decoder) MsgSubscriberFunc { return func(spec MsgSpec, queue string, handler MsgHandler, opts ...interface{}) error { sub := &subscription{ spec: spec, queue: queue, handler: handler, stanOptions: []stan.SubscriptionOption{}, decoder: decoder, } for _, opt := range opts { option, ok := opt.(SubOption) if !ok { return fmt.Errorf("Expect SubOption but got %v", opt) } if err := option(sub); err != nil { return err } } return dc.subscribeOne(sub) } } func (dc *DurConn) goConnect(wait bool) { dc.wg.Add(1) go func() { defer dc.wg.Done() dc.connect(wait) }() } func (dc *DurConn) connect(wait bool) { if wait { time.Sleep(dc.reconnectWait) } dc.connectMu.Lock() defer dc.connectMu.Unlock() // Reset connection: release old connection { dc.mu.Lock() if dc.closed { dc.mu.Unlock() dc.logger.Info("closed when reseting connection") return } sc := dc.sc scStaleCh := dc.scStaleCh dc.sc = nil dc.scStaleCh = nil dc.mu.Unlock() if sc != nil { sc.Close() close(scStaleCh) } } // Connect var sc stan.Conn { opts := []stan.Option{ stan.Pings(dc.stanOptPingInterval, dc.stanOptPingMaxOut), stan.PubAckWait(dc.stanOptPubAckWait), stan.NatsConn(dc.nc), // NOTE: ConnectionLostHandler is used to be notified if the Streaming connection // is closed due to unexpected errors. // The callback will not be invoked on normal Conn.Close(). stan.SetConnectionLostHandler(func(sc stan.Conn, err error) { dc.logger.Error(err, "connection lost") dc.disconnectCb(sc) // reconnect after a while dc.goConnect(true) }), } // NOTE: Use a UUID-like id as client id sine we only use durable queue subscription. // See: https://groups.google.com/d/msg/natsio/SkWAdSU1AgU/tCX9f3ONBQAJ var err error sc, err = stan.Connect(dc.clusterID, xid.New().String(), opts...) if err != nil { // reconnect immediately. dc.logger.Error(err, "connect failed") dc.goConnect(true) return } dc.logger.Info("connect successfully") dc.connectCb(sc) } // Update new connection var subs []*subscription scStaleCh := make(chan struct{}) { dc.mu.Lock() if dc.closed { dc.mu.Unlock() sc.Close() close(scStaleCh) dc.logger.Info("closed when updating connection") return } dc.sc = sc dc.scStaleCh = scStaleCh for _, sub := range dc.subs { subs = append(subs, sub) } dc.mu.Unlock() } // Re-subscribe dc.goSubscribeAll(subs, sc, scStaleCh) } func (dc *DurConn) publishAsync(ctx context.Context, spec MsgSpec, msg interface{}, encoder npenc.Encoder, cb func(error)) error { if err := AssertMsgType(spec, msg); err != nil { return err } m := &nppbmsg.MessageWithMD{ MetaData: nppbmd.NewMetaData(npmd.MDFromOutgoingContext(ctx)), } if err := encoder.EncodeData(msg, &m.MsgFormat, &m.MsgBytes); err != nil { return err } mData, err := proto.Marshal(m) if err != nil { return err } dc.mu.RLock() closed := dc.closed sc := dc.sc dc.mu.RUnlock() if closed { return ErrClosed } if sc == nil { return ErrNotConnected } // Publish. // TODO: sc.PublishAsync maybe block in some rare condition: // see https://github.com/nats-io/stan.go/issues/210 _, err = sc.PublishAsync( subjectFormat(dc.subjectPrefix, spec.SubjectName()), mData, func(_ string, err error) { cb(err) }, ) return err } func (dc *DurConn) subscribeOne(sub *subscription) error { key := [2]string{sub.spec.SubjectName(), sub.queue} dc.mu.Lock() defer dc.mu.Unlock() if dc.closed { return ErrClosed } if _, ok := dc.subs[key]; ok { return ErrDupSubscription } // subscribe if sc is not nil if dc.sc != nil { if err := dc.subscribe(sub, dc.sc); err != nil { return err } } dc.subs[key] = sub return nil } func (dc *DurConn) goSubscribeAll(subs []*subscription, sc stan.Conn, scStaleCh chan struct{}) { dc.wg.Add(1) go func() { defer dc.wg.Done() dc.subscribeAll(subs, sc, scStaleCh) }() } // NOTE: subscribe until all success or scStaleCh closed. func (dc *DurConn)
(subs []*subscription, sc stan.Conn, scStaleCh chan struct{}) { success := make([]bool, len(subs)) for { n := 0 for i, sub := range subs { if success[i] { // Already success. n++ continue } if err := dc.subscribe(sub, sc); err != nil { continue } success[i] = true n++ select { case <-scStaleCh: dc.logger.Info("subscribe stale") return default: } } if n >= len(subs) { // All success. return } select { case <-scStaleCh: dc.logger.Info("subscribe stale during retry wait") return case
subscribeAll
identifier_name
circuitpusher.py
--src {IP} --dst {IP} --add --name {circuit-name} adds a new circuit between src and dst devices Currently ip circuit is supported. ARP is automatically supported. Currently a simple circuit record storage is provided in a text file circuits.json in the working directory. The file is not protected and does not clean itself between controller restarts. The file is needed for correct operation and the user should make sure deleting the file when floodlight controller is restarted. b) circuitpusher.py --controller={IP}:{rest port} --delete --name {circuit-name} deletes a created circuit (as recorded in circuits.json) using the previously given name @author kcwang """ import os import sys import subprocess import json import argparse import io import time # parse circuit options. Currently supports add and delete actions. # Syntax: # circuitpusher --controller {IP:REST_PORT} --add --name {CIRCUIT_NAME} --type ip --src {IP} --dst {IP} # circuitpusher --controller {IP:REST_PORT} --delete --name {CIRCUIT_NAME} parser = argparse.ArgumentParser(description='Circuit Pusher') parser.add_argument('--controller', dest='controllerRestIp', action='store', default='localhost:8080', help='controller IP:RESTport, e.g., localhost:8080 or A.B.C.D:8080') parser.add_argument('--add', dest='action', action='store_const', const='add', default='add', help='action: add, delete') parser.add_argument('--delete', dest='action', action='store_const', const='delete', default='add', help='action: add, delete') parser.add_argument('--type', dest='type', action='store', default='ip', help='valid types: ip') parser.add_argument('--src', dest='srcAddress', action='store', default='0.0.0.0', help='source address: if type=ip, A.B.C.D') parser.add_argument('--dst', dest='dstAddress', action='store', default='0.0.0.0', help='destination address: if type=ip, A.B.C.D') parser.add_argument('--name', dest='circuitName', action='store', default='circuit-1', help='name for circuit, e.g., circuit-1') #user catches if len(sys.argv) == 1: command = './circuitpusher.py -h' instruct = os.popen(command).read() print instruct exit(1) elif sys.argv[1] == "help": command = './circuitpusher.py -h' instruct = os.popen(command).read() print instruct exit(1) #parse arguments args = parser.parse_args() print args controllerRestIp = args.controllerRestIp # first check if a local file exists, which needs to be updated after add/delete if os.path.exists('./circuits.json'):
else: lines={} if args.action=='add': circuitDb = open('./circuits.json','a') for line in lines: data = json.loads(line) if data['name']==(args.circuitName): print "Circuit %s exists already. Use new name to create." % args.circuitName sys.exit() else: circuitExists = False # retrieve source and destination device attachment points # using DeviceManager rest API command = "curl -s http://%s/wm/device/?ipv4=%s" % (args.controllerRestIp, args.srcAddress) result = os.popen(command).read() parsedResult = json.loads(result) print command+"\n" sourceSwitch = parsedResult[0]['attachmentPoint'][0]['switchDPID'] sourcePort = parsedResult[0]['attachmentPoint'][0]['port'] command = "curl -s http://%s/wm/device/?ipv4=%s" % (args.controllerRestIp, args.dstAddress) result = os.popen(command).read() parsedResult = json.loads(result) print command+"\n" destSwitch = parsedResult[0]['attachmentPoint'][0]['switchDPID'] destPort = parsedResult[0]['attachmentPoint'][0]['port'] print "Creating circuit:" print "from source device at switch %s port %s" % (sourceSwitch,sourcePort) print "to destination device at switch %s port %s"% (destSwitch,destPort) # retrieving route from source to destination # using Routing rest API command = "curl -s http://%s/wm/topology/route/%s/%s/%s/%s/json" % (controllerRestIp, sourceSwitch, sourcePort, destSwitch, destPort) result = os.popen(command).read() parsedResult = json.loads(result) print command+"\n" print result+"\n" for i in range(len(parsedResult)): if i % 2 == 0: ap1Dpid = parsedResult[i]['switch'] ap1Port = parsedResult[i]['port'] print ap1Dpid, ap1Port else: ap2Dpid = parsedResult[i]['switch'] ap2Port = parsedResult[i]['port'] print ap2Dpid, ap2Port # send one flow mod per pair of APs in route # using StaticFlowPusher rest API # IMPORTANT NOTE: current Floodlight StaticflowEntryPusher # assumes all flow entries to have unique name across all switches # this will most possibly be relaxed later, but for now we # encode each flow entry's name with both switch dpid, user # specified name, and flow type (f: forward, r: reverse, farp/rarp: arp) command = "curl -s -d '{\"switch\": \"%s\", \"name\":\"%s\", \"src-ip\":\"%s\", \"dst-ip\":\"%s\", \"ether-type\":\"%s\", \"cookie\":\"0\", \"priority\":\"32768\", \"ingress-port\":\"%s\",\"active\":\"true\", \"actions\":\"output=%s\"}' http://%s/wm/staticflowentrypusher/json" % (ap1Dpid, ap1Dpid+"."+args.circuitName+".f", args.srcAddress, args.dstAddress, "0x800", ap1Port, ap2Port, controllerRestIp) result = os.popen(command).read() print command command = "curl -s -d '{\"switch\": \"%s\", \"name\":\"%s\", \"ether-type\":\"%s\", \"cookie\":\"0\", \"priority\":\"32768\", \"ingress-port\":\"%s\",\"active\":\"true\", \"actions\":\"output=%s\"}' http://%s/wm/staticflowentrypusher/json" % (ap1Dpid, ap1Dpid+"."+args.circuitName+".farp", "0x806", ap1Port, ap2Port, controllerRestIp) result = os.popen(command).read() print command command = "curl -s -d '{\"switch\": \"%s\", \"name\":\"%s\", \"src-ip\":\"%s\", \"dst-ip\":\"%s\", \"ether-type\":\"%s\", \"cookie\":\"0\", \"priority\":\"32768\", \"ingress-port\":\"%s\",\"active\":\"true\", \"actions\":\"output=%s\"}' http://%s/wm/staticflowentrypusher/json" % (ap1Dpid, ap1Dpid+"."+args.circuitName+".r", args.dstAddress, args.srcAddress, "0x800", ap2Port, ap1Port, controllerRestIp) result = os.popen(command).read() print command command = "curl -s -d '{\"switch\": \"%s\", \"name\":\"%s\", \"ether-type\":\"%s\", \"cookie\":\"0\", \"priority\":\"32768\", \"ingress-port\":\"%s\",\"active\":\"true\", \"actions\":\"output=%s\"}' http://%s/wm/staticflowentrypusher/json" % (ap1Dpid, ap1Dpid+"."+args.circuitName+".rarp", "0x806", ap2Port, ap1Port, controllerRestIp) result = os.popen(command).read() print command # store created circuit attributes in local ./circuits.json datetime = time.asctime() circuitParams = {'name':args.circuitName, 'Dpid':ap1Dpid, 'inPort':ap1Port, 'outPort':ap2Port, 'datetime':datetime} str = json.dumps(circuitParams) circuitDb.write(str+"\n") # confirm successful circuit creation # using controller rest API command="curl -s http://%s/wm/core/switch/all/flow/json| python -mjson.tool" % (controllerRestIp) result = os.popen(command).read() print command + "\n" + result elif args.action=='delete': circuitDb = open('./circuits.json','w') # removing previously created flow from switches # using StaticFlowPusher rest API # currently, circuitpusher records created circuits in local file ./circuits.db
circuitDb = open('./circuits.json','r') lines = circuitDb.readlines() circuitDb.close()
conditional_block
circuitpusher.py
ip --src {IP} --dst {IP} --add --name {circuit-name} adds a new circuit between src and dst devices Currently ip circuit is supported. ARP is automatically supported. Currently a simple circuit record storage is provided in a text file circuits.json in the working directory. The file is not protected and does not clean itself between controller restarts. The file is needed for correct operation and the user should make sure deleting the file when floodlight controller is restarted. b) circuitpusher.py --controller={IP}:{rest port} --delete --name {circuit-name} deletes a created circuit (as recorded in circuits.json) using the previously given name @author kcwang """ import os import sys import subprocess import json import argparse import io import time # parse circuit options. Currently supports add and delete actions. # Syntax: # circuitpusher --controller {IP:REST_PORT} --add --name {CIRCUIT_NAME} --type ip --src {IP} --dst {IP} # circuitpusher --controller {IP:REST_PORT} --delete --name {CIRCUIT_NAME} parser = argparse.ArgumentParser(description='Circuit Pusher') parser.add_argument('--controller', dest='controllerRestIp', action='store', default='localhost:8080', help='controller IP:RESTport, e.g., localhost:8080 or A.B.C.D:8080') parser.add_argument('--add', dest='action', action='store_const', const='add', default='add', help='action: add, delete') parser.add_argument('--delete', dest='action', action='store_const', const='delete', default='add', help='action: add, delete') parser.add_argument('--type', dest='type', action='store', default='ip', help='valid types: ip') parser.add_argument('--src', dest='srcAddress', action='store', default='0.0.0.0', help='source address: if type=ip, A.B.C.D') parser.add_argument('--dst', dest='dstAddress', action='store', default='0.0.0.0', help='destination address: if type=ip, A.B.C.D') parser.add_argument('--name', dest='circuitName', action='store', default='circuit-1', help='name for circuit, e.g., circuit-1') #user catches if len(sys.argv) == 1: command = './circuitpusher.py -h' instruct = os.popen(command).read() print instruct exit(1) elif sys.argv[1] == "help": command = './circuitpusher.py -h' instruct = os.popen(command).read() print instruct exit(1) #parse arguments
controllerRestIp = args.controllerRestIp # first check if a local file exists, which needs to be updated after add/delete if os.path.exists('./circuits.json'): circuitDb = open('./circuits.json','r') lines = circuitDb.readlines() circuitDb.close() else: lines={} if args.action=='add': circuitDb = open('./circuits.json','a') for line in lines: data = json.loads(line) if data['name']==(args.circuitName): print "Circuit %s exists already. Use new name to create." % args.circuitName sys.exit() else: circuitExists = False # retrieve source and destination device attachment points # using DeviceManager rest API command = "curl -s http://%s/wm/device/?ipv4=%s" % (args.controllerRestIp, args.srcAddress) result = os.popen(command).read() parsedResult = json.loads(result) print command+"\n" sourceSwitch = parsedResult[0]['attachmentPoint'][0]['switchDPID'] sourcePort = parsedResult[0]['attachmentPoint'][0]['port'] command = "curl -s http://%s/wm/device/?ipv4=%s" % (args.controllerRestIp, args.dstAddress) result = os.popen(command).read() parsedResult = json.loads(result) print command+"\n" destSwitch = parsedResult[0]['attachmentPoint'][0]['switchDPID'] destPort = parsedResult[0]['attachmentPoint'][0]['port'] print "Creating circuit:" print "from source device at switch %s port %s" % (sourceSwitch,sourcePort) print "to destination device at switch %s port %s"% (destSwitch,destPort) # retrieving route from source to destination # using Routing rest API command = "curl -s http://%s/wm/topology/route/%s/%s/%s/%s/json" % (controllerRestIp, sourceSwitch, sourcePort, destSwitch, destPort) result = os.popen(command).read() parsedResult = json.loads(result) print command+"\n" print result+"\n" for i in range(len(parsedResult)): if i % 2 == 0: ap1Dpid = parsedResult[i]['switch'] ap1Port = parsedResult[i]['port'] print ap1Dpid, ap1Port else: ap2Dpid = parsedResult[i]['switch'] ap2Port = parsedResult[i]['port'] print ap2Dpid, ap2Port # send one flow mod per pair of APs in route # using StaticFlowPusher rest API # IMPORTANT NOTE: current Floodlight StaticflowEntryPusher # assumes all flow entries to have unique name across all switches # this will most possibly be relaxed later, but for now we # encode each flow entry's name with both switch dpid, user # specified name, and flow type (f: forward, r: reverse, farp/rarp: arp) command = "curl -s -d '{\"switch\": \"%s\", \"name\":\"%s\", \"src-ip\":\"%s\", \"dst-ip\":\"%s\", \"ether-type\":\"%s\", \"cookie\":\"0\", \"priority\":\"32768\", \"ingress-port\":\"%s\",\"active\":\"true\", \"actions\":\"output=%s\"}' http://%s/wm/staticflowentrypusher/json" % (ap1Dpid, ap1Dpid+"."+args.circuitName+".f", args.srcAddress, args.dstAddress, "0x800", ap1Port, ap2Port, controllerRestIp) result = os.popen(command).read() print command command = "curl -s -d '{\"switch\": \"%s\", \"name\":\"%s\", \"ether-type\":\"%s\", \"cookie\":\"0\", \"priority\":\"32768\", \"ingress-port\":\"%s\",\"active\":\"true\", \"actions\":\"output=%s\"}' http://%s/wm/staticflowentrypusher/json" % (ap1Dpid, ap1Dpid+"."+args.circuitName+".farp", "0x806", ap1Port, ap2Port, controllerRestIp) result = os.popen(command).read() print command command = "curl -s -d '{\"switch\": \"%s\", \"name\":\"%s\", \"src-ip\":\"%s\", \"dst-ip\":\"%s\", \"ether-type\":\"%s\", \"cookie\":\"0\", \"priority\":\"32768\", \"ingress-port\":\"%s\",\"active\":\"true\", \"actions\":\"output=%s\"}' http://%s/wm/staticflowentrypusher/json" % (ap1Dpid, ap1Dpid+"."+args.circuitName+".r", args.dstAddress, args.srcAddress, "0x800", ap2Port, ap1Port, controllerRestIp) result = os.popen(command).read() print command command = "curl -s -d '{\"switch\": \"%s\", \"name\":\"%s\", \"ether-type\":\"%s\", \"cookie\":\"0\", \"priority\":\"32768\", \"ingress-port\":\"%s\",\"active\":\"true\", \"actions\":\"output=%s\"}' http://%s/wm/staticflowentrypusher/json" % (ap1Dpid, ap1Dpid+"."+args.circuitName+".rarp", "0x806", ap2Port, ap1Port, controllerRestIp) result = os.popen(command).read() print command # store created circuit attributes in local ./circuits.json datetime = time.asctime() circuitParams = {'name':args.circuitName, 'Dpid':ap1Dpid, 'inPort':ap1Port, 'outPort':ap2Port, 'datetime':datetime} str = json.dumps(circuitParams) circuitDb.write(str+"\n") # confirm successful circuit creation # using controller rest API command="curl -s http://%s/wm/core/switch/all/flow/json| python -mjson.tool" % (controllerRestIp) result = os.popen(command).read() print command + "\n" + result elif args.action=='delete': circuitDb = open('./circuits.json','w') # removing previously created flow from switches # using StaticFlowPusher rest API # currently, circuitpusher records created circuits in local file ./circuits.db # with
args = parser.parse_args() print args
random_line_split
play15old2.rs
; impl Board { pub fn new() -> Self { let mut arr = [[0u8; WIDTH]; HEIGHT]; for y in 0..WIDTH { for x in 0..HEIGHT { arr[y][x] = ((y * WIDTH + x + 1) % (WIDTH * HEIGHT)) as u8 } } Board(arr) } pub fn from_array(arr: [[u8; WIDTH]; HEIGHT]) -> Result<Self, BoardCreateError> { let w = WIDTH; let h = HEIGHT; let mut tile_count = vec![0; w * h]; for y in 0..HEIGHT { for x in 0..WIDTH { tile_count.get_mut(arr[y][x] as usize).map(|x| *x += 1); } } let has_one_of_all = tile_count.iter().all(|x| *x == 1); if has_one_of_all { Ok(Board(arr)) } else { Err(BoardCreateError) } } pub fn size(&self) -> (usize, usize) { (WIDTH, HEIGHT) } pub fn empty_at(&self) -> (usize, usize) { for y in 0..HEIGHT { for x in 0..WIDTH { if self.0[y][x] == 0 { return (x, y); } } } panic!() } #[inline(always)] pub fn swap(&mut self, p1: (usize, usize), p2: (usize, usize)) { let arr = &mut self.0; let t1 = arr[p1.1][p1.0]; let t2 = arr[p2.1][p2.0]; arr[p1.1][p1.0] = t2; arr[p2.1][p2.0] = t1; } pub fn apply(&mut self, dir: Dir) -> Result<(), ()> { let (zx, zy) = self.empty_at(); let (w, h) = self.size(); match dir { Dir::Right if zx < w - 1 => { self.swap((zx, zy), (zx + 1, zy)); Ok(()) } Dir::Down if zy < h - 1 => { self.swap((zx, zy), (zx, zy + 1)); Ok(()) } Dir::Left if zx > 0 => { self.swap((zx, zy), (zx - 1, zy)); Ok(()) } Dir::Up if zy > 0 => { self.swap((zx, zy), (zx, zy - 1)); Ok(()) } _ => Err(()), } } // // pub fn possible_steps_with<F: FnMut(Board, u8)>(&self, mut f: F) { // let (zx, zy) = self.empty_at(); // let w = self.0.shape()[0]; // let h = self.0.shape()[1]; // if zx < w - 1 { // // Направо // let mut b = self.clone(); // b.0.swap((zx, zy), (zx + 1, zy)); // let moved = b.0[(zx, zy)]; // f(b, moved) // } // if zy < h - 1 { // // Вниз // let mut b = self.clone(); // b.0.swap((zx, zy), (zx, zy + 1)); // let moved = b.0[(zx, zy)]; // f(b, moved) // } // if zx > 0 { // // Налево // let mut b = self.clone(); // b.0.swap((zx, zy), (zx - 1, zy)); // let moved = b.0[(zx, zy)]; // f(b, moved) // } // if zy > 0 { // // Вверх // let mut b = self.clone(); // b.0.swap((zx, zy), (zx, zy - 1)); // let moved = b.0[(zx, zy)]; // f(b, moved) // } // } pub fn is_solved(&self) -> bool { let (w, h) = self.size(); for y in 0..h { for x in 0..w { if ((y * w + x + 1) % (w * h)) as u8 != self.0[y][x] { return false; } } } true } pub fn can_solve(&self) -> bool { // let (w, h) = self.size(); // let mut flat = Vec::<u8>::with_capacity(w * h); // for y in 0..h { // for x in 0..w { // flat.push(self.0[(x, y)]); // } // } // let (_zx, zy) = self.empty_at(); // let sum: usize = (0..flat.len()) // .map(|i| { // let c = flat[i] as usize; // let c = if c == 0 { w * h } else { c }; // let k = flat[i..] // .iter() // .map(|x| if *x == 0 { (w * h) } else { *x as usize }) // .filter(|x| *x < c) // .count(); // k // }) // .sum(); // let n = sum + zy; // n % 2 == 0 true } pub fn wrong_tiles(&self) -> usize { let (w, h) = self.size(); let mut c = 0; for y in 0..h { for x in 0..w { if ((y * w + x + 1) % (w * h)) as u8 != self.0[y][x] { c += 1; } } } c } pub fn solve(&self) -> Result<Path, ()> { if !self.can_solve() { return Err(()); } let mut checked_position_length = HashMap::new(); let mut heap = BinaryHeap::with_capacity(1000); heap.push(QPath(Path::new(self.clone()))); let mut i = 0; loop { i += 1; let current = heap.pop().unwrap(); let last = checked_position_length.get_mut(&current.0.current_board); let remove_longer = |heap: &mut BinaryHeap<QPath>, to_remove: Board| { heap.retain(|qpath| qpath.0.current_board != to_remove); }; if i % 10_000 == 0 { println!( "iter = {}e4, path len = {}, euristic = {}, in heap {} el", i / 10_000, current.0.path().len(), current.0.current_board().wrong_tiles(), heap.len() ); } match last { Some(last) if *last <= current.0.path.len() => continue, Some(last) => { *last = current.0.path.len(); //remove_longer(&mut heap, current.0.current_board); } _ => { checked_position_length.insert(current.0.current_board, current.0.path.len()); //remove_longer(&mut heap, current.0.current_board); } } // println!("Current board with {}", current.cost()); if current.0.current_board().is_solved() { return Ok(current.0); } let mut push_or_ignore = |dir| { // Oh... Remove? if heap.len() > 1_000_000 { let mut replacement = BinaryHeap::with_capacity(1_000_005); for _i in 0..10_000 { replacement.push(heap.pop().unwrap()); } heap = replacement; } // ^^^^^^^ let mut c = &current; let path = c.0.push_step_cloned(dir); if let Ok(path) = path { if !checked_position_length.contains_key(path.current_board()) { heap.push(QPath::new(path)); } } }; // 15 2 1 12 8 5 6 11 4 9 10 7 3 14 13 0 push_or_ignore(Dir::Up); push_or_ignore(Dir::Right); push_or_ignore(Dir::Down); push_or_ignore(Dir::Left); } } pub fn inner(&self) -> &[[u8; WIDTH]; HEIGHT] { &self.0 } } impl Display for Board
BoardCreateError
identifier_name
play15old2.rs
.get_mut(arr[y][x] as usize).map(|x| *x += 1); } } let has_one_of_all = tile_count.iter().all(|x| *x == 1); if has_one_of_all { Ok(Board(arr)) } else { Err(BoardCreateError) } } pub fn size(&self) -> (usize, usize) { (WIDTH, HEIGHT) } pub fn empty_at(&self) -> (usize, usize)
#[inline(always)] pub fn swap(&mut self, p1: (usize, usize), p2: (usize, usize)) { let arr = &mut self.0; let t1 = arr[p1.1][p1.0]; let t2 = arr[p2.1][p2.0]; arr[p1.1][p1.0] = t2; arr[p2.1][p2.0] = t1; } pub fn apply(&mut self, dir: Dir) -> Result<(), ()> { let (zx, zy) = self.empty_at(); let (w, h) = self.size(); match dir { Dir::Right if zx < w - 1 => { self.swap((zx, zy), (zx + 1, zy)); Ok(()) } Dir::Down if zy < h - 1 => { self.swap((zx, zy), (zx, zy + 1)); Ok(()) } Dir::Left if zx > 0 => { self.swap((zx, zy), (zx - 1, zy)); Ok(()) } Dir::Up if zy > 0 => { self.swap((zx, zy), (zx, zy - 1)); Ok(()) } _ => Err(()), } } // // pub fn possible_steps_with<F: FnMut(Board, u8)>(&self, mut f: F) { // let (zx, zy) = self.empty_at(); // let w = self.0.shape()[0]; // let h = self.0.shape()[1]; // if zx < w - 1 { // // Направо // let mut b = self.clone(); // b.0.swap((zx, zy), (zx + 1, zy)); // let moved = b.0[(zx, zy)]; // f(b, moved) // } // if zy < h - 1 { // // Вниз // let mut b = self.clone(); // b.0.swap((zx, zy), (zx, zy + 1)); // let moved = b.0[(zx, zy)]; // f(b, moved) // } // if zx > 0 { // // Налево // let mut b = self.clone(); // b.0.swap((zx, zy), (zx - 1, zy)); // let moved = b.0[(zx, zy)]; // f(b, moved) // } // if zy > 0 { // // Вверх // let mut b = self.clone(); // b.0.swap((zx, zy), (zx, zy - 1)); // let moved = b.0[(zx, zy)]; // f(b, moved) // } // } pub fn is_solved(&self) -> bool { let (w, h) = self.size(); for y in 0..h { for x in 0..w { if ((y * w + x + 1) % (w * h)) as u8 != self.0[y][x] { return false; } } } true } pub fn can_solve(&self) -> bool { // let (w, h) = self.size(); // let mut flat = Vec::<u8>::with_capacity(w * h); // for y in 0..h { // for x in 0..w { // flat.push(self.0[(x, y)]); // } // } // let (_zx, zy) = self.empty_at(); // let sum: usize = (0..flat.len()) // .map(|i| { // let c = flat[i] as usize; // let c = if c == 0 { w * h } else { c }; // let k = flat[i..] // .iter() // .map(|x| if *x == 0 { (w * h) } else { *x as usize }) // .filter(|x| *x < c) // .count(); // k // }) // .sum(); // let n = sum + zy; // n % 2 == 0 true } pub fn wrong_tiles(&self) -> usize { let (w, h) = self.size(); let mut c = 0; for y in 0..h { for x in 0..w { if ((y * w + x + 1) % (w * h)) as u8 != self.0[y][x] { c += 1; } } } c } pub fn solve(&self) -> Result<Path, ()> { if !self.can_solve() { return Err(()); } let mut checked_position_length = HashMap::new(); let mut heap = BinaryHeap::with_capacity(1000); heap.push(QPath(Path::new(self.clone()))); let mut i = 0; loop { i += 1; let current = heap.pop().unwrap(); let last = checked_position_length.get_mut(&current.0.current_board); let remove_longer = |heap: &mut BinaryHeap<QPath>, to_remove: Board| { heap.retain(|qpath| qpath.0.current_board != to_remove); }; if i % 10_000 == 0 { println!( "iter = {}e4, path len = {}, euristic = {}, in heap {} el", i / 10_000, current.0.path().len(), current.0.current_board().wrong_tiles(), heap.len() ); } match last { Some(last) if *last <= current.0.path.len() => continue, Some(last) => { *last = current.0.path.len(); //remove_longer(&mut heap, current.0.current_board); } _ => { checked_position_length.insert(current.0.current_board, current.0.path.len()); //remove_longer(&mut heap, current.0.current_board); } } // println!("Current board with {}", current.cost()); if current.0.current_board().is_solved() { return Ok(current.0); } let mut push_or_ignore = |dir| { // Oh... Remove? if heap.len() > 1_000_000 { let mut replacement = BinaryHeap::with_capacity(1_000_005); for _i in 0..10_000 { replacement.push(heap.pop().unwrap()); } heap = replacement; } // ^^^^^^^ let mut c = &current; let path = c.0.push_step_cloned(dir); if let Ok(path) = path { if !checked_position_length.contains_key(path.current_board()) { heap.push(QPath::new(path)); } } }; // 15 2 1 12 8 5 6 11 4 9 10 7 3 14 13 0 push_or_ignore(Dir::Up); push_or_ignore(Dir::Right); push_or_ignore(Dir::Down); push_or_ignore(Dir::Left); } } pub fn inner(&self) -> &[[u8; WIDTH]; HEIGHT] { &self.0 } } impl Display for Board { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { let (w, h) = self.size(); for y in 0..h { for x in 0..w { match w * h { 0..=9 => write!(f, "{:1} ", self.0[y][x])?, 10..=99 => write!(f, "{:2} ", self.0[y][x])?, 100..=999 => write!(f, "{:3} ", self.0[y][x])?, _ => panic!(""), }; } writeln!(f)?; }
{ for y in 0..HEIGHT { for x in 0..WIDTH { if self.0[y][x] == 0 { return (x, y); } } } panic!() }
identifier_body
play15old2.rs
zx, zy), (zx, zy + 1)); Ok(()) } Dir::Left if zx > 0 => { self.swap((zx, zy), (zx - 1, zy)); Ok(()) } Dir::Up if zy > 0 => { self.swap((zx, zy), (zx, zy - 1)); Ok(()) } _ => Err(()), } } // // pub fn possible_steps_with<F: FnMut(Board, u8)>(&self, mut f: F) { // let (zx, zy) = self.empty_at(); // let w = self.0.shape()[0]; // let h = self.0.shape()[1]; // if zx < w - 1 { // // Направо // let mut b = self.clone(); // b.0.swap((zx, zy), (zx + 1, zy)); // let moved = b.0[(zx, zy)]; // f(b, moved) // } // if zy < h - 1 { // // Вниз // let mut b = self.clone(); // b.0.swap((zx, zy), (zx, zy + 1)); // let moved = b.0[(zx, zy)]; // f(b, moved) // } // if zx > 0 { // // Налево // let mut b = self.clone(); // b.0.swap((zx, zy), (zx - 1, zy)); // let moved = b.0[(zx, zy)]; // f(b, moved) // } // if zy > 0 { // // Вверх // let mut b = self.clone(); // b.0.swap((zx, zy), (zx, zy - 1)); // let moved = b.0[(zx, zy)]; // f(b, moved) // } // } pub fn is_solved(&self) -> bool { let (w, h) = self.size(); for y in 0..h { for x in 0..w { if ((y * w + x + 1) % (w * h)) as u8 != self.0[y][x] { return false; } } } true } pub fn can_solve(&self) -> bool { // let (w, h) = self.size(); // let mut flat = Vec::<u8>::with_capacity(w * h); // for y in 0..h { // for x in 0..w { // flat.push(self.0[(x, y)]); // } // } // let (_zx, zy) = self.empty_at(); // let sum: usize = (0..flat.len()) // .map(|i| { // let c = flat[i] as usize; // let c = if c == 0 { w * h } else { c }; // let k = flat[i..] // .iter() // .map(|x| if *x == 0 { (w * h) } else { *x as usize }) // .filter(|x| *x < c) // .count(); // k // }) // .sum(); // let n = sum + zy; // n % 2 == 0 true } pub fn wrong_tiles(&self) -> usize { let (w, h) = self.size(); let mut c = 0; for y in 0..h { for x in 0..w { if ((y * w + x + 1) % (w * h)) as u8 != self.0[y][x] { c += 1; } } } c } pub fn solve(&self) -> Result<Path, ()> { if !self.can_solve() { return Err(()); } let mut checked_position_length = HashMap::new(); let mut heap = BinaryHeap::with_capacity(1000); heap.push(QPath(Path::new(self.clone()))); let mut i = 0; loop { i += 1; let current = heap.pop().unwrap(); let last = checked_position_length.get_mut(&current.0.current_board); let remove_longer = |heap: &mut BinaryHeap<QPath>, to_remove: Board| { heap.retain(|qpath| qpath.0.current_board != to_remove); }; if i % 10_000 == 0 { println!( "iter = {}e4, path len = {}, euristic = {}, in heap {} el", i / 10_000, current.0.path().len(), current.0.current_board().wrong_tiles(), heap.len() ); } match last { Some(last) if *last <= current.0.path.len() => continue, Some(last) => { *last = current.0.path.len(); //remove_longer(&mut heap, current.0.current_board); } _ => { checked_position_length.insert(current.0.current_board, current.0.path.len()); //remove_longer(&mut heap, current.0.current_board); } } // println!("Current board with {}", current.cost()); if current.0.current_board().is_solved() { return Ok(current.0); } let mut push_or_ignore = |dir| { // Oh... Remove? if heap.len() > 1_000_000 { let mut replacement = BinaryHeap::with_capacity(1_000_005); for _i in 0..10_000 { replacement.push(heap.pop().unwrap()); } heap = replacement; } // ^^^^^^^ let mut c = &current; let path = c.0.push_step_cloned(dir); if let Ok(path) = path { if !checked_position_length.contains_key(path.current_board()) { heap.push(QPath::new(path)); } } }; // 15 2 1 12 8 5 6 11 4 9 10 7 3 14 13 0 push_or_ignore(Dir::Up); push_or_ignore(Dir::Right); push_or_ignore(Dir::Down); push_or_ignore(Dir::Left); } } pub fn inner(&self) -> &[[u8; WIDTH]; HEIGHT] { &self.0 } } impl Display for Board { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { let (w, h) = self.size(); for y in 0..h { for x in 0..w { match w * h { 0..=9 => write!(f, "{:1} ", self.0[y][x])?, 10..=99 => write!(f, "{:2} ", self.0[y][x])?, 100..=999 => write!(f, "{:3} ", self.0[y][x])?, _ => panic!(""), }; } writeln!(f)?; } Ok(()) } } #[derive(Copy, Clone, Eq, PartialEq, Debug, Hash)] pub enum Dir { Up, Right, Down, Left, } #[derive(Clone, Debug, Hash, Eq, PartialEq)] pub struct Path { current_board: Board, path: Vec<Dir>, } impl Path { pub fn current_board(&self) -> &Board { &self.current_board } pub fn path(&self) -> &Vec<Dir> { &self.path } pub fn len(&self) -> usize { self.path.len() } } impl Path { pub fn new(start_board: Board) -> Self { Self { current_board: start_board, path: Vec::new(), } } pub fn push_step(&mut self, dir: Dir) -> Result<(), ()> { self.current_board.apply(dir).map(|_| self.path.push(dir)) } pub fn push_step_cloned(&self, dir: Dir) -> Result<Self, ()> { let mut board_clone = self.current_board.clone(); board_clone.apply(dir)?; let mut path_clone = self.path.clone(); path_clone.push(dir); Ok(Self { current_board: board_clone, path: path_clone, }) } } #[derive(Clone)] struct QPath(Path); impl QPath { fn new(p: Path) -> Self { Self(p) } pub fn cost(&self) -> usize { let g = self.0.len(); let f = self.0.current_board.wrong_tiles(); // let f: usize = self // .0 // .current_board() // .inner()
random_line_split
code_quality_eval.py
(hash, type): """ Method used to search for a specific blob, commit or tree. If a tree is searched for, the result is splitted into its components (blobs and directories), which are again splitted into their mode, hash and name. In the case of a commit, we split the information string and the tree hash and parent's commit hash are returned """ out = bash('echo ' + hash + ' | ~/lookup/showCnt ' + type) if type == 'tree': return [blob.split(';') for blob in out.strip().split('\n')] if type == 'commit': splitted = out.split(';') # the tree and parent commit hashes are the second and third word, respectively # the commit time is the last word, from which we discard the timezone and cast it to int return splitted[1], splitted[2], int(splitted[-1].split()[0]) return out # files used in continuous integration ci_files = [ '\.gitlab\-ci\.yml', '\.travis\.yml', 'Jenkinsfile', 'buddy\.yml', '\.drone\.yml', 'circle\.yml', '\.circleci', 'bamboo\.yaml', 'codeship\-steps\.yml', '\.teamcity', 'wercker\.yml', 'appveyor\.yml', 'bitrise\.yml', 'codefresh\.yml', 'solano\.yml', 'shippable\.yml', 'phpci\.yml', 'cloudbuild\.yaml' ] def ci_lookup(tree_hash): """ Method used to check the usage of Continuous Integration in a tree, given its hash. """ query = 'echo ' + tree_hash + ' | ~/lookup/showCnt tree | egrep "' + '|'.join(ci_files) +'"' out = bash(query) """ # alternate method blobs = search(tree_hash, 'tree') index = {'mode':1, 'hash':1, 'name':2} ci = False for blob in blobs: name = blob[index['name']] hash = blob[index['hash']] if ((name in ci_files) or (name in ci_config_dir and ';'+ci_config_dir[name] in search(hash, 'tree'))): ci = True break """ return bool(out) def calc_CI_introductions(commits, author): """ Alternative way to check_if_introduction, to compare performance. """ # using a dictionary that has the commits' hashes as keys, # so as to not search multiple times for the same commit CI_checked = {} # delete contents open('introductions.csv', 'w').close() # for every commit, we look up whether the author included a CI file, # that did not exist in the parent commit for count, commit in enumerate(commits): # status update if (count + 1) % 50 == 0: print count + 1, ' / ', len(commits) tree_hash, parent_commit_hash, time = search(commit, 'commit') if tree_hash not in CI_checked: CI_checked[tree_hash] = ci_lookup(tree_hash) # controlling for the case of multiple parent commits all_parent_CI = False for parent in parent_commit_hash.split(':'): # controlling for the case of no parent commits if parent == '': break parent_tree_hash = search(parent, 'commit')[0] if parent_tree_hash not in CI_checked: parent_CI = ci_lookup(parent_tree_hash) CI_checked[parent_tree_hash] = parent_CI else: parent_CI = CI_checked[parent_tree_hash] # checking all the parent commits for the usage of CI all_parent_CI = all_parent_CI or parent_CI # if the tree has a CI file, while the parent tree does not, increase the CI score if CI_checked[tree_hash] and not all_parent_CI: out = bash('echo ' + commit + ' | ~/lookup/getValues c2P') main_proj = out.strip().split(';')[1] f = open("introductions.csv", "a") f.write(author + ', ' + 'CI' + ', ' + str(time) + ', ' + main_proj + '\n') f.close() print 'wrote' print (current_time()-start_time)/len(commits), 'seconds per commit' def check_if_introduction(commit, result): """ We check the parent commit to see if its child commit introduced or modified a CI config file. """ tree_hash, parent_commit_hash, time = search(commit, 'commit') # controlling for the case of no parent commits if parent_commit_hash == '': return True # controlling for the case of multiple parent commits all_parent_CI = False for parent in parent_commit_hash.split(':'): parent_tree_hash = search(parent, 'commit')[0] parent_CI = ci_lookup(parent_tree_hash) # checking all the parent commits for the usage of CI all_parent_CI = all_parent_CI or parent_CI # if the tree has a CI file, while the parent tree does not, it is an introduction return not all_parent_CI def calc_CI(commits, author): """ Used to investigate how many commits, from a user, modified a CI configuration file. Unix commands are used for a better performance. """ # delete contents open('modifications.csv', 'w').close() open('introductions.csv', 'w').close() for count, commit in enumerate(commits): # status update if (count + 1) % 50 == 0: print commit, '.. ..', count + 1, ' / ', len(commits) # c2f does seems to result in a tie error, so c2b and b2f is used instead #getting the blobs query = ("for x in $(echo " + commit + " | ~/lookup/getValues c2b |" + # splitting on the semicolon and discarding the newlines " awk -v RS='[;\\n]' 1 |" + # discarding the commit's hash (it appears before the blobs' hashes) " tail -n+2); do" + # for each blob, we look up it's filename " echo $x | ~/lookup/getValues b2f;" + " done |" + # we discard the first field of the results (blobs' hash) " cut -d ';' -f2 |" + # we check whether one of the modified files is a CI configuration file " egrep '" + "|".join(ci_files) + "'") result = bash(query) if result: out = bash('echo ' + commit + ' | ~/lookup/getValues c2P') main_proj = out.strip().split(';')[1] time = search(commit, 'commit')[2] if check_if_introduction(commit, result): f = open("introductions.csv", "a") print 'introduction' else: f = open("modifications.csv", "a") print 'modification' f.write(author + ', ' + 'CI' + ', ' + str(time) + ', ' + main_proj + '\n') f.close() print 'wrote: -->', commit def calc_CI_diff(commits, author): """ Method written as a faster alternative to calc_CI. It seems to be 30 times faster. """ # delete contents open('modifications.csv', 'w').close() open('introductions.csv', 'w').close() for count, commit in enumerate(commits): #status update if (count + 1) % 50 == 0: print commit, '.. ..', count + 1, ' / ', len(commits) # cmputeDiff2.perl seems to produce junk to the stdout occasionally diff = bash("echo " + commit + " | ssh da4 ~/lookup/cmputeDiff2.perl") # if a CI configuration file is in the diff if re.search("|".join(ci_files), diff): out = bash('echo ' + commit + ' | ~/lookup/getValues c2P') main_proj = out.strip().split(';')[1] time = search(commit, 'commit')[2] for blob in diff.split(): # looking for the CI config blob and checking if parent blob exists if re.search("|".join(ci_files), blob): # if we have both an introduction and a modification # in the same commit, we count it as an introduction if blob.endswith(';'): # if we don't have the parent blob, after the last semicolon, # it is an introduction f = open("introductions.csv", "a") print 'introduction' else: f = open("modifications.csv", "a") print 'modification' break f.write(author + ', ' + 'CI' + ', ' + str(time) + ', ' + main_proj + '\n') f.close() print 'wrote: -->',
search
identifier_name
code_quality_eval.py
{} # delete contents open('introductions.csv', 'w').close() # for every commit, we look up whether the author included a CI file, # that did not exist in the parent commit for count, commit in enumerate(commits): # status update if (count + 1) % 50 == 0: print count + 1, ' / ', len(commits) tree_hash, parent_commit_hash, time = search(commit, 'commit') if tree_hash not in CI_checked: CI_checked[tree_hash] = ci_lookup(tree_hash) # controlling for the case of multiple parent commits all_parent_CI = False for parent in parent_commit_hash.split(':'): # controlling for the case of no parent commits if parent == '': break parent_tree_hash = search(parent, 'commit')[0] if parent_tree_hash not in CI_checked: parent_CI = ci_lookup(parent_tree_hash) CI_checked[parent_tree_hash] = parent_CI else: parent_CI = CI_checked[parent_tree_hash] # checking all the parent commits for the usage of CI all_parent_CI = all_parent_CI or parent_CI # if the tree has a CI file, while the parent tree does not, increase the CI score if CI_checked[tree_hash] and not all_parent_CI: out = bash('echo ' + commit + ' | ~/lookup/getValues c2P') main_proj = out.strip().split(';')[1] f = open("introductions.csv", "a") f.write(author + ', ' + 'CI' + ', ' + str(time) + ', ' + main_proj + '\n') f.close() print 'wrote' print (current_time()-start_time)/len(commits), 'seconds per commit' def check_if_introduction(commit, result): """ We check the parent commit to see if its child commit introduced or modified a CI config file. """ tree_hash, parent_commit_hash, time = search(commit, 'commit') # controlling for the case of no parent commits if parent_commit_hash == '': return True # controlling for the case of multiple parent commits all_parent_CI = False for parent in parent_commit_hash.split(':'): parent_tree_hash = search(parent, 'commit')[0] parent_CI = ci_lookup(parent_tree_hash) # checking all the parent commits for the usage of CI all_parent_CI = all_parent_CI or parent_CI # if the tree has a CI file, while the parent tree does not, it is an introduction return not all_parent_CI def calc_CI(commits, author): """ Used to investigate how many commits, from a user, modified a CI configuration file. Unix commands are used for a better performance. """ # delete contents open('modifications.csv', 'w').close() open('introductions.csv', 'w').close() for count, commit in enumerate(commits): # status update if (count + 1) % 50 == 0: print commit, '.. ..', count + 1, ' / ', len(commits) # c2f does seems to result in a tie error, so c2b and b2f is used instead #getting the blobs query = ("for x in $(echo " + commit + " | ~/lookup/getValues c2b |" + # splitting on the semicolon and discarding the newlines " awk -v RS='[;\\n]' 1 |" + # discarding the commit's hash (it appears before the blobs' hashes) " tail -n+2); do" + # for each blob, we look up it's filename " echo $x | ~/lookup/getValues b2f;" + " done |" + # we discard the first field of the results (blobs' hash) " cut -d ';' -f2 |" + # we check whether one of the modified files is a CI configuration file " egrep '" + "|".join(ci_files) + "'") result = bash(query) if result: out = bash('echo ' + commit + ' | ~/lookup/getValues c2P') main_proj = out.strip().split(';')[1] time = search(commit, 'commit')[2] if check_if_introduction(commit, result): f = open("introductions.csv", "a") print 'introduction' else: f = open("modifications.csv", "a") print 'modification' f.write(author + ', ' + 'CI' + ', ' + str(time) + ', ' + main_proj + '\n') f.close() print 'wrote: -->', commit def calc_CI_diff(commits, author): """ Method written as a faster alternative to calc_CI. It seems to be 30 times faster. """ # delete contents open('modifications.csv', 'w').close() open('introductions.csv', 'w').close() for count, commit in enumerate(commits): #status update if (count + 1) % 50 == 0: print commit, '.. ..', count + 1, ' / ', len(commits) # cmputeDiff2.perl seems to produce junk to the stdout occasionally diff = bash("echo " + commit + " | ssh da4 ~/lookup/cmputeDiff2.perl") # if a CI configuration file is in the diff if re.search("|".join(ci_files), diff): out = bash('echo ' + commit + ' | ~/lookup/getValues c2P') main_proj = out.strip().split(';')[1] time = search(commit, 'commit')[2] for blob in diff.split(): # looking for the CI config blob and checking if parent blob exists if re.search("|".join(ci_files), blob): # if we have both an introduction and a modification # in the same commit, we count it as an introduction if blob.endswith(';'): # if we don't have the parent blob, after the last semicolon, # it is an introduction f = open("introductions.csv", "a") print 'introduction' else: f = open("modifications.csv", "a") print 'modification' break f.write(author + ', ' + 'CI' + ', ' + str(time) + ', ' + main_proj + '\n') f.close() print 'wrote: -->', commit def find_links(author, end_time, method='sh'):
#### Start building the regular expression that will be used to search for unit testing libraries, #### in the commit's blobs #### # Java java_lib = ['io.restassured', 'org.openqa.selenium', 'org.spockframework', 'jtest', 'org.springframework.test', 'org.dbunit', 'org.jwalk', 'org.mockito', 'org.junit'] java_regex = (['import\s+'+s.replace('.', '\.') for s in java_lib]) java_all_reg = '|'.join(java_regex) # Perl perl_all_reg = 'use\s+Test::' # Javascript js = ['assert', 'mocha', 'jasmine', 'ava', 'jest', 'karma', 'storybook', 'tape', 'cypress', 'puppeteer', 'chai', 'qunit', 'sinon', 'casper', 'buster'] js_regex = (["require\([\\\'\\\"]" + s + "[\\\'\\\"]\)" for s in js]) js_all_reg = '|'.join(js_regex) # C# c_sharp = ['NUnit', 'Microsoft\.VisualStudio\.TestTools\.UnitTesting', 'Xunit', 'csUnit', 'MbUnit'] c_sharp_regex = (["using\s+" + s for s in c_sharp]) c_sharp_all_reg = '|'.join(c_sharp_regex) # C and C++ c = ['cmocka', 'unity', 'CppuTest', 'embUnit', 'CUnit', 'CuTest', 'check', 'gtest', 'uCUnit', 'munit', 'minunit', 'acutest', 'boost/test', 'UnitTest\+\+', 'cpptest', 'cppunit', 'catch', 'bandit', 'tut'] c_regex = (['#include\s+[<\\\"]' + s + '\.h[>\\\"]'for s in c]) c_all_reg = '|'.join(c_regex) # PHP php = ['PHPUnit', 'Codeception', 'Behat', 'PhpSpec', 'Storyplayer
""" Method used to find the neighbours of a given author, i.e. the authors that affected the given author's use of good coding practices. A timestamp is also given to define the time till which we find the connections. """ out = bash('echo "'+ author + '" | ~/lookup/getValues a2P') pr = [x for x in out.strip().split(';')[1:]] if method == 'pr_timeline': p = Proj() for project in pr: rows = p.project_timeline(['time','repo', 'author'], project) for row in rows: print row
identifier_body
code_quality_eval.py
which are again splitted into their mode, hash and name. In the case of a commit, we split the information string and the tree hash and parent's commit hash are returned """ out = bash('echo ' + hash + ' | ~/lookup/showCnt ' + type) if type == 'tree': return [blob.split(';') for blob in out.strip().split('\n')] if type == 'commit': splitted = out.split(';') # the tree and parent commit hashes are the second and third word, respectively # the commit time is the last word, from which we discard the timezone and cast it to int return splitted[1], splitted[2], int(splitted[-1].split()[0]) return out # files used in continuous integration ci_files = [ '\.gitlab\-ci\.yml', '\.travis\.yml', 'Jenkinsfile', 'buddy\.yml', '\.drone\.yml', 'circle\.yml', '\.circleci', 'bamboo\.yaml', 'codeship\-steps\.yml', '\.teamcity', 'wercker\.yml', 'appveyor\.yml', 'bitrise\.yml', 'codefresh\.yml', 'solano\.yml', 'shippable\.yml', 'phpci\.yml', 'cloudbuild\.yaml' ] def ci_lookup(tree_hash): """ Method used to check the usage of Continuous Integration in a tree, given its hash. """ query = 'echo ' + tree_hash + ' | ~/lookup/showCnt tree | egrep "' + '|'.join(ci_files) +'"' out = bash(query) """ # alternate method blobs = search(tree_hash, 'tree') index = {'mode':1, 'hash':1, 'name':2} ci = False for blob in blobs: name = blob[index['name']] hash = blob[index['hash']] if ((name in ci_files) or (name in ci_config_dir and ';'+ci_config_dir[name] in search(hash, 'tree'))): ci = True break """ return bool(out) def calc_CI_introductions(commits, author): """ Alternative way to check_if_introduction, to compare performance. """ # using a dictionary that has the commits' hashes as keys, # so as to not search multiple times for the same commit CI_checked = {} # delete contents open('introductions.csv', 'w').close() # for every commit, we look up whether the author included a CI file, # that did not exist in the parent commit for count, commit in enumerate(commits): # status update if (count + 1) % 50 == 0: print count + 1, ' / ', len(commits) tree_hash, parent_commit_hash, time = search(commit, 'commit') if tree_hash not in CI_checked: CI_checked[tree_hash] = ci_lookup(tree_hash) # controlling for the case of multiple parent commits all_parent_CI = False for parent in parent_commit_hash.split(':'): # controlling for the case of no parent commits if parent == '': break parent_tree_hash = search(parent, 'commit')[0] if parent_tree_hash not in CI_checked: parent_CI = ci_lookup(parent_tree_hash) CI_checked[parent_tree_hash] = parent_CI else: parent_CI = CI_checked[parent_tree_hash] # checking all the parent commits for the usage of CI all_parent_CI = all_parent_CI or parent_CI # if the tree has a CI file, while the parent tree does not, increase the CI score if CI_checked[tree_hash] and not all_parent_CI: out = bash('echo ' + commit + ' | ~/lookup/getValues c2P') main_proj = out.strip().split(';')[1] f = open("introductions.csv", "a") f.write(author + ', ' + 'CI' + ', ' + str(time) + ', ' + main_proj + '\n') f.close() print 'wrote' print (current_time()-start_time)/len(commits), 'seconds per commit' def check_if_introduction(commit, result): """ We check the parent commit to see if its child commit introduced or modified a CI config file. """ tree_hash, parent_commit_hash, time = search(commit, 'commit') # controlling for the case of no parent commits if parent_commit_hash == '': return True # controlling for the case of multiple parent commits all_parent_CI = False for parent in parent_commit_hash.split(':'): parent_tree_hash = search(parent, 'commit')[0] parent_CI = ci_lookup(parent_tree_hash) # checking all the parent commits for the usage of CI all_parent_CI = all_parent_CI or parent_CI # if the tree has a CI file, while the parent tree does not, it is an introduction return not all_parent_CI def calc_CI(commits, author): """ Used to investigate how many commits, from a user, modified a CI configuration file. Unix commands are used for a better performance. """ # delete contents open('modifications.csv', 'w').close() open('introductions.csv', 'w').close() for count, commit in enumerate(commits): # status update if (count + 1) % 50 == 0: print commit, '.. ..', count + 1, ' / ', len(commits) # c2f does seems to result in a tie error, so c2b and b2f is used instead #getting the blobs query = ("for x in $(echo " + commit + " | ~/lookup/getValues c2b |" + # splitting on the semicolon and discarding the newlines " awk -v RS='[;\\n]' 1 |" + # discarding the commit's hash (it appears before the blobs' hashes) " tail -n+2); do" + # for each blob, we look up it's filename " echo $x | ~/lookup/getValues b2f;" + " done |" + # we discard the first field of the results (blobs' hash) " cut -d ';' -f2 |" + # we check whether one of the modified files is a CI configuration file " egrep '" + "|".join(ci_files) + "'") result = bash(query) if result: out = bash('echo ' + commit + ' | ~/lookup/getValues c2P') main_proj = out.strip().split(';')[1] time = search(commit, 'commit')[2] if check_if_introduction(commit, result): f = open("introductions.csv", "a") print 'introduction' else: f = open("modifications.csv", "a") print 'modification' f.write(author + ', ' + 'CI' + ', ' + str(time) + ', ' + main_proj + '\n') f.close() print 'wrote: -->', commit def calc_CI_diff(commits, author): """ Method written as a faster alternative to calc_CI. It seems to be 30 times faster. """ # delete contents open('modifications.csv', 'w').close() open('introductions.csv', 'w').close() for count, commit in enumerate(commits): #status update if (count + 1) % 50 == 0: print commit, '.. ..', count + 1, ' / ', len(commits) # cmputeDiff2.perl seems to produce junk to the stdout occasionally diff = bash("echo " + commit + " | ssh da4 ~/lookup/cmputeDiff2.perl") # if a CI configuration file is in the diff if re.search("|".join(ci_files), diff): out = bash('echo ' + commit + ' | ~/lookup/getValues c2P') main_proj = out.strip().split(';')[1] time = search(commit, 'commit')[2] for blob in diff.split(): # looking for the CI config blob and checking if parent blob exists if re.search("|".join(ci_files), blob): # if we have both an introduction and a modification # in the same commit, we count it as an introduction if blob.endswith(';'): # if we don't have the parent blob, after the last semicolon, # it is an introduction f = open("introductions.csv", "a") print 'introduction' else: f = open("modifications.csv", "a") print 'modification' break f.write(author + ', ' + 'CI' + ', ' + str(time) + ', ' + main_proj + '\n') f.close() print 'wrote: -->', commit def find_links(author, end_time, method='sh'): """ Method used to find
If a tree is searched for, the result is splitted into its components (blobs and directories),
random_line_split
code_quality_eval.py
{} # delete contents open('introductions.csv', 'w').close() # for every commit, we look up whether the author included a CI file, # that did not exist in the parent commit for count, commit in enumerate(commits): # status update if (count + 1) % 50 == 0: print count + 1, ' / ', len(commits) tree_hash, parent_commit_hash, time = search(commit, 'commit') if tree_hash not in CI_checked:
# controlling for the case of multiple parent commits all_parent_CI = False for parent in parent_commit_hash.split(':'): # controlling for the case of no parent commits if parent == '': break parent_tree_hash = search(parent, 'commit')[0] if parent_tree_hash not in CI_checked: parent_CI = ci_lookup(parent_tree_hash) CI_checked[parent_tree_hash] = parent_CI else: parent_CI = CI_checked[parent_tree_hash] # checking all the parent commits for the usage of CI all_parent_CI = all_parent_CI or parent_CI # if the tree has a CI file, while the parent tree does not, increase the CI score if CI_checked[tree_hash] and not all_parent_CI: out = bash('echo ' + commit + ' | ~/lookup/getValues c2P') main_proj = out.strip().split(';')[1] f = open("introductions.csv", "a") f.write(author + ', ' + 'CI' + ', ' + str(time) + ', ' + main_proj + '\n') f.close() print 'wrote' print (current_time()-start_time)/len(commits), 'seconds per commit' def check_if_introduction(commit, result): """ We check the parent commit to see if its child commit introduced or modified a CI config file. """ tree_hash, parent_commit_hash, time = search(commit, 'commit') # controlling for the case of no parent commits if parent_commit_hash == '': return True # controlling for the case of multiple parent commits all_parent_CI = False for parent in parent_commit_hash.split(':'): parent_tree_hash = search(parent, 'commit')[0] parent_CI = ci_lookup(parent_tree_hash) # checking all the parent commits for the usage of CI all_parent_CI = all_parent_CI or parent_CI # if the tree has a CI file, while the parent tree does not, it is an introduction return not all_parent_CI def calc_CI(commits, author): """ Used to investigate how many commits, from a user, modified a CI configuration file. Unix commands are used for a better performance. """ # delete contents open('modifications.csv', 'w').close() open('introductions.csv', 'w').close() for count, commit in enumerate(commits): # status update if (count + 1) % 50 == 0: print commit, '.. ..', count + 1, ' / ', len(commits) # c2f does seems to result in a tie error, so c2b and b2f is used instead #getting the blobs query = ("for x in $(echo " + commit + " | ~/lookup/getValues c2b |" + # splitting on the semicolon and discarding the newlines " awk -v RS='[;\\n]' 1 |" + # discarding the commit's hash (it appears before the blobs' hashes) " tail -n+2); do" + # for each blob, we look up it's filename " echo $x | ~/lookup/getValues b2f;" + " done |" + # we discard the first field of the results (blobs' hash) " cut -d ';' -f2 |" + # we check whether one of the modified files is a CI configuration file " egrep '" + "|".join(ci_files) + "'") result = bash(query) if result: out = bash('echo ' + commit + ' | ~/lookup/getValues c2P') main_proj = out.strip().split(';')[1] time = search(commit, 'commit')[2] if check_if_introduction(commit, result): f = open("introductions.csv", "a") print 'introduction' else: f = open("modifications.csv", "a") print 'modification' f.write(author + ', ' + 'CI' + ', ' + str(time) + ', ' + main_proj + '\n') f.close() print 'wrote: -->', commit def calc_CI_diff(commits, author): """ Method written as a faster alternative to calc_CI. It seems to be 30 times faster. """ # delete contents open('modifications.csv', 'w').close() open('introductions.csv', 'w').close() for count, commit in enumerate(commits): #status update if (count + 1) % 50 == 0: print commit, '.. ..', count + 1, ' / ', len(commits) # cmputeDiff2.perl seems to produce junk to the stdout occasionally diff = bash("echo " + commit + " | ssh da4 ~/lookup/cmputeDiff2.perl") # if a CI configuration file is in the diff if re.search("|".join(ci_files), diff): out = bash('echo ' + commit + ' | ~/lookup/getValues c2P') main_proj = out.strip().split(';')[1] time = search(commit, 'commit')[2] for blob in diff.split(): # looking for the CI config blob and checking if parent blob exists if re.search("|".join(ci_files), blob): # if we have both an introduction and a modification # in the same commit, we count it as an introduction if blob.endswith(';'): # if we don't have the parent blob, after the last semicolon, # it is an introduction f = open("introductions.csv", "a") print 'introduction' else: f = open("modifications.csv", "a") print 'modification' break f.write(author + ', ' + 'CI' + ', ' + str(time) + ', ' + main_proj + '\n') f.close() print 'wrote: -->', commit def find_links(author, end_time, method='sh'): """ Method used to find the neighbours of a given author, i.e. the authors that affected the given author's use of good coding practices. A timestamp is also given to define the time till which we find the connections. """ out = bash('echo "'+ author + '" | ~/lookup/getValues a2P') pr = [x for x in out.strip().split(';')[1:]] if method == 'pr_timeline': p = Proj() for project in pr: rows = p.project_timeline(['time','repo', 'author'], project) for row in rows: print row #### Start building the regular expression that will be used to search for unit testing libraries, #### in the commit's blobs #### # Java java_lib = ['io.restassured', 'org.openqa.selenium', 'org.spockframework', 'jtest', 'org.springframework.test', 'org.dbunit', 'org.jwalk', 'org.mockito', 'org.junit'] java_regex = (['import\s+'+s.replace('.', '\.') for s in java_lib]) java_all_reg = '|'.join(java_regex) # Perl perl_all_reg = 'use\s+Test::' # Javascript js = ['assert', 'mocha', 'jasmine', 'ava', 'jest', 'karma', 'storybook', 'tape', 'cypress', 'puppeteer', 'chai', 'qunit', 'sinon', 'casper', 'buster'] js_regex = (["require\([\\\'\\\"]" + s + "[\\\'\\\"]\)" for s in js]) js_all_reg = '|'.join(js_regex) # C# c_sharp = ['NUnit', 'Microsoft\.VisualStudio\.TestTools\.UnitTesting', 'Xunit', 'csUnit', 'MbUnit'] c_sharp_regex = (["using\s+" + s for s in c_sharp]) c_sharp_all_reg = '|'.join(c_sharp_regex) # C and C++ c = ['cmocka', 'unity', 'CppuTest', 'embUnit', 'CUnit', 'CuTest', 'check', 'gtest', 'uCUnit', 'munit', 'minunit', 'acutest', 'boost/test', 'UnitTest\+\+', 'cpptest', 'cppunit', 'catch', 'bandit', 'tut'] c_regex = (['#include\s+[<\\\"]' + s + '\.h[>\\\"]'for s in c]) c_all_reg = '|'.join(c_regex) # PHP php = ['PHPUnit', 'Codeception', 'Behat', 'PhpSpec', 'Storyplayer',
CI_checked[tree_hash] = ci_lookup(tree_hash)
conditional_block
aas220_poster.py
sim_light_curve.mjd sim_mag = sim_light_curve.mag sim_err = sim_light_curve.error while True: if len(sim_mjd) < min_num_observations: break dcs = simu.compute_delta_chi_squared((sim_mjd, sim_mag, sim_err), force_fit=True) delta_chi_squareds.append(dcs) prune = np.arange(len(sim_mjd)) np.random.shuffle(prune) prune = prune[::2] sim_mjd = sim_mjd[prune] sim_mag = sim_mag[prune] sim_err = sim_err[prune] data_dict[sampling][timescale].append(delta_chi_squareds) f = open("data/aas_survey_detection_efficiency.pickle", "w") pickle.dump(data_dict, f) f.close() f = open("data/aas_survey_detection_efficiency.pickle", "r") data_dict = pickle.load(f) # Plotting stuff plt.figure(figsize=(15,15)) dcs_cutoff = 300. num_observations = [2**x for x in range(int(np.log2(max_num_observations)), int(np.log2(min_num_observations))-1, -1)] linestyles = {"uniform" : "--", "clumpy" : "-"} linecolors = {1. : "k", 10. : "r", 100. : "c"} for sampling in data_dict.keys(): for timescale in data_dict[sampling].keys(): data = np.array(data_dict[sampling][timescale]) efficiencies = [] for col,num_obs in enumerate(num_observations): efficiencies.append(np.sum(data[:,col] > dcs_cutoff) / len(data[:,col])) plt.plot(np.log2(num_observations), efficiencies, ls=linestyles[sampling], color=linecolors[timescale], label=r"$t_E={}$ day, {} sampling".format(int(timescale), sampling), lw=3) #plt.axvline(np.log2(625.), c="g", ls="--", lw=2, label="PTF Praesepe fields") plt.xlabel("Number of Observations / 1 year", size=label_font_size) plt.ylabel(r"Detection Efficiency $\mathcal{E}(t_E)$", size=label_font_size) plt.title("Simulated Detection Efficiency for\nDifferent Sampling Patterns", size=title_font_size) # Change tick label size ax = plt.gca() for label in ax.get_xticklabels() + ax.get_yticklabels(): label.set_fontsize(tick_font_size) ax.set_xticklabels(num_observations[::-1]) legend = plt.legend(loc="upper left", shadow=True, fancybox=True) legendtext = legend.get_texts() plt.setp(legendtext, fontsize=tick_font_size) # the legend text fontsize plt.tight_layout() #plt.show() plt.savefig("plots/aas_survey_detection_efficiency.png") def variability_indices(): import PraesepeLightCurves as plc # TODO: Sample timescale from the distribution that Amanda will send me # TODO: Fix legend in post-editing plc.aas_figure() def variability_indices_detection_efficiency(): """ This figure should show the detection efficiency curve for the Praesepe data for each variability index (cut at 2-sigma) """ filename = "data/praesepe_detection_efficiency.npy" # Load the simulation results sim_results = np.load(filename) var_indices = session.query(VariabilityIndices).join(LightCurve).filter(LightCurve.objid < 100000).all() styles = [(3,"-."), (3,":"), (3,"--"), (1.5,"--"), (2,"-")] colors = ["c", "m", "g", "y", "k"] plt.figure(figsize=(15,15)) for ii,idx in enumerate(["j", "k", "eta", "sigma_mu", "delta_chi_squared"]): values = [getattr(x, idx) for x in var_indices] sigma = np.std(values) mu = np.mean(values) sim_results[np.isnan(sim_results["tE"])] = 0. detections = sim_results[(np.fabs(sim_results[idx]) > (mu + 2.*sigma)) | (np.fabs(sim_results[idx]) < (mu - 2.*sigma))] detections = detections[detections["event_added"] == True] tE_counts, tE_bin_edges = np.histogram(detections["tE"], bins=timescale_bins) total_counts, total_bin_edges = np.histogram(sim_results[sim_results["event_added"] == True]["tE"], bins=timescale_bins) lw,ls = styles[ii] plt.semilogx((total_bin_edges[1:]+total_bin_edges[:-1])/2, tE_counts / total_counts, c=colors[ii], lw=lw, label=r"{}".format(parameter_to_label[idx]), ls=ls) plt.xlabel(r"$t_E$ [days]", size=label_font_size) plt.ylabel(r"Detection Efficiency $\mathcal{E}(t_E)$", size=label_font_size) plt.ylim(0., 1.0) t = plt.title("PTF Detection Efficiency for Praesepe Light Curves", size=title_font_size) t.set_y(1.04) # Change tick label size ax = plt.gca() for label in ax.get_xticklabels() + ax.get_yticklabels(): label.set_fontsize(tick_font_size) leg = plt.legend(shadow=True, fancybox=True) legendtext = leg.get_texts() plt.setp(legendtext, fontsize=label_font_size) plt.tight_layout() plt.savefig("plots/aas_var_indices_detection_efficiency.png") #plt.show() def systematics_552(): # Bad1 http://kanaloa.ipac.caltech.edu/ibe/data/ptf/dev/process/proc/2010/04/17/f2/c6/p13/v1/PTF_201004172696_i_p_scie_t062817_u011575385_f02_p110002_c06.fits?center=127.403,19.6476deg&size=100px # Bad2 http://kanaloa.ipac.caltech.edu/ibe/data/ptf/dev/process/proc/2010/04/17/f2/c6/p13/v1/PTF_201004173180_i_p_scie_t073759_u011575280_f02_p110002_c06.fits?center=127.403,19.6476deg&size=100px # Good http://kanaloa.ipac.caltech.edu/ibe/data/ptf/dev/process/proc/2010/04/07/f2/c6/p13/v1/PTF_201004071439_i_p_scie_t032714_u011539562_f02_p110002_c06.fits?center=127.403,19.6476deg&size=100px mjd_offset = 54832 lc1 = session.query(LightCurve).filter(LightCurve.objid == 552).one() bad_light_curve = PTFLightCurve.fromDBLightCurve(lc1) light_curves = session.query(LightCurve).filter(func.q3c_radial_query(LightCurve.ra, LightCurve.dec, lc1.ra, lc1.dec,30/3600.)).all() bad_obs1 = 55303.26964 bad_obs2 = 55303.31804 good_obs = 55293.14391 # seeing, airmass, filename, mjd imlist = np.genfromtxt("data/aas_552_imagelist.txt", skiprows=4, usecols=[11,12,20,25], dtype=[("seeing", float), ("airmass", float), ("filename", "|S100"), ("mjd", float)]) idx_sort = np.argsort(imlist["mjd"]) imlist = imlist[idx_sort] print "Bad1:", imlist["filename"][imlist["mjd"] == 55303.26964] print "Bad2:", imlist["filename"][imlist["mjd"] == 55303.31804] print "Good:", imlist["filename"][imlist["mjd"] == 55293.14391] return plt.plot(imlist["mjd"], imlist["seeing"], "r.") plt.show()
def systematics_9347():
random_line_split
aas220_poster.py
for row in low_cadence: ogle_low_cadence_fields.append(coverageplots.OGLEField(row["ra"], row["dec"])) coverage_plot = coverageplots.PTFCoveragePlot(figsize=(30,15), projection="aitoff") coverage_plot.addFields(ptf_fields, label="PTF", color_by_observations=True) coverage_plot.addFields(ogle_low_cadence_fields + ogle_high_cadence_fields, label="OGLE-IV", color="c", alpha=0.15) #coverage_plot.addFields(ogle_high_cadence_fields, label="OGLE-IV - high cadence", color="r", alpha=0.15) # Now I need to add globular and open clusters to the plot! open_clusters = np.genfromtxt("data/open_clusters.csv", usecols=[0,1,2,11], dtype=[("name","|S20"),("ra","|S8"), ("dec","|S9"), ("diameter", float)], delimiter=",").view(np.recarray) for ii,cluster in enumerate(open_clusters): ra_deg = g.RA(cluster["ra"]).degrees dec_rad = g.Dec(cluster["dec"]).radians if ii == 0: circle = matplotlib.patches.Ellipse((np.radians(-ra_deg+180), dec_rad), width=np.radians(cluster["diameter"]/60./np.cos(dec_rad)), height=np.radians(cluster["diameter"]/60.), alpha=.4, edgecolor="r", facecolor="r", label="Open Clusters") else: circle = matplotlib.patches.Ellipse((np.radians(-ra_deg+180), dec_rad), width=np.radians(cluster["diameter"]/60./np.cos(dec_rad)), height=np.radians(cluster["diameter"]/60.), alpha=.4, edgecolor="r", facecolor="r") coverage_plot.axis.add_patch(circle) """ globular_clusters = np.genfromtxt("data/allGlobularClusters.txt", dtype=[("r_h",float),("ra","|S11"), ("dec","|S12")], delimiter=",").view(np.recarray) for ii,cluster in enumerate(globular_clusters): ra_deg = g.RA(cluster["ra"]).degrees dec_rad = g.Dec(cluster["dec"]).radians diameter = 2.*cluster["r_h"]*10 if ii == 0: circle = matplotlib.patches.Ellipse((np.radians(-ra_deg+180), dec_rad), width=np.radians(diameter/60./np.cos(dec_rad)), height=np.radians(diameter/60.), alpha=.4, edgecolor="g", facecolor="g", label="Globular Clusters") else: circle = matplotlib.patches.Ellipse((np.radians(-ra_deg+180), dec_rad), width=np.radians(diameter/60./np.cos(dec_rad)), height=np.radians(diameter/60.), alpha=.4, edgecolor="g", facecolor="g") coverage_plot.axis.add_patch(circle) """ coverage_plot.addLegend() coverage_plot.title.set_fontsize(title_font_size) legendtext = coverage_plot.legend.get_texts() plt.setp(legendtext, fontsize=label_font_size) # the legend text fontsize #plt.show() coverage_plot.figure.savefig("plots/aas_ptf_coverage.png") # To be used by the Praesepe timescale distribution plot and the # detection efficiency timescale_bins = np.logspace(np.log10(1), np.log10(1000), 100) # from 1 day to 1000 days def praesepe_timescale_distribution(): filename = "data/praesepeTimeScales.npy" timescales = np.load(filename) plt.figure(figsize=(15,15)) plt.hist(timescales, bins=timescale_bins, normed=True) plt.xscale("log") plt.xlabel(r"$t_E$ [days]", size=label_font_size) t = plt.title("Normalized timescale distribution for Praesepe field", size=title_font_size) t.set_y(1.04) ax = plt.gca() for label in ax.get_xticklabels(): label.set_fontsize(tick_font_size) ax.set_yticklabels([]) plt.savefig("plots/aas_praesepe_timescale.png") def praesepe_event_rate(): filename = "data/praesepeTimeScales.npy" timescales = np.load(filename) global_event_rate = 0.0081 # # To get the event rate distribution, I have to normalize the timescale dist. so # the integral from 0 to infinity = global event rate timescale_pdf, bin_edges = np.histogram(timescales, bins=timescale_bins, density=True) event_rate_distribution = timescale_pdf*global_event_rate # Get the Praesepe detection efficiency filename = "data/praesepe_detection_efficiency.npy" # Load the simulation results sim_results = np.load(filename) # Get the RMS scatter of delta chi-squared for the vanilla light curves dcs = [x[0] for x in session.query(VariabilityIndices.delta_chi_squared).join(LightCurve).filter(LightCurve.objid < 100000).all()] sigma = np.std(dcs) # 2*sigma ~ 300 sim_results[np.isnan(sim_results["tE"])] = 0. detections = sim_results[sim_results["delta_chi_squared"] > 2.*sigma] detections = detections[detections["event_added"] == True] #detections = sim_results[(sim_results["delta_chi_squared"] > 2.*sigma)] #detections = detections[detections["event_added"] == True] tE_counts, tE_bin_edges = np.histogram(detections["tE"], bins=timescale_bins) total_counts, bin_edges = np.histogram(sim_results[sim_results["event_added"] == True]["tE"], bins=timescale_bins) detection_efficiency_distribution = tE_counts / total_counts bin_widths = bin_edges[1:] - bin_edges[:-1] # Compute number of events! # - detection_efficiency_distribution is dE/dt_E # - event_rate_distribution is dN/dt_E # - bin_widths give us dt_E N_exp = np.sum(detection_efficiency_distribution * event_rate_distribution / 365. * bin_widths) * 102. # days of Praesepe obs. # Number of events if we had observed it consistently for 3 years N_exp_all_survey = np.sum(detection_efficiency_distribution * event_rate_distribution / 365. * bin_widths) * 1095. # days of Praesepe obs. print "Number of events in our Praesepe sample (102 days): {} +/- {}".format(N_exp, np.sqrt(N_exp)) print "Number of events if we had observed it consistently for 3 years: {} +/- {}".format(N_exp_all_survey, np.sqrt(N_exp_all_survey)) def praesepe_detection_efficiency(): filename = "data/praesepe_detection_efficiency.npy" if not os.path.exists(filename): # Select out just the Praesepe light curves (objid < 100000) light_curve_generator = de.AllPraesepeLightCurves(limit=10000, random=True) sim_results = de.run_simulation(light_curve_generator, N=100) np.save(filename, sim_results) # Load the simulation results sim_results = np.load(filename) # Get the RMS scatter of delta chi-squared for the vanilla light curves dcs = [x[0] for x in session.query(VariabilityIndices.delta_chi_squared).join(LightCurve).filter(LightCurve.obj
raw_field_data = pf.open("data/exposureData.fits")[1].data unq_field_ids = np.unique(raw_field_data.field_id) ptf_fields = [] for field_id in unq_field_ids: one_field_data = raw_field_data[raw_field_data.field_id == field_id] mean_ra = np.mean(one_field_data.ra) / 15. mean_dec = np.mean(one_field_data.dec) observations = len(one_field_data) / len(np.unique(one_field_data.ccd_id)) ptf_fields.append(coverageplots.PTFField(mean_ra, mean_dec, id=field_id, number_of_observations=observations)) # OGLE: high_cadence = np.genfromtxt("data/ogle4_common.txt", names=["ra","dec","l","b"], usecols=[6,7,8,9]).view(np.recarray) low_cadence = np.genfromtxt("data/ogle4_less_frequent.txt", names=["ra","dec","l","b"], usecols=[6,7,8,9]).view(np.recarray) ogle_high_cadence_fields = [] for row in high_cadence: ogle_high_cadence_fields.append(coverageplots.OGLEField(row["ra"], row["dec"])) ogle_low_cadence_fields = []
identifier_body
aas220_poster.py
np.sum(detection_efficiency_distribution * event_rate_distribution / 365. * bin_widths) * 1095. # days of Praesepe obs. print "Number of events in our Praesepe sample (102 days): {} +/- {}".format(N_exp, np.sqrt(N_exp)) print "Number of events if we had observed it consistently for 3 years: {} +/- {}".format(N_exp_all_survey, np.sqrt(N_exp_all_survey)) def praesepe_detection_efficiency(): filename = "data/praesepe_detection_efficiency.npy" if not os.path.exists(filename): # Select out just the Praesepe light curves (objid < 100000) light_curve_generator = de.AllPraesepeLightCurves(limit=10000, random=True) sim_results = de.run_simulation(light_curve_generator, N=100) np.save(filename, sim_results) # Load the simulation results sim_results = np.load(filename) # Get the RMS scatter of delta chi-squared for the vanilla light curves dcs = [x[0] for x in session.query(VariabilityIndices.delta_chi_squared).join(LightCurve).filter(LightCurve.objid < 100000).all()] sigma = np.std(dcs) # 2*sigma ~ 300 sim_results[np.isnan(sim_results["tE"])] = 0. detections = sim_results[sim_results["delta_chi_squared"] > 2.*sigma] detections = detections[detections["event_added"] == True] #detections = sim_results[(sim_results["delta_chi_squared"] > 2.*sigma)] #detections = detections[detections["event_added"] == True] tE_counts, tE_bin_edges = np.histogram(detections["tE"], bins=timescale_bins) total_counts, total_bin_edges = np.histogram(sim_results[sim_results["event_added"] == True]["tE"], bins=timescale_bins) detection_efficiency = tE_counts / total_counts bin_widths = total_bin_edges[1:] - total_bin_edges[:-1] #print np.sum(bin_widths*detection_efficiency) #return plt.figure(figsize=(15,15)) # Multiply by 2 because we only put events in 50% of the cases plt.semilogx((total_bin_edges[1:]+total_bin_edges[:-1])/2, tE_counts / total_counts, 'k-', lw=3) plt.xlabel(r"$t_E$ [days]", size=label_font_size) plt.ylabel(r"Detection Efficiency $\mathcal{E}(t_E)$", size=label_font_size) plt.ylim(0., 0.75) t = plt.title("PTF Detection Efficiency for Praesepe Light Curves", size=title_font_size) t.set_y(1.04) # Change tick label size ax = plt.gca() for label in ax.get_xticklabels() + ax.get_yticklabels(): label.set_fontsize(tick_font_size) plt.tight_layout() plt.savefig("plots/aas_praesepe_detection_efficiency.png") def random_praesepe_light_curve(): objid = np.random.randint(82415) try: lc = session.query(LightCurve).filter(LightCurve.objid == objid).one() except: lc = session.query(LightCurve).filter(LightCurve.objid == 101).one() return lc def survey_detection_effieciency(): """ Here I want a figure that shows how the detection efficiency changes for uniform, random, and clumpy observations """ baseline = 365 #days # TODO: Rerun with 1024, add praesepe line max_num_observations = 1024 min_num_observations = 16 num_clumps = 4 num_iterations = 10000 if not os.path.exists("data/aas_survey_detection_efficiency.pickle"): data_dict = {"clumpy" : {1. : [], 10. : [], 100 : []}, "uniform" : {1. : [], 10. : [], 100 : []}} for timescale in [1., 10., 100.]: for sampling in ["clumpy", "uniform"]: #, "random"]: if sampling == "random": mjd = np.random.random(max_num_observations)*baseline elif sampling == "clumpy": sparse_samples = np.random.random(max_num_observations/2)*baseline clumps = [] days = [] sum = 0. pts_per_clump = max_num_observations / 2 / num_clumps for ii in range(num_clumps): day = np.random.randint(365) if day in days: continue days.append(day) clumpy_samples = np.linspace(day+0.1, day+0.6, pts_per_clump) clumps.append(clumpy_samples) clumps.append(sparse_samples) mjd = np.concatenate(tuple(clumps)) plt.plot(mjd, [1.]*len(mjd), 'ro', alpha=0.4) plt.show() elif sampling == "uniform": mjd = np.linspace(0., baseline, max_num_observations) for jj in range(num_iterations): lc = random_praesepe_light_curve() if len(lc.mag) < 100: continue dupe_mags = np.array(lc.mag*15) dupe_err = np.array(list(lc.error)*15) shuffled_idx = np.arange(0, len(dupe_mags)) np.random.shuffle(shuffled_idx) mags = dupe_mags[shuffled_idx] err = dupe_err[shuffled_idx] sim_light_curve = simu.SimulatedLightCurve(mjd=mjd, mag=mags[:len(mjd)], error=err[:len(mjd)]) sim_light_curve.addMicrolensingEvent(tE=timescale) #sim_light_curve.plot() delta_chi_squareds = [] sim_mjd = sim_light_curve.mjd sim_mag = sim_light_curve.mag sim_err = sim_light_curve.error while True: if len(sim_mjd) < min_num_observations: break dcs = simu.compute_delta_chi_squared((sim_mjd, sim_mag, sim_err), force_fit=True) delta_chi_squareds.append(dcs) prune = np.arange(len(sim_mjd)) np.random.shuffle(prune) prune = prune[::2] sim_mjd = sim_mjd[prune] sim_mag = sim_mag[prune] sim_err = sim_err[prune] data_dict[sampling][timescale].append(delta_chi_squareds) f = open("data/aas_survey_detection_efficiency.pickle", "w") pickle.dump(data_dict, f) f.close() f = open("data/aas_survey_detection_efficiency.pickle", "r") data_dict = pickle.load(f) # Plotting stuff plt.figure(figsize=(15,15)) dcs_cutoff = 300. num_observations = [2**x for x in range(int(np.log2(max_num_observations)), int(np.log2(min_num_observations))-1, -1)] linestyles = {"uniform" : "--", "clumpy" : "-"} linecolors = {1. : "k", 10. : "r", 100. : "c"} for sampling in data_dict.keys(): for timescale in data_dict[sampling].keys(): data = np.array(data_dict[sampling][timescale]) efficiencies = [] for col,num_obs in enumerate(num_observations): efficiencies.append(np.sum(data[:,col] > dcs_cutoff) / len(data[:,col])) plt.plot(np.log2(num_observations), efficiencies, ls=linestyles[sampling], color=linecolors[timescale], label=r"$t_E={}$ day, {} sampling".format(int(timescale), sampling), lw=3) #plt.axvline(np.log2(625.), c="g", ls="--", lw=2, label="PTF Praesepe fields") plt.xlabel("Number of Observations / 1 year", size=label_font_size) plt.ylabel(r"Detection Efficiency $\mathcal{E}(t_E)$", size=label_font_size) plt.title("Simulated Detection Efficiency for\nDifferent Sampling Patterns", size=title_font_size) # Change tick label size ax = plt.gca() for label in ax.get_xticklabels() + ax.get_yticklabels(): label.set_fontsize(tick_font_size) ax.set_xticklabels(num_observations[::-1]) legend = plt.legend(loc="upper left", shadow=True, fancybox=True) legendtext = legend.get_texts() plt.setp(legendtext, fontsize=tick_font_size) # the legend text fontsize plt.tight_layout() #plt.show() plt.savefig("plots/aas_survey_detection_efficiency.png") def
variability_indices
identifier_name
aas220_poster.py
/60./np.cos(dec_rad)), height=np.radians(diameter/60.), alpha=.4, edgecolor="g", facecolor="g") coverage_plot.axis.add_patch(circle) """ coverage_plot.addLegend() coverage_plot.title.set_fontsize(title_font_size) legendtext = coverage_plot.legend.get_texts() plt.setp(legendtext, fontsize=label_font_size) # the legend text fontsize #plt.show() coverage_plot.figure.savefig("plots/aas_ptf_coverage.png") # To be used by the Praesepe timescale distribution plot and the # detection efficiency timescale_bins = np.logspace(np.log10(1), np.log10(1000), 100) # from 1 day to 1000 days def praesepe_timescale_distribution(): filename = "data/praesepeTimeScales.npy" timescales = np.load(filename) plt.figure(figsize=(15,15)) plt.hist(timescales, bins=timescale_bins, normed=True) plt.xscale("log") plt.xlabel(r"$t_E$ [days]", size=label_font_size) t = plt.title("Normalized timescale distribution for Praesepe field", size=title_font_size) t.set_y(1.04) ax = plt.gca() for label in ax.get_xticklabels(): label.set_fontsize(tick_font_size) ax.set_yticklabels([]) plt.savefig("plots/aas_praesepe_timescale.png") def praesepe_event_rate(): filename = "data/praesepeTimeScales.npy" timescales = np.load(filename) global_event_rate = 0.0081 # # To get the event rate distribution, I have to normalize the timescale dist. so # the integral from 0 to infinity = global event rate timescale_pdf, bin_edges = np.histogram(timescales, bins=timescale_bins, density=True) event_rate_distribution = timescale_pdf*global_event_rate # Get the Praesepe detection efficiency filename = "data/praesepe_detection_efficiency.npy" # Load the simulation results sim_results = np.load(filename) # Get the RMS scatter of delta chi-squared for the vanilla light curves dcs = [x[0] for x in session.query(VariabilityIndices.delta_chi_squared).join(LightCurve).filter(LightCurve.objid < 100000).all()] sigma = np.std(dcs) # 2*sigma ~ 300 sim_results[np.isnan(sim_results["tE"])] = 0. detections = sim_results[sim_results["delta_chi_squared"] > 2.*sigma] detections = detections[detections["event_added"] == True] #detections = sim_results[(sim_results["delta_chi_squared"] > 2.*sigma)] #detections = detections[detections["event_added"] == True] tE_counts, tE_bin_edges = np.histogram(detections["tE"], bins=timescale_bins) total_counts, bin_edges = np.histogram(sim_results[sim_results["event_added"] == True]["tE"], bins=timescale_bins) detection_efficiency_distribution = tE_counts / total_counts bin_widths = bin_edges[1:] - bin_edges[:-1] # Compute number of events! # - detection_efficiency_distribution is dE/dt_E # - event_rate_distribution is dN/dt_E # - bin_widths give us dt_E N_exp = np.sum(detection_efficiency_distribution * event_rate_distribution / 365. * bin_widths) * 102. # days of Praesepe obs. # Number of events if we had observed it consistently for 3 years N_exp_all_survey = np.sum(detection_efficiency_distribution * event_rate_distribution / 365. * bin_widths) * 1095. # days of Praesepe obs. print "Number of events in our Praesepe sample (102 days): {} +/- {}".format(N_exp, np.sqrt(N_exp)) print "Number of events if we had observed it consistently for 3 years: {} +/- {}".format(N_exp_all_survey, np.sqrt(N_exp_all_survey)) def praesepe_detection_efficiency(): filename = "data/praesepe_detection_efficiency.npy" if not os.path.exists(filename): # Select out just the Praesepe light curves (objid < 100000) light_curve_generator = de.AllPraesepeLightCurves(limit=10000, random=True) sim_results = de.run_simulation(light_curve_generator, N=100) np.save(filename, sim_results) # Load the simulation results sim_results = np.load(filename) # Get the RMS scatter of delta chi-squared for the vanilla light curves dcs = [x[0] for x in session.query(VariabilityIndices.delta_chi_squared).join(LightCurve).filter(LightCurve.objid < 100000).all()] sigma = np.std(dcs) # 2*sigma ~ 300 sim_results[np.isnan(sim_results["tE"])] = 0. detections = sim_results[sim_results["delta_chi_squared"] > 2.*sigma] detections = detections[detections["event_added"] == True] #detections = sim_results[(sim_results["delta_chi_squared"] > 2.*sigma)] #detections = detections[detections["event_added"] == True] tE_counts, tE_bin_edges = np.histogram(detections["tE"], bins=timescale_bins) total_counts, total_bin_edges = np.histogram(sim_results[sim_results["event_added"] == True]["tE"], bins=timescale_bins) detection_efficiency = tE_counts / total_counts bin_widths = total_bin_edges[1:] - total_bin_edges[:-1] #print np.sum(bin_widths*detection_efficiency) #return plt.figure(figsize=(15,15)) # Multiply by 2 because we only put events in 50% of the cases plt.semilogx((total_bin_edges[1:]+total_bin_edges[:-1])/2, tE_counts / total_counts, 'k-', lw=3) plt.xlabel(r"$t_E$ [days]", size=label_font_size) plt.ylabel(r"Detection Efficiency $\mathcal{E}(t_E)$", size=label_font_size) plt.ylim(0., 0.75) t = plt.title("PTF Detection Efficiency for Praesepe Light Curves", size=title_font_size) t.set_y(1.04) # Change tick label size ax = plt.gca() for label in ax.get_xticklabels() + ax.get_yticklabels(): label.set_fontsize(tick_font_size) plt.tight_layout() plt.savefig("plots/aas_praesepe_detection_efficiency.png") def random_praesepe_light_curve(): objid = np.random.randint(82415) try: lc = session.query(LightCurve).filter(LightCurve.objid == objid).one() except: lc = session.query(LightCurve).filter(LightCurve.objid == 101).one() return lc def survey_detection_effieciency(): """ Here I want a figure that shows how the detection efficiency changes for uniform, random, and clumpy observations """ baseline = 365 #days # TODO: Rerun with 1024, add praesepe line max_num_observations = 1024 min_num_observations = 16 num_clumps = 4 num_iterations = 10000 if not os.path.exists("data/aas_survey_detection_efficiency.pickle"):
clumps.append(sparse_samples) mjd = np.concatenate(tuple(clumps)) plt.plot(mjd, [1.]*len(mjd), 'ro', alpha=0.4) plt.show() elif sampling == "uniform": mjd = np.linspace(0., baseline,
data_dict = {"clumpy" : {1. : [], 10. : [], 100 : []}, "uniform" : {1. : [], 10. : [], 100 : []}} for timescale in [1., 10., 100.]: for sampling in ["clumpy", "uniform"]: #, "random"]: if sampling == "random": mjd = np.random.random(max_num_observations)*baseline elif sampling == "clumpy": sparse_samples = np.random.random(max_num_observations/2)*baseline clumps = [] days = [] sum = 0. pts_per_clump = max_num_observations / 2 / num_clumps for ii in range(num_clumps): day = np.random.randint(365) if day in days: continue days.append(day) clumpy_samples = np.linspace(day+0.1, day+0.6, pts_per_clump) clumps.append(clumpy_samples)
conditional_block
get_models.py
v return c def consolidate_json(json_list): json_obj = {} for json in json_list: json_obj.update(json) json_dict = OrderedDict(sorted(json_obj.items(), key=lambda t: t[1]['type'])) return json_dict def get_summary(data): summary = {} for device, info in data.items(): if device in supported_devices: #models.json format data summary[device]=len(info) else: #unifi devices format data if info['type'] not in summary: summary[info['type']] = 1 else: summary[info['type']] += 1 return summary def update_models(file, data): if os.path.exists(file): log.warn('Updating file: %s, press ^C if you want to exit!' % file) with open(file) as f: models = json.loads(f.read(), object_pairs_hook=OrderedDict) new_models = OrderedDict() #ensure we have an entry for each supported type (even if it's blank) for type in supported_devices: if not models.get(type): models[type] = {} new_models[type] = {} log.info('NOTE! currently supported devices are: %s' % supported_devices) for device, info in data.items(): type = info['type'].upper() result = True if type in supported_devices and device not in models[type]: #check for UDN if type == 'UDM': new_models[type][device]=info #add to models database (even though UDM section isn't currently used) elif type == 'UAP': #all that is needed for AP's new_models[type][device]={} new_models[type][device]['name']=info['name'] log.info('Added %s device: %s - %s' % (type, device, info['name'])) continue else: for existing_device, existing_info in models[type].items(): if info['name'].upper() in existing_info['name'].upper(): log.info('========New Device %s =========' % device) log.info('looks like %s - %s is similar to %s - %s' % (device, info['name'], existing_device, existing_info['name'])) if query_yes_no("Do you want to copy it into the database? (if You select No, you can add it as a new device)"): new_models[type][device]=existing_info log.info('Device: %s copied' % new_models[type][device]) result = False else: result = query_yes_no("Do you want to add it as a new device to the database?") if not result: continue #new device log.info('========New Device %s =========' % device) log.info('found new device: %s - %s, type: %s' % (device, info['name'], type)) if not query_yes_no("Do you want to add it to the database?"): continue else: #UDM only if type == 'UDM': log.info('This is a %s device, you have to choose what type of device to add it as' % type) while True: try: options = [{option: choice.upper()} for option, choice in enumerate(info['subtypes'])] sel_option = query_number('please select one of the following options: %s' % options, 0) type = info['subtypes'][sel_option].upper() break except exception as e: log.error('error: %s' % e) #add new device name new_models[type][device]={} new_models[type][device]['name']=info['name'] if type != 'UAP': #only way for UAP to get here is if a UDM was selected as a UAP, this will skip over in that case. if info.get('features'): poe = info['features'].get('poe',0) new_models[type][device]['poe'] = True if poe == 1 else False if info.get('diagram'): diagram = info['diagram'] log.info('here is a diagram of the device: %s' % pprint(diagram)) rows = len(diagram) standard, sfp, sfp_plus = extract_ports_list(info['ports']) if len(standard) > 0: log.info('ports %s are standard ports' % standard ) standard = len(standard) if len(sfp) > 0: log.info('ports %s are sfp ports' % sfp ) sfp = len(sfp) if len(sfp_plus) > 0: log.info('ports %s are sfp+ ports' % sfp_plus ) sfp_plus = len(sfp_plus) new_models[type][device]['ports']={'number':standard,'rows':0 if standard==0 else 1 if standard <= 8 else 2} new_models[type][device]['sfp']={'number':sfp,'rows': sfp if sfp < 2 else 2} new_models[type][device]['sfp+']={'number':sfp_plus,'rows':sfp_plus if sfp_plus < 2 else 2} if standard > 0: rows = new_models[type][device]['ports']['rows'] new_models[type][device]['ports']['rows'] = query_number('how many ROWS of standard ports are there? (eg, 1,2)', rows) if sfp > 0: rows = new_models[type][device]['sfp']['rows'] new_models[type][device]['sfp']['rows'] = query_number('how many ROWS of sfp ports are there? (eg, 1,2)', rows) if sfp_plus > 0: rows = new_models[type][device]['sfp+']['rows'] new_models[type][device]['sfp+']['rows'] = query_number('how many ROWS of sfp+ ports are there? (eg, 1,2)', rows) if sfp > 0 or sfp_plus > 0: while not new_models[type][device].get('order'): if query_yes_no("Are the first ports (from the left) standard ports?"): new_models[type][device]['order'] = [0,1,2] else: if query_yes_no("Are the first ports (from the left) sfp ports?"): new_models[type][device]['order'] = [1,0,2] else: if query_yes_no("Are the first ports (from the left) sfp+ ports?"): new_models[type][device]['order'] = [2,0,1] else: log.error('OK, the first ports must be standard, sfp, or sfp+ ports. try again') log.debug('Device: %s added' % new_models[type][device]) log.info('Device: %s added' % new_models[type][device]['name']) log.debug('The following new devices have been added: %s' % pprint(new_models)) log.info('New devices: %s' % get_summary(new_models)) all_models = OrderedDict() all_models.update(models) if not any([value for value in get_summary(new_models).values()]): log.info('No New Models Found') #return else: if query_yes_no("Do you want to add them to the database?"): all_models = merge_dicts(models, new_models) log.info('database updated') if query_yes_no("Do you want to add the full Unifi data to the database (recommended)?"): for type, devices in all_models.copy().items(): for device in devices: if device in data: #log.info("adding : %s to models[%s][%s]['unifi']" % (data[device], type, device)) all_models[type][device]['unifi'] = data[device].copy() log.debug('The following data will be written to the database: %s' % pprint(all_models)) log.info('total devices: %s' % get_summary(all_models)) if query_yes_no("Do you want to overwrite the %s file?" % file, None): #backup original file from shutil import copyfile copyfile(file, file+'.org') #write models file out with open(file, 'w') as f: f.write(pprint(all_models)) log.info('File: %s Updated' % file) log.info('Total devices: %s' % get_summary(all_models)) else: log.info('File: %s NOT updated' % file) def extract_ports_list(ports): ''' returns ports list from unifi data as tuple of lists of port number ints eg ([0,1,2,3], [4,5],[]) (standard []. sfp[], sfp_plus[]) NOTE, USG's start at port 0, but switches start at port 1. ''' standard = []
sfp = [] sfp_plus = [] if isinstance(ports, (list, dict)): #standard = [x for x in range(1,len(ports)+1,1)]
random_line_split
get_models.py
def update(self,iteration): iteration = max(min(iteration, self.total), 0) str_format = "{0:." + str(self.decimals) + "f}" percents = str_format.format(100 * (iteration / float(self.total))) filled_length = int(round(self.bar_length * iteration / float(self.total))) #bar = b'█'.decode('utf8') * filled_length + '-' * (self.bar_length - filled_length) bar = '█' * filled_length + '-' * (self.bar_length - filled_length) output = '\r%s |%s| %s%s %s' % (self.prefix, bar, percents, '%', self.suffix) current_output_len = len(output) diff = self.prev_output_len - current_output_len if diff > 0: #if output is shorter than previously output += ' ' * diff #pad output with spaces self.prev_output_len = current_output_len sys.stdout.write(output) sys.stdout.flush() def newline(): output = '\n' sys.stdout.write(output) sys.stdout.flush() def pprint(obj): """Pretty JSON dump of an object.""" return json.dumps(obj, sort_keys=True, indent=2, separators=(',', ': ')) def deduplicate_list(input_list): return list(dict.fromkeys(input_list)) def get_js_web_urls(url): global requests import requests #pip3 install requests from requests.packages.urllib3.exceptions import InsecureRequestWarning requests.packages.urllib3.disable_warnings(InsecureRequestWarning) from bs4 import BeautifulSoup #pip3 install bs4 r = requests.get(url, verify=False, timeout=10) soup = BeautifulSoup(r.content,features="html.parser") src = [sc["src"] for sc in soup.select("script[src]")] return src def get_js_from_web(base_url,url,tempdir = 'tempDir'+os.sep): log.info('retrieving js from: %s' % base_url+url) if not os.path.exists(tempdir): os.mkdir(tempdir) try: data = None r = requests.get(base_url+url, verify=False, timeout=10) assert r.status_code == 200 data = r.text if data: os.makedirs(os.path.dirname(tempdir+url), exist_ok=True) with open(tempdir+url,'w+') as f: f.write(data) return tempdir+url except (AssertionError, requests.ConnectionError, requests.Timeout) as e: log.error("Connection failed error: %s" % e) except Exception as e: log.exception("unknown exception: %s" % e) return None def get_js_files(unifi_dir): js_files = [] for root, dir, files in os.walk(unifi_dir): for file in files: if file.endswith('.js'): js_files.append(root+os.sep+file) return js_files def find_models_file(files, pattern): models_files = [] pattern = re.compile(pattern) for file in files: for i, line in enumerate(open(file)): for match in re.finditer(pattern, line): #log.info('Found in file %s on line %s: %s' % (file, i+1, match.group())) models_files.append(file) continue return models_files def find_json(files, pattern_match, all=False): pattern = re.compile(r".*\.exports=(\{.*?\}\}\}\})\}.*") json_obj = [] for file in files: size = os.path.getsize(file) pos = 0 progress = progress_bar(100, decimals=0, bar_length=40) progress.prefix='%s%s' % ('...' if len(file) > 30 else '', file[max(0,len(file)-27):]) with open(file) as dataFile: for line, data in enumerate(dataFile,1): pos += len(data) #log.info("searching line: %d, %d%%" % (line, int(pos*100//size))) progress.update(pos*100.0/size) match_iter = pattern.finditer(data) for match in match_iter: if pattern_match in match.group(1): json_string = match.group(1) json_string = json_string.replace("!0", "1").replace("!1", "0").strip() try: jsonObj = hjson.loads(json_string) json_obj.append(jsonObj) progress.suffix="matches: %d" % len(json_obj) log.debug('Found json: %s' % pprint(jsonObj)) if not all: newline() log.info("Total Json matches found: %d" % len(json_obj)) return json_obj except json.decoder.JSONDecodeError as e: newline() log.error('Json Error: %s' % e) newline() log.info("Total Json matches found: %d" % len(json_obj)) return json_obj def merge_dicts(a, b): c = a.copy() for k,v in b.items(): if isinstance(v, dict): d = a.get(k, None) if isinstance(d,dict): c[k] = merge_dicts(v, d) else: c[k]=v else: c[k]=v return c def consolidate_json(json_list): json_obj = {} for json in json_list: json_obj.update(json) json_dict = OrderedDict(sorted(json_obj.items(), key=lambda t: t[1]['type'])) return json_dict def get_summary(data): summary = {} for device, info in data.items(): if device in supported_devices: #models.json format data summary[device]=len(info) else: #unifi devices format data if info['type'] not in summary: summary[info['type']] = 1 else: summary[info['type']] += 1 return summary def update_models(file, data): if os.path.exists(file): log.warn('Updating file: %s, press ^C if you want to exit!' % file) with open(file) as f: models = json.loads(f.read(), object_pairs_hook=OrderedDict) new_models = OrderedDict() #ensure we have an entry for each supported type (even if it's blank) for type in supported_devices: if not models.get(type): models[type] = {} new_models[type] = {} log.info('NOTE! currently supported devices are: %s' % supported_devices) for device, info in data.items(): type = info['type'].upper() result = True if type in supported_devices and device not in models[type]: #check for UDN if type == 'UDM': new_models[type][device]=info #add to models database (even though UDM section isn't currently used) elif type == 'UAP': #all that is needed for AP's new_models[type][device]={} new_models[type][device]['name']=info['name'] log.info('Added %s device: %s - %s' % (type, device, info['name'])) continue else: for existing_device, existing_info in models[type].items(): if info['name'].upper() in existing_info['name'].upper(): log.info('========New Device %s =========' % device) log.info('looks like %s - %s is similar to %s - %s' % (device, info['name'], existing_device, existing_info['name'])) if query_yes_no("Do you want to copy it into the database? (if You select No, you can add it as a new device)"): new_models[type][device]=existing_info log.info('Device: %s copied' % new_models[type][device]) result = False else: result = query_yes_no("Do you want to add it as a new device to the database?") if not result: continue #new device log.info('========New Device %s =========' % device) log.info('found new device: %s - %s, type: %s' % (device, info['name'], type)) if not query_yes_no("Do you want to add it to the database?"): continue else: #UDM only if type == 'UDM': log.info('This is a %s device, you have to choose what type of device to add it as' % type) while True: try: options = [{option: choice.upper()} for option, choice in enumerate(info['subtypes'])] sel_option = query_number('please select one of the following options: %s' % options, 0) type = info['subtypes'][sel_option].upper() break except exception as e: log.error('error: %s' % e) #add new device name new_models[type][device]={} new_models[type
self.total = total self.prefix = prefix self.suffix = suffix self.decimals = decimals self.bar_length = bar_length self.prev_output_len = 0
identifier_body
get_models.py
= len(diagram) standard, sfp, sfp_plus = extract_ports_list(info['ports']) if len(standard) > 0: log.info('ports %s are standard ports' % standard ) standard = len(standard) if len(sfp) > 0: log.info('ports %s are sfp ports' % sfp ) sfp = len(sfp) if len(sfp_plus) > 0: log.info('ports %s are sfp+ ports' % sfp_plus ) sfp_plus = len(sfp_plus) new_models[type][device]['ports']={'number':standard,'rows':0 if standard==0 else 1 if standard <= 8 else 2} new_models[type][device]['sfp']={'number':sfp,'rows': sfp if sfp < 2 else 2} new_models[type][device]['sfp+']={'number':sfp_plus,'rows':sfp_plus if sfp_plus < 2 else 2} if standard > 0: rows = new_models[type][device]['ports']['rows'] new_models[type][device]['ports']['rows'] = query_number('how many ROWS of standard ports are there? (eg, 1,2)', rows) if sfp > 0: rows = new_models[type][device]['sfp']['rows'] new_models[type][device]['sfp']['rows'] = query_number('how many ROWS of sfp ports are there? (eg, 1,2)', rows) if sfp_plus > 0: rows = new_models[type][device]['sfp+']['rows'] new_models[type][device]['sfp+']['rows'] = query_number('how many ROWS of sfp+ ports are there? (eg, 1,2)', rows) if sfp > 0 or sfp_plus > 0: while not new_models[type][device].get('order'): if query_yes_no("Are the first ports (from the left) standard ports?"): new_models[type][device]['order'] = [0,1,2] else: if query_yes_no("Are the first ports (from the left) sfp ports?"): new_models[type][device]['order'] = [1,0,2] else: if query_yes_no("Are the first ports (from the left) sfp+ ports?"): new_models[type][device]['order'] = [2,0,1] else: log.error('OK, the first ports must be standard, sfp, or sfp+ ports. try again') log.debug('Device: %s added' % new_models[type][device]) log.info('Device: %s added' % new_models[type][device]['name']) log.debug('The following new devices have been added: %s' % pprint(new_models)) log.info('New devices: %s' % get_summary(new_models)) all_models = OrderedDict() all_models.update(models) if not any([value for value in get_summary(new_models).values()]): log.info('No New Models Found') #return else: if query_yes_no("Do you want to add them to the database?"): all_models = merge_dicts(models, new_models) log.info('database updated') if query_yes_no("Do you want to add the full Unifi data to the database (recommended)?"): for type, devices in all_models.copy().items(): for device in devices: if device in data: #log.info("adding : %s to models[%s][%s]['unifi']" % (data[device], type, device)) all_models[type][device]['unifi'] = data[device].copy() log.debug('The following data will be written to the database: %s' % pprint(all_models)) log.info('total devices: %s' % get_summary(all_models)) if query_yes_no("Do you want to overwrite the %s file?" % file, None): #backup original file from shutil import copyfile copyfile(file, file+'.org') #write models file out with open(file, 'w') as f: f.write(pprint(all_models)) log.info('File: %s Updated' % file) log.info('Total devices: %s' % get_summary(all_models)) else: log.info('File: %s NOT updated' % file) def extract_ports_list(ports): ''' returns ports list from unifi data as tuple of lists of port number ints eg ([0,1,2,3], [4,5],[]) (standard []. sfp[], sfp_plus[]) NOTE, USG's start at port 0, but switches start at port 1. ''' standard = [] sfp = [] sfp_plus = [] if isinstance(ports, (list, dict)): #standard = [x for x in range(1,len(ports)+1,1)] standard = [x for x in range(len(ports))] if ports.get('standard'): standard = ports_list_decode(ports['standard']) if ports.get('sfp'): sfp = ports_list_decode(ports['sfp']) if ports.get('plus'): sfp_plus = ports_list_decode(ports['plus']) return standard, sfp, sfp_plus def ports_list_decode(ports): ports_list = [] if isinstance(ports, int): ports_list = [x for x in range(1,ports+1,1)] if isinstance(ports, list): ports_list = ports if isinstance(ports, str): #log.info('Ports is a string: %s' % ports) ports_string_list = ports.split('-') ports_list = [x for x in range(int(ports_string_list[0]),int(ports_string_list[-1])+1,1)] return ports_list def query_yes_no(question, default="yes"): """Ask a yes/no question via input() and return their answer. "question" is a string that is presented to the user. "default" is the presumed answer if the user just hits <Enter>. It must be "yes" (the default), "no" or None (meaning an answer is required of the user). The "answer" return value is True for "yes" or False for "no". """ valid = {"yes": True, "y": True, "ye": True, "no": False, "n": False} if default is None: prompt = " [y/n] " elif default == "yes": prompt = " [Y/n] " elif default == "no": prompt = " [y/N] " else: raise ValueError("invalid default answer: '%s'" % default) while True: sys.stdout.write(question + prompt) choice = input().lower() if default is not None and choice == '': return valid[default] elif choice in valid: return valid[choice] else: sys.stdout.write("Please respond with 'yes' or 'no' " "(or 'y' or 'n').\n") def query_number(question, default=1): """Ask a numerical question via input() and return their answer. "question" is a string that is presented to the user. "default" is the presumed answer if the user just hits <Enter>. It must be a number or None (meaning an answer is required of the user). The "answer" return value is an int. """ if default is None: prompt = " [] " else: prompt = " [%d] " % default while True: sys.stdout.write(question + prompt) choice = input().lower() if default is not None and choice == '': return int(default) elif choice.isdigit(): return int(choice) else: sys.stdout.write("Please respond with a number\n") def secondsToStr(elapsed=None): if elapsed is None: return time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) else: return str(timedelta(seconds=elapsed)) def sigterm_handler(signal, frame): log.info('Received SIGTERM signal') sys.exit(0) def setup_logger(logger_name, log_file, level=logging.DEBUG, console=False): try: l = logging.getLogger(logger_name) formatter = logging.Formatter('[%(levelname)1.1s %(asctime)s] (%(name)-5s) %(message)s') if log_file is not None: fileHandler = logging.handlers.RotatingFileHandler(log_file, mode='a', maxBytes=2000000, backupCount=5) fileHandler.setFormatter(formatter) if console == True: formatter = logging.Formatter('[%(levelname)1.1s %(name)-5s] %(message)s') streamHandler = logging.StreamHandler() streamHandler.setFormatter(formatter) l.setLevel(level) if log_file is not None: l.addHandler(fileHandler) if console == True: l.addHandler(streamHandler) except Exception as e: print("Error in Logging setup: %s - do you have permission to write the log file??" % e) sys.exit(1) def main
():
identifier_name
get_models.py
('Found in file %s on line %s: %s' % (file, i+1, match.group())) models_files.append(file) continue return models_files def find_json(files, pattern_match, all=False): pattern = re.compile(r".*\.exports=(\{.*?\}\}\}\})\}.*") json_obj = [] for file in files: size = os.path.getsize(file) pos = 0 progress = progress_bar(100, decimals=0, bar_length=40) progress.prefix='%s%s' % ('...' if len(file) > 30 else '', file[max(0,len(file)-27):]) with open(file) as dataFile: for line, data in enumerate(dataFile,1): pos += len(data) #log.info("searching line: %d, %d%%" % (line, int(pos*100//size))) progress.update(pos*100.0/size) match_iter = pattern.finditer(data) for match in match_iter: if pattern_match in match.group(1): json_string = match.group(1) json_string = json_string.replace("!0", "1").replace("!1", "0").strip() try: jsonObj = hjson.loads(json_string) json_obj.append(jsonObj) progress.suffix="matches: %d" % len(json_obj) log.debug('Found json: %s' % pprint(jsonObj)) if not all: newline() log.info("Total Json matches found: %d" % len(json_obj)) return json_obj except json.decoder.JSONDecodeError as e: newline() log.error('Json Error: %s' % e) newline() log.info("Total Json matches found: %d" % len(json_obj)) return json_obj def merge_dicts(a, b): c = a.copy() for k,v in b.items(): if isinstance(v, dict): d = a.get(k, None) if isinstance(d,dict): c[k] = merge_dicts(v, d) else: c[k]=v else: c[k]=v return c def consolidate_json(json_list): json_obj = {} for json in json_list: json_obj.update(json) json_dict = OrderedDict(sorted(json_obj.items(), key=lambda t: t[1]['type'])) return json_dict def get_summary(data): summary = {} for device, info in data.items(): if device in supported_devices: #models.json format data summary[device]=len(info) else: #unifi devices format data if info['type'] not in summary: summary[info['type']] = 1 else: summary[info['type']] += 1 return summary def update_models(file, data): if os.path.exists(file): log.warn('Updating file: %s, press ^C if you want to exit!' % file) with open(file) as f: models = json.loads(f.read(), object_pairs_hook=OrderedDict) new_models = OrderedDict() #ensure we have an entry for each supported type (even if it's blank) for type in supported_devices: if not models.get(type): models[type] = {} new_models[type] = {} log.info('NOTE! currently supported devices are: %s' % supported_devices) for device, info in data.items(): type = info['type'].upper() result = True if type in supported_devices and device not in models[type]: #check for UDN if type == 'UDM': new_models[type][device]=info #add to models database (even though UDM section isn't currently used) elif type == 'UAP': #all that is needed for AP's new_models[type][device]={} new_models[type][device]['name']=info['name'] log.info('Added %s device: %s - %s' % (type, device, info['name'])) continue else: for existing_device, existing_info in models[type].items(): if info['name'].upper() in existing_info['name'].upper(): log.info('========New Device %s =========' % device) log.info('looks like %s - %s is similar to %s - %s' % (device, info['name'], existing_device, existing_info['name'])) if query_yes_no("Do you want to copy it into the database? (if You select No, you can add it as a new device)"): new_models[type][device]=existing_info log.info('Device: %s copied' % new_models[type][device]) result = False else: result = query_yes_no("Do you want to add it as a new device to the database?") if not result: continue #new device log.info('========New Device %s =========' % device) log.info('found new device: %s - %s, type: %s' % (device, info['name'], type)) if not query_yes_no("Do you want to add it to the database?"): continue else: #UDM only if type == 'UDM': log.info('This is a %s device, you have to choose what type of device to add it as' % type) while True: try: options = [{option: choice.upper()} for option, choice in enumerate(info['subtypes'])] sel_option = query_number('please select one of the following options: %s' % options, 0) type = info['subtypes'][sel_option].upper() break except exception as e: log.error('error: %s' % e) #add new device name new_models[type][device]={} new_models[type][device]['name']=info['name'] if type != 'UAP': #only way for UAP to get here is if a UDM was selected as a UAP, this will skip over in that case. if info.get('features'): poe = info['features'].get('poe',0) new_models[type][device]['poe'] = True if poe == 1 else False if info.get('diagram'): diagram = info['diagram'] log.info('here is a diagram of the device: %s' % pprint(diagram)) rows = len(diagram) standard, sfp, sfp_plus = extract_ports_list(info['ports']) if len(standard) > 0: log.info('ports %s are standard ports' % standard ) standard = len(standard) if len(sfp) > 0: log.info('ports %s are sfp ports' % sfp ) sfp = len(sfp) if len(sfp_plus) > 0: log.info('ports %s are sfp+ ports' % sfp_plus ) sfp_plus = len(sfp_plus) new_models[type][device]['ports']={'number':standard,'rows':0 if standard==0 else 1 if standard <= 8 else 2} new_models[type][device]['sfp']={'number':sfp,'rows': sfp if sfp < 2 else 2} new_models[type][device]['sfp+']={'number':sfp_plus,'rows':sfp_plus if sfp_plus < 2 else 2} if standard > 0: rows = new_models[type][device]['ports']['rows'] new_models[type][device]['ports']['rows'] = query_number('how many ROWS of standard ports are there? (eg, 1,2)', rows) if sfp > 0: rows = new_models[type][device]['sfp']['rows'] new_models[type][device]['sfp']['rows'] = query_number('how many ROWS of sfp ports are there? (eg, 1,2)', rows) if sfp_plus > 0: rows = new_models[type][device]['sfp+']['rows'] new_models[type][device]['sfp+']['rows'] = query_number('how many ROWS of sfp+ ports are there? (eg, 1,2)', rows) if sfp > 0 or sfp_plus > 0: while not new_models[type][device].get('order'): if query_yes_no("Are the first ports (from the left) standard ports?"): new_models[type][device]['order'] = [0,1,2] else: if query_yes_no("Are the first ports (from the left) sfp ports?"): new_models[type][device]['order'] = [1,0,2] else: if query_yes_no("Are the first ports (from the left) sfp+ ports?"): new_
else: log.error('OK, the first ports must be standard, sfp, or sfp+ ports. try again') log.debug('Device: %s added' % new_models[type][device]) log.info('Device: %s added' % new_models[type][device]['name']) log.debug('The following new
models[type][device]['order'] = [2,0,1]
conditional_block
webhdfs.rs
"); for input in fs { let input_path = Path::new(&input); let output_file = input_path.file_name().expect2("file name must be specified if no output file is given"); let output = target_dir.join(&Path::new(output_file)); let mut out = File::create(&output).expect2("Could not create output file"); client.get_file(&input, &mut out).expect2("get error") } } } } } } fn version() -> ! { println!( "{} ({}) version {}", env!("CARGO_PKG_DESCRIPTION"), env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION") ); std::process::exit(0); } fn usage() -> ! { println!("USAGE: webhdfs <options>... <command> <files>... webhdfs -h|--help webhdfs -v|--version options: -U|--uri|--url <url> API entrypoint -u|--user <string> User name -d|--doas <string> DoAs username -T|--dt <string> Delegation token -t|--timeout <unsigned> Default timeout in seconds -N|--natmap-file <filepath> Path to NAT mappings file -n|--natmap-entry <k=v> NAT mapping (multiple options are Ok) command and files: -v|--version Print version and exit -h|--help Print this thelp screen and exit --save-config <filepath> Save the effective configuration to the file -g|--get <remote-filepath> <local-path> -g|--get <remote-filepath> -g|--get <remote-filepath>.. <local-dirpath> Get files from HDFS "); std::process::exit(1); } enum Operation { Get(Vec<String>) } fn parse_command_line() -> (SyncHdfsClient, Operation) { use std::time::Duration; use std::collections::HashMap; use commandline::*; enum Sw { Uri, User, Doas, DToken, Timeout, NMFile, NMEntry, SaveConfig } enum Op { Get } struct S { sw: Option<Sw>, op: Option<Op>, files: Vec<String>, uri: Option<String>, user: Option<String>, doas: Option<String>, dtoken: Option<String>, timeout: Option<Duration>, natmap: Option<HashMap<String, String>>, save_config: Option<String>, } let s0 = S { sw: None, op: None, files: vec![], uri: None, user: None, doas:None, timeout: None, dtoken: None, natmap: None, save_config: None }; let result = commandline::parse_cmdln(s0, |mut s, arg| if let Some(sw) = s.sw.take() { match sw { Sw::Uri => S { uri: Some(arg.arg()), ..s }, Sw::User => S { user: Some(arg.arg()), ..s }, Sw::Doas => S { doas: Some(arg.arg()), ..s }, Sw::DToken => S { dtoken: Some(arg.arg()), ..s }, Sw::SaveConfig => S { save_config: Some(arg.arg()), ..s }, Sw::Timeout => S { timeout: Some(Duration::from_secs(arg.arg().parse().expect2("Invalid timeout duration"))), ..s }, Sw::NMFile => S { natmap: Some(config::read_kv_file(&arg.arg()).expect2("malformed natmap file")), ..s }, Sw::NMEntry => { let mut nm = if let Some(nm) = s.natmap { nm } else { HashMap::new() }; let (k, v) = config::split_kv(arg.arg()).expect2("invalid natmap entry"); nm.insert(k, v); S { natmap: Some(nm), ..s } } } } else { match arg.switch_ref() { "-v"|"--version" => version(), "-h"|"--help" => usage(), "-g"|"--get" => S { op: Some(Op::Get), ..s }, "-U"|"--uri"|"--url" => S { sw: Some(Sw::Uri), ..s }, "-u"|"--user" => S { sw: Some(Sw::User), ..s }, "-d"|"--doas" => S { sw: Some(Sw::Doas), ..s }, "-T"|"--dt" => S { sw: Some(Sw::DToken), ..s }, "-t"|"--timeout" => S { sw: Some(Sw::Timeout), ..s }, "-N"|"--natmap-file" => S { sw: Some(Sw::NMFile), ..s }, "-n"|"--natmap-entry" => S { sw: Some(Sw::NMEntry), ..s }, "--save-config" => S { sw: Some(Sw::SaveConfig), ..s }, _ => { s.files.push(arg.arg()); s} } }); if result.sw.is_some() { error_exit("invalid command line at the end", "") } if let Some(f) = result.save_config { if result.op.is_some() { error_exit("--save-config must be used alone", "") } let uri = result.uri.expect2("must specify --uri when saving config"); let cfg = config::Config::new(uri.parse().expect2("Cannot parse URI")); config::write_config(&std::path::Path::new(&f), &cfg, true); std::process::exit(0); } else { let operation = if let Some(op) = result.op { op } else { error_exit("must specify operation", "") }; //build context let mut cx = if let Some(uri) = result.uri { SyncHdfsClientBuilder::new(uri.parse().expect2("Cannot parse URI")) } else { SyncHdfsClientBuilder::from_config_opt().expect2("No configuration files were found, and no mandatory options (--uri) were specified") }; if let Some(user) = result.user { cx = cx.user_name(user) } if let Some(doas) = result.doas { cx = cx.doas(doas) } if let Some(timeout) = result.timeout { cx = cx.default_timeout(timeout) } if let Some(natmap) = result.natmap { cx = cx.natmap(NatMap::new(natmap.into_iter()).expect2("Invalid natmap")) } if let Some(dtoken) = result.dtoken { cx = cx.delegation_token(dtoken) } let client = cx.build().expect2("Cannot build SyncHdfsClient"); let operation = match operation { Op::Get => if result.files.len() > 0 { Operation::Get(result.files) } else
}; (client, operation) } } //------------------------- mod commandline { /// Prints two-part message to stderr and exits pub fn error_exit(msg: &str, detail: &str) -> ! { eprint!("Error: {}", msg); if detail.is_empty() { eprintln!() } else { eprintln!(" ({})", detail); } std::process::exit(1) } /// Expect2 function pub trait Expect2<T> { /// Same as Result::expect but the error message is brief and not intimidating fn expect2(self, msg: &str) -> T; } impl<T, E: std::error::Error> Expect2<T> for std::result::Result<T, E> { fn expect2(self, msg: &str) -> T { match self { Ok(v) => v, Err(e) => error_exit(msg, &e.to_string()) } } } impl<T> Expect2<T> for Option<T> { fn expect2(self, msg: &str) -> T { match self { Some(v) => v, None => error_exit(msg, "") } } } #[derive(Debug)] pub enum CmdLn { Switch(String), Arg(String), Item(String) } impl std::fmt::Display for CmdLn { fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { CmdLn::Switch(s) => write!(fmt, "Switch '{}'", s), CmdLn::Arg(s) => write!(fmt, "Arg '{}'", s), CmdLn::Item(s) => write!(fmt, "Item '{}'", s) } } } impl CmdLn { /// Splits command line argruments if needed /// - _ if bypass => Item(_) /// - '--sw=arg' => Switch('--sw') Arg('arg') /// - '-abc' => Item('-a') Item('-b') Item('-c') /// - '--' => *bypass = true; [] /// - _ => Item(_) fn convert_arg(bypass
{ error_exit("must specify at least one input file for --get", "") }
conditional_block
webhdfs.rs
"); for input in fs { let input_path = Path::new(&input); let output_file = input_path.file_name().expect2("file name must be specified if no output file is given"); let output = target_dir.join(&Path::new(output_file)); let mut out = File::create(&output).expect2("Could not create output file"); client.get_file(&input, &mut out).expect2("get error") } } } } } } fn version() -> ! { println!( "{} ({}) version {}", env!("CARGO_PKG_DESCRIPTION"), env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION") ); std::process::exit(0); } fn usage() -> ! { println!("USAGE: webhdfs <options>... <command> <files>... webhdfs -h|--help webhdfs -v|--version options: -U|--uri|--url <url> API entrypoint -u|--user <string> User name -d|--doas <string> DoAs username -T|--dt <string> Delegation token -t|--timeout <unsigned> Default timeout in seconds -N|--natmap-file <filepath> Path to NAT mappings file -n|--natmap-entry <k=v> NAT mapping (multiple options are Ok) command and files: -v|--version Print version and exit -h|--help Print this thelp screen and exit --save-config <filepath> Save the effective configuration to the file -g|--get <remote-filepath> <local-path> -g|--get <remote-filepath> -g|--get <remote-filepath>.. <local-dirpath> Get files from HDFS "); std::process::exit(1); } enum Operation { Get(Vec<String>) } fn parse_command_line() -> (SyncHdfsClient, Operation) { use std::time::Duration; use std::collections::HashMap; use commandline::*; enum Sw { Uri, User, Doas, DToken, Timeout, NMFile, NMEntry, SaveConfig } enum Op { Get } struct S { sw: Option<Sw>, op: Option<Op>, files: Vec<String>, uri: Option<String>, user: Option<String>, doas: Option<String>, dtoken: Option<String>, timeout: Option<Duration>, natmap: Option<HashMap<String, String>>, save_config: Option<String>, } let s0 = S { sw: None, op: None, files: vec![], uri: None, user: None, doas:None, timeout: None, dtoken: None, natmap: None, save_config: None }; let result = commandline::parse_cmdln(s0, |mut s, arg| if let Some(sw) = s.sw.take() { match sw { Sw::Uri => S { uri: Some(arg.arg()), ..s }, Sw::User => S { user: Some(arg.arg()), ..s }, Sw::Doas => S { doas: Some(arg.arg()), ..s }, Sw::DToken => S { dtoken: Some(arg.arg()), ..s }, Sw::SaveConfig => S { save_config: Some(arg.arg()), ..s }, Sw::Timeout => S { timeout: Some(Duration::from_secs(arg.arg().parse().expect2("Invalid timeout duration"))), ..s }, Sw::NMFile => S { natmap: Some(config::read_kv_file(&arg.arg()).expect2("malformed natmap file")), ..s }, Sw::NMEntry => { let mut nm = if let Some(nm) = s.natmap { nm } else { HashMap::new() }; let (k, v) = config::split_kv(arg.arg()).expect2("invalid natmap entry"); nm.insert(k, v); S { natmap: Some(nm), ..s } } } } else { match arg.switch_ref() { "-v"|"--version" => version(), "-h"|"--help" => usage(), "-g"|"--get" => S { op: Some(Op::Get), ..s }, "-U"|"--uri"|"--url" => S { sw: Some(Sw::Uri), ..s }, "-u"|"--user" => S { sw: Some(Sw::User), ..s }, "-d"|"--doas" => S { sw: Some(Sw::Doas), ..s }, "-T"|"--dt" => S { sw: Some(Sw::DToken), ..s }, "-t"|"--timeout" => S { sw: Some(Sw::Timeout), ..s }, "-N"|"--natmap-file" => S { sw: Some(Sw::NMFile), ..s }, "-n"|"--natmap-entry" => S { sw: Some(Sw::NMEntry), ..s }, "--save-config" => S { sw: Some(Sw::SaveConfig), ..s }, _ => { s.files.push(arg.arg()); s} } }); if result.sw.is_some() { error_exit("invalid command line at the end", "") } if let Some(f) = result.save_config { if result.op.is_some() { error_exit("--save-config must be used alone", "") } let uri = result.uri.expect2("must specify --uri when saving config"); let cfg = config::Config::new(uri.parse().expect2("Cannot parse URI")); config::write_config(&std::path::Path::new(&f), &cfg, true); std::process::exit(0); } else { let operation = if let Some(op) = result.op { op } else { error_exit("must specify operation", "") }; //build context let mut cx = if let Some(uri) = result.uri { SyncHdfsClientBuilder::new(uri.parse().expect2("Cannot parse URI")) } else { SyncHdfsClientBuilder::from_config_opt().expect2("No configuration files were found, and no mandatory options (--uri) were specified") }; if let Some(user) = result.user { cx = cx.user_name(user) } if let Some(doas) = result.doas { cx = cx.doas(doas) } if let Some(timeout) = result.timeout { cx = cx.default_timeout(timeout) } if let Some(natmap) = result.natmap { cx = cx.natmap(NatMap::new(natmap.into_iter()).expect2("Invalid natmap")) } if let Some(dtoken) = result.dtoken { cx = cx.delegation_token(dtoken) } let client = cx.build().expect2("Cannot build SyncHdfsClient"); let operation = match operation { Op::Get => if result.files.len() > 0 { Operation::Get(result.files) } else { error_exit("must specify at least one input file for --get", "") } }; (client, operation) } } //------------------------- mod commandline { /// Prints two-part message to stderr and exits pub fn error_exit(msg: &str, detail: &str) -> ! { eprint!("Error: {}", msg); if detail.is_empty() { eprintln!() } else { eprintln!(" ({})", detail); } std::process::exit(1) } /// Expect2 function pub trait Expect2<T> { /// Same as Result::expect but the error message is brief and not intimidating fn expect2(self, msg: &str) -> T; } impl<T, E: std::error::Error> Expect2<T> for std::result::Result<T, E> { fn expect2(self, msg: &str) -> T { match self { Ok(v) => v, Err(e) => error_exit(msg, &e.to_string()) } } } impl<T> Expect2<T> for Option<T> { fn expect2(self, msg: &str) -> T
} #[derive(Debug)] pub enum CmdLn { Switch(String), Arg(String), Item(String) } impl std::fmt::Display for CmdLn { fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { CmdLn::Switch(s) => write!(fmt, "Switch '{}'", s), CmdLn::Arg(s) => write!(fmt, "Arg '{}'", s), CmdLn::Item(s) => write!(fmt, "Item '{}'", s) } } } impl CmdLn { /// Splits command line argruments if needed /// - _ if bypass => Item(_) /// - '--sw=arg' => Switch('--sw') Arg('arg') /// - '-abc' => Item('-a') Item('-b') Item('-c') /// - '--' => *bypass = true; [] /// - _ => Item(_) fn convert_arg(bypass
{ match self { Some(v) => v, None => error_exit(msg, "") } }
identifier_body
webhdfs.rs
dir"); for input in fs { let input_path = Path::new(&input); let output_file = input_path.file_name().expect2("file name must be specified if no output file is given"); let output = target_dir.join(&Path::new(output_file)); let mut out = File::create(&output).expect2("Could not create output file"); client.get_file(&input, &mut out).expect2("get error") } } } } } } fn version() -> ! { println!( "{} ({}) version {}", env!("CARGO_PKG_DESCRIPTION"), env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION") ); std::process::exit(0); } fn usage() -> ! { println!("USAGE: webhdfs <options>... <command> <files>... webhdfs -h|--help webhdfs -v|--version options: -U|--uri|--url <url> API entrypoint -u|--user <string> User name -d|--doas <string> DoAs username -T|--dt <string> Delegation token -t|--timeout <unsigned> Default timeout in seconds -N|--natmap-file <filepath> Path to NAT mappings file -n|--natmap-entry <k=v> NAT mapping (multiple options are Ok) command and files: -v|--version Print version and exit -h|--help Print this thelp screen and exit --save-config <filepath> Save the effective configuration to the file -g|--get <remote-filepath> <local-path> -g|--get <remote-filepath> -g|--get <remote-filepath>.. <local-dirpath> Get files from HDFS "); std::process::exit(1); } enum Operation { Get(Vec<String>) } fn parse_command_line() -> (SyncHdfsClient, Operation) { use std::time::Duration; use std::collections::HashMap; use commandline::*; enum Sw { Uri, User, Doas, DToken, Timeout, NMFile, NMEntry, SaveConfig } enum Op { Get } struct S { sw: Option<Sw>, op: Option<Op>, files: Vec<String>, uri: Option<String>, user: Option<String>, doas: Option<String>, dtoken: Option<String>, timeout: Option<Duration>, natmap: Option<HashMap<String, String>>, save_config: Option<String>, } let s0 = S { sw: None, op: None, files: vec![], uri: None, user: None, doas:None, timeout: None, dtoken: None, natmap: None, save_config: None }; let result = commandline::parse_cmdln(s0, |mut s, arg| if let Some(sw) = s.sw.take() { match sw { Sw::Uri => S { uri: Some(arg.arg()), ..s }, Sw::User => S { user: Some(arg.arg()), ..s }, Sw::Doas => S { doas: Some(arg.arg()), ..s }, Sw::DToken => S { dtoken: Some(arg.arg()), ..s }, Sw::SaveConfig => S { save_config: Some(arg.arg()), ..s }, Sw::Timeout => S { timeout: Some(Duration::from_secs(arg.arg().parse().expect2("Invalid timeout duration"))), ..s }, Sw::NMFile => S { natmap: Some(config::read_kv_file(&arg.arg()).expect2("malformed natmap file")), ..s }, Sw::NMEntry => { let mut nm = if let Some(nm) = s.natmap { nm } else { HashMap::new() }; let (k, v) = config::split_kv(arg.arg()).expect2("invalid natmap entry"); nm.insert(k, v); S { natmap: Some(nm), ..s } } } } else { match arg.switch_ref() { "-v"|"--version" => version(), "-h"|"--help" => usage(), "-g"|"--get" => S { op: Some(Op::Get), ..s }, "-U"|"--uri"|"--url" => S { sw: Some(Sw::Uri), ..s }, "-u"|"--user" => S { sw: Some(Sw::User), ..s }, "-d"|"--doas" => S { sw: Some(Sw::Doas), ..s }, "-T"|"--dt" => S { sw: Some(Sw::DToken), ..s }, "-t"|"--timeout" => S { sw: Some(Sw::Timeout), ..s }, "-N"|"--natmap-file" => S { sw: Some(Sw::NMFile), ..s }, "-n"|"--natmap-entry" => S { sw: Some(Sw::NMEntry), ..s }, "--save-config" => S { sw: Some(Sw::SaveConfig), ..s }, _ => { s.files.push(arg.arg()); s} } }); if result.sw.is_some() { error_exit("invalid command line at the end", "") } if let Some(f) = result.save_config { if result.op.is_some() { error_exit("--save-config must be used alone", "") } let uri = result.uri.expect2("must specify --uri when saving config"); let cfg = config::Config::new(uri.parse().expect2("Cannot parse URI")); config::write_config(&std::path::Path::new(&f), &cfg, true); std::process::exit(0); } else { let operation = if let Some(op) = result.op { op } else { error_exit("must specify operation", "") }; //build context let mut cx = if let Some(uri) = result.uri { SyncHdfsClientBuilder::new(uri.parse().expect2("Cannot parse URI")) } else { SyncHdfsClientBuilder::from_config_opt().expect2("No configuration files were found, and no mandatory options (--uri) were specified") }; if let Some(user) = result.user { cx = cx.user_name(user) } if let Some(doas) = result.doas { cx = cx.doas(doas) } if let Some(timeout) = result.timeout { cx = cx.default_timeout(timeout) } if let Some(natmap) = result.natmap { cx = cx.natmap(NatMap::new(natmap.into_iter()).expect2("Invalid natmap")) } if let Some(dtoken) = result.dtoken { cx = cx.delegation_token(dtoken) } let client = cx.build().expect2("Cannot build SyncHdfsClient"); let operation = match operation { Op::Get => if result.files.len() > 0 { Operation::Get(result.files) } else { error_exit("must specify at least one input file for --get", "") } }; (client, operation) } } //------------------------- mod commandline { /// Prints two-part message to stderr and exits pub fn error_exit(msg: &str, detail: &str) -> ! { eprint!("Error: {}", msg); if detail.is_empty() { eprintln!() } else { eprintln!(" ({})", detail); } std::process::exit(1) } /// Expect2 function pub trait Expect2<T> { /// Same as Result::expect but the error message is brief and not intimidating fn expect2(self, msg: &str) -> T; } impl<T, E: std::error::Error> Expect2<T> for std::result::Result<T, E> { fn expect2(self, msg: &str) -> T { match self { Ok(v) => v, Err(e) => error_exit(msg, &e.to_string()) } } } impl<T> Expect2<T> for Option<T> { fn expect2(self, msg: &str) -> T { match self { Some(v) => v, None => error_exit(msg, "") } } } #[derive(Debug)] pub enum CmdLn {
impl std::fmt::Display for CmdLn { fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { CmdLn::Switch(s) => write!(fmt, "Switch '{}'", s), CmdLn::Arg(s) => write!(fmt, "Arg '{}'", s), CmdLn::Item(s) => write!(fmt, "Item '{}'", s) } } } impl CmdLn { /// Splits command line argruments if needed /// - _ if bypass => Item(_) /// - '--sw=arg' => Switch('--sw') Arg('arg') /// - '-abc' => Item('-a') Item('-b') Item('-c') /// - '--' => *bypass = true; [] /// - _ => Item(_) fn convert_arg(bypass:
Switch(String), Arg(String), Item(String) }
random_line_split
webhdfs.rs
"); for input in fs { let input_path = Path::new(&input); let output_file = input_path.file_name().expect2("file name must be specified if no output file is given"); let output = target_dir.join(&Path::new(output_file)); let mut out = File::create(&output).expect2("Could not create output file"); client.get_file(&input, &mut out).expect2("get error") } } } } } } fn
() -> ! { println!( "{} ({}) version {}", env!("CARGO_PKG_DESCRIPTION"), env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION") ); std::process::exit(0); } fn usage() -> ! { println!("USAGE: webhdfs <options>... <command> <files>... webhdfs -h|--help webhdfs -v|--version options: -U|--uri|--url <url> API entrypoint -u|--user <string> User name -d|--doas <string> DoAs username -T|--dt <string> Delegation token -t|--timeout <unsigned> Default timeout in seconds -N|--natmap-file <filepath> Path to NAT mappings file -n|--natmap-entry <k=v> NAT mapping (multiple options are Ok) command and files: -v|--version Print version and exit -h|--help Print this thelp screen and exit --save-config <filepath> Save the effective configuration to the file -g|--get <remote-filepath> <local-path> -g|--get <remote-filepath> -g|--get <remote-filepath>.. <local-dirpath> Get files from HDFS "); std::process::exit(1); } enum Operation { Get(Vec<String>) } fn parse_command_line() -> (SyncHdfsClient, Operation) { use std::time::Duration; use std::collections::HashMap; use commandline::*; enum Sw { Uri, User, Doas, DToken, Timeout, NMFile, NMEntry, SaveConfig } enum Op { Get } struct S { sw: Option<Sw>, op: Option<Op>, files: Vec<String>, uri: Option<String>, user: Option<String>, doas: Option<String>, dtoken: Option<String>, timeout: Option<Duration>, natmap: Option<HashMap<String, String>>, save_config: Option<String>, } let s0 = S { sw: None, op: None, files: vec![], uri: None, user: None, doas:None, timeout: None, dtoken: None, natmap: None, save_config: None }; let result = commandline::parse_cmdln(s0, |mut s, arg| if let Some(sw) = s.sw.take() { match sw { Sw::Uri => S { uri: Some(arg.arg()), ..s }, Sw::User => S { user: Some(arg.arg()), ..s }, Sw::Doas => S { doas: Some(arg.arg()), ..s }, Sw::DToken => S { dtoken: Some(arg.arg()), ..s }, Sw::SaveConfig => S { save_config: Some(arg.arg()), ..s }, Sw::Timeout => S { timeout: Some(Duration::from_secs(arg.arg().parse().expect2("Invalid timeout duration"))), ..s }, Sw::NMFile => S { natmap: Some(config::read_kv_file(&arg.arg()).expect2("malformed natmap file")), ..s }, Sw::NMEntry => { let mut nm = if let Some(nm) = s.natmap { nm } else { HashMap::new() }; let (k, v) = config::split_kv(arg.arg()).expect2("invalid natmap entry"); nm.insert(k, v); S { natmap: Some(nm), ..s } } } } else { match arg.switch_ref() { "-v"|"--version" => version(), "-h"|"--help" => usage(), "-g"|"--get" => S { op: Some(Op::Get), ..s }, "-U"|"--uri"|"--url" => S { sw: Some(Sw::Uri), ..s }, "-u"|"--user" => S { sw: Some(Sw::User), ..s }, "-d"|"--doas" => S { sw: Some(Sw::Doas), ..s }, "-T"|"--dt" => S { sw: Some(Sw::DToken), ..s }, "-t"|"--timeout" => S { sw: Some(Sw::Timeout), ..s }, "-N"|"--natmap-file" => S { sw: Some(Sw::NMFile), ..s }, "-n"|"--natmap-entry" => S { sw: Some(Sw::NMEntry), ..s }, "--save-config" => S { sw: Some(Sw::SaveConfig), ..s }, _ => { s.files.push(arg.arg()); s} } }); if result.sw.is_some() { error_exit("invalid command line at the end", "") } if let Some(f) = result.save_config { if result.op.is_some() { error_exit("--save-config must be used alone", "") } let uri = result.uri.expect2("must specify --uri when saving config"); let cfg = config::Config::new(uri.parse().expect2("Cannot parse URI")); config::write_config(&std::path::Path::new(&f), &cfg, true); std::process::exit(0); } else { let operation = if let Some(op) = result.op { op } else { error_exit("must specify operation", "") }; //build context let mut cx = if let Some(uri) = result.uri { SyncHdfsClientBuilder::new(uri.parse().expect2("Cannot parse URI")) } else { SyncHdfsClientBuilder::from_config_opt().expect2("No configuration files were found, and no mandatory options (--uri) were specified") }; if let Some(user) = result.user { cx = cx.user_name(user) } if let Some(doas) = result.doas { cx = cx.doas(doas) } if let Some(timeout) = result.timeout { cx = cx.default_timeout(timeout) } if let Some(natmap) = result.natmap { cx = cx.natmap(NatMap::new(natmap.into_iter()).expect2("Invalid natmap")) } if let Some(dtoken) = result.dtoken { cx = cx.delegation_token(dtoken) } let client = cx.build().expect2("Cannot build SyncHdfsClient"); let operation = match operation { Op::Get => if result.files.len() > 0 { Operation::Get(result.files) } else { error_exit("must specify at least one input file for --get", "") } }; (client, operation) } } //------------------------- mod commandline { /// Prints two-part message to stderr and exits pub fn error_exit(msg: &str, detail: &str) -> ! { eprint!("Error: {}", msg); if detail.is_empty() { eprintln!() } else { eprintln!(" ({})", detail); } std::process::exit(1) } /// Expect2 function pub trait Expect2<T> { /// Same as Result::expect but the error message is brief and not intimidating fn expect2(self, msg: &str) -> T; } impl<T, E: std::error::Error> Expect2<T> for std::result::Result<T, E> { fn expect2(self, msg: &str) -> T { match self { Ok(v) => v, Err(e) => error_exit(msg, &e.to_string()) } } } impl<T> Expect2<T> for Option<T> { fn expect2(self, msg: &str) -> T { match self { Some(v) => v, None => error_exit(msg, "") } } } #[derive(Debug)] pub enum CmdLn { Switch(String), Arg(String), Item(String) } impl std::fmt::Display for CmdLn { fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { CmdLn::Switch(s) => write!(fmt, "Switch '{}'", s), CmdLn::Arg(s) => write!(fmt, "Arg '{}'", s), CmdLn::Item(s) => write!(fmt, "Item '{}'", s) } } } impl CmdLn { /// Splits command line argruments if needed /// - _ if bypass => Item(_) /// - '--sw=arg' => Switch('--sw') Arg('arg') /// - '-abc' => Item('-a') Item('-b') Item('-c') /// - '--' => *bypass = true; [] /// - _ => Item(_) fn convert_arg(bypass:
version
identifier_name
deepobject.go
/oapi-codegen/pkg/types" ) func marshalDeepObject(in interface{}, path []string) ([]string, error) { var result []string switch t := in.(type) { case []interface{}: // For the array, we will use numerical subscripts of the form [x], // in the same order as the array. for i, iface := range t { newPath := append(path, strconv.Itoa(i)) fields, err := marshalDeepObject(iface, newPath) if err != nil { return nil, errors.Wrap(err, "error traversing array") } result = append(result, fields...) } case map[string]interface{}: // For a map, each key (field name) becomes a member of the path, and // we recurse. First, sort the keys. keys := make([]string, len(t)) i := 0 for k := range t { keys[i] = k i++ } sort.Strings(keys) // Now, for each key, we recursively marshal it. for _, k := range keys { newPath := append(path, k) fields, err := marshalDeepObject(t[k], newPath) if err != nil { return nil, errors.Wrap(err, "error traversing map") } result = append(result, fields...) } default: // Now, for a concrete value, we will turn the path elements // into a deepObject style set of subscripts. [a, b, c] turns into // [a][b][c] prefix := "[" + strings.Join(path, "][") + "]" result = []string{ prefix + fmt.Sprintf("=%v", t), } } return result, nil } func MarshalDeepObject(i interface{}, paramName string) (string, error) { // We're going to marshal to JSON and unmarshal into an interface{}, // which will use the json pkg to deal with all the field annotations. We // can then walk the generic object structure to produce a deepObject. This // isn't efficient and it would be more efficient to reflect on our own, // but it's complicated, error-prone code. buf, err := json.Marshal(i) if err != nil { return "", errors.Wrap(err, "failed to marshal input to JSON") } var i2 interface{} err = json.Unmarshal(buf, &i2) if err != nil { return "", errors.Wrap(err, "failed to unmarshal JSON") } fields, err := marshalDeepObject(i2, nil) if err != nil { return "", errors.Wrap(err, "error traversing JSON structure") } // Prefix the param name to each subscripted field. for i := range fields { fields[i] = paramName + fields[i] } return strings.Join(fields, "&"), nil } type fieldOrValue struct { fields map[string]fieldOrValue value string } func (f *fieldOrValue) appendPathValue(path []string, value string) { fieldName := path[0] if len(path) == 1 { f.fields[fieldName] = fieldOrValue{value: value} return } pv, found := f.fields[fieldName] if !found { pv = fieldOrValue{ fields: make(map[string]fieldOrValue), } f.fields[fieldName] = pv } pv.appendPathValue(path[1:], value) } func makeFieldOrValue(paths [][]string, values []string) fieldOrValue { f := fieldOrValue{ fields: make(map[string]fieldOrValue), } for i := range paths { path := paths[i] value := values[i] f.appendPathValue(path, value) } return f } func
(dst interface{}, paramName string, params url.Values) error { // Params are all the query args, so we need those that look like // "paramName["... var fieldNames []string var fieldValues []string searchStr := paramName + "[" for pName, pValues := range params { if strings.HasPrefix(pName, searchStr) { // trim the parameter name from the full name. pName = pName[len(paramName):] fieldNames = append(fieldNames, pName) if len(pValues) != 1 { return fmt.Errorf("%s has multiple values", pName) } fieldValues = append(fieldValues, pValues[0]) } } // Now, for each field, reconstruct its subscript path and value paths := make([][]string, len(fieldNames)) for i, path := range fieldNames { path = strings.TrimLeft(path, "[") path = strings.TrimRight(path, "]") paths[i] = strings.Split(path, "][") } fieldPaths := makeFieldOrValue(paths, fieldValues) err := assignPathValues(dst, fieldPaths) if err != nil { return errors.Wrap(err, "error assigning value to destination") } return nil } // This returns a field name, either using the variable name, or the json // annotation if that exists. func getFieldName(f reflect.StructField) string { n := f.Name tag, found := f.Tag.Lookup("json") if found { // If we have a json field, and the first part of it before the // first comma is non-empty, that's our field name. parts := strings.Split(tag, ",") if parts[0] != "" { n = parts[0] } } return n } // Create a map of field names that we'll see in the deepObject to reflect // field indices on the given type. func fieldIndicesByJsonTag(i interface{}) (map[string]int, error) { t := reflect.TypeOf(i) if t.Kind() != reflect.Struct { return nil, errors.New("expected a struct as input") } n := t.NumField() fieldMap := make(map[string]int) for i := 0; i < n; i++ { field := t.Field(i) fieldName := getFieldName(field) fieldMap[fieldName] = i } return fieldMap, nil } func assignPathValues(dst interface{}, pathValues fieldOrValue) error { //t := reflect.TypeOf(dst) v := reflect.ValueOf(dst) iv := reflect.Indirect(v) it := iv.Type() switch it.Kind() { case reflect.Slice: sliceLength := len(pathValues.fields) dstSlice := reflect.MakeSlice(it, sliceLength, sliceLength) err := assignSlice(dstSlice, pathValues) if err != nil { return errors.Wrap(err, "error assigning slice") } iv.Set(dstSlice) return nil case reflect.Struct: // Some special types we care about are structs. Handle them // here. They may be redefined, so we need to do some hoop // jumping. If the types are aliased, we need to type convert // the pointer, then set the value of the dereference pointer. // We check to see if the object implements the Binder interface first. if dst, isBinder := v.Interface().(Binder); isBinder { return dst.Bind(pathValues.value) } // Then check the legacy types if it.ConvertibleTo(reflect.TypeOf(types.Date{})) { var date types.Date var err error date.Time, err = time.Parse(types.DateFormat, pathValues.value) if err != nil { return errors.Wrap(err, "invalid date format") } dst := iv if it != reflect.TypeOf(types.Date{}) { // Types are aliased, convert the pointers. ivPtr := iv.Addr() aPtr := ivPtr.Convert(reflect.TypeOf(&types.Date{})) dst = reflect.Indirect(aPtr) } dst.Set(reflect.ValueOf(date)) } if it.ConvertibleTo(reflect.TypeOf(time.Time{})) { var tm time.Time var err error tm, err = time.Parse(time.RFC3339Nano, pathValues.value) if err != nil { // Fall back to parsing it as a date. tm, err = time.Parse(types.DateFormat, pathValues.value) if err != nil { return fmt.Errorf("error parsing tim as RFC3339 or 2006-01-02 time: %s", err) } return errors.Wrap(err, "invalid date format") } dst := iv if it != reflect.TypeOf(time.Time{}) { // Types are aliased, convert the pointers. ivPtr := iv.Addr() aPtr := ivPtr.Convert(reflect.TypeOf(&time.Time{})) dst = reflect.Indirect(aPtr) } dst.Set(reflect.ValueOf(tm)) } fieldMap, err := fieldIndicesByJsonTag(iv.Interface()) if err != nil { return errors.Wrap(err, "failed enumerating fields") } for _, fieldName := range sortedFieldOrValueKeys(pathValues.fields) { fieldValue := pathValues.fields[fieldName] fieldIndex, found := fieldMap[fieldName] if !found { return fmt.Errorf("field [%s] is not present in destination object", fieldName) }
UnmarshalDeepObject
identifier_name
deepobject.go
api-codegen/pkg/types" ) func marshalDeepObject(in interface{}, path []string) ([]string, error) { var result []string switch t := in.(type) { case []interface{}: // For the array, we will use numerical subscripts of the form [x], // in the same order as the array. for i, iface := range t { newPath := append(path, strconv.Itoa(i)) fields, err := marshalDeepObject(iface, newPath) if err != nil { return nil, errors.Wrap(err, "error traversing array") } result = append(result, fields...) } case map[string]interface{}: // For a map, each key (field name) becomes a member of the path, and // we recurse. First, sort the keys. keys := make([]string, len(t)) i := 0 for k := range t { keys[i] = k i++ } sort.Strings(keys) // Now, for each key, we recursively marshal it. for _, k := range keys { newPath := append(path, k) fields, err := marshalDeepObject(t[k], newPath) if err != nil { return nil, errors.Wrap(err, "error traversing map") } result = append(result, fields...) } default: // Now, for a concrete value, we will turn the path elements // into a deepObject style set of subscripts. [a, b, c] turns into // [a][b][c] prefix := "[" + strings.Join(path, "][") + "]" result = []string{ prefix + fmt.Sprintf("=%v", t), } } return result, nil } func MarshalDeepObject(i interface{}, paramName string) (string, error) { // We're going to marshal to JSON and unmarshal into an interface{}, // which will use the json pkg to deal with all the field annotations. We // can then walk the generic object structure to produce a deepObject. This // isn't efficient and it would be more efficient to reflect on our own, // but it's complicated, error-prone code. buf, err := json.Marshal(i) if err != nil { return "", errors.Wrap(err, "failed to marshal input to JSON") } var i2 interface{} err = json.Unmarshal(buf, &i2) if err != nil { return "", errors.Wrap(err, "failed to unmarshal JSON") } fields, err := marshalDeepObject(i2, nil) if err != nil { return "", errors.Wrap(err, "error traversing JSON structure") } // Prefix the param name to each subscripted field. for i := range fields { fields[i] = paramName + fields[i] } return strings.Join(fields, "&"), nil } type fieldOrValue struct { fields map[string]fieldOrValue value string } func (f *fieldOrValue) appendPathValue(path []string, value string) { fieldName := path[0] if len(path) == 1
pv, found := f.fields[fieldName] if !found { pv = fieldOrValue{ fields: make(map[string]fieldOrValue), } f.fields[fieldName] = pv } pv.appendPathValue(path[1:], value) } func makeFieldOrValue(paths [][]string, values []string) fieldOrValue { f := fieldOrValue{ fields: make(map[string]fieldOrValue), } for i := range paths { path := paths[i] value := values[i] f.appendPathValue(path, value) } return f } func UnmarshalDeepObject(dst interface{}, paramName string, params url.Values) error { // Params are all the query args, so we need those that look like // "paramName["... var fieldNames []string var fieldValues []string searchStr := paramName + "[" for pName, pValues := range params { if strings.HasPrefix(pName, searchStr) { // trim the parameter name from the full name. pName = pName[len(paramName):] fieldNames = append(fieldNames, pName) if len(pValues) != 1 { return fmt.Errorf("%s has multiple values", pName) } fieldValues = append(fieldValues, pValues[0]) } } // Now, for each field, reconstruct its subscript path and value paths := make([][]string, len(fieldNames)) for i, path := range fieldNames { path = strings.TrimLeft(path, "[") path = strings.TrimRight(path, "]") paths[i] = strings.Split(path, "][") } fieldPaths := makeFieldOrValue(paths, fieldValues) err := assignPathValues(dst, fieldPaths) if err != nil { return errors.Wrap(err, "error assigning value to destination") } return nil } // This returns a field name, either using the variable name, or the json // annotation if that exists. func getFieldName(f reflect.StructField) string { n := f.Name tag, found := f.Tag.Lookup("json") if found { // If we have a json field, and the first part of it before the // first comma is non-empty, that's our field name. parts := strings.Split(tag, ",") if parts[0] != "" { n = parts[0] } } return n } // Create a map of field names that we'll see in the deepObject to reflect // field indices on the given type. func fieldIndicesByJsonTag(i interface{}) (map[string]int, error) { t := reflect.TypeOf(i) if t.Kind() != reflect.Struct { return nil, errors.New("expected a struct as input") } n := t.NumField() fieldMap := make(map[string]int) for i := 0; i < n; i++ { field := t.Field(i) fieldName := getFieldName(field) fieldMap[fieldName] = i } return fieldMap, nil } func assignPathValues(dst interface{}, pathValues fieldOrValue) error { //t := reflect.TypeOf(dst) v := reflect.ValueOf(dst) iv := reflect.Indirect(v) it := iv.Type() switch it.Kind() { case reflect.Slice: sliceLength := len(pathValues.fields) dstSlice := reflect.MakeSlice(it, sliceLength, sliceLength) err := assignSlice(dstSlice, pathValues) if err != nil { return errors.Wrap(err, "error assigning slice") } iv.Set(dstSlice) return nil case reflect.Struct: // Some special types we care about are structs. Handle them // here. They may be redefined, so we need to do some hoop // jumping. If the types are aliased, we need to type convert // the pointer, then set the value of the dereference pointer. // We check to see if the object implements the Binder interface first. if dst, isBinder := v.Interface().(Binder); isBinder { return dst.Bind(pathValues.value) } // Then check the legacy types if it.ConvertibleTo(reflect.TypeOf(types.Date{})) { var date types.Date var err error date.Time, err = time.Parse(types.DateFormat, pathValues.value) if err != nil { return errors.Wrap(err, "invalid date format") } dst := iv if it != reflect.TypeOf(types.Date{}) { // Types are aliased, convert the pointers. ivPtr := iv.Addr() aPtr := ivPtr.Convert(reflect.TypeOf(&types.Date{})) dst = reflect.Indirect(aPtr) } dst.Set(reflect.ValueOf(date)) } if it.ConvertibleTo(reflect.TypeOf(time.Time{})) { var tm time.Time var err error tm, err = time.Parse(time.RFC3339Nano, pathValues.value) if err != nil { // Fall back to parsing it as a date. tm, err = time.Parse(types.DateFormat, pathValues.value) if err != nil { return fmt.Errorf("error parsing tim as RFC3339 or 2006-01-02 time: %s", err) } return errors.Wrap(err, "invalid date format") } dst := iv if it != reflect.TypeOf(time.Time{}) { // Types are aliased, convert the pointers. ivPtr := iv.Addr() aPtr := ivPtr.Convert(reflect.TypeOf(&time.Time{})) dst = reflect.Indirect(aPtr) } dst.Set(reflect.ValueOf(tm)) } fieldMap, err := fieldIndicesByJsonTag(iv.Interface()) if err != nil { return errors.Wrap(err, "failed enumerating fields") } for _, fieldName := range sortedFieldOrValueKeys(pathValues.fields) { fieldValue := pathValues.fields[fieldName] fieldIndex, found := fieldMap[fieldName] if !found { return fmt.Errorf("field [%s] is not present in destination object", fieldName) }
{ f.fields[fieldName] = fieldOrValue{value: value} return }
conditional_block
deepobject.go
/oapi-codegen/pkg/types" ) func marshalDeepObject(in interface{}, path []string) ([]string, error) { var result []string switch t := in.(type) { case []interface{}: // For the array, we will use numerical subscripts of the form [x], // in the same order as the array. for i, iface := range t { newPath := append(path, strconv.Itoa(i)) fields, err := marshalDeepObject(iface, newPath) if err != nil { return nil, errors.Wrap(err, "error traversing array") } result = append(result, fields...) } case map[string]interface{}: // For a map, each key (field name) becomes a member of the path, and // we recurse. First, sort the keys. keys := make([]string, len(t)) i := 0 for k := range t { keys[i] = k i++ } sort.Strings(keys) // Now, for each key, we recursively marshal it. for _, k := range keys { newPath := append(path, k) fields, err := marshalDeepObject(t[k], newPath) if err != nil { return nil, errors.Wrap(err, "error traversing map") } result = append(result, fields...) } default: // Now, for a concrete value, we will turn the path elements // into a deepObject style set of subscripts. [a, b, c] turns into // [a][b][c] prefix := "[" + strings.Join(path, "][") + "]" result = []string{ prefix + fmt.Sprintf("=%v", t), } } return result, nil } func MarshalDeepObject(i interface{}, paramName string) (string, error) { // We're going to marshal to JSON and unmarshal into an interface{}, // which will use the json pkg to deal with all the field annotations. We // can then walk the generic object structure to produce a deepObject. This // isn't efficient and it would be more efficient to reflect on our own, // but it's complicated, error-prone code. buf, err := json.Marshal(i) if err != nil { return "", errors.Wrap(err, "failed to marshal input to JSON") } var i2 interface{} err = json.Unmarshal(buf, &i2) if err != nil { return "", errors.Wrap(err, "failed to unmarshal JSON") } fields, err := marshalDeepObject(i2, nil) if err != nil { return "", errors.Wrap(err, "error traversing JSON structure") } // Prefix the param name to each subscripted field. for i := range fields { fields[i] = paramName + fields[i] } return strings.Join(fields, "&"), nil
fields map[string]fieldOrValue value string } func (f *fieldOrValue) appendPathValue(path []string, value string) { fieldName := path[0] if len(path) == 1 { f.fields[fieldName] = fieldOrValue{value: value} return } pv, found := f.fields[fieldName] if !found { pv = fieldOrValue{ fields: make(map[string]fieldOrValue), } f.fields[fieldName] = pv } pv.appendPathValue(path[1:], value) } func makeFieldOrValue(paths [][]string, values []string) fieldOrValue { f := fieldOrValue{ fields: make(map[string]fieldOrValue), } for i := range paths { path := paths[i] value := values[i] f.appendPathValue(path, value) } return f } func UnmarshalDeepObject(dst interface{}, paramName string, params url.Values) error { // Params are all the query args, so we need those that look like // "paramName["... var fieldNames []string var fieldValues []string searchStr := paramName + "[" for pName, pValues := range params { if strings.HasPrefix(pName, searchStr) { // trim the parameter name from the full name. pName = pName[len(paramName):] fieldNames = append(fieldNames, pName) if len(pValues) != 1 { return fmt.Errorf("%s has multiple values", pName) } fieldValues = append(fieldValues, pValues[0]) } } // Now, for each field, reconstruct its subscript path and value paths := make([][]string, len(fieldNames)) for i, path := range fieldNames { path = strings.TrimLeft(path, "[") path = strings.TrimRight(path, "]") paths[i] = strings.Split(path, "][") } fieldPaths := makeFieldOrValue(paths, fieldValues) err := assignPathValues(dst, fieldPaths) if err != nil { return errors.Wrap(err, "error assigning value to destination") } return nil } // This returns a field name, either using the variable name, or the json // annotation if that exists. func getFieldName(f reflect.StructField) string { n := f.Name tag, found := f.Tag.Lookup("json") if found { // If we have a json field, and the first part of it before the // first comma is non-empty, that's our field name. parts := strings.Split(tag, ",") if parts[0] != "" { n = parts[0] } } return n } // Create a map of field names that we'll see in the deepObject to reflect // field indices on the given type. func fieldIndicesByJsonTag(i interface{}) (map[string]int, error) { t := reflect.TypeOf(i) if t.Kind() != reflect.Struct { return nil, errors.New("expected a struct as input") } n := t.NumField() fieldMap := make(map[string]int) for i := 0; i < n; i++ { field := t.Field(i) fieldName := getFieldName(field) fieldMap[fieldName] = i } return fieldMap, nil } func assignPathValues(dst interface{}, pathValues fieldOrValue) error { //t := reflect.TypeOf(dst) v := reflect.ValueOf(dst) iv := reflect.Indirect(v) it := iv.Type() switch it.Kind() { case reflect.Slice: sliceLength := len(pathValues.fields) dstSlice := reflect.MakeSlice(it, sliceLength, sliceLength) err := assignSlice(dstSlice, pathValues) if err != nil { return errors.Wrap(err, "error assigning slice") } iv.Set(dstSlice) return nil case reflect.Struct: // Some special types we care about are structs. Handle them // here. They may be redefined, so we need to do some hoop // jumping. If the types are aliased, we need to type convert // the pointer, then set the value of the dereference pointer. // We check to see if the object implements the Binder interface first. if dst, isBinder := v.Interface().(Binder); isBinder { return dst.Bind(pathValues.value) } // Then check the legacy types if it.ConvertibleTo(reflect.TypeOf(types.Date{})) { var date types.Date var err error date.Time, err = time.Parse(types.DateFormat, pathValues.value) if err != nil { return errors.Wrap(err, "invalid date format") } dst := iv if it != reflect.TypeOf(types.Date{}) { // Types are aliased, convert the pointers. ivPtr := iv.Addr() aPtr := ivPtr.Convert(reflect.TypeOf(&types.Date{})) dst = reflect.Indirect(aPtr) } dst.Set(reflect.ValueOf(date)) } if it.ConvertibleTo(reflect.TypeOf(time.Time{})) { var tm time.Time var err error tm, err = time.Parse(time.RFC3339Nano, pathValues.value) if err != nil { // Fall back to parsing it as a date. tm, err = time.Parse(types.DateFormat, pathValues.value) if err != nil { return fmt.Errorf("error parsing tim as RFC3339 or 2006-01-02 time: %s", err) } return errors.Wrap(err, "invalid date format") } dst := iv if it != reflect.TypeOf(time.Time{}) { // Types are aliased, convert the pointers. ivPtr := iv.Addr() aPtr := ivPtr.Convert(reflect.TypeOf(&time.Time{})) dst = reflect.Indirect(aPtr) } dst.Set(reflect.ValueOf(tm)) } fieldMap, err := fieldIndicesByJsonTag(iv.Interface()) if err != nil { return errors.Wrap(err, "failed enumerating fields") } for _, fieldName := range sortedFieldOrValueKeys(pathValues.fields) { fieldValue := pathValues.fields[fieldName] fieldIndex, found := fieldMap[fieldName] if !found { return fmt.Errorf("field [%s] is not present in destination object", fieldName) } field
} type fieldOrValue struct {
random_line_split
deepobject.go
api-codegen/pkg/types" ) func marshalDeepObject(in interface{}, path []string) ([]string, error) { var result []string switch t := in.(type) { case []interface{}: // For the array, we will use numerical subscripts of the form [x], // in the same order as the array. for i, iface := range t { newPath := append(path, strconv.Itoa(i)) fields, err := marshalDeepObject(iface, newPath) if err != nil { return nil, errors.Wrap(err, "error traversing array") } result = append(result, fields...) } case map[string]interface{}: // For a map, each key (field name) becomes a member of the path, and // we recurse. First, sort the keys. keys := make([]string, len(t)) i := 0 for k := range t { keys[i] = k i++ } sort.Strings(keys) // Now, for each key, we recursively marshal it. for _, k := range keys { newPath := append(path, k) fields, err := marshalDeepObject(t[k], newPath) if err != nil { return nil, errors.Wrap(err, "error traversing map") } result = append(result, fields...) } default: // Now, for a concrete value, we will turn the path elements // into a deepObject style set of subscripts. [a, b, c] turns into // [a][b][c] prefix := "[" + strings.Join(path, "][") + "]" result = []string{ prefix + fmt.Sprintf("=%v", t), } } return result, nil } func MarshalDeepObject(i interface{}, paramName string) (string, error) { // We're going to marshal to JSON and unmarshal into an interface{}, // which will use the json pkg to deal with all the field annotations. We // can then walk the generic object structure to produce a deepObject. This // isn't efficient and it would be more efficient to reflect on our own, // but it's complicated, error-prone code. buf, err := json.Marshal(i) if err != nil { return "", errors.Wrap(err, "failed to marshal input to JSON") } var i2 interface{} err = json.Unmarshal(buf, &i2) if err != nil { return "", errors.Wrap(err, "failed to unmarshal JSON") } fields, err := marshalDeepObject(i2, nil) if err != nil { return "", errors.Wrap(err, "error traversing JSON structure") } // Prefix the param name to each subscripted field. for i := range fields { fields[i] = paramName + fields[i] } return strings.Join(fields, "&"), nil } type fieldOrValue struct { fields map[string]fieldOrValue value string } func (f *fieldOrValue) appendPathValue(path []string, value string)
func makeFieldOrValue(paths [][]string, values []string) fieldOrValue { f := fieldOrValue{ fields: make(map[string]fieldOrValue), } for i := range paths { path := paths[i] value := values[i] f.appendPathValue(path, value) } return f } func UnmarshalDeepObject(dst interface{}, paramName string, params url.Values) error { // Params are all the query args, so we need those that look like // "paramName["... var fieldNames []string var fieldValues []string searchStr := paramName + "[" for pName, pValues := range params { if strings.HasPrefix(pName, searchStr) { // trim the parameter name from the full name. pName = pName[len(paramName):] fieldNames = append(fieldNames, pName) if len(pValues) != 1 { return fmt.Errorf("%s has multiple values", pName) } fieldValues = append(fieldValues, pValues[0]) } } // Now, for each field, reconstruct its subscript path and value paths := make([][]string, len(fieldNames)) for i, path := range fieldNames { path = strings.TrimLeft(path, "[") path = strings.TrimRight(path, "]") paths[i] = strings.Split(path, "][") } fieldPaths := makeFieldOrValue(paths, fieldValues) err := assignPathValues(dst, fieldPaths) if err != nil { return errors.Wrap(err, "error assigning value to destination") } return nil } // This returns a field name, either using the variable name, or the json // annotation if that exists. func getFieldName(f reflect.StructField) string { n := f.Name tag, found := f.Tag.Lookup("json") if found { // If we have a json field, and the first part of it before the // first comma is non-empty, that's our field name. parts := strings.Split(tag, ",") if parts[0] != "" { n = parts[0] } } return n } // Create a map of field names that we'll see in the deepObject to reflect // field indices on the given type. func fieldIndicesByJsonTag(i interface{}) (map[string]int, error) { t := reflect.TypeOf(i) if t.Kind() != reflect.Struct { return nil, errors.New("expected a struct as input") } n := t.NumField() fieldMap := make(map[string]int) for i := 0; i < n; i++ { field := t.Field(i) fieldName := getFieldName(field) fieldMap[fieldName] = i } return fieldMap, nil } func assignPathValues(dst interface{}, pathValues fieldOrValue) error { //t := reflect.TypeOf(dst) v := reflect.ValueOf(dst) iv := reflect.Indirect(v) it := iv.Type() switch it.Kind() { case reflect.Slice: sliceLength := len(pathValues.fields) dstSlice := reflect.MakeSlice(it, sliceLength, sliceLength) err := assignSlice(dstSlice, pathValues) if err != nil { return errors.Wrap(err, "error assigning slice") } iv.Set(dstSlice) return nil case reflect.Struct: // Some special types we care about are structs. Handle them // here. They may be redefined, so we need to do some hoop // jumping. If the types are aliased, we need to type convert // the pointer, then set the value of the dereference pointer. // We check to see if the object implements the Binder interface first. if dst, isBinder := v.Interface().(Binder); isBinder { return dst.Bind(pathValues.value) } // Then check the legacy types if it.ConvertibleTo(reflect.TypeOf(types.Date{})) { var date types.Date var err error date.Time, err = time.Parse(types.DateFormat, pathValues.value) if err != nil { return errors.Wrap(err, "invalid date format") } dst := iv if it != reflect.TypeOf(types.Date{}) { // Types are aliased, convert the pointers. ivPtr := iv.Addr() aPtr := ivPtr.Convert(reflect.TypeOf(&types.Date{})) dst = reflect.Indirect(aPtr) } dst.Set(reflect.ValueOf(date)) } if it.ConvertibleTo(reflect.TypeOf(time.Time{})) { var tm time.Time var err error tm, err = time.Parse(time.RFC3339Nano, pathValues.value) if err != nil { // Fall back to parsing it as a date. tm, err = time.Parse(types.DateFormat, pathValues.value) if err != nil { return fmt.Errorf("error parsing tim as RFC3339 or 2006-01-02 time: %s", err) } return errors.Wrap(err, "invalid date format") } dst := iv if it != reflect.TypeOf(time.Time{}) { // Types are aliased, convert the pointers. ivPtr := iv.Addr() aPtr := ivPtr.Convert(reflect.TypeOf(&time.Time{})) dst = reflect.Indirect(aPtr) } dst.Set(reflect.ValueOf(tm)) } fieldMap, err := fieldIndicesByJsonTag(iv.Interface()) if err != nil { return errors.Wrap(err, "failed enumerating fields") } for _, fieldName := range sortedFieldOrValueKeys(pathValues.fields) { fieldValue := pathValues.fields[fieldName] fieldIndex, found := fieldMap[fieldName] if !found { return fmt.Errorf("field [%s] is not present in destination object", fieldName) }
{ fieldName := path[0] if len(path) == 1 { f.fields[fieldName] = fieldOrValue{value: value} return } pv, found := f.fields[fieldName] if !found { pv = fieldOrValue{ fields: make(map[string]fieldOrValue), } f.fields[fieldName] = pv } pv.appendPathValue(path[1:], value) }
identifier_body
server.go
if tc, ok := conn.(*net.TCPConn); ok { // tcp请求需要设置keepAlive保证链接的稳定性能 tc.SetKeepAlive(true) tc.SetKeepAlivePeriod(time.Minute * 5) // 5分钟没有响应报错 tc.SetLinger(10) // 关闭连接的行为 设置数据在断开时候也能在后台发送 } conn, ok := s.Plugins.DoPostConnAccept(conn) if !ok { // 不允许链接则关闭(可能是限流没通过,验证没通过,业务方面的自己用插件扩展...) s.closeChannel(conn) } s.connMu.Lock() s.activeConn[conn] = struct{}{} s.connMu.Unlock() go s.serveConn(conn) } } // 开始处理消息 func (s *Server) serveConn(conn net.Conn) { // 单个conn协程中没有权限影响主进程panic,所有panic会这一层处理 defer func() { if err := recover(); err != nil { // 发生panic buf := make([]byte, 65536) size := runtime.Stack(buf, false) if size > 65536 { size = 65536 } buf = buf[:size] log.ErrorF("conn 发生panic,原因%s, 客户端地址%s, 堆栈信息 %s", err, conn.RemoteAddr(), buf) } s.connMu.Lock() delete(s.activeConn, conn) s.connMu.Unlock() s.Plugins.DoPostConnClose(conn) }() // 判断此时服务是否已经关闭 if s.isShutdown() { s.closeChannel(conn) return } now := time.Now() // tls连接需要先握手 if tlsL, ok := conn.(*tls.Conn); ok { if s.readTimeout != 0 { tlsL.SetReadDeadline(now.Add(s.readTimeout)) } if s.writeTimeout != 0 { tlsL.SetWriteDeadline(now.Add(s.writeTimeout)) } if err := tlsL.Handshake(); err != nil { log.ErrorF("tls尝试握手失败,原因:%s,addr:", err, tlsL.RemoteAddr()) return } } // 初始化读取缓冲区 rBuff := bufio.NewReaderSize(conn, ReadBuffSize) for { // 判断此时服务是否已经关闭 if s.isShutdown() { s.closeChannel(conn) return } if s.readTimeout != 0 { // 设置读取的超时时间 conn.SetReadDeadline(now.Add(s.readTimeout)) } ctx := share.WithValue(context.Background(), RemoteConnContextKey, conn) request, err := s.readRequest(ctx, rBuff) if err != nil { if err == io.EOF { log.InfoF("客户端已经关闭链接c%s", conn.RemoteAddr()) } else if strings.Contains(err.Error(), "use of closed network connection") { log.InfoF("连接已经被关闭%s", conn.RemoteAddr()) } else { log.WarnF("rpc 读取数据失败,错误原因%v", err) } return } // 要开始写入了 if s.writeTimeout != 0 { conn.SetWriteDeadline(now.Add(s.writeTimeout)) } // 将开始时间写上下文中 ctx = share.WithLocalValue(ctx, StartRequestContextKey, time.Now().UnixNano()) if !request.IsHeartbeat() { // auth鉴权 err := s.auth(ctx, request) if err != nil { // 鉴权失败 if !request.IsOneway() { // 需要回复客户端鉴权失败 response := request.Clone() // 复制一个请求出来 response.SetMessageType(protocol.Response) // 设置为response消息 handleError(response, err) data := response.EncodeSlicePointer() _, err = conn.Write(*data) protocol.PutData(data) s.Plugins.DoPostWriteResponse(ctx, request, response, err) protocol.FreeMsg(response) } else { // 不需要回复 s.Plugins.DoPreWriteResponse(ctx, request, nil) } protocol.FreeMsg(request) log.InfoF("连接鉴权失败,%s,错误原因%v", conn.RemoteAddr(), err) return } } // 下面需要处理消息了噢 go func() { // 正在处理的消息数量+1 atomic.AddInt32(&s.handlerMsgNum, 1) // 正在处理消息的数量-1 defer atomic.AddInt32(&s.handlerMsgNum, - 1) if request.IsHeartbeat() { // 如果是客户端心跳 request.SetMessageType(protocol.Response) data := request.EncodeSlicePointer() conn.Write(*data) protocol.PutData(data) return } // 不是心跳初始化返给客户端的meta responseMetadata := make(map[string]string) // 先将服务端的metadata方法进去 ctx = share.WithLocalValue(ctx, share.ReqMetaDataKey, request.Metadata) // 再将客户端的metadata放进去 ctx = share.WithLocalValue(ctx, share.ResMetaDataKey, responseMetadata) s.Plugins.DoPreHandleRequest(ctx, request) // 开始处理请求了 response, err := s.handleRequest(ctx, request) if err != nil { log.WarnF("处理请求错误: %v", err) } s.Plugins.DoPreWriteResponse(ctx, request, response) if !request.IsOneway() { // 需要回复客户端 // 从ctx中拿出meta信息 responseMetadataCtx := ctx.Value(share.ResMetaDataKey).(map[string]string) if len(responseMetadata) > 0 { meta := response.Metadata if meta == nil { meta = responseMetadataCtx } else { for k, v := range responseMetadataCtx { if meta[k] == "" { meta[k] = v } } } } if len(response.Payload) > 1024 && request.CompressType() != protocol.None { response.SetCompressType(request.CompressType()) } data := response.EncodeSlicePointer() conn.Write(*data) protocol.PutData(data) } s.Plugins.DoPostWriteResponse(ctx, request, response, err) protocol.FreeMsg(response) protocol.FreeMsg(request) }() } } // 处理单个请求 func (s *Server) handleRequest(ctx context.Context, request *protocol.Message) (*protocol.Message, error) { var err error serviceName := request.ServicePath methodName := request.ServiceMethod response := request.Clone() response.SetMessageType(protocol.Response) s.serviceMapMu.RLock() service, ok := s.serviceMap[serviceName] s.serviceMapMu.RUnlock() if !ok { // 都没注册直接返回错误 err = errors.New(fmt.Sprintf("不能找到服务发现者为%s的服务", serviceName)) return handleError(response, err) } methodType, ok := service.method[methodName] if !ok { // 看看是否注册了函数的调用 if _, ok := service.function[methodName]; ok { protocol.FreeMsg(response) // 这里创建对象要回收的 return s.handleRequestForFunction(ctx, request) } err = errors.New(fmt.Sprintf("不能找到服务提供者%s下方法名为%s的方法", serviceName, methodName)) return handleError(response, err) } requestType := ObjectPool.Get(methodType.requestType) defer ObjectPool.Put(methodType.requestType, requestType) codec := share.Codecs[request.SerializeType()] if codec == nil { err = fmt.Errorf("不能找到对应的的序列化方式:%T", request.SerializeType()) return handleError(response, err) } err = codec.Decode(request.Payload, requestType) if err != nil { return handleError(response, err) } responseType := ObjectPool.Get(methodType.responseType) defer ObjectPool.Put(methodType.responseType, responseType) requestType, err = s.Plugins.DoPreCall(ctx, serviceName, methodName, requestType) if err != nil { return handleError(response, err) } if methodType.requestType.Kind() != reflect.Ptr { // 不是指针 err = service.call(ctx, methodType, reflect.ValueOf(requestType).Elem(), reflect.ValueOf(responseType)) } else { err = service.call(ctx, methodType, reflect.ValueOf(requestType), reflect.ValueOf(responseType)) } if err != nil { return handleError(response, err) } responseType, err = s.Plugins.DoPostCall(ctx, serviceName, methodName, requestType, responseType) if err != nil { return handleError(response, err) } if !request.IsOneway() { data, err := codec.Encode(responseType) if err != nil { return handleError(response, err) } response.Payload = data } return response, nil } // 处理函数类型
的 func (s *Server) handleRequestForFunction(ctx context.Context, request *protocol.Message) (*protocol.Message, error) {
conditional_block
server.go
, false) if size > 65536 { size = 65536 } buf = buf[:size] log.ErrorF("conn 发生panic,原因%s, 客户端地址%s, 堆栈信息 %s", err, conn.RemoteAddr(), buf) } s.connMu.Lock() delete(s.activeConn, conn) s.connMu.Unlock() s.Plugins.DoPostConnClose(conn) }() // 判断此时服务是否已经关闭 if s.isShutdown() { s.closeChannel(conn) return } now := time.Now() // tls连接需要先握手 if tlsL, ok := conn.(*tls.Conn); ok { if s.readTimeout != 0 { tlsL.SetReadDeadline(now.Add(s.readTimeout)) } if s.writeTimeout != 0 { tlsL.SetWriteDeadline(now.Add(s.writeTimeout)) } if err := tlsL.Handshake(); err != nil { log.ErrorF("tls尝试握手失败,原因:%s,addr:", err, tlsL.RemoteAddr()) return } } // 初始化读取缓冲区 rBuff := bufio.NewReaderSize(conn, ReadBuffSize) for { // 判断此时服务是否已经关闭 if s.isShutdown() { s.closeChannel(conn) return } if s.readTimeout != 0 { // 设置读取的超时时间 conn.SetReadDeadline(now.Add(s.readTimeout)) } ctx := share.WithValue(context.Background(), RemoteConnContextKey, conn) request, err := s.readRequest(ctx, rBuff) if err != nil { if err == io.EOF { log.InfoF("客户端已经关闭链接c%s", conn.RemoteAddr()) } else if strings.Contains(err.Error(), "use of closed network connection") { log.InfoF("连接已经被关闭%s", conn.RemoteAddr()) } else { log.WarnF("rpc 读取数据失败,错误原因%v", err) } return } // 要开始写入了 if s.writeTimeout != 0 { conn.SetWriteDeadline(now.Add(s.writeTimeout)) } // 将开始时间写上下文中 ctx = share.WithLocalValue(ctx, StartRequestContextKey, time.Now().UnixNano()) if !request.IsHeartbeat() { // auth鉴权 err := s.auth(ctx, request) if err != nil { // 鉴权失败 if !request.IsOneway() { // 需要回复客户端鉴权失败 response := request.Clone() // 复制一个请求出来 response.SetMessageType(protocol.Response) // 设置为response消息 handleError(response, err) data := response.EncodeSlicePointer() _, err = conn.Write(*data) protocol.PutData(data) s.Plugins.DoPostWriteResponse(ctx, request, response, err) protocol.FreeMsg(response) } else { // 不需要回复 s.Plugins.DoPreWriteResponse(ctx, request, nil) } protocol.FreeMsg(request) log.InfoF("连接鉴权失败,%s,错误原因%v", conn.RemoteAddr(), err) return } } // 下面需要处理消息了噢 go func() { // 正在处理的消息数量+1 atomic.AddInt32(&s.handlerMsgNum, 1) // 正在处理消息的数量-1 defer atomic.AddInt32(&s.handlerMsgNum, - 1) if request.IsHeartbeat() { // 如果是客户端心跳 request.SetMessageType(protocol.Response) data := request.EncodeSlicePointer() conn.Write(*data) protocol.PutData(data) return } // 不是心跳初始化返给客户端的meta responseMetadata := make(map[string]string) // 先将服务端的metadata方法进去 ctx = share.WithLocalValue(ctx, share.ReqMetaDataKey, request.Metadata) // 再将客户端的metadata放进去 ctx = share.WithLocalValue(ctx, share.ResMetaDataKey, responseMetadata) s.Plugins.DoPreHandleRequest(ctx, request) // 开始处理请求了 response, err := s.handleRequest(ctx, request) if err != nil { log.WarnF("处理请求错误: %v", err) } s.Plugins.DoPreWriteResponse(ctx, request, response) if !request.IsOneway() { // 需要回复客户端 // 从ctx中拿出meta信息 responseMetadataCtx := ctx.Value(share.ResMetaDataKey).(map[string]string) if len(responseMetadata) > 0 { meta := response.Metadata if meta == nil { meta = responseMetadataCtx } else { for k, v := range responseMetadataCtx { if meta[k] == "" { meta[k] = v } } } } if len(response.Payload) > 1024 && request.CompressType() != protocol.None { response.SetCompressType(request.CompressType()) } data := response.EncodeSlicePointer() conn.Write(*data) protocol.PutData(data) } s.Plugins.DoPostWriteResponse(ctx, request, response, err) protocol.FreeMsg(response) protocol.FreeMsg(request) }() } } // 处理单个请求 func (s *Server) handleRequest(ctx context.Context, request *protocol.Message) (*protocol.Message, error) { var err error serviceName := request.ServicePath methodName := request.ServiceMethod response := request.Clone() response.SetMessageType(protocol.Response) s.serviceMapMu.RLock() service, ok := s.serviceMap[serviceName] s.serviceMapMu.RUnlock() if !ok { // 都没注册直接返回错误 err = errors.New(fmt.Sprintf("不能找到服务发现者为%s的服务", serviceName)) return handleError(response, err) } methodType, ok := service.method[methodName] if !ok { // 看看是否注册了函数的调用 if _, ok := service.function[methodName]; ok { protocol.FreeMsg(response) // 这里创建对象要回收的 return s.handleRequestForFunction(ctx, request) } err = errors.New(fmt.Sprintf("不能找到服务提供者%s下方法名为%s的方法", serviceName, methodName)) return handleError(response, err) } requestType := ObjectPool.Get(methodType.requestType) defer ObjectPool.Put(methodType.requestType, requestType) codec := share.Codecs[request.SerializeType()] if codec == nil { err = fmt.Errorf("不能找到对应的的序列化方式:%T", request.SerializeType()) return handleError(response, err) } err = codec.Decode(request.Payload, requestType) if err != nil { return handleError(response, err) } responseType := ObjectPool.Get(methodType.responseType) defer ObjectPool.Put(methodType.responseType, responseType) requestType, err = s.Plugins.DoPreCall(ctx, serviceName, methodName, requestType) if err != nil { return handleError(response, err) } if methodType.requestType.Kind() != reflect.Ptr { // 不是指针 err = service.call(ctx, methodType, reflect.ValueOf(requestType).Elem(), reflect.ValueOf(responseType)) } else { err = service.call(ctx, methodType, reflect.ValueOf(requestType), reflect.ValueOf(responseType)) } if err != nil { return handleError(response, err) } responseType, err = s.Plugins.DoPostCall(ctx, serviceName, methodName, requestType, responseType) if err != nil { return handleError(response, err) } if !request.IsOneway() { data, err := codec.Encode(responseType) if err != nil { return handleError(response, err) } response.Payload = data } return response, nil } // 处理函数类型的 func (s *Server) handleRequestForFunction(ctx context.Context, request *protocol.Message) (*protocol.Message, error) { var err error response := request.Clone() response.SetMessageType(protocol.Response) serviceName := request.ServicePath methodName := request.ServiceMethod s.serviceMapMu.RLock() service, ok := s.serviceMap[serviceName] s.serviceMapMu.RUnlock() if !ok { // 都没注册直接返回错误 err = errors.New(fmt.Sprintf("不能找到服务发现者为%s的服务", serviceName)) return handleError(response, err) } funcType := service.function[serviceName] if funcType == nil { err = errors.New(fmt.Sprintf("不能找到服务发现者为%s对应的函数调用%s", serviceName, methodName)) return handleError(response, err) } requestType := ObjectPool.Get(funcType.requestType) defer ObjectPool.Put(funcType.requestType, requestType) codec := share.Codecs[request.SerializeType()] if codec == nil { err = fmt.Errorf("不能找到对应的的序列化方式:%T", request.SerializeType()) return handleError(response, err) } err = codec.Decode(request.Payload, requestType) if err != nil { return handleError(response, err) } responseType := ObjectPool.Get(funcType.responseType)
defer ObjectPool.Put(funcType.responseType, responseType)
random_line_split
server.go
n处理 func (s *Server) serveListener(ln net.Listener) error { // 定义临时错误的延迟时间 var tempDelay time.Duration s.connMu.Lock() s.ln = ln s.connMu.Unlock() for { conn, err := ln.Accept() if err != nil { select { case <-s.doneChan: return ErrServerClosed default: } // 如果错误断言为网络错误,且是一个临时的(比如当时网络环境差,dns服务器不稳定引起的),稍后可能会自动恢复的 // 不能直接返回错误,应该等待一段时间才返回错误 // 等待的时间为上一次的两倍最大等待1s // 参考官方http包实现的 if ne, ok := conn.(net.Error); ok && ne.Temporary() { if tempDelay == 0 { tempDelay = 5 * time.Millisecond } else { tempDelay = tempDelay * 2 } if tempDelay > time.Second { // 大于1秒直接返回错误 return err } time.Sleep(tempDelay) log.ErrorF("rpc服务接受conn异常,正在重试, 原因%v, sleep %d", err, tempDelay) continue } if strings.Contains(err.Error(), "listener closed") { // 服务关闭 return ErrServerClosed } return err } // 成功请求延迟时间置为0 tempDelay = 0 if tc, ok := conn.(*net.TCPConn); ok { // tcp请求需要设置keepAlive保证链接的稳定性能 tc.SetKeepAlive(true) tc.SetKeepAlivePeriod(time.Minute * 5) // 5分钟没有响应报错 tc.SetLinger(10) // 关闭连接的行为 设置数据在断开时候也能在后台发送 } conn, ok := s.Plugins.DoPostConnAccept(conn) if !ok { // 不允许链接则关闭(可能是限流没通过,验证没通过,业务方面的自己用插件扩展...) s.closeChannel(conn) } s.connMu.Lock() s.activeConn[conn] = struct{}{} s.connMu.Unlock() go s.serveConn(conn) } } // 开始处理消息 func (s *Server) serveConn(conn net.Conn) { // 单个conn协程中没有权限影响主进程panic,所有panic会这一层处理 defer func() { if err := recover(); err != nil { // 发生panic buf := make([]byte, 65536) size := runtime.Stack(buf, false) if size > 65536 { size = 65536 } buf = buf[:size] log.ErrorF("conn 发生panic,原因%s, 客户端地址%s, 堆栈信息 %s", err, conn.RemoteAddr(), buf) } s.connMu.Lock() delete(s.activeConn, conn) s.connMu.Unlock() s.Plugins.DoPostConnClose(conn) }() // 判断此时服务是否已经关闭 if s.isShutdown() { s.closeChannel(conn) return } now := time.Now() // tls连接需要先握手 if tlsL, ok := conn.(*tls.Conn); ok { if s.readTimeout != 0 { tlsL.SetReadDeadline(now.Add(s.readTimeout)) } if s.writeTimeout != 0 { tlsL.SetWriteDeadline(now.Add(s.writeTimeout)) } if err := tlsL.Handshake(); err != nil { log.ErrorF("tls尝试握手失败,原因:%s,addr:", err, tlsL.RemoteAddr()) return } } // 初始化读取缓冲区 rBuff := bufio.NewReaderSize(conn, ReadBuffSize) for { // 判断此时服务是否已经关闭 if s.isShutdown() { s.closeChannel(conn) return } if s.readTimeout != 0 { // 设置读取的超时时间 conn.SetReadDeadline(now.Add(s.readTimeout)) } ctx := share.WithValue(context.Background(), RemoteConnContextKey, conn) request, err := s.readRequest(ctx, rBuff) if err != nil { if err == io.EOF { log.InfoF("客户端已经关闭链接c%s", conn.RemoteAddr()) } else if strings.Contains(err.Error(), "use of closed network connection") { log.InfoF("连接已经被关闭%s", conn.RemoteAddr()) } else { log.WarnF("rpc 读取数据失败,错误原因%v", err) } return } // 要开始写入了 if s.writeTimeout != 0 { conn.SetWriteDeadline(now.Add(s.writeTimeout)) } // 将开始时间写上下文中 ctx = share.WithLocalValue(ctx, StartRequestContextKey, time.Now().UnixNano()) if !request.IsHeartbeat() { // auth鉴权 err := s.auth(ctx, request) if err != nil { // 鉴权失败 if !request.IsOneway() { // 需要回复客户端鉴权失败 response := request.Clone() // 复制一个请求出来 response.SetMessageType(protocol.Response) // 设置为response消息 handleError(response, err) data := response.EncodeSlicePointer() _, err = conn.Write(*data) protocol.PutData(data) s.Plugins.DoPostWriteResponse(ctx, request, response, err) protocol.FreeMsg(response) } else { // 不需要回复 s.Plugins.DoPreWriteResponse(ctx, request, nil) } protocol.FreeMsg(request) log.InfoF("连接鉴权失败,%s,错误原因%v", conn.RemoteAddr(), err) return } } // 下面需要处理消息了噢 go func() { // 正在处理的消息数量+1 atomic.AddInt32(&s.handlerMsgNum, 1) // 正在处理消息的数量-1 defer atomic.AddInt32(&s.handlerMsgNum, - 1) if request.IsHeartbeat() { // 如果是客户端心跳 request.SetMessageType(protocol.Response) data := request.EncodeSlicePointer() conn.Write(*data) protocol.PutData(data) return } // 不是心跳初始化返给客户端的meta responseMetadata := make(map[string]string) // 先将服务端的metadata方法进去 ctx = share.WithLocalValue(ctx, share.ReqMetaDataKey, request.Metadata) // 再将客户端的metadata放进去 ctx = share.WithLocalValue(ctx, share.ResMetaDataKey, responseMetadata) s.Plugins.DoPreHandleRequest(ctx, request) // 开始处理请求了 response, err := s.handleRequest(ctx, request) if err != nil { log.WarnF("处理请求错误: %v", err) } s.Plugins.DoPreWriteResponse(ctx, request, response) if !request.IsOneway() { // 需要回复客户端 // 从ctx中拿出meta信息 responseMetadataCtx := ctx.Value(share.ResMetaDataKey).(map[string]string) if len(responseMetadata) > 0 { meta := response.Metadata if meta == nil { meta = responseMetadataCtx } else { for k, v := range responseMetadataCtx { if meta[k] == "" { meta[k] = v } } } } if len(response.Payload) > 1024 && request.CompressType() != protocol.None { response.SetCompressType(request.CompressType()) } data := response.EncodeSlicePointer() conn.Write(*data) protocol.PutData(data) } s.Plugins.DoPostWriteResponse(ctx, request, response, err) protocol.FreeMsg(response) protocol.FreeMsg(request) }() } } // 处理单个请求 func (s *Server) handleRequest(ctx context.Context, request *protocol.Message) (*protocol.Message, error) { var err error serviceName := request.ServicePath methodName := request.ServiceMethod response := request.Clone() response.SetMessageType(protocol.Response) s.serviceMapMu.RLock() service, ok := s.serviceMap[serviceName] s.serviceMapMu.RUnlock() if !ok { // 都没注册直接返回错误 err = errors.New(fmt.Sprintf("不能找到服务发现者为%s的服务", serviceName)) return handleError(response, err) } methodType, ok := service.method[methodName] if !ok { // 看看是否注册了函数的调用 if _, ok := service.function[methodName]; ok { protocol.FreeMsg(response) // 这里创建对象要回收的 return s.handleRequestForFunction(ctx, request) } err = errors.New(fmt.Sprintf("不能找到服务提供者%s下方法名为%s的方法", serviceName, methodName)) return handleError(response, err) } requestType := ObjectPool
erCon
identifier_name
server.go
// 看看是否注册了函数的调用 if _, ok := service.function[methodName]; ok { protocol.FreeMsg(response) // 这里创建对象要回收的 return s.handleRequestForFunction(ctx, request) } err = errors.New(fmt.Sprintf("不能找到服务提供者%s下方法名为%s的方法", serviceName, methodName)) return handleError(response, err) } requestType := ObjectPool.Get(methodType.requestType) defer ObjectPool.Put(methodType.requestType, requestType) codec := share.Codecs[request.SerializeType()] if codec == nil { err = fmt.Errorf("不能找到对应的的序列化方式:%T", request.SerializeType()) return handleError(response, err) } err = codec.Decode(request.Payload, requestType) if err != nil { return handleError(response, err) } responseType := ObjectPool.Get(methodType.responseType) defer ObjectPool.Put(methodType.responseType, responseType) requestType, err = s.Plugins.DoPreCall(ctx, serviceName, methodName, requestType) if err != nil { return handleError(response, err) } if methodType.requestType.Kind() != reflect.Ptr { // 不是指针 err = service.call(ctx, methodType, reflect.ValueOf(requestType).Elem(), reflect.ValueOf(responseType)) } else { err = service.call(ctx, methodType, reflect.ValueOf(requestType), reflect.ValueOf(responseType)) } if err != nil { return handleError(response, err) } responseType, err = s.Plugins.DoPostCall(ctx, serviceName, methodName, requestType, responseType) if err != nil { return handleError(response, err) } if !request.IsOneway() { data, err := codec.Encode(responseType) if err != nil { return handleError(response, err) } response.Payload = data } return response, nil } // 处理函数类型的 func (s *Server) handleRequestForFunction(ctx context.Context, request *protocol.Message) (*protocol.Message, error) { var err error response := request.Clone() response.SetMessageType(protocol.Response) serviceName := request.ServicePath methodName := request.ServiceMethod s.serviceMapMu.RLock() service, ok := s.serviceMap[serviceName] s.serviceMapMu.RUnlock() if !ok { // 都没注册直接返回错误 err = errors.New(fmt.Sprintf("不能找到服务发现者为%s的服务", serviceName)) return handleError(response, err) } funcType := service.function[serviceName] if funcType == nil { err = errors.New(fmt.Sprintf("不能找到服务发现者为%s对应的函数调用%s", serviceName, methodName)) return handleError(response, err) } requestType := ObjectPool.Get(funcType.requestType) defer ObjectPool.Put(funcType.requestType, requestType) codec := share.Codecs[request.SerializeType()] if codec == nil { err = fmt.Errorf("不能找到对应的的序列化方式:%T", request.SerializeType()) return handleError(response, err) } err = codec.Decode(request.Payload, requestType) if err != nil { return handleError(response, err) } responseType := ObjectPool.Get(funcType.responseType) defer ObjectPool.Put(funcType.responseType, responseType) requestType, err = s.Plugins.DoPreCall(ctx, serviceName, methodName, requestType) if err != nil { return handleError(response, err) } if funcType.requestType.Kind() != reflect.Ptr { // 不是指针 err = service.callForFunc(ctx, funcType, reflect.ValueOf(requestType).Elem(), reflect.ValueOf(responseType)) } else { err = service.callForFunc(ctx, funcType, reflect.ValueOf(requestType), reflect.ValueOf(responseType)) } if err != nil { return handleError(response, err) } responseType, err = s.Plugins.DoPostCall(ctx, serviceName, methodName, requestType, responseType) if err != nil { return handleError(response, err) } if !request.IsOneway() { data, err := codec.Encode(responseType) if err != nil { return handleError(response, err) } response.Payload = data } return response, nil } // 鉴权 func (s *Server) auth(ctx context.Context, request *protocol.Message) error { if s.AuthFunc == nil { return nil } token := request.Metadata[share.AuthKey] return s.AuthFunc(ctx, request, token) } // 暴力关闭服务(生产环境不建议使用,建议使用Shutdown) func (s *Server) Close() error { s.serviceMapMu.Lock() defer s.serviceMapMu.Unlock() var err error if s.ln != nil { err = s.ln.Close() } for conn, _ := range s.activeConn { err = conn.Close() delete(s.activeConn, conn) s.Plugins.DoPostConnClose(conn) } return err } // 优雅的关闭服务, // 先关闭tcp监听,使得不再有conn连接进来 // 关闭每个conn的读端,使其不再收客户端数据 // 循环等待服务正在处理的消息数量变为0 (使得所有正在处理的消息都能处理完成) // 关闭网关服务 // 依次关闭conn的读和写 func (s *Server) Shutdown(ctx context.Context) error { var err error if atomic.CompareAndSwapInt32(&s.inShutdown, 0, 1) { // 保证结束进程只执行一次 log.Info("服务开始关闭...") // 先关闭tcp链接的读端(写端要等所有请求都结束后才能关闭) s.connMu.Lock() if s.ln != nil { s.ln.Close() // 关闭监听 } for conn, _ := range s.activeConn { if lConn, ok := conn.(*net.TCPConn); ok { lConn.CloseRead() } } s.connMu.Unlock() ticker := time.NewTicker(time.Second) // 监听间隔 defer ticker.Stop() outer: for { if s.checkMsgHandlerFinish() { break } select { case <-ctx.Done(): break outer case <-ticker.C: } } if s.gatewayHttpServer != nil { if err = s.closeHTTP1APIGateway(ctx); err != nil { log.WarnF("关闭http网关时出错:%v", err) } else { log.Info("http网关服务已经关闭") } } s.connMu.Lock() for conn, _ := range s.activeConn { conn.Close() delete(s.activeConn, conn) s.Plugins.DoPostConnClose(conn) } s.closeDoneChanLocked() s.connMu.Unlock() } return err } // 检测服务处理的消息是否处理完成 func (s *Server) checkMsgHandlerFinish() bool { size := atomic.LoadInt32(&s.handlerMsgNum) log.InfoF("还需要处理%d条消息", size) return size == 0 } // 关闭结束通道(如果别的协程已经关闭,则直接返回) func (s *Server) closeDoneChanLocked() { select { case <-s.doneChan: return default: close(s.doneChan) } } // 监听结束服务事件(terminated) func (s *Server) startShutdownServe() { go func(s *Server) { log.InfoF("rpc listen at %s", s.ln.Addr().String()) c := make(chan os.Signal, 1) signal.Notify(c, syscall.SIGTERM) sg := <-c if sg.String() == "terminated" { if s.onShutdown != nil && len(s.onShutdown) > 0 { for _, shutdown := range s.onShutdown { shutdown(s) } } err := s.Shutdown(context.Background()) if err != nil { log.Error(err.Error()) } } }(s) } // 关闭链接 func (s *Server) closeChannel(conn net.Conn) { s.connMu.Lock() defer s.connMu.Unlock() delete(s.activeConn, conn) conn.Close() } // 判断服务是否已经关闭了 func (s *Server) isShutdown() bool { return atomic.LoadInt32(&s.inShutdown) == 1 } func (s *Server) readRequest(ctx context.Context, rBuff io.Reader) (*protocol.Message, error) { var err error err = s.Plugins.DoPreReadRequest(ctx) if err != nil { return nil, err } request := protocol.GetPooledMsg() err = s.Plugins.DoPreReadRequest(ctx) if err != nil { return nil, err } // 开始解码 err = request.Decode(rBuff) if err == io.EOF { // io.EOF代表读完了 return request, err } pErr := s.Plugins.DoPostReadRequest(ctx, request, err) if err == nil { // 看看插件里面的调用会报什么错误 err = pErr } return request, err } // 处理错误 func handleError(response *protocol.Message, err error) (*protocol.Message, error) { response.SetMessageStatusType(protocol.Error) if response.Metadata == nil { response.Metadata = make(map[string]string, 10) } response.Metadata[protocol.ServiceError] = err.Error() return response, err }
identifier_body
prefilter.rs
// them. if !self.ascii_case_insensitive { if let Some(pre) = self.memmem.build() { return Some(pre); } } match (self.start_bytes.build(), self.rare_bytes.build()) { // If we could build both start and rare prefilters, then there are // a few cases in which we'd want to use the start-byte prefilter // over the rare-byte prefilter, since the former has lower // overhead. (prestart @ Some(_), prerare @ Some(_)) => { // If the start-byte prefilter can scan for a smaller number // of bytes than the rare-byte prefilter, then it's probably // faster. let has_fewer_bytes = self.start_bytes.count < self.rare_bytes.count; // Otherwise, if the combined frequency rank of the detected // bytes in the start-byte prefilter is "close" to the combined // frequency rank of the rare-byte prefilter, then we pick // the start-byte prefilter even if the rare-byte prefilter // heuristically searches for rare bytes. This is because the // rare-byte prefilter has higher constant costs, so we tend to // prefer the start-byte prefilter when we can. let has_rarer_bytes = self.start_bytes.rank_sum <= self.rare_bytes.rank_sum + 50; if has_fewer_bytes || has_rarer_bytes { prestart } else { prerare } } (prestart @ Some(_), None) => prestart, (None, prerare @ Some(_)) => prerare, (None, None) if self.ascii_case_insensitive => None, (None, None) => { self.packed.as_ref().and_then(|b| b.build()).map(|s| { let memory_usage = s.memory_usage(); Prefilter { finder: Arc::new(Packed(s)), memory_usage } }) } } } /// Add a literal string to this prefilter builder. pub(crate) fn add(&mut self, bytes: &[u8]) { if bytes.is_empty() { self.enabled = false; } if !self.enabled { return; } self.count += 1; self.start_bytes.add(bytes); self.rare_bytes.add(bytes); self.memmem.add(bytes); if let Some(ref mut pbuilder) = self.packed { pbuilder.add(bytes); } } } /// A type that wraps a packed searcher and implements the `Prefilter` /// interface. #[derive(Clone, Debug)] struct Packed(packed::Searcher); impl PrefilterI for Packed { fn find_in(&self, haystack: &[u8], span: Span) -> Candidate { self.0 .find_in(&haystack, span) .map_or(Candidate::None, Candidate::Match) } } /// A builder for constructing a prefilter that uses memmem. #[derive(Debug, Default)] struct MemmemBuilder { /// The number of patterns that have been added. count: usize, /// The singular pattern to search for. This is only set when count==1. one: Option<Vec<u8>>, } impl MemmemBuilder { fn build(&self) -> Option<Prefilter> { #[cfg(all(feature = "std", feature = "perf-literal"))] fn imp(builder: &MemmemBuilder) -> Option<Prefilter> { let pattern = builder.one.as_ref()?; assert_eq!(1, builder.count); let finder = Arc::new(Memmem( memchr::memmem::Finder::new(pattern).into_owned(), )); let memory_usage = pattern.len(); Some(Prefilter { finder, memory_usage }) } #[cfg(not(all(feature = "std", feature = "perf-literal")))] fn imp(_: &MemmemBuilder) -> Option<Prefilter> { None } imp(self) } fn add(&mut self, bytes: &[u8]) { self.count += 1; if self.count == 1 { self.one = Some(bytes.to_vec()); } else { self.one = None; } } } /// A type that wraps a SIMD accelerated single substring search from the /// `memchr` crate for use as a prefilter. /// /// Currently, this prefilter is only active for Aho-Corasick searchers with /// a single pattern. In theory, this could be extended to support searchers /// that have a common prefix of more than one byte (for one byte, we would use /// memchr), but it's not clear if it's worth it or not. /// /// Also, unfortunately, this currently also requires the 'std' feature to /// be enabled. That's because memchr doesn't have a no-std-but-with-alloc /// mode, and so APIs like Finder::into_owned aren't available when 'std' is /// disabled. But there should be an 'alloc' feature that brings in APIs like /// Finder::into_owned but doesn't use std-only features like runtime CPU /// feature detection. #[cfg(all(feature = "std", feature = "perf-literal"))] #[derive(Clone, Debug)] struct Memmem(memchr::memmem::Finder<'static>); #[cfg(all(feature = "std", feature = "perf-literal"))] impl PrefilterI for Memmem { fn find_in(&self, haystack: &[u8], span: Span) -> Candidate { use crate::util::primitives::PatternID; self.0.find(&haystack[span]).map_or(Candidate::None, |i| { let start = span.start + i; let end = start + self.0.needle().len(); // N.B. We can declare a match and use a fixed pattern ID here // because a Memmem prefilter is only ever created for searchers // with exactly one pattern. Thus, every match is always a match // and it is always for the first and only pattern. Candidate::Match(Match::new(PatternID::ZERO, start..end)) }) } } /// A builder for constructing a rare byte prefilter. /// /// A rare byte prefilter attempts to pick out a small set of rare bytes that /// occurr in the patterns, and then quickly scan to matches of those rare /// bytes. #[derive(Clone, Debug)] struct
{ /// Whether this prefilter should account for ASCII case insensitivity or /// not. ascii_case_insensitive: bool, /// A set of rare bytes, indexed by byte value. rare_set: ByteSet, /// A set of byte offsets associated with bytes in a pattern. An entry /// corresponds to a particular bytes (its index) and is only non-zero if /// the byte occurred at an offset greater than 0 in at least one pattern. /// /// If a byte's offset is not representable in 8 bits, then the rare bytes /// prefilter becomes inert. byte_offsets: RareByteOffsets, /// Whether this is available as a prefilter or not. This can be set to /// false during construction if a condition is seen that invalidates the /// use of the rare-byte prefilter. available: bool, /// The number of bytes set to an active value in `byte_offsets`. count: usize, /// The sum of frequency ranks for the rare bytes detected. This is /// intended to give a heuristic notion of how rare the bytes are. rank_sum: u16, } /// A set of byte offsets, keyed by byte. #[derive(Clone, Copy)] struct RareByteOffsets { /// Each entry corresponds to the maximum offset of the corresponding /// byte across all patterns seen. set: [RareByteOffset; 256], } impl RareByteOffsets { /// Create a new empty set of rare byte offsets. pub(crate) fn empty() -> RareByteOffsets { RareByteOffsets { set: [RareByteOffset::default(); 256] } } /// Add the given offset for the given byte to this set. If the offset is /// greater than the existing offset, then it overwrites the previous /// value and returns false. If there is no previous value set, then this /// sets it and returns true. pub(crate) fn set(&mut self, byte: u8, off: RareByteOffset) { self.set[byte as usize].max = cmp::max(self.set[byte as usize].max, off.max); } } impl core::fmt::Debug for RareByteOffsets { fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { let mut offsets = vec![]; for off in self.set.iter() { if off.max > 0 { offsets.push(off); } } f.debug_struct("RareByteOffsets").field("set", &offsets).finish() } } /// Offsets associated with an occurrence of a "rare" byte in any of the /// patterns used to construct a single Aho-Corasick automaton. #[derive(Clone, Copy, Debug)] struct RareByteOffset { /// The maximum offset at which a particular byte occurs from the start /// of any pattern. This is used as a shift amount. That is, when an /// occurrence
RareBytesBuilder
identifier_name
prefilter.rs
rare_set: ByteSet, /// A set of byte offsets associated with bytes in a pattern. An entry /// corresponds to a particular bytes (its index) and is only non-zero if /// the byte occurred at an offset greater than 0 in at least one pattern. /// /// If a byte's offset is not representable in 8 bits, then the rare bytes /// prefilter becomes inert. byte_offsets: RareByteOffsets, /// Whether this is available as a prefilter or not. This can be set to /// false during construction if a condition is seen that invalidates the /// use of the rare-byte prefilter. available: bool, /// The number of bytes set to an active value in `byte_offsets`. count: usize, /// The sum of frequency ranks for the rare bytes detected. This is /// intended to give a heuristic notion of how rare the bytes are. rank_sum: u16, } /// A set of byte offsets, keyed by byte. #[derive(Clone, Copy)] struct RareByteOffsets { /// Each entry corresponds to the maximum offset of the corresponding /// byte across all patterns seen. set: [RareByteOffset; 256], } impl RareByteOffsets { /// Create a new empty set of rare byte offsets. pub(crate) fn empty() -> RareByteOffsets { RareByteOffsets { set: [RareByteOffset::default(); 256] } } /// Add the given offset for the given byte to this set. If the offset is /// greater than the existing offset, then it overwrites the previous /// value and returns false. If there is no previous value set, then this /// sets it and returns true. pub(crate) fn set(&mut self, byte: u8, off: RareByteOffset) { self.set[byte as usize].max = cmp::max(self.set[byte as usize].max, off.max); } } impl core::fmt::Debug for RareByteOffsets { fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { let mut offsets = vec![]; for off in self.set.iter() { if off.max > 0 { offsets.push(off); } } f.debug_struct("RareByteOffsets").field("set", &offsets).finish() } } /// Offsets associated with an occurrence of a "rare" byte in any of the /// patterns used to construct a single Aho-Corasick automaton. #[derive(Clone, Copy, Debug)] struct RareByteOffset { /// The maximum offset at which a particular byte occurs from the start /// of any pattern. This is used as a shift amount. That is, when an /// occurrence of this byte is found, the candidate position reported by /// the prefilter is `position_of_byte - max`, such that the automaton /// will begin its search at a position that is guaranteed to observe a /// match. /// /// To avoid accidentally quadratic behavior, a prefilter is considered /// ineffective when it is asked to start scanning from a position that it /// has already scanned past. /// /// Using a `u8` here means that if we ever see a pattern that's longer /// than 255 bytes, then the entire rare byte prefilter is disabled. max: u8, } impl Default for RareByteOffset { fn default() -> RareByteOffset { RareByteOffset { max: 0 } } } impl RareByteOffset { /// Create a new rare byte offset. If the given offset is too big, then /// None is returned. In that case, callers should render the rare bytes /// prefilter inert. fn new(max: usize) -> Option<RareByteOffset> { if max > u8::MAX as usize { None } else { Some(RareByteOffset { max: max as u8 }) } } } impl RareBytesBuilder { /// Create a new builder for constructing a rare byte prefilter. fn new() -> RareBytesBuilder { RareBytesBuilder { ascii_case_insensitive: false, rare_set: ByteSet::empty(), byte_offsets: RareByteOffsets::empty(), available: true, count: 0, rank_sum: 0, } } /// Enable ASCII case insensitivity. When set, byte strings added to this /// builder will be interpreted without respect to ASCII case. fn ascii_case_insensitive(mut self, yes: bool) -> RareBytesBuilder { self.ascii_case_insensitive = yes; self } /// Build the rare bytes prefilter. /// /// If there are more than 3 distinct rare bytes found, or if heuristics /// otherwise determine that this prefilter should not be used, then `None` /// is returned. fn build(&self) -> Option<Prefilter> { #[cfg(feature = "perf-literal")] fn imp(builder: &RareBytesBuilder) -> Option<Prefilter> { if !builder.available || builder.count > 3 { return None; } let (mut bytes, mut len) = ([0; 3], 0); for b in 0..=255 { if builder.rare_set.contains(b) { bytes[len] = b as u8; len += 1; } } let finder: Arc<dyn PrefilterI> = match len { 0 => return None, 1 => Arc::new(RareBytesOne { byte1: bytes[0], offset: builder.byte_offsets.set[bytes[0] as usize], }), 2 => Arc::new(RareBytesTwo { offsets: builder.byte_offsets, byte1: bytes[0], byte2: bytes[1], }), 3 => Arc::new(RareBytesThree { offsets: builder.byte_offsets, byte1: bytes[0], byte2: bytes[1], byte3: bytes[2], }), _ => unreachable!(), }; Some(Prefilter { finder, memory_usage: 0 }) } #[cfg(not(feature = "perf-literal"))] fn imp(_: &RareBytesBuilder) -> Option<Prefilter> { None } imp(self) } /// Add a byte string to this builder. /// /// All patterns added to an Aho-Corasick automaton should be added to this /// builder before attempting to construct the prefilter. fn add(&mut self, bytes: &[u8]) { // If we've already given up, then do nothing. if !self.available { return; } // If we've already blown our budget, then don't waste time looking // for more rare bytes. if self.count > 3 { self.available = false; return; } // If the pattern is too long, then our offset table is bunk, so // give up. if bytes.len() >= 256 { self.available = false; return; } let mut rarest = match bytes.get(0) { None => return, Some(&b) => (b, freq_rank(b)), }; // The idea here is to look for the rarest byte in each pattern, and // add that to our set. As a special exception, if we see a byte that // we've already added, then we immediately stop and choose that byte, // even if there's another rare byte in the pattern. This helps us // apply the rare byte optimization in more cases by attempting to pick // bytes that are in common between patterns. So for example, if we // were searching for `Sherlock` and `lockjaw`, then this would pick // `k` for both patterns, resulting in the use of `memchr` instead of // `memchr2` for `k` and `j`. let mut found = false; for (pos, &b) in bytes.iter().enumerate() { self.set_offset(pos, b); if found { continue; } if self.rare_set.contains(b) { found = true; continue; } let rank = freq_rank(b); if rank < rarest.1 { rarest = (b, rank); } } if !found { self.add_rare_byte(rarest.0); } } fn set_offset(&mut self, pos: usize, byte: u8) { // This unwrap is OK because pos is never bigger than our max. let offset = RareByteOffset::new(pos).unwrap(); self.byte_offsets.set(byte, offset); if self.ascii_case_insensitive { self.byte_offsets.set(opposite_ascii_case(byte), offset); } } fn add_rare_byte(&mut self, byte: u8) { self.add_one_rare_byte(byte); if self.ascii_case_insensitive { self.add_one_rare_byte(opposite_ascii_case(byte)); } } fn add_one_rare_byte(&mut self, byte: u8) { if !self.rare_set.contains(byte)
{ self.rare_set.add(byte); self.count += 1; self.rank_sum += freq_rank(byte) as u16; }
conditional_block
prefilter.rs
// them. if !self.ascii_case_insensitive { if let Some(pre) = self.memmem.build() { return Some(pre); } } match (self.start_bytes.build(), self.rare_bytes.build()) { // If we could build both start and rare prefilters, then there are // a few cases in which we'd want to use the start-byte prefilter // over the rare-byte prefilter, since the former has lower // overhead. (prestart @ Some(_), prerare @ Some(_)) => { // If the start-byte prefilter can scan for a smaller number // of bytes than the rare-byte prefilter, then it's probably // faster. let has_fewer_bytes = self.start_bytes.count < self.rare_bytes.count; // Otherwise, if the combined frequency rank of the detected // bytes in the start-byte prefilter is "close" to the combined // frequency rank of the rare-byte prefilter, then we pick // the start-byte prefilter even if the rare-byte prefilter // heuristically searches for rare bytes. This is because the // rare-byte prefilter has higher constant costs, so we tend to // prefer the start-byte prefilter when we can. let has_rarer_bytes = self.start_bytes.rank_sum <= self.rare_bytes.rank_sum + 50; if has_fewer_bytes || has_rarer_bytes { prestart } else { prerare } } (prestart @ Some(_), None) => prestart, (None, prerare @ Some(_)) => prerare, (None, None) if self.ascii_case_insensitive => None, (None, None) => { self.packed.as_ref().and_then(|b| b.build()).map(|s| { let memory_usage = s.memory_usage(); Prefilter { finder: Arc::new(Packed(s)), memory_usage } }) } } } /// Add a literal string to this prefilter builder. pub(crate) fn add(&mut self, bytes: &[u8]) { if bytes.is_empty() { self.enabled = false; } if !self.enabled { return; } self.count += 1; self.start_bytes.add(bytes); self.rare_bytes.add(bytes); self.memmem.add(bytes); if let Some(ref mut pbuilder) = self.packed { pbuilder.add(bytes); } } } /// A type that wraps a packed searcher and implements the `Prefilter` /// interface. #[derive(Clone, Debug)] struct Packed(packed::Searcher); impl PrefilterI for Packed { fn find_in(&self, haystack: &[u8], span: Span) -> Candidate { self.0 .find_in(&haystack, span) .map_or(Candidate::None, Candidate::Match) } } /// A builder for constructing a prefilter that uses memmem. #[derive(Debug, Default)] struct MemmemBuilder { /// The number of patterns that have been added. count: usize, /// The singular pattern to search for. This is only set when count==1. one: Option<Vec<u8>>, } impl MemmemBuilder { fn build(&self) -> Option<Prefilter> { #[cfg(all(feature = "std", feature = "perf-literal"))] fn imp(builder: &MemmemBuilder) -> Option<Prefilter> { let pattern = builder.one.as_ref()?; assert_eq!(1, builder.count); let finder = Arc::new(Memmem( memchr::memmem::Finder::new(pattern).into_owned(), )); let memory_usage = pattern.len(); Some(Prefilter { finder, memory_usage }) } #[cfg(not(all(feature = "std", feature = "perf-literal")))] fn imp(_: &MemmemBuilder) -> Option<Prefilter> { None } imp(self) } fn add(&mut self, bytes: &[u8]) { self.count += 1; if self.count == 1 { self.one = Some(bytes.to_vec()); } else { self.one = None; } } } /// A type that wraps a SIMD accelerated single substring search from the /// `memchr` crate for use as a prefilter. /// /// Currently, this prefilter is only active for Aho-Corasick searchers with /// a single pattern. In theory, this could be extended to support searchers /// that have a common prefix of more than one byte (for one byte, we would use /// memchr), but it's not clear if it's worth it or not. /// /// Also, unfortunately, this currently also requires the 'std' feature to /// be enabled. That's because memchr doesn't have a no-std-but-with-alloc /// mode, and so APIs like Finder::into_owned aren't available when 'std' is /// disabled. But there should be an 'alloc' feature that brings in APIs like /// Finder::into_owned but doesn't use std-only features like runtime CPU /// feature detection. #[cfg(all(feature = "std", feature = "perf-literal"))] #[derive(Clone, Debug)] struct Memmem(memchr::memmem::Finder<'static>); #[cfg(all(feature = "std", feature = "perf-literal"))] impl PrefilterI for Memmem { fn find_in(&self, haystack: &[u8], span: Span) -> Candidate
} /// A builder for constructing a rare byte prefilter. /// /// A rare byte prefilter attempts to pick out a small set of rare bytes that /// occurr in the patterns, and then quickly scan to matches of those rare /// bytes. #[derive(Clone, Debug)] struct RareBytesBuilder { /// Whether this prefilter should account for ASCII case insensitivity or /// not. ascii_case_insensitive: bool, /// A set of rare bytes, indexed by byte value. rare_set: ByteSet, /// A set of byte offsets associated with bytes in a pattern. An entry /// corresponds to a particular bytes (its index) and is only non-zero if /// the byte occurred at an offset greater than 0 in at least one pattern. /// /// If a byte's offset is not representable in 8 bits, then the rare bytes /// prefilter becomes inert. byte_offsets: RareByteOffsets, /// Whether this is available as a prefilter or not. This can be set to /// false during construction if a condition is seen that invalidates the /// use of the rare-byte prefilter. available: bool, /// The number of bytes set to an active value in `byte_offsets`. count: usize, /// The sum of frequency ranks for the rare bytes detected. This is /// intended to give a heuristic notion of how rare the bytes are. rank_sum: u16, } /// A set of byte offsets, keyed by byte. #[derive(Clone, Copy)] struct RareByteOffsets { /// Each entry corresponds to the maximum offset of the corresponding /// byte across all patterns seen. set: [RareByteOffset; 256], } impl RareByteOffsets { /// Create a new empty set of rare byte offsets. pub(crate) fn empty() -> RareByteOffsets { RareByteOffsets { set: [RareByteOffset::default(); 256] } } /// Add the given offset for the given byte to this set. If the offset is /// greater than the existing offset, then it overwrites the previous /// value and returns false. If there is no previous value set, then this /// sets it and returns true. pub(crate) fn set(&mut self, byte: u8, off: RareByteOffset) { self.set[byte as usize].max = cmp::max(self.set[byte as usize].max, off.max); } } impl core::fmt::Debug for RareByteOffsets { fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { let mut offsets = vec![]; for off in self.set.iter() { if off.max > 0 { offsets.push(off); } } f.debug_struct("RareByteOffsets").field("set", &offsets).finish() } } /// Offsets associated with an occurrence of a "rare" byte in any of the /// patterns used to construct a single Aho-Corasick automaton. #[derive(Clone, Copy, Debug)] struct RareByteOffset { /// The maximum offset at which a particular byte occurs from the start /// of any pattern. This is used as a shift amount. That is, when an ///
{ use crate::util::primitives::PatternID; self.0.find(&haystack[span]).map_or(Candidate::None, |i| { let start = span.start + i; let end = start + self.0.needle().len(); // N.B. We can declare a match and use a fixed pattern ID here // because a Memmem prefilter is only ever created for searchers // with exactly one pattern. Thus, every match is always a match // and it is always for the first and only pattern. Candidate::Match(Match::new(PatternID::ZERO, start..end)) }) }
identifier_body
prefilter.rs
(bytes); } } } /// A type that wraps a packed searcher and implements the `Prefilter` /// interface. #[derive(Clone, Debug)] struct Packed(packed::Searcher); impl PrefilterI for Packed { fn find_in(&self, haystack: &[u8], span: Span) -> Candidate { self.0 .find_in(&haystack, span) .map_or(Candidate::None, Candidate::Match) } } /// A builder for constructing a prefilter that uses memmem. #[derive(Debug, Default)] struct MemmemBuilder { /// The number of patterns that have been added. count: usize, /// The singular pattern to search for. This is only set when count==1. one: Option<Vec<u8>>, } impl MemmemBuilder { fn build(&self) -> Option<Prefilter> { #[cfg(all(feature = "std", feature = "perf-literal"))] fn imp(builder: &MemmemBuilder) -> Option<Prefilter> { let pattern = builder.one.as_ref()?; assert_eq!(1, builder.count); let finder = Arc::new(Memmem( memchr::memmem::Finder::new(pattern).into_owned(), )); let memory_usage = pattern.len(); Some(Prefilter { finder, memory_usage }) } #[cfg(not(all(feature = "std", feature = "perf-literal")))] fn imp(_: &MemmemBuilder) -> Option<Prefilter> { None } imp(self) } fn add(&mut self, bytes: &[u8]) { self.count += 1; if self.count == 1 { self.one = Some(bytes.to_vec()); } else { self.one = None; } } } /// A type that wraps a SIMD accelerated single substring search from the /// `memchr` crate for use as a prefilter. /// /// Currently, this prefilter is only active for Aho-Corasick searchers with /// a single pattern. In theory, this could be extended to support searchers /// that have a common prefix of more than one byte (for one byte, we would use /// memchr), but it's not clear if it's worth it or not. /// /// Also, unfortunately, this currently also requires the 'std' feature to /// be enabled. That's because memchr doesn't have a no-std-but-with-alloc /// mode, and so APIs like Finder::into_owned aren't available when 'std' is /// disabled. But there should be an 'alloc' feature that brings in APIs like /// Finder::into_owned but doesn't use std-only features like runtime CPU /// feature detection. #[cfg(all(feature = "std", feature = "perf-literal"))] #[derive(Clone, Debug)] struct Memmem(memchr::memmem::Finder<'static>); #[cfg(all(feature = "std", feature = "perf-literal"))] impl PrefilterI for Memmem { fn find_in(&self, haystack: &[u8], span: Span) -> Candidate { use crate::util::primitives::PatternID; self.0.find(&haystack[span]).map_or(Candidate::None, |i| { let start = span.start + i; let end = start + self.0.needle().len(); // N.B. We can declare a match and use a fixed pattern ID here // because a Memmem prefilter is only ever created for searchers // with exactly one pattern. Thus, every match is always a match // and it is always for the first and only pattern. Candidate::Match(Match::new(PatternID::ZERO, start..end)) }) } } /// A builder for constructing a rare byte prefilter. /// /// A rare byte prefilter attempts to pick out a small set of rare bytes that /// occurr in the patterns, and then quickly scan to matches of those rare /// bytes. #[derive(Clone, Debug)] struct RareBytesBuilder { /// Whether this prefilter should account for ASCII case insensitivity or /// not. ascii_case_insensitive: bool, /// A set of rare bytes, indexed by byte value. rare_set: ByteSet, /// A set of byte offsets associated with bytes in a pattern. An entry /// corresponds to a particular bytes (its index) and is only non-zero if /// the byte occurred at an offset greater than 0 in at least one pattern. /// /// If a byte's offset is not representable in 8 bits, then the rare bytes /// prefilter becomes inert. byte_offsets: RareByteOffsets, /// Whether this is available as a prefilter or not. This can be set to /// false during construction if a condition is seen that invalidates the /// use of the rare-byte prefilter. available: bool, /// The number of bytes set to an active value in `byte_offsets`. count: usize, /// The sum of frequency ranks for the rare bytes detected. This is /// intended to give a heuristic notion of how rare the bytes are. rank_sum: u16, } /// A set of byte offsets, keyed by byte. #[derive(Clone, Copy)] struct RareByteOffsets { /// Each entry corresponds to the maximum offset of the corresponding /// byte across all patterns seen. set: [RareByteOffset; 256], } impl RareByteOffsets { /// Create a new empty set of rare byte offsets. pub(crate) fn empty() -> RareByteOffsets { RareByteOffsets { set: [RareByteOffset::default(); 256] } } /// Add the given offset for the given byte to this set. If the offset is /// greater than the existing offset, then it overwrites the previous /// value and returns false. If there is no previous value set, then this /// sets it and returns true. pub(crate) fn set(&mut self, byte: u8, off: RareByteOffset) { self.set[byte as usize].max = cmp::max(self.set[byte as usize].max, off.max); } } impl core::fmt::Debug for RareByteOffsets { fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { let mut offsets = vec![]; for off in self.set.iter() { if off.max > 0 { offsets.push(off); } } f.debug_struct("RareByteOffsets").field("set", &offsets).finish() } } /// Offsets associated with an occurrence of a "rare" byte in any of the /// patterns used to construct a single Aho-Corasick automaton. #[derive(Clone, Copy, Debug)] struct RareByteOffset { /// The maximum offset at which a particular byte occurs from the start /// of any pattern. This is used as a shift amount. That is, when an /// occurrence of this byte is found, the candidate position reported by /// the prefilter is `position_of_byte - max`, such that the automaton /// will begin its search at a position that is guaranteed to observe a /// match. /// /// To avoid accidentally quadratic behavior, a prefilter is considered /// ineffective when it is asked to start scanning from a position that it /// has already scanned past. /// /// Using a `u8` here means that if we ever see a pattern that's longer /// than 255 bytes, then the entire rare byte prefilter is disabled. max: u8, } impl Default for RareByteOffset { fn default() -> RareByteOffset { RareByteOffset { max: 0 } } } impl RareByteOffset { /// Create a new rare byte offset. If the given offset is too big, then /// None is returned. In that case, callers should render the rare bytes /// prefilter inert. fn new(max: usize) -> Option<RareByteOffset> { if max > u8::MAX as usize { None } else { Some(RareByteOffset { max: max as u8 }) } } } impl RareBytesBuilder { /// Create a new builder for constructing a rare byte prefilter. fn new() -> RareBytesBuilder { RareBytesBuilder { ascii_case_insensitive: false, rare_set: ByteSet::empty(), byte_offsets: RareByteOffsets::empty(), available: true, count: 0, rank_sum: 0, } } /// Enable ASCII case insensitivity. When set, byte strings added to this /// builder will be interpreted without respect to ASCII case. fn ascii_case_insensitive(mut self, yes: bool) -> RareBytesBuilder { self.ascii_case_insensitive = yes; self } /// Build the rare bytes prefilter. /// /// If there are more than 3 distinct rare bytes found, or if heuristics /// otherwise determine that this prefilter should not be used, then `None` /// is returned. fn build(&self) -> Option<Prefilter> { #[cfg(feature = "perf-literal")] fn imp(builder: &RareBytesBuilder) -> Option<Prefilter> { if !builder.available || builder.count > 3 { return None;
} let (mut bytes, mut len) = ([0; 3], 0);
random_line_split
code.go
s' is a read only variable", "You cannot set the given variable as it is a read-only variable.") VT03011 = errorWithoutState("VT03011", vtrpcpb.Code_INVALID_ARGUMENT, "invalid value type: %v", "The given value type is not accepted.") VT03012 = errorWithoutState("VT03012", vtrpcpb.Code_INVALID_ARGUMENT, "invalid syntax: %s", "The syntax is invalid. Please refer to the MySQL documentation for the proper syntax.") VT03013 = errorWithState("VT03013", vtrpcpb.Code_INVALID_ARGUMENT, NonUniqTable, "not unique table/alias: '%s'", "This table or alias name is already use. Please use another one that is unique.") VT03014 = errorWithState("VT03014", vtrpcpb.Code_INVALID_ARGUMENT, BadFieldError, "unknown column '%d' in '%s'", "The given column is unknown.") VT03015 = errorWithoutState("VT03015", vtrpcpb.Code_INVALID_ARGUMENT, "column has duplicate set values: '%v'", "Cannot assign multiple values to a column in an update statement.") VT03016 = errorWithoutState("VT03016", vtrpcpb.Code_INVALID_ARGUMENT, "unknown vindex column: '%s'", "The given column is unknown in the vindex table.") VT03017 = errorWithState("VT03017", vtrpcpb.Code_INVALID_ARGUMENT, SyntaxError, "where clause can only be of the type 'pos > <value>'", "This vstream where clause can only be a greater than filter.") VT03018 = errorWithoutState("VT03018", vtrpcpb.Code_INVALID_ARGUMENT, "NEXT used on a non-sequence table", "You cannot use the NEXT syntax on a table that is not a sequence table.") VT03019 = errorWithoutState("VT03019", vtrpcpb.Code_INVALID_ARGUMENT, "column %s not found", "The given column was not found or is not available.") VT03020 = errorWithoutState("VT03020", vtrpcpb.Code_INVALID_ARGUMENT, "column %s not found in subquery", "The given column was not found in the subquery.")
VT03022 = errorWithoutState("VT03022", vtrpcpb.Code_INVALID_ARGUMENT, "column %v not found in %v", "The given column cannot be found.") VT03023 = errorWithoutState("VT03023", vtrpcpb.Code_INVALID_ARGUMENT, "INSERT not supported when targeting a key range: %s", "When targeting a range of shards, Vitess does not know which shard to send the INSERT to.") VT03024 = errorWithoutState("VT03024", vtrpcpb.Code_INVALID_ARGUMENT, "'%s' user defined variable does not exists", "The query cannot be prepared using the user defined variable as it does not exists for this session.") VT03025 = errorWithState("VT03025", vtrpcpb.Code_INVALID_ARGUMENT, WrongArguments, "Incorrect arguments to %s", "The execute statement have wrong number of arguments") VT05001 = errorWithState("VT05001", vtrpcpb.Code_NOT_FOUND, DbDropExists, "cannot drop database '%s'; database does not exists", "The given database does not exist; Vitess cannot drop it.") VT05002 = errorWithState("VT05002", vtrpcpb.Code_NOT_FOUND, BadDb, "cannot alter database '%s'; unknown database", "The given database does not exist; Vitess cannot alter it.") VT05003 = errorWithState("VT05003", vtrpcpb.Code_NOT_FOUND, BadDb, "unknown database '%s' in vschema", "The given database does not exist in the VSchema.") VT05004 = errorWithState("VT05004", vtrpcpb.Code_NOT_FOUND, UnknownTable, "table '%s' does not exist", "The given table is unknown.") VT05005 = errorWithState("VT05005", vtrpcpb.Code_NOT_FOUND, NoSuchTable, "table '%s' does not exist in keyspace '%s'", "The given table does not exist in this keyspace.") VT05006 = errorWithState("VT05006", vtrpcpb.Code_NOT_FOUND, UnknownSystemVariable, "unknown system variable '%s'", "The given system variable is unknown.") VT05007 = errorWithoutState("VT05007", vtrpcpb.Code_NOT_FOUND, "no table info", "Table information is not available.") VT06001 = errorWithState("VT06001", vtrpcpb.Code_ALREADY_EXISTS, DbCreateExists, "cannot create database '%s'; database exists", "The given database name already exists.") VT07001 = errorWithState("VT07001", vtrpcpb.Code_PERMISSION_DENIED, KillDeniedError, "%s", "Kill statement is not allowed. More in docs about how to enable it and its limitations.") VT09001 = errorWithState("VT09001", vtrpcpb.Code_FAILED_PRECONDITION, RequiresPrimaryKey, PrimaryVindexNotSet, "the table does not have a primary vindex, the operation is impossible.") VT09002 = errorWithState("VT09002", vtrpcpb.Code_FAILED_PRECONDITION, InnodbReadOnly, "%s statement with a replica target", "This type of DML statement is not allowed on a replica target.") VT09003 = errorWithoutState("VT09003", vtrpcpb.Code_FAILED_PRECONDITION, "INSERT query does not have primary vindex column '%v' in the column list", "A vindex column is mandatory for the insert, please provide one.") VT09004 = errorWithoutState("VT09004", vtrpcpb.Code_FAILED_PRECONDITION, "INSERT should contain column list or the table should have authoritative columns in vschema", "You need to provide the list of columns you want to insert, or provide a VSchema with authoritative columns. If schema tracking is disabled you can enable it to automatically have authoritative columns.") VT09005 = errorWithState("VT09005", vtrpcpb.Code_FAILED_PRECONDITION, NoDB, "no database selected: use keyspace<:shard><@type> or keyspace<[range]><@type> (<> are optional)", "A database must be selected.") VT09006 = errorWithoutState("VT09006", vtrpcpb.Code_FAILED_PRECONDITION, "%s VITESS_MIGRATION works only on primary tablet", "VITESS_MIGRATION commands work only on primary tablets, you must send such commands to a primary tablet.") VT09007 = errorWithoutState("VT09007", vtrpcpb.Code_FAILED_PRECONDITION, "%s VITESS_THROTTLED_APPS works only on primary tablet", "VITESS_THROTTLED_APPS commands work only on primary tablet, you must send such commands to a primary tablet.") VT09008 = errorWithoutState("VT09008", vtrpcpb.Code_FAILED_PRECONDITION, "vexplain queries/all will actually run queries", "vexplain queries/all will actually run queries. `/*vt+ EXECUTE_DML_QUERIES */` must be set to run DML queries in vtexplain. Example: `vexplain /*vt+ EXECUTE_DML_QUERIES */ queries delete from t1`") VT09009 = errorWithoutState("VT09009", vtrpcpb.Code_FAILED_PRECONDITION, "stream is supported only for primary tablet type, current type: %v", "Stream is only supported for primary tablets, please use a stream on those tablets.") VT09010 = errorWithoutState("VT09010", vtrpcpb.Code_FAILED_PRECONDITION, "SHOW VITESS_THROTTLER STATUS works only on primary tablet", "SHOW VITESS_THROTTLER STATUS works only on primary tablet.") VT09011 = errorWithState("VT09011", vtrpcpb.Code_FAILED_PRECONDITION, UnknownStmtHandler, "Unknown prepared statement handler (%s) given to %s", "The prepared statement is not available") VT09012 = errorWithoutState("VT09012", vtrpcpb.Code_FAILED_PRECONDITION, "%s statement with %s tablet not allowed", "This type of statement is not allowed on the given tablet.") VT09013 = errorWithoutState("VT09013",
VT03021 = errorWithoutState("VT03021", vtrpcpb.Code_INVALID_ARGUMENT, "ambiguous column reference: %v", "The given column is ambiguous. You can use a table qualifier to make it unambiguous.")
random_line_split
code.go
keyspace<:shard><@type> or keyspace<[range]><@type> (<> are optional)", "A database must be selected.") VT09006 = errorWithoutState("VT09006", vtrpcpb.Code_FAILED_PRECONDITION, "%s VITESS_MIGRATION works only on primary tablet", "VITESS_MIGRATION commands work only on primary tablets, you must send such commands to a primary tablet.") VT09007 = errorWithoutState("VT09007", vtrpcpb.Code_FAILED_PRECONDITION, "%s VITESS_THROTTLED_APPS works only on primary tablet", "VITESS_THROTTLED_APPS commands work only on primary tablet, you must send such commands to a primary tablet.") VT09008 = errorWithoutState("VT09008", vtrpcpb.Code_FAILED_PRECONDITION, "vexplain queries/all will actually run queries", "vexplain queries/all will actually run queries. `/*vt+ EXECUTE_DML_QUERIES */` must be set to run DML queries in vtexplain. Example: `vexplain /*vt+ EXECUTE_DML_QUERIES */ queries delete from t1`") VT09009 = errorWithoutState("VT09009", vtrpcpb.Code_FAILED_PRECONDITION, "stream is supported only for primary tablet type, current type: %v", "Stream is only supported for primary tablets, please use a stream on those tablets.") VT09010 = errorWithoutState("VT09010", vtrpcpb.Code_FAILED_PRECONDITION, "SHOW VITESS_THROTTLER STATUS works only on primary tablet", "SHOW VITESS_THROTTLER STATUS works only on primary tablet.") VT09011 = errorWithState("VT09011", vtrpcpb.Code_FAILED_PRECONDITION, UnknownStmtHandler, "Unknown prepared statement handler (%s) given to %s", "The prepared statement is not available") VT09012 = errorWithoutState("VT09012", vtrpcpb.Code_FAILED_PRECONDITION, "%s statement with %s tablet not allowed", "This type of statement is not allowed on the given tablet.") VT09013 = errorWithoutState("VT09013", vtrpcpb.Code_FAILED_PRECONDITION, "semi-sync plugins are not loaded", "Durability policy wants Vitess to use semi-sync, but the MySQL instances don't have the semi-sync plugin loaded.") VT09014 = errorWithoutState("VT09014", vtrpcpb.Code_FAILED_PRECONDITION, "vindex cannot be modified", "The vindex cannot be used as table in DML statement") VT09015 = errorWithoutState("VT09015", vtrpcpb.Code_FAILED_PRECONDITION, "schema tracking required", "This query cannot be planned without more information on the SQL schema. Please turn on schema tracking or add authoritative columns information to your VSchema.") VT09016 = errorWithState("VT09016", vtrpcpb.Code_FAILED_PRECONDITION, RowIsReferenced2, "Cannot delete or update a parent row: a foreign key constraint fails", "SET DEFAULT is not supported by InnoDB") VT10001 = errorWithoutState("VT10001", vtrpcpb.Code_ABORTED, "foreign key constraints are not allowed", "Foreign key constraints are not allowed, see https://vitess.io/blog/2021-06-15-online-ddl-why-no-fk/.") VT12001 = errorWithoutState("VT12001", vtrpcpb.Code_UNIMPLEMENTED, "unsupported: %s", "This statement is unsupported by Vitess. Please rewrite your query to use supported syntax.") VT12002 = errorWithoutState("VT12002", vtrpcpb.Code_UNIMPLEMENTED, "unsupported: cross-shard foreign keys", "Vitess does not support cross shard foreign keys.") VT12003 = errorWithoutState("VT12002", vtrpcpb.Code_UNIMPLEMENTED, "unsupported: foreign keys management at vitess", "Vitess does not support managing foreign keys tables.") // VT13001 General Error VT13001 = errorWithoutState("VT13001", vtrpcpb.Code_INTERNAL, "[BUG] %s", "This error should not happen and is a bug. Please file an issue on GitHub: https://github.com/vitessio/vitess/issues/new/choose.") VT13002 = errorWithoutState("VT13002", vtrpcpb.Code_INTERNAL, "unexpected AST struct for query: %s", "This error should not happen and is a bug. Please file an issue on GitHub: https://github.com/vitessio/vitess/issues/new/choose.") VT14001 = errorWithoutState("VT14001", vtrpcpb.Code_UNAVAILABLE, "connection error", "The connection failed.") VT14002 = errorWithoutState("VT14002", vtrpcpb.Code_UNAVAILABLE, "no available connection", "No available connection.") VT14003 = errorWithoutState("VT14003", vtrpcpb.Code_UNAVAILABLE, "no connection for tablet %v", "No connection for the given tablet.") VT14004 = errorWithoutState("VT14004", vtrpcpb.Code_UNAVAILABLE, "cannot find keyspace for: %s", "The specified keyspace could not be found.") VT14005 = errorWithoutState("VT14005", vtrpcpb.Code_UNAVAILABLE, "cannot lookup sidecar database for keyspace: %s", "Failed to read sidecar database identifier.") Errors = []func(args ...any) *VitessError{ VT03001, VT03002, VT03003, VT03004, VT03005, VT03006, VT03007, VT03008, VT03009, VT03010, VT03011, VT03012, VT03013, VT03014, VT03015, VT03016, VT03017, VT03018, VT03019, VT03020, VT03021, VT03022, VT03023, VT03024, VT03025, VT05001, VT05002, VT05003, VT05004, VT05005, VT05006, VT05007, VT06001, VT07001, VT09001, VT09002, VT09003, VT09004, VT09005, VT09006, VT09007, VT09008, VT09009, VT09010, VT09011, VT09012, VT09013, VT09014, VT09015, VT09016, VT10001, VT12001, VT12002, VT12003, VT13001, VT13002, VT14001, VT14002, VT14003, VT14004, VT14005, } ) type VitessError struct { Err error Description string ID string State State } func (o *VitessError) Error() string { return o.Err.Error() } func (o *VitessError) Cause() error { return o.Err } var _ error = (*VitessError)(nil) func errorWithoutState(id string, code vtrpcpb.Code, short, long string) func(args ...any) *VitessError
{ return func(args ...any) *VitessError { s := short if len(args) != 0 { s = fmt.Sprintf(s, args...) } return &VitessError{ Err: New(code, id+": "+s), Description: long, ID: id, } } }
identifier_body
code.go
.") VT09005 = errorWithState("VT09005", vtrpcpb.Code_FAILED_PRECONDITION, NoDB, "no database selected: use keyspace<:shard><@type> or keyspace<[range]><@type> (<> are optional)", "A database must be selected.") VT09006 = errorWithoutState("VT09006", vtrpcpb.Code_FAILED_PRECONDITION, "%s VITESS_MIGRATION works only on primary tablet", "VITESS_MIGRATION commands work only on primary tablets, you must send such commands to a primary tablet.") VT09007 = errorWithoutState("VT09007", vtrpcpb.Code_FAILED_PRECONDITION, "%s VITESS_THROTTLED_APPS works only on primary tablet", "VITESS_THROTTLED_APPS commands work only on primary tablet, you must send such commands to a primary tablet.") VT09008 = errorWithoutState("VT09008", vtrpcpb.Code_FAILED_PRECONDITION, "vexplain queries/all will actually run queries", "vexplain queries/all will actually run queries. `/*vt+ EXECUTE_DML_QUERIES */` must be set to run DML queries in vtexplain. Example: `vexplain /*vt+ EXECUTE_DML_QUERIES */ queries delete from t1`") VT09009 = errorWithoutState("VT09009", vtrpcpb.Code_FAILED_PRECONDITION, "stream is supported only for primary tablet type, current type: %v", "Stream is only supported for primary tablets, please use a stream on those tablets.") VT09010 = errorWithoutState("VT09010", vtrpcpb.Code_FAILED_PRECONDITION, "SHOW VITESS_THROTTLER STATUS works only on primary tablet", "SHOW VITESS_THROTTLER STATUS works only on primary tablet.") VT09011 = errorWithState("VT09011", vtrpcpb.Code_FAILED_PRECONDITION, UnknownStmtHandler, "Unknown prepared statement handler (%s) given to %s", "The prepared statement is not available") VT09012 = errorWithoutState("VT09012", vtrpcpb.Code_FAILED_PRECONDITION, "%s statement with %s tablet not allowed", "This type of statement is not allowed on the given tablet.") VT09013 = errorWithoutState("VT09013", vtrpcpb.Code_FAILED_PRECONDITION, "semi-sync plugins are not loaded", "Durability policy wants Vitess to use semi-sync, but the MySQL instances don't have the semi-sync plugin loaded.") VT09014 = errorWithoutState("VT09014", vtrpcpb.Code_FAILED_PRECONDITION, "vindex cannot be modified", "The vindex cannot be used as table in DML statement") VT09015 = errorWithoutState("VT09015", vtrpcpb.Code_FAILED_PRECONDITION, "schema tracking required", "This query cannot be planned without more information on the SQL schema. Please turn on schema tracking or add authoritative columns information to your VSchema.") VT09016 = errorWithState("VT09016", vtrpcpb.Code_FAILED_PRECONDITION, RowIsReferenced2, "Cannot delete or update a parent row: a foreign key constraint fails", "SET DEFAULT is not supported by InnoDB") VT10001 = errorWithoutState("VT10001", vtrpcpb.Code_ABORTED, "foreign key constraints are not allowed", "Foreign key constraints are not allowed, see https://vitess.io/blog/2021-06-15-online-ddl-why-no-fk/.") VT12001 = errorWithoutState("VT12001", vtrpcpb.Code_UNIMPLEMENTED, "unsupported: %s", "This statement is unsupported by Vitess. Please rewrite your query to use supported syntax.") VT12002 = errorWithoutState("VT12002", vtrpcpb.Code_UNIMPLEMENTED, "unsupported: cross-shard foreign keys", "Vitess does not support cross shard foreign keys.") VT12003 = errorWithoutState("VT12002", vtrpcpb.Code_UNIMPLEMENTED, "unsupported: foreign keys management at vitess", "Vitess does not support managing foreign keys tables.") // VT13001 General Error VT13001 = errorWithoutState("VT13001", vtrpcpb.Code_INTERNAL, "[BUG] %s", "This error should not happen and is a bug. Please file an issue on GitHub: https://github.com/vitessio/vitess/issues/new/choose.") VT13002 = errorWithoutState("VT13002", vtrpcpb.Code_INTERNAL, "unexpected AST struct for query: %s", "This error should not happen and is a bug. Please file an issue on GitHub: https://github.com/vitessio/vitess/issues/new/choose.") VT14001 = errorWithoutState("VT14001", vtrpcpb.Code_UNAVAILABLE, "connection error", "The connection failed.") VT14002 = errorWithoutState("VT14002", vtrpcpb.Code_UNAVAILABLE, "no available connection", "No available connection.") VT14003 = errorWithoutState("VT14003", vtrpcpb.Code_UNAVAILABLE, "no connection for tablet %v", "No connection for the given tablet.") VT14004 = errorWithoutState("VT14004", vtrpcpb.Code_UNAVAILABLE, "cannot find keyspace for: %s", "The specified keyspace could not be found.") VT14005 = errorWithoutState("VT14005", vtrpcpb.Code_UNAVAILABLE, "cannot lookup sidecar database for keyspace: %s", "Failed to read sidecar database identifier.") Errors = []func(args ...any) *VitessError{ VT03001, VT03002, VT03003, VT03004, VT03005, VT03006, VT03007, VT03008, VT03009, VT03010, VT03011, VT03012, VT03013, VT03014, VT03015, VT03016, VT03017, VT03018, VT03019, VT03020, VT03021, VT03022, VT03023, VT03024, VT03025, VT05001, VT05002, VT05003, VT05004, VT05005, VT05006, VT05007, VT06001, VT07001, VT09001, VT09002, VT09003, VT09004, VT09005, VT09006, VT09007, VT09008, VT09009, VT09010, VT09011, VT09012, VT09013, VT09014, VT09015, VT09016, VT10001, VT12001, VT12002, VT12003, VT13001, VT13002, VT14001, VT14002, VT14003, VT14004, VT14005, } ) type VitessError struct { Err error Description string ID string State State } func (o *VitessError) Error() string { return o.Err.Error() } func (o *VitessError) Cause() error { return o.Err } var _ error = (*VitessError)(nil) func errorWithoutState(id string, code vtrpcpb.Code, short, long string) func(args ...any) *VitessError { return func(args ...any) *VitessError { s := short if len(args) != 0
{ s = fmt.Sprintf(s, args...) }
conditional_block
code.go
index column '%v' in the column list", "A vindex column is mandatory for the insert, please provide one.") VT09004 = errorWithoutState("VT09004", vtrpcpb.Code_FAILED_PRECONDITION, "INSERT should contain column list or the table should have authoritative columns in vschema", "You need to provide the list of columns you want to insert, or provide a VSchema with authoritative columns. If schema tracking is disabled you can enable it to automatically have authoritative columns.") VT09005 = errorWithState("VT09005", vtrpcpb.Code_FAILED_PRECONDITION, NoDB, "no database selected: use keyspace<:shard><@type> or keyspace<[range]><@type> (<> are optional)", "A database must be selected.") VT09006 = errorWithoutState("VT09006", vtrpcpb.Code_FAILED_PRECONDITION, "%s VITESS_MIGRATION works only on primary tablet", "VITESS_MIGRATION commands work only on primary tablets, you must send such commands to a primary tablet.") VT09007 = errorWithoutState("VT09007", vtrpcpb.Code_FAILED_PRECONDITION, "%s VITESS_THROTTLED_APPS works only on primary tablet", "VITESS_THROTTLED_APPS commands work only on primary tablet, you must send such commands to a primary tablet.") VT09008 = errorWithoutState("VT09008", vtrpcpb.Code_FAILED_PRECONDITION, "vexplain queries/all will actually run queries", "vexplain queries/all will actually run queries. `/*vt+ EXECUTE_DML_QUERIES */` must be set to run DML queries in vtexplain. Example: `vexplain /*vt+ EXECUTE_DML_QUERIES */ queries delete from t1`") VT09009 = errorWithoutState("VT09009", vtrpcpb.Code_FAILED_PRECONDITION, "stream is supported only for primary tablet type, current type: %v", "Stream is only supported for primary tablets, please use a stream on those tablets.") VT09010 = errorWithoutState("VT09010", vtrpcpb.Code_FAILED_PRECONDITION, "SHOW VITESS_THROTTLER STATUS works only on primary tablet", "SHOW VITESS_THROTTLER STATUS works only on primary tablet.") VT09011 = errorWithState("VT09011", vtrpcpb.Code_FAILED_PRECONDITION, UnknownStmtHandler, "Unknown prepared statement handler (%s) given to %s", "The prepared statement is not available") VT09012 = errorWithoutState("VT09012", vtrpcpb.Code_FAILED_PRECONDITION, "%s statement with %s tablet not allowed", "This type of statement is not allowed on the given tablet.") VT09013 = errorWithoutState("VT09013", vtrpcpb.Code_FAILED_PRECONDITION, "semi-sync plugins are not loaded", "Durability policy wants Vitess to use semi-sync, but the MySQL instances don't have the semi-sync plugin loaded.") VT09014 = errorWithoutState("VT09014", vtrpcpb.Code_FAILED_PRECONDITION, "vindex cannot be modified", "The vindex cannot be used as table in DML statement") VT09015 = errorWithoutState("VT09015", vtrpcpb.Code_FAILED_PRECONDITION, "schema tracking required", "This query cannot be planned without more information on the SQL schema. Please turn on schema tracking or add authoritative columns information to your VSchema.") VT09016 = errorWithState("VT09016", vtrpcpb.Code_FAILED_PRECONDITION, RowIsReferenced2, "Cannot delete or update a parent row: a foreign key constraint fails", "SET DEFAULT is not supported by InnoDB") VT10001 = errorWithoutState("VT10001", vtrpcpb.Code_ABORTED, "foreign key constraints are not allowed", "Foreign key constraints are not allowed, see https://vitess.io/blog/2021-06-15-online-ddl-why-no-fk/.") VT12001 = errorWithoutState("VT12001", vtrpcpb.Code_UNIMPLEMENTED, "unsupported: %s", "This statement is unsupported by Vitess. Please rewrite your query to use supported syntax.") VT12002 = errorWithoutState("VT12002", vtrpcpb.Code_UNIMPLEMENTED, "unsupported: cross-shard foreign keys", "Vitess does not support cross shard foreign keys.") VT12003 = errorWithoutState("VT12002", vtrpcpb.Code_UNIMPLEMENTED, "unsupported: foreign keys management at vitess", "Vitess does not support managing foreign keys tables.") // VT13001 General Error VT13001 = errorWithoutState("VT13001", vtrpcpb.Code_INTERNAL, "[BUG] %s", "This error should not happen and is a bug. Please file an issue on GitHub: https://github.com/vitessio/vitess/issues/new/choose.") VT13002 = errorWithoutState("VT13002", vtrpcpb.Code_INTERNAL, "unexpected AST struct for query: %s", "This error should not happen and is a bug. Please file an issue on GitHub: https://github.com/vitessio/vitess/issues/new/choose.") VT14001 = errorWithoutState("VT14001", vtrpcpb.Code_UNAVAILABLE, "connection error", "The connection failed.") VT14002 = errorWithoutState("VT14002", vtrpcpb.Code_UNAVAILABLE, "no available connection", "No available connection.") VT14003 = errorWithoutState("VT14003", vtrpcpb.Code_UNAVAILABLE, "no connection for tablet %v", "No connection for the given tablet.") VT14004 = errorWithoutState("VT14004", vtrpcpb.Code_UNAVAILABLE, "cannot find keyspace for: %s", "The specified keyspace could not be found.") VT14005 = errorWithoutState("VT14005", vtrpcpb.Code_UNAVAILABLE, "cannot lookup sidecar database for keyspace: %s", "Failed to read sidecar database identifier.") Errors = []func(args ...any) *VitessError{ VT03001, VT03002, VT03003, VT03004, VT03005, VT03006, VT03007, VT03008, VT03009, VT03010, VT03011, VT03012, VT03013, VT03014, VT03015, VT03016, VT03017, VT03018, VT03019, VT03020, VT03021, VT03022, VT03023, VT03024, VT03025, VT05001, VT05002, VT05003, VT05004, VT05005, VT05006, VT05007, VT06001, VT07001, VT09001, VT09002, VT09003, VT09004, VT09005, VT09006, VT09007, VT09008, VT09009, VT09010, VT09011, VT09012, VT09013, VT09014, VT09015, VT09016, VT10001, VT12001, VT12002, VT12003, VT13001, VT13002, VT14001, VT14002, VT14003, VT14004, VT14005, } ) type VitessError struct { Err error Description string ID string State State } func (o *VitessError)
Error
identifier_name