@@ -30,7 +30,7 @@ use crate::{
3030use super :: TimeFilter ;
3131
3232pub static DASHBOARDS : Lazy < Dashboards > = Lazy :: new ( Dashboards :: default) ;
33- pub const CURRENT_DASHBOARD_VERSION : & str = "v2 " ;
33+ pub const CURRENT_DASHBOARD_VERSION : & str = "v3 " ;
3434
3535#[ derive( Debug , Serialize , Deserialize , Default , Clone ) ]
3636pub struct Tiles {
@@ -62,6 +62,25 @@ pub struct CircularChartConfig {
6262pub struct GraphConfig {
6363 x_key : String ,
6464 y_keys : Vec < String > ,
65+ graph_type : Option < GraphType > ,
66+ orientation : Option < Orientation > ,
67+ }
68+
69+ #[ derive( Debug , Serialize , Deserialize , Default , Clone ) ]
70+ #[ serde( rename_all = "lowercase" ) ]
71+ pub enum GraphType {
72+ #[ default]
73+ Default ,
74+ Stacked ,
75+ Percent ,
76+ }
77+
78+ #[ derive( Debug , Serialize , Deserialize , Default , Clone ) ]
79+ #[ serde( rename_all = "lowercase" ) ]
80+ pub enum Orientation {
81+ #[ default]
82+ Horizontal ,
83+ Vertical ,
6584}
6685
6786#[ derive( Debug , Serialize , Deserialize , Default , Clone ) ]
@@ -96,32 +115,62 @@ impl Dashboards {
96115 let mut this = vec ! [ ] ;
97116 let store = CONFIG . storage ( ) . get_object_store ( ) ;
98117 let dashboards = store. get_all_dashboards ( ) . await . unwrap_or_default ( ) ;
99-
100118 for dashboard in dashboards {
101119 if dashboard. is_empty ( ) {
102120 continue ;
103121 }
104122 let mut dashboard_value = serde_json:: from_slice :: < serde_json:: Value > ( & dashboard) ?;
105123 if let Some ( meta) = dashboard_value. clone ( ) . as_object ( ) {
106124 let version = meta. get ( "version" ) . and_then ( |version| version. as_str ( ) ) ;
107- let user_id = meta. get ( "user_id" ) . and_then ( |user_id| user_id. as_str ( ) ) ;
108125 let dashboard_id = meta
109126 . get ( "dashboard_id" )
110127 . and_then ( |dashboard_id| dashboard_id. as_str ( ) ) ;
111-
112- if version == Some ( "v1" ) {
113- dashboard_value = migrate_v1_v2 ( dashboard_value) ;
114- if let ( Some ( user_id) , Some ( dashboard_id) ) = ( user_id, dashboard_id) {
115- let path = dashboard_path ( user_id, & format ! ( "{}.json" , dashboard_id) ) ;
128+ match version {
129+ Some ( "v1" ) => {
130+ dashboard_value = migrate_v1_v2 ( dashboard_value) ;
131+ dashboard_value = migrate_v2_v3 ( dashboard_value) ;
132+ let user_id = dashboard_value
133+ . as_object ( )
134+ . unwrap ( )
135+ . get ( "user_id" )
136+ . and_then ( |user_id| user_id. as_str ( ) ) ;
137+ let path = dashboard_path (
138+ user_id. unwrap ( ) ,
139+ & format ! ( "{}.json" , dashboard_id. unwrap( ) ) ,
140+ ) ;
116141 let dashboard_bytes = to_bytes ( & dashboard_value) ;
117142 store. put_object ( & path, dashboard_bytes. clone ( ) ) . await ?;
118143 if let Ok ( dashboard) = serde_json:: from_slice :: < Dashboard > ( & dashboard_bytes)
119144 {
145+ this. retain ( |d : & Dashboard | d. dashboard_id != dashboard. dashboard_id ) ;
146+ this. push ( dashboard) ;
147+ }
148+ }
149+ Some ( "v2" ) => {
150+ dashboard_value = migrate_v2_v3 ( dashboard_value) ;
151+ let user_id = dashboard_value
152+ . as_object ( )
153+ . unwrap ( )
154+ . get ( "user_id" )
155+ . and_then ( |user_id| user_id. as_str ( ) ) ;
156+ let path = dashboard_path (
157+ user_id. unwrap ( ) ,
158+ & format ! ( "{}.json" , dashboard_id. unwrap( ) ) ,
159+ ) ;
160+ let dashboard_bytes = to_bytes ( & dashboard_value) ;
161+ store. put_object ( & path, dashboard_bytes. clone ( ) ) . await ?;
162+ if let Ok ( dashboard) = serde_json:: from_slice :: < Dashboard > ( & dashboard_bytes)
163+ {
164+ this. retain ( |d| d. dashboard_id != dashboard. dashboard_id ) ;
165+ this. push ( dashboard) ;
166+ }
167+ }
168+ _ => {
169+ if let Ok ( dashboard) = serde_json:: from_slice :: < Dashboard > ( & dashboard) {
170+ this. retain ( |d| d. dashboard_id != dashboard. dashboard_id ) ;
120171 this. push ( dashboard) ;
121172 }
122173 }
123- } else if let Ok ( dashboard) = serde_json:: from_slice :: < Dashboard > ( & dashboard) {
124- this. push ( dashboard) ;
125174 }
126175 }
127176 }
@@ -176,6 +225,55 @@ fn migrate_v1_v2(mut dashboard_meta: Value) -> Value {
176225 "version" . to_owned ( ) ,
177226 Value :: String ( CURRENT_DASHBOARD_VERSION . into ( ) ) ,
178227 ) ;
228+ let tiles = dashboard_meta_map
229+ . get_mut ( "tiles" )
230+ . unwrap ( )
231+ . as_array_mut ( )
232+ . unwrap ( ) ;
233+ for tile in tiles. iter_mut ( ) {
234+ let tile_map = tile. as_object_mut ( ) . unwrap ( ) ;
235+ let visualization = tile_map
236+ . get_mut ( "visualization" )
237+ . unwrap ( )
238+ . as_object_mut ( )
239+ . unwrap ( ) ;
240+ visualization. insert ( "tick_config" . to_owned ( ) , Value :: Array ( vec ! [ ] ) ) ;
241+ }
242+
243+ dashboard_meta
244+ }
245+
246+ fn migrate_v2_v3 ( mut dashboard_meta : Value ) -> Value {
247+ let dashboard_meta_map = dashboard_meta. as_object_mut ( ) . unwrap ( ) ;
248+
249+ dashboard_meta_map. insert (
250+ "version" . to_owned ( ) ,
251+ Value :: String ( CURRENT_DASHBOARD_VERSION . into ( ) ) ,
252+ ) ;
253+ let tiles = dashboard_meta_map
254+ . get_mut ( "tiles" )
255+ . unwrap ( )
256+ . as_array_mut ( )
257+ . unwrap ( ) ;
258+ for tile in tiles {
259+ let tile_map = tile. as_object_mut ( ) . unwrap ( ) ;
260+ let visualization = tile_map
261+ . get_mut ( "visualization" )
262+ . unwrap ( )
263+ . as_object_mut ( )
264+ . unwrap ( ) ;
265+ if visualization. get ( "graph_config" ) . is_some ( )
266+ && !visualization. get ( "graph_config" ) . unwrap ( ) . is_null ( )
267+ {
268+ let graph_config = visualization
269+ . get_mut ( "graph_config" )
270+ . unwrap ( )
271+ . as_object_mut ( )
272+ . unwrap ( ) ;
273+ graph_config. insert ( "orientation" . to_owned ( ) , Value :: String ( "horizontal" . into ( ) ) ) ;
274+ graph_config. insert ( "graph_type" . to_owned ( ) , Value :: String ( "default" . into ( ) ) ) ;
275+ }
276+ }
179277
180278 dashboard_meta
181279}
0 commit comments