Skip to content

Commit

Permalink
Bug/time panic (#1262)
Browse files Browse the repository at this point in the history
* Removed unwraps that were throwing an error

* Removed unwraps and changed at semantics

* missed some brackets

* fmt

* Start can't be greater than end - fixed vertex

* F

* Found an issue with expanding

* Fixed tests
  • Loading branch information
miratepuffin authored Sep 14, 2023
1 parent bcd0354 commit 8345032
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 30 deletions.
28 changes: 17 additions & 11 deletions raphtory/src/db/api/view/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,13 @@ pub trait TimeOps {

/// Create a view including all events until `end` (inclusive)
fn at<T: IntoTime>(&self, end: T) -> Self::WindowedViewType {
self.window(i64::MIN, end.into_time().saturating_add(1))
let end = end.into_time();
let start = self.start().unwrap_or(end);
if start > end {
self.window(end, end.saturating_add(1))
} else {
self.window(start, end.saturating_add(1))
}
}

/// Creates a `WindowSet` with the given `step` size
Expand Down Expand Up @@ -146,7 +152,10 @@ impl<T: TimeOps + Clone> Iterator for WindowSet<T> {
fn next(&mut self) -> Option<Self::Item> {
if self.cursor < self.end + self.step {
let window_end = self.cursor;
let window_start = self.window.map(|w| window_end - w).unwrap_or(i64::MIN);
let window_start = self
.window
.map(|w| window_end - w)
.unwrap_or(self.view.start().unwrap_or(window_end));
let window = self.view.window(window_start, window_end);
self.cursor = self.cursor + self.step;
Some(window)
Expand Down Expand Up @@ -210,15 +219,14 @@ mod time_tests {

#[test]
fn expanding() {
let min = i64::MIN;
let g = graph_with_timeline(1, 7);
let windows = g.expanding(2).unwrap();
let expected = vec![(min, 3), (min, 5), (min, 7)];
let expected = vec![(1, 3), (1, 5), (1, 7)];
assert_bounds(windows, expected);

let g = graph_with_timeline(1, 6);
let windows = g.expanding(2).unwrap();
let expected = vec![(min, 3), (min, 5), (min, 7)];
let expected = vec![(1, 3), (1, 5), (1, 7)];
assert_bounds(windows, expected.clone());

let g = graph_with_timeline(0, 9).window(1, 6);
Expand Down Expand Up @@ -280,15 +288,13 @@ mod time_tests {

#[test]
fn expanding_dates() {
let min = i64::MIN;

let start = "2020-06-06 00:00:00".try_into_time().unwrap();
let end = "2020-06-07 23:59:59.999".try_into_time().unwrap();
let g = graph_with_timeline(start, end);
let windows = g.expanding("1 day").unwrap();
let expected = vec![
(min, "2020-06-07 00:00:00".try_into_time().unwrap()),
(min, "2020-06-08 00:00:00".try_into_time().unwrap()),
(start, "2020-06-07 00:00:00".try_into_time().unwrap()),
(start, "2020-06-08 00:00:00".try_into_time().unwrap()),
];
assert_bounds(windows, expected);

Expand All @@ -297,8 +303,8 @@ mod time_tests {
let g = graph_with_timeline(start, end);
let windows = g.expanding("1 day").unwrap();
let expected = vec![
(min, "2020-06-07 00:00:00".try_into_time().unwrap()),
(min, "2020-06-08 00:00:00".try_into_time().unwrap()),
(start, "2020-06-07 00:00:00".try_into_time().unwrap()),
(start, "2020-06-08 00:00:00".try_into_time().unwrap()),
];
assert_bounds(windows, expected);
}
Expand Down
4 changes: 1 addition & 3 deletions raphtory/src/db/api/view/vertex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,7 @@ pub trait VertexListOps:
) -> Self::IterType<<Self::Vertex as TimeOps>::WindowedViewType>;

/// Create views for the vertices including all events until `end` (inclusive)
fn at(self, end: i64) -> Self::IterType<<Self::Vertex as TimeOps>::WindowedViewType> {
self.window(i64::MIN, end.saturating_add(1))
}
fn at(self, end: i64) -> Self::IterType<<Self::Vertex as TimeOps>::WindowedViewType>;

/// Returns the ids of vertices in the list.
///
Expand Down
8 changes: 8 additions & 0 deletions raphtory/src/db/graph/vertex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,10 @@ impl<G: GraphViewOps> VertexListOps for Box<dyn Iterator<Item = VertexView<G>> +
Box::new(self.map(move |v| v.window(t_start, t_end)))
}

fn at(self, end: i64) -> Self::IterType<<Self::Vertex as TimeOps>::WindowedViewType> {
Box::new(self.map(move |v| v.at(end)))
}

fn id(self) -> BoxedIter<u64> {
Box::new(self.map(|v| v.id()))
}
Expand Down Expand Up @@ -420,6 +424,10 @@ impl<G: GraphViewOps> VertexListOps for BoxedIter<BoxedIter<VertexView<G>>> {
Box::new(self.map(move |it| it.window(t_start, t_end)))
}

fn at(self, end: i64) -> Self::IterType<<Self::Vertex as TimeOps>::WindowedViewType> {
Box::new(self.map(move |v| v.at(end)))
}

fn id(self) -> BoxedIter<Self::ValueType<u64>> {
Box::new(self.map(|it| it.id()))
}
Expand Down
5 changes: 2 additions & 3 deletions raphtory/src/db/graph/views/window_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,8 @@ use crate::{
},
view::{
internal::{
Base, DelegateCoreOps, DynamicGraph, EdgeFilter, EdgeFilterOps, GraphOps,
Immutable, InheritCoreOps, InheritLayerOps, InheritMaterialize, IntoDynamic,
TimeSemantics,
Base, DynamicGraph, EdgeFilter, EdgeFilterOps, GraphOps, Immutable, InheritCoreOps,
InheritLayerOps, InheritMaterialize, IntoDynamic, TimeSemantics,
},
BoxedIter,
},
Expand Down
4 changes: 4 additions & 0 deletions raphtory/src/db/task/vertex/eval_vertex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,10 @@ impl<'a, G: GraphViewOps, CS: ComputeState, S: 'static> VertexListOps
Box::new(self.map(move |v| v.window(t_start, t_end)))
}

fn at(self, end: i64) -> Self::IterType<<Self::Vertex as TimeOps>::WindowedViewType> {
Box::new(self.map(move |v| v.at(end)))
}

fn id(self) -> Self::IterType<u64> {
Box::new(self.map(|v| v.id()))
}
Expand Down
4 changes: 4 additions & 0 deletions raphtory/src/db/task/vertex/window_eval_vertex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,10 @@ impl<'a, G: GraphViewOps, CS: ComputeState, S: 'static> VertexListOps
Box::new(self.map(move |v| v.window(t_start, t_end)))
}

fn at(self, end: i64) -> Self::IterType<<Self::Vertex as TimeOps>::WindowedViewType> {
Box::new(self.map(move |v| v.at(end)))
}

fn id(self) -> Self::IterType<u64> {
Box::new(self.map(|v| v.id()))
}
Expand Down
10 changes: 5 additions & 5 deletions raphtory/src/python/graph/edge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ impl PyEdge {
/// the start datetime of the Edge.
pub fn start_date_time(&self) -> Option<NaiveDateTime> {
let start_time = self.edge.start()?;
Some(NaiveDateTime::from_timestamp_millis(start_time).unwrap())
NaiveDateTime::from_timestamp_millis(start_time)
}

/// Get the end time of the Edge.
Expand All @@ -215,7 +215,7 @@ impl PyEdge {
/// The end datetime of the Edge
pub fn end_date_time(&self) -> Option<NaiveDateTime> {
let end_time = self.edge.end()?;
Some(NaiveDateTime::from_timestamp_millis(end_time).unwrap())
NaiveDateTime::from_timestamp_millis(end_time)
}

/// Get the duration of the Edge.
Expand Down Expand Up @@ -337,7 +337,7 @@ impl PyEdge {
/// Returns:
/// the earliest datetime of an edge
pub fn earliest_date_time(&self) -> Option<NaiveDateTime> {
Some(NaiveDateTime::from_timestamp_millis(self.edge.earliest_time()?).unwrap())
NaiveDateTime::from_timestamp_millis(self.edge.earliest_time()?)
}

/// Gets the latest time of an edge.
Expand All @@ -354,7 +354,7 @@ impl PyEdge {
/// the latest datetime of an edge
pub fn latest_date_time(&self) -> Option<NaiveDateTime> {
let latest_time = self.edge.latest_time()?;
Some(NaiveDateTime::from_timestamp_millis(latest_time).unwrap())
NaiveDateTime::from_timestamp_millis(latest_time)
}

/// Gets the time of an exploded edge.
Expand Down Expand Up @@ -387,7 +387,7 @@ impl PyEdge {
/// (datetime) the datetime of an exploded edge
pub fn date_time(&self) -> Option<NaiveDateTime> {
let date_time = self.edge.time()?;
Some(NaiveDateTime::from_timestamp_millis(date_time).unwrap())
NaiveDateTime::from_timestamp_millis(date_time)
}

/// Displays the Edge as a string.
Expand Down
8 changes: 4 additions & 4 deletions raphtory/src/python/graph/vertex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ impl PyVertex {
/// The earliest datetime that the vertex exists as an integer.
pub fn earliest_date_time(&self) -> Option<NaiveDateTime> {
let earliest_time = self.vertex.earliest_time()?;
Some(NaiveDateTime::from_timestamp_millis(earliest_time).unwrap())
NaiveDateTime::from_timestamp_millis(earliest_time)
}

/// Returns the latest time that the vertex exists.
Expand All @@ -165,7 +165,7 @@ impl PyVertex {
/// The latest datetime that the vertex exists as an integer.
pub fn latest_date_time(&self) -> Option<NaiveDateTime> {
let latest_time = self.vertex.latest_time()?;
Some(NaiveDateTime::from_timestamp_millis(latest_time).unwrap())
NaiveDateTime::from_timestamp_millis(latest_time)
}

/// The properties of the vertex
Expand Down Expand Up @@ -266,7 +266,7 @@ impl PyVertex {
/// The earliest datetime that this vertex is valid or None if the vertex is valid for all times.
pub fn start_date_time(&self) -> Option<NaiveDateTime> {
let start_time = self.vertex.start()?;
Some(NaiveDateTime::from_timestamp_millis(start_time).unwrap())
NaiveDateTime::from_timestamp_millis(start_time)
}

/// Gets the latest time that this vertex is valid.
Expand All @@ -283,7 +283,7 @@ impl PyVertex {
/// The latest datetime that this vertex is valid or None if the vertex is valid for all times.
pub fn end_date_time(&self) -> Option<NaiveDateTime> {
let end_time = self.vertex.end()?;
Some(NaiveDateTime::from_timestamp_millis(end_time).unwrap())
NaiveDateTime::from_timestamp_millis(end_time)
}

/// Creates a `PyVertexWindowSet` with the given `step` size and optional `start` and `end` times,
Expand Down
8 changes: 4 additions & 4 deletions raphtory/src/python/graph/views/graph_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ impl PyGraphView {
/// the datetime of the earliest activity in the graph
pub fn earliest_date_time(&self) -> Option<NaiveDateTime> {
let earliest_time = self.graph.earliest_time()?;
Some(NaiveDateTime::from_timestamp_millis(earliest_time).unwrap())
NaiveDateTime::from_timestamp_millis(earliest_time)
}

/// Timestamp of latest activity in the graph
Expand All @@ -139,7 +139,7 @@ impl PyGraphView {
/// the datetime of the latest activity in the graph
pub fn latest_date_time(&self) -> Option<NaiveDateTime> {
let latest_time = self.graph.latest_time()?;
Some(NaiveDateTime::from_timestamp_millis(latest_time).unwrap())
NaiveDateTime::from_timestamp_millis(latest_time)
}

/// Number of edges in the graph
Expand Down Expand Up @@ -252,7 +252,7 @@ impl PyGraphView {
/// the default start datetime for perspectives over the view
pub fn start_date_time(&self) -> Option<NaiveDateTime> {
let start_time = self.graph.start()?;
Some(NaiveDateTime::from_timestamp_millis(start_time).unwrap())
NaiveDateTime::from_timestamp_millis(start_time)
}

/// Returns the default end time for perspectives over the view
Expand All @@ -274,7 +274,7 @@ impl PyGraphView {
/// the default end datetime for perspectives over the view
pub fn end_date_time(&self) -> Option<NaiveDateTime> {
let end_time = self.graph.end()?;
Some(NaiveDateTime::from_timestamp_millis(end_time).unwrap())
NaiveDateTime::from_timestamp_millis(end_time)
}

/// Creates a `WindowSet` with the given `step` size and optional `start` and `end` times,
Expand Down

0 comments on commit 8345032

Please sign in to comment.