@@ -53,7 +53,7 @@ pub struct Dashboard {
53
53
pub dashboard_id : Option < Ulid > ,
54
54
pub modified : Option < DateTime < Utc > > ,
55
55
dashboard_type : Option < DashboardType > ,
56
- pub tiles : Vec < Tile > ,
56
+ pub tiles : Option < Vec < Tile > > ,
57
57
}
58
58
59
59
#[ derive( Default , Debug ) ]
@@ -88,25 +88,22 @@ impl Dashboards {
88
88
user_id : & str ,
89
89
dashboard : & mut Dashboard ,
90
90
) -> Result < ( ) , DashboardError > {
91
- let mut s = self . 0 . write ( ) . await ;
92
91
let dashboard_id = Ulid :: new ( ) ;
93
92
dashboard. author = Some ( user_id. to_string ( ) ) ;
94
93
dashboard. dashboard_id = Some ( dashboard_id) ;
95
94
dashboard. version = Some ( CURRENT_DASHBOARD_VERSION . to_string ( ) ) ;
96
95
dashboard. modified = Some ( Utc :: now ( ) ) ;
97
96
dashboard. dashboard_type = Some ( DashboardType :: Dashboard ) ;
98
- for tile in dashboard. tiles . iter_mut ( ) {
99
- tile. tile_id = Ulid :: new ( ) ;
100
- }
101
- s. push ( dashboard. clone ( ) ) ;
102
-
103
97
let path = dashboard_path ( user_id, & format ! ( "{}.json" , dashboard_id) ) ;
104
98
105
99
let store = PARSEABLE . storage . get_object_store ( ) ;
106
100
let dashboard_bytes = serde_json:: to_vec ( & dashboard) ?;
107
101
store
108
102
. put_object ( & path, Bytes :: from ( dashboard_bytes) )
109
103
. await ?;
104
+
105
+ self . 0 . write ( ) . await . push ( dashboard. clone ( ) ) ;
106
+
110
107
Ok ( ( ) )
111
108
}
112
109
@@ -116,20 +113,18 @@ impl Dashboards {
116
113
dashboard_id : Ulid ,
117
114
dashboard : & mut Dashboard ,
118
115
) -> Result < ( ) , DashboardError > {
119
- let mut s = self . 0 . write ( ) . await ;
120
- if self . get_dashboard ( dashboard_id) . await . is_none ( ) {
116
+ if self
117
+ . get_dashboard_by_user ( dashboard_id, user_id)
118
+ . await
119
+ . is_none ( )
120
+ {
121
121
return Err ( DashboardError :: Metadata ( "Dashboard does not exist" ) ) ;
122
122
}
123
+ dashboard. author = Some ( user_id. to_string ( ) ) ;
123
124
dashboard. dashboard_id = Some ( dashboard_id) ;
124
- dashboard. modified = Some ( Utc :: now ( ) ) ;
125
125
dashboard. version = Some ( CURRENT_DASHBOARD_VERSION . to_string ( ) ) ;
126
- for tile in dashboard. tiles . iter_mut ( ) {
127
- if tile. tile_id . is_nil ( ) {
128
- tile. tile_id = Ulid :: new ( ) ;
129
- }
130
- }
131
- s. retain ( |d| d. dashboard_id != dashboard. dashboard_id ) ;
132
- s. push ( dashboard. clone ( ) ) ;
126
+ dashboard. modified = Some ( Utc :: now ( ) ) ;
127
+ dashboard. dashboard_type = Some ( DashboardType :: Dashboard ) ;
133
128
134
129
let path = dashboard_path ( user_id, & format ! ( "{}.json" , dashboard_id) ) ;
135
130
@@ -138,6 +133,13 @@ impl Dashboards {
138
133
store
139
134
. put_object ( & path, Bytes :: from ( dashboard_bytes) )
140
135
. await ?;
136
+
137
+ self . 0
138
+ . write ( )
139
+ . await
140
+ . retain ( |d| d. dashboard_id != dashboard. dashboard_id ) ;
141
+ self . 0 . write ( ) . await . push ( dashboard. clone ( ) ) ;
142
+
141
143
Ok ( ( ) )
142
144
}
143
145
@@ -146,15 +148,21 @@ impl Dashboards {
146
148
user_id : & str ,
147
149
dashboard_id : Ulid ,
148
150
) -> Result < ( ) , DashboardError > {
149
- let mut s = self . 0 . write ( ) . await ;
150
-
151
- if self . get_dashboard ( dashboard_id) . await . is_none ( ) {
151
+ if self
152
+ . get_dashboard_by_user ( dashboard_id, user_id)
153
+ . await
154
+ . is_none ( )
155
+ {
152
156
return Err ( DashboardError :: Metadata ( "Dashboard does not exist" ) ) ;
153
157
}
154
- s . retain ( |d| * d . dashboard_id . as_ref ( ) . unwrap ( ) != dashboard_id ) ;
158
+
155
159
let path = dashboard_path ( user_id, & format ! ( "{}.json" , dashboard_id) ) ;
156
160
let store = PARSEABLE . storage . get_object_store ( ) ;
157
161
store. delete_object ( & path) . await ?;
162
+ self . 0
163
+ . write ( )
164
+ . await
165
+ . retain ( |d| * d. dashboard_id . as_ref ( ) . unwrap ( ) != dashboard_id) ;
158
166
159
167
Ok ( ( ) )
160
168
}
@@ -168,6 +176,22 @@ impl Dashboards {
168
176
. cloned ( )
169
177
}
170
178
179
+ pub async fn get_dashboard_by_user (
180
+ & self ,
181
+ dashboard_id : Ulid ,
182
+ user_id : & str ,
183
+ ) -> Option < Dashboard > {
184
+ self . 0
185
+ . read ( )
186
+ . await
187
+ . iter ( )
188
+ . find ( |d| {
189
+ * d. dashboard_id . as_ref ( ) . unwrap ( ) == dashboard_id
190
+ && d. author == Some ( user_id. to_string ( ) )
191
+ } )
192
+ . cloned ( )
193
+ }
194
+
171
195
pub async fn list_dashboards ( & self ) -> Vec < Dashboard > {
172
196
let read = self . 0 . read ( ) . await ;
173
197
0 commit comments