1
1
use std:: fmt:: Write ;
2
2
use std:: path:: Path ;
3
3
4
- use chrono:: { DateTime , Local } ;
4
+ use chrono:: { DateTime , Local , TimeZone } ;
5
5
use plotters:: prelude:: * ;
6
6
use tracing:: trace;
7
7
@@ -23,10 +23,6 @@ pub fn draw_checks(checks: &[Check], file: impl AsRef<Path>) -> Result<(), Analy
23
23
times. sort ( ) ;
24
24
let timespan =
25
25
times. first ( ) . unwrap ( ) [ 0 ] . timestamp_parsed ( ) ..times. last ( ) . unwrap ( ) [ 0 ] . timestamp_parsed ( ) ;
26
- let mut x_axis: Vec < _ > = Vec :: new ( ) ;
27
- for t in 0 ..time_grouped. len ( ) {
28
- x_axis. push ( t) ;
29
- }
30
26
31
27
for group in time_grouped. values ( ) {
32
28
data. push ( (
@@ -36,6 +32,13 @@ pub fn draw_checks(checks: &[Check], file: impl AsRef<Path>) -> Result<(), Analy
36
32
}
37
33
data. sort_by_key ( |a| a. 0 ) ;
38
34
35
+ let cpt = super :: checks_per_time_group ( checks. iter ( ) ) ;
36
+ let mut checks_per_time: Vec < ( DateTime < Local > , usize ) > = cpt
37
+ . iter ( )
38
+ . map ( |( k, v) | ( Local . timestamp_opt ( * k, 0 ) . unwrap ( ) , * v) )
39
+ . collect ( ) ;
40
+ checks_per_time. sort_by_key ( |a| a. 0 ) ;
41
+
39
42
let root = BitMapBackend :: new ( outfile, ( 1920 , 1080 ) ) . into_drawing_area ( ) ;
40
43
root. fill ( & WHITE ) . map_err ( |e| AnalysisError :: GraphDraw {
41
44
reason : e. to_string ( ) ,
@@ -45,11 +48,16 @@ pub fn draw_checks(checks: &[Check], file: impl AsRef<Path>) -> Result<(), Analy
45
48
. margin ( 10 )
46
49
. caption ( "Outage Severity over all time" , ( "sans-serif" , 60 ) )
47
50
. set_label_area_size ( LabelAreaPosition :: Left , 60 )
48
- . set_label_area_size ( LabelAreaPosition :: Bottom , 30 )
49
- . build_cartesian_2d ( timespan, 0.0 ..1.0 )
51
+ . set_label_area_size ( LabelAreaPosition :: Right , 60 )
52
+ . set_label_area_size ( LabelAreaPosition :: Bottom , 60 )
53
+ . build_cartesian_2d ( timespan. clone ( ) , 0.0 ..1.0 )
50
54
. map_err ( |e| AnalysisError :: GraphDraw {
51
55
reason : e. to_string ( ) ,
52
- } ) ?;
56
+ } ) ?
57
+ . set_secondary_coord (
58
+ timespan,
59
+ 0f64 ..checks_per_time. last ( ) . map ( |a| a. 1 as f64 ) . unwrap ( ) ,
60
+ ) ;
53
61
54
62
chart
55
63
. configure_mesh ( )
@@ -61,17 +69,24 @@ pub fn draw_checks(checks: &[Check], file: impl AsRef<Path>) -> Result<(), Analy
61
69
. map_err ( |e| AnalysisError :: GraphDraw {
62
70
reason : e. to_string ( ) ,
63
71
} ) ?;
72
+ chart
73
+ . configure_secondary_axes ( )
74
+ . y_desc ( "Amount of Checks" )
75
+ . draw ( )
76
+ . map_err ( |e| AnalysisError :: GraphDraw {
77
+ reason : e. to_string ( ) ,
78
+ } ) ?;
79
+
80
+ let processed_serevity_data = data. iter ( ) . map ( |( a, b) | ( * a, f64:: from ( * b) ) ) ;
81
+ chart
82
+ . draw_series ( AreaSeries :: new ( processed_serevity_data, 0.0 , RED . mix ( 0.2 ) ) . border_style ( RED ) )
83
+ . map_err ( |e| AnalysisError :: GraphDraw {
84
+ reason : e. to_string ( ) ,
85
+ } ) ?;
64
86
65
- let processed_data = data. iter ( ) . map ( |( a, b) | ( * a, f64:: from ( * b) ) ) ;
66
- trace ! ( "dumping whole processed data: \n {}" , {
67
- let mut buf = String :: new( ) ;
68
- for ( idx, row) in processed_data. clone( ) . enumerate( ) {
69
- writeln!( buf, "{:08},{},{:.06}" , idx, row. 0 , row. 1 ) . unwrap( ) ;
70
- }
71
- buf
72
- } ) ;
87
+ let checks_per_time_f64 = checks_per_time. into_iter ( ) . map ( |( t, v) | ( t, v as f64 ) ) ;
73
88
chart
74
- . draw_series ( AreaSeries :: new ( processed_data , 0.0 , RED . mix ( 0.2 ) ) . border_style ( RED ) )
89
+ . draw_series ( AreaSeries :: new ( checks_per_time_f64 , 0.0 , BLUE . mix ( 0.2 ) ) )
75
90
. map_err ( |e| AnalysisError :: GraphDraw {
76
91
reason : e. to_string ( ) ,
77
92
} ) ?;
0 commit comments