@@ -3,6 +3,7 @@ use std::fmt::{Debug, Display};
33
44use crate :: app:: DuckDBConn ;
55use crate :: utils:: validate;
6+ use crate :: web:: routes:: dashboard:: GraphValue ;
67use duckdb:: params;
78use eyre:: Result ;
89use itertools:: Itertools ;
@@ -24,11 +25,11 @@ pub struct DateRange {
2425
2526impl DateRange {
2627 pub fn start ( & self ) -> OffsetDateTime {
27- OffsetDateTime :: from_unix_timestamp ( self . start as i64 ) . unwrap_or ( OffsetDateTime :: UNIX_EPOCH )
28+ OffsetDateTime :: from_unix_timestamp ( ( self . start / 1000 ) as i64 ) . unwrap_or ( OffsetDateTime :: UNIX_EPOCH )
2829 }
2930
3031 pub fn end ( & self ) -> OffsetDateTime {
31- OffsetDateTime :: from_unix_timestamp ( self . end as i64 ) . unwrap_or ( OffsetDateTime :: UNIX_EPOCH )
32+ OffsetDateTime :: from_unix_timestamp ( ( self . end / 1000 ) as i64 ) . unwrap_or ( OffsetDateTime :: UNIX_EPOCH )
3233 }
3334
3435 pub fn prev ( & self ) -> DateRange {
@@ -77,7 +78,7 @@ pub enum FilterType {
7778 IsNull ,
7879}
7980
80- pub type ReportGraph = Vec < u64 > ;
81+ pub type ReportGraph = Vec < GraphValue > ;
8182pub type ReportTable = BTreeMap < String , u64 > ;
8283
8384#[ derive( Object , Clone , Debug , Default ) ]
@@ -86,7 +87,7 @@ pub struct ReportStats {
8687 pub total_views : u64 ,
8788 pub total_sessions : u64 ,
8889 pub unique_visitors : u64 ,
89- pub avg_views_per_session : u64 , // 3 decimal places
90+ pub avg_views_per_session : f64 ,
9091}
9192
9293#[ derive( Object , Debug ) ]
@@ -152,7 +153,7 @@ pub fn overall_report(
152153 metric : & Metric ,
153154) -> Result < ReportGraph > {
154155 if entities. is_empty ( ) {
155- return Ok ( vec ! [ 0 ; data_points as usize ] ) ;
156+ return Ok ( vec ! [ GraphValue :: U64 ( 0 ) ; data_points as usize ] ) ;
156157 }
157158
158159 // recheck the validity of the entity IDs to be super sure there's no SQL injection
@@ -217,14 +218,14 @@ pub fn overall_report(
217218
218219 match metric {
219220 Metric :: Views | Metric :: UniqueVisitors | Metric :: Sessions => {
220- let rows = stmt. query_map ( duckdb:: params_from_iter ( params) , |row| row. get ( 1 ) ) ?;
221- let report_graph = rows. collect :: < Result < Vec < u64 > , duckdb:: Error > > ( ) ?;
221+ let rows = stmt. query_map ( duckdb:: params_from_iter ( params) , |row| Ok ( GraphValue :: U64 ( row. get ( 1 ) ? ) ) ) ?;
222+ let report_graph = rows. collect :: < Result < Vec < GraphValue > , duckdb:: Error > > ( ) ?;
222223 Ok ( report_graph)
223224 }
224225 Metric :: AvgViewsPerSession => {
225- let rows = stmt. query_map ( duckdb:: params_from_iter ( params) , |row| row. get ( 1 ) ) ?;
226- let report_graph = rows. collect :: < Result < Vec < f64 > , duckdb:: Error > > ( ) ?;
227- Ok ( report_graph. iter ( ) . map ( |v| ( v * 1000.0 ) . round ( ) as u64 ) . collect ( ) )
226+ let rows = stmt. query_map ( duckdb:: params_from_iter ( params) , |row| Ok ( GraphValue :: F64 ( row. get ( 1 ) ? ) ) ) ?;
227+ let report_graph = rows. collect :: < Result < Vec < GraphValue > , duckdb:: Error > > ( ) ?;
228+ Ok ( report_graph)
228229 }
229230 }
230231}
@@ -295,7 +296,7 @@ pub fn overall_stats(
295296 total_views : row. get ( 0 ) ?,
296297 total_sessions : row. get ( 1 ) ?,
297298 unique_visitors : row. get ( 2 ) ?,
298- avg_views_per_session : ( row. get :: < _ , Option < f64 > > ( 3 ) ?. unwrap_or ( 0.0 ) * 1000.0 ) . round ( ) as u64 ,
299+ avg_views_per_session : row. get :: < _ , Option < f64 > > ( 3 ) ?. unwrap_or ( 0.0 ) ,
299300 } )
300301 } ) ?;
301302
0 commit comments