perspective_js/view.rs
1// ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
2// ┃ ██████ ██████ ██████ █ █ █ █ █ █▄ ▀███ █ ┃
3// ┃ ▄▄▄▄▄█ █▄▄▄▄▄ ▄▄▄▄▄█ ▀▀▀▀▀█▀▀▀▀▀ █ ▀▀▀▀▀█ ████████▌▐███ ███▄ ▀█ █ ▀▀▀▀▀ ┃
4// ┃ █▀▀▀▀▀ █▀▀▀▀▀ █▀██▀▀ ▄▄▄▄▄ █ ▄▄▄▄▄█ ▄▄▄▄▄█ ████████▌▐███ █████▄ █ ▄▄▄▄▄ ┃
5// ┃ █ ██████ █ ▀█▄ █ ██████ █ ███▌▐███ ███████▄ █ ┃
6// ┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫
7// ┃ Copyright (c) 2017, the Perspective Authors. ┃
8// ┃ ╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌ ┃
9// ┃ This file is part of the Perspective library, distributed under the terms ┃
10// ┃ of the [Apache License 2.0](https://www.apache.org/licenses/LICENSE-2.0). ┃
11// ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
12
13use js_sys::{Array, ArrayBuffer, Function, Object};
14use perspective_client::{
15 ColumnWindow, OnUpdateData, OnUpdateOptions, ViewWindow, assert_view_api,
16};
17use wasm_bindgen::prelude::*;
18use wasm_bindgen_futures::spawn_local;
19
20#[cfg(doc)]
21use crate::table::Table;
22use crate::utils::{ApiFuture, ApiResult, JsValueSerdeExt, LocalPollLoop};
23
24#[wasm_bindgen]
25unsafe extern "C" {
26 #[wasm_bindgen(typescript_type = "ViewWindow")]
27 #[derive(Clone)]
28 pub type JsViewWindow;
29
30 #[wasm_bindgen(typescript_type = "ColumnWindow")]
31 #[derive(Clone)]
32 pub type JsColumnWindow;
33
34 #[wasm_bindgen(method, setter, js_name = "formatted")]
35 pub fn set_formatted(this: &JsViewWindow, x: bool);
36
37 #[wasm_bindgen(typescript_type = "OnUpdateOptions")]
38 pub type JsOnUpdateOptions;
39
40}
41
42impl From<ViewWindow> for JsViewWindow {
43 fn from(value: ViewWindow) -> Self {
44 JsViewWindow::from_serde_ext(&value)
45 .unwrap()
46 .unchecked_into()
47 }
48}
49
50fn scalar_to_jsvalue(scalar: &perspective_client::config::Scalar) -> JsValue {
51 match scalar {
52 perspective_client::config::Scalar::Float(x) => JsValue::from_f64(*x),
53 perspective_client::config::Scalar::String(x) => JsValue::from_str(x),
54 perspective_client::config::Scalar::Bool(x) => JsValue::from_bool(*x),
55 perspective_client::config::Scalar::Null => JsValue::NULL,
56 }
57}
58
59/// The [`View`] struct is Perspective's query and serialization interface. It
60/// represents a query on the `Table`'s dataset and is always created from an
61/// existing `Table` instance via the [`Table::view`] method.
62///
63/// [`View`]s are immutable with respect to the arguments provided to the
64/// [`Table::view`] method; to change these parameters, you must create a new
65/// [`View`] on the same [`Table`]. However, each [`View`] is _live_ with
66/// respect to the [`Table`]'s data, and will (within a conflation window)
67/// update with the latest state as its parent [`Table`] updates, including
68/// incrementally recalculating all aggregates, pivots, filters, etc. [`View`]
69/// query parameters are composable, in that each parameter works independently
70/// _and_ in conjunction with each other, and there is no limit to the number of
71/// pivots, filters, etc. which can be applied.
72#[wasm_bindgen]
73#[derive(Clone)]
74pub struct View(pub(crate) perspective_client::View);
75
76assert_view_api!(View);
77
78impl From<perspective_client::View> for View {
79 fn from(value: perspective_client::View) -> Self {
80 View(value)
81 }
82}
83
84#[wasm_bindgen]
85impl View {
86 #[doc(hidden)]
87 pub fn __get_model(&self) -> View {
88 self.clone()
89 }
90
91 /// Returns an array of strings containing the column paths of the [`View`]
92 /// without any of the source columns.
93 ///
94 /// A column path shows the columns that a given cell belongs to after
95 /// pivots are applied.
96 #[wasm_bindgen]
97 pub async fn column_paths(&self, window: Option<JsColumnWindow>) -> ApiResult<JsValue> {
98 let window = window.into_serde_ext::<Option<ColumnWindow>>()?;
99 let columns = self.0.column_paths(window.unwrap_or_default()).await?;
100 Ok(JsValue::from_serde_ext(&columns)?)
101 }
102
103 /// Delete this [`View`] and clean up all resources associated with it.
104 /// [`View`] objects do not stop consuming resources or processing
105 /// updates when they are garbage collected - you must call this method
106 /// to reclaim these.
107 #[wasm_bindgen]
108 pub async fn delete(self) -> ApiResult<()> {
109 self.0.delete().await?;
110 Ok(())
111 }
112
113 /// Returns this [`View`]'s _dimensions_, row and column count, as well as
114 /// those of the [`crate::Table`] from which it was derived.
115 ///
116 /// - `num_table_rows` - The number of rows in the underlying
117 /// [`crate::Table`].
118 /// - `num_table_columns` - The number of columns in the underlying
119 /// [`crate::Table`] (including the `index` column if this
120 /// [`crate::Table`] was constructed with one).
121 /// - `num_view_rows` - The number of rows in this [`View`]. If this
122 /// [`View`] has a `group_by` clause, `num_view_rows` will also include
123 /// aggregated rows.
124 /// - `num_view_columns` - The number of columns in this [`View`]. If this
125 /// [`View`] has a `split_by` clause, `num_view_columns` will include all
126 /// _column paths_, e.g. the number of `columns` clause times the number
127 /// of `split_by` groups.
128 #[wasm_bindgen]
129 pub async fn dimensions(&self) -> ApiResult<JsValue> {
130 let dimensions = self.0.dimensions().await?;
131 Ok(JsValue::from_serde_ext(&dimensions)?)
132 }
133
134 /// The expression schema of this [`View`], which contains only the
135 /// expressions created on this [`View`]. See [`View::schema`] for
136 /// details.
137 #[wasm_bindgen]
138 pub async fn expression_schema(&self) -> ApiResult<JsValue> {
139 let schema = self.0.expression_schema().await?;
140 Ok(JsValue::from_serde_ext(&schema)?)
141 }
142
143 /// A copy of the config object passed to the [`Table::view`] method which
144 /// created this [`View`].
145 #[wasm_bindgen]
146 pub async fn get_config(&self) -> ApiResult<JsValue> {
147 let config = self.0.get_config().await?;
148 Ok(JsValue::from_serde_ext(&config)?)
149 }
150
151 /// Calculates the [min, max] of the leaf nodes of a column `column_name`.
152 ///
153 /// # Returns
154 ///
155 /// A tuple of [min, max], whose types are column and aggregate dependent.
156 #[wasm_bindgen]
157 pub async fn get_min_max(&self, name: String) -> ApiResult<Array> {
158 let result = self.0.get_min_max(name).await?;
159 let arr = Array::new();
160 arr.push(&scalar_to_jsvalue(&result.0));
161 arr.push(&scalar_to_jsvalue(&result.1));
162 Ok(arr)
163 }
164
165 /// The number of aggregated rows in this [`View`]. This is affected by the
166 /// "group_by" configuration parameter supplied to this view's contructor.
167 ///
168 /// # Returns
169 ///
170 /// The number of aggregated rows.
171 #[wasm_bindgen]
172 pub async fn num_rows(&self) -> ApiResult<i32> {
173 let size = self.0.num_rows().await?;
174 Ok(size as i32)
175 }
176
177 /// The schema of this [`View`].
178 ///
179 /// The [`View`] schema differs from the `schema` returned by
180 /// [`Table::schema`]; it may have different column names due to
181 /// `expressions` or `columns` configs, or it maye have _different
182 /// column types_ due to the application og `group_by` and `aggregates`
183 /// config. You can think of [`Table::schema`] as the _input_ schema and
184 /// [`View::schema`] as the _output_ schema of a Perspective pipeline.
185 #[wasm_bindgen]
186 pub async fn schema(&self) -> ApiResult<JsValue> {
187 let schema = self.0.schema().await?;
188 Ok(JsValue::from_serde_ext(&schema)?)
189 }
190
191 /// Serializes a [`View`] to the Apache Arrow data format.
192 #[wasm_bindgen]
193 pub async fn to_arrow(&self, window: Option<JsViewWindow>) -> ApiResult<ArrayBuffer> {
194 let window = window.into_serde_ext::<Option<ViewWindow>>()?;
195 let arrow = self.0.to_arrow(window.unwrap_or_default()).await?;
196 Ok(js_sys::Uint8Array::from(&arrow[..])
197 .buffer()
198 .unchecked_into())
199 }
200
201 /// Serializes this [`View`] to a string of JSON data. Useful if you want to
202 /// save additional round trip serialize/deserialize cycles.
203 #[wasm_bindgen]
204 pub async fn to_columns_string(&self, window: Option<JsViewWindow>) -> ApiResult<String> {
205 let window = window.into_serde_ext::<Option<ViewWindow>>()?;
206 let json = self.0.to_columns_string(window.unwrap_or_default()).await?;
207 Ok(json)
208 }
209
210 /// Serializes this [`View`] to JavaScript objects in a column-oriented
211 /// format.
212 #[wasm_bindgen]
213 pub async fn to_columns(&self, window: Option<JsViewWindow>) -> ApiResult<Object> {
214 let json = self.to_columns_string(window).await?;
215 Ok(js_sys::JSON::parse(&json)?.unchecked_into())
216 }
217
218 /// Render this `View` as a JSON string.
219 #[wasm_bindgen]
220 pub async fn to_json_string(&self, window: Option<JsViewWindow>) -> ApiResult<String> {
221 let window = window.into_serde_ext::<Option<ViewWindow>>()?;
222 let json = self.0.to_json_string(window.unwrap_or_default()).await?;
223 Ok(json)
224 }
225
226 /// Serializes this [`View`] to JavaScript objects in a row-oriented
227 /// format.
228 #[wasm_bindgen]
229 pub async fn to_json(&self, window: Option<JsViewWindow>) -> ApiResult<Array> {
230 let json = self.to_json_string(window).await?;
231 Ok(js_sys::JSON::parse(&json)?.unchecked_into())
232 }
233
234 /// Renders this [`View`] as an [NDJSON](https://github.com/ndjson/ndjson-spec)
235 /// formatted [`String`].
236 #[wasm_bindgen]
237 pub async fn to_ndjson(&self, window: Option<JsViewWindow>) -> ApiResult<String> {
238 let window = window.into_serde_ext::<Option<ViewWindow>>()?;
239 let ndjson = self.0.to_ndjson(window.unwrap_or_default()).await?;
240 Ok(ndjson)
241 }
242
243 /// Serializes this [`View`] to CSV data in a standard format.
244 #[wasm_bindgen]
245 pub async fn to_csv(&self, window: Option<JsViewWindow>) -> ApiResult<String> {
246 let window = window.into_serde_ext::<Option<ViewWindow>>()?;
247 Ok(self.0.to_csv(window.unwrap_or_default()).await?)
248 }
249
250 /// Register a callback with this [`View`]. Whenever the view's underlying
251 /// table emits an update, this callback will be invoked with an object
252 /// containing `port_id`, indicating which port the update fired on, and
253 /// optionally `delta`, which is the new data that was updated for each
254 /// cell or each row.
255 ///
256 /// # Arguments
257 ///
258 /// - `on_update` - A callback function invoked on update, which receives an
259 /// object with two keys: `port_id`, indicating which port the update was
260 /// triggered on, and `delta`, whose value is dependent on the mode
261 /// parameter.
262 /// - `options` - If this is provided as `OnUpdateOptions { mode:
263 /// Some(OnUpdateMode::Row) }`, then `delta` is an Arrow of the updated
264 /// rows. Otherwise `delta` will be [`Option::None`].
265 ///
266 /// # JavaScript Examples
267 ///
268 /// ```javascript
269 /// // Attach an `on_update` callback
270 /// view.on_update((updated) => console.log(updated.port_id));
271 /// ```
272 ///
273 /// ```javascript
274 /// // `on_update` with row deltas
275 /// view.on_update((updated) => console.log(updated.delta), { mode: "row" });
276 /// ```
277 #[wasm_bindgen]
278 pub fn on_update(
279 &self,
280 on_update_js: Function,
281 options: Option<JsOnUpdateOptions>,
282 ) -> ApiFuture<u32> {
283 let poll_loop = LocalPollLoop::new(move |args: OnUpdateData| {
284 let js_obj = JsValue::from_serde_ext(&*args)?;
285 on_update_js.call1(&JsValue::UNDEFINED, &js_obj)
286 });
287
288 let on_update = Box::new(move |msg| poll_loop.poll(msg));
289 let view = self.0.clone();
290 ApiFuture::new(async move {
291 let on_update_opts = options
292 .into_serde_ext::<Option<OnUpdateOptions>>()?
293 .unwrap_or_default();
294
295 let id = view.on_update(on_update, on_update_opts).await?;
296 Ok(id)
297 })
298 }
299
300 /// Unregister a previously registered update callback with this [`View`].
301 ///
302 /// # Arguments
303 ///
304 /// - `id` - A callback `id` as returned by a recipricol call to
305 /// [`View::on_update`].
306 #[wasm_bindgen]
307 pub async fn remove_update(&self, callback_id: u32) -> ApiResult<()> {
308 Ok(self.0.remove_update(callback_id).await?)
309 }
310
311 /// Register a callback with this [`View`]. Whenever the [`View`] is
312 /// deleted, this callback will be invoked.
313 #[wasm_bindgen]
314 pub fn on_delete(&self, on_delete: Function) -> ApiFuture<u32> {
315 let view = self.clone();
316 ApiFuture::new(async move {
317 let emit = LocalPollLoop::new(move |()| on_delete.call0(&JsValue::UNDEFINED));
318 let on_delete = Box::new(move || spawn_local(emit.poll(())));
319 Ok(view.0.on_delete(on_delete).await?)
320 })
321 }
322
323 /// The number of aggregated columns in this [`View`]. This is affected by
324 /// the "split_by" configuration parameter supplied to this view's
325 /// contructor.
326 ///
327 /// # Returns
328 ///
329 /// The number of aggregated columns.
330 #[wasm_bindgen]
331 pub async fn num_columns(&self) -> ApiResult<u32> {
332 // TODO: This is broken because of how split by creates a
333 // cartesian product of columns * unique values.
334 Ok(self.0.dimensions().await?.num_view_columns)
335 }
336
337 /// Unregister a previously registered [`View::on_delete`] callback.
338 #[wasm_bindgen]
339 pub fn remove_delete(&self, callback_id: u32) -> ApiFuture<()> {
340 let client = self.0.clone();
341 ApiFuture::new(async move {
342 client.remove_delete(callback_id).await?;
343 Ok(())
344 })
345 }
346
347 /// Collapses the `group_by` row at `row_index`.
348 #[wasm_bindgen]
349 pub async fn collapse(&self, row_index: u32) -> ApiResult<u32> {
350 Ok(self.0.collapse(row_index).await?)
351 }
352
353 /// Expand the `group_by` row at `row_index`.
354 #[wasm_bindgen]
355 pub async fn expand(&self, row_index: u32) -> ApiResult<u32> {
356 Ok(self.0.expand(row_index).await?)
357 }
358
359 /// Set expansion `depth` of the `group_by` tree.
360 #[wasm_bindgen]
361 pub async fn set_depth(&self, depth: u32) -> ApiResult<()> {
362 Ok(self.0.set_depth(depth).await?)
363 }
364}