Skip to main content

fiberplane_models/notebooks/
mod.rs

1use crate::comments::UserSummary;
2use crate::data_sources::SelectedDataSources;
3use crate::front_matter_schemas::FrontMatterSchema;
4pub use crate::labels::Label;
5use crate::timestamps::*;
6use base64uuid::Base64Uuid;
7#[cfg(feature = "fp-bindgen")]
8use fp_bindgen::prelude::Serializable;
9use serde::{Deserialize, Serialize};
10use serde_json::{Map, Value};
11use std::collections::HashMap;
12use strum_macros::Display;
13use typed_builder::TypedBuilder;
14use url::Url;
15
16mod cells;
17use crate::names::Name;
18use crate::views::RelativeTime;
19pub use cells::*;
20
21pub mod front_matter;
22pub use front_matter::FrontMatter;
23pub mod operations;
24
25#[derive(Debug, Deserialize, Serialize, Clone, PartialEq, TypedBuilder)]
26#[cfg_attr(
27    feature = "fp-bindgen",
28    derive(Serializable),
29    fp(rust_module = "fiberplane_models::notebooks")
30)]
31#[non_exhaustive]
32#[serde(rename_all = "camelCase")]
33pub struct Notebook {
34    #[builder(default, setter(into))]
35    pub id: String,
36
37    #[builder(default, setter(into))]
38    pub workspace_id: Base64Uuid,
39
40    #[builder(setter(into))]
41    pub created_at: Timestamp,
42
43    #[builder(setter(into))]
44    pub updated_at: Timestamp,
45
46    pub time_range: TimeRange,
47
48    #[builder(default, setter(into))]
49    pub title: String,
50
51    #[builder(default)]
52    pub cells: Vec<Cell>,
53
54    pub revision: u32,
55
56    #[builder(default)]
57    pub visibility: NotebookVisibility,
58
59    #[builder(default)]
60    pub read_only: bool,
61
62    pub created_by: CreatedBy,
63
64    #[builder(default)]
65    #[serde(default)]
66    pub selected_data_sources: SelectedDataSources,
67
68    #[builder(default)]
69    #[serde(default)]
70    pub labels: Vec<Label>,
71
72    #[builder(default)]
73    #[serde(default)]
74    pub front_matter: FrontMatter,
75
76    #[builder(default)]
77    #[serde(default)]
78    pub front_matter_schema: FrontMatterSchema,
79}
80
81#[derive(Debug, Deserialize, Serialize, Clone, PartialEq, TypedBuilder)]
82#[cfg_attr(
83    feature = "fp-bindgen",
84    derive(Serializable),
85    fp(rust_module = "fiberplane_models::notebooks")
86)]
87#[non_exhaustive]
88#[serde(rename_all = "camelCase")]
89pub struct NewNotebook {
90    #[builder(setter(into))]
91    pub title: String,
92
93    #[builder(default)]
94    pub cells: Vec<Cell>,
95
96    #[builder(setter(into))]
97    pub time_range: NewTimeRange,
98
99    #[builder(default)]
100    #[serde(default)]
101    pub selected_data_sources: SelectedDataSources,
102
103    #[builder(default)]
104    #[serde(default)]
105    pub labels: Vec<Label>,
106
107    #[builder(default)]
108    #[serde(default)]
109    pub front_matter: FrontMatter,
110
111    /// The "inline" front matter schema for the notebook.
112    ///
113    /// It will always be expanded first and have priority over anything defined in
114    /// `front_matter_collections`. The keys in common will be ignored.
115    #[builder(default)]
116    #[serde(default)]
117    pub front_matter_schema: FrontMatterSchema,
118
119    /// A list of front matter schema names that exist in the target workspace.
120    ///
121    /// If `front_matter_collections` and `front_matter_schema` are both mentioned, then:
122    /// - the `front_matter_schema` will be the first elements of the notebook front matter,
123    /// - and keys from the named schema that already exist in `front_matter_schema` (or an earlier
124    ///   entry in the list) will be ignored.
125    #[builder(default)]
126    #[serde(skip_serializing_if = "Vec::is_empty", default)]
127    pub front_matter_collections: Vec<Name>,
128}
129
130impl From<Notebook> for NewNotebook {
131    fn from(notebook: Notebook) -> Self {
132        NewNotebook {
133            title: notebook.title,
134            cells: notebook.cells,
135            time_range: notebook.time_range.into(),
136            selected_data_sources: notebook.selected_data_sources,
137            labels: notebook.labels,
138            front_matter: notebook.front_matter,
139            front_matter_schema: notebook.front_matter_schema,
140            front_matter_collections: Vec::new(),
141        }
142    }
143}
144
145#[derive(Debug, Deserialize, Serialize, Clone, PartialEq, Eq)]
146#[cfg_attr(
147    feature = "fp-bindgen",
148    derive(Serializable),
149    fp(rust_module = "fiberplane_models::notebooks")
150)]
151#[non_exhaustive]
152#[serde(tag = "type", rename_all = "snake_case")]
153pub enum CreatedBy {
154    User(UserSummary),
155    Trigger(TriggerSummary),
156    Onboarding,
157    Unknown,
158}
159
160impl CreatedBy {
161    pub fn name(&self) -> String {
162        match self {
163            CreatedBy::User(user) => user.name.clone(),
164            CreatedBy::Trigger(trigger) => trigger.title.clone(),
165            CreatedBy::Onboarding => "Onboarding".to_string(),
166            CreatedBy::Unknown => String::from("Unknown"),
167        }
168    }
169}
170
171#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq, TypedBuilder)]
172#[cfg_attr(
173    feature = "fp-bindgen",
174    derive(Serializable),
175    fp(rust_module = "fiberplane_models::notebooks")
176)]
177#[non_exhaustive]
178#[serde(rename_all = "camelCase")]
179pub struct TriggerSummary {
180    #[builder(setter(into))]
181    pub id: Base64Uuid,
182
183    #[builder(setter(into))]
184    pub title: String,
185
186    #[builder(setter(into))]
187    pub template_id: Base64Uuid,
188
189    #[builder(setter(into))]
190    pub created_at: Timestamp,
191
192    #[builder(setter(into))]
193    pub updated_at: Timestamp,
194}
195
196#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Deserialize, Serialize, Display)]
197#[cfg_attr(
198    feature = "fp-bindgen",
199    derive(Serializable),
200    fp(rust_module = "fiberplane_models::notebooks")
201)]
202#[non_exhaustive]
203#[serde(rename_all = "snake_case")]
204pub enum NotebookVisibility {
205    #[default]
206    Private,
207    Public,
208}
209
210#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, Serialize, TypedBuilder)]
211#[cfg_attr(
212    feature = "fp-bindgen",
213    derive(Serializable),
214    fp(rust_module = "fiberplane_models::notebooks")
215)]
216#[non_exhaustive]
217#[serde(rename_all = "camelCase")]
218pub struct UpdateNotebook {
219    pub visibility: NotebookVisibility,
220}
221
222#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize, TypedBuilder)]
223#[cfg_attr(
224    feature = "fp-bindgen",
225    derive(Serializable),
226    fp(rust_module = "fiberplane_models::notebooks")
227)]
228#[non_exhaustive]
229#[serde(rename_all = "camelCase")]
230pub struct NotebookCopyDestination {
231    #[builder(setter(into))]
232    pub title: String,
233
234    #[builder(setter(into))]
235    pub workspace_id: Base64Uuid,
236}
237
238#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, Serialize, TypedBuilder)]
239#[cfg_attr(
240    feature = "fp-bindgen",
241    derive(Serializable),
242    fp(rust_module = "fiberplane_models::notebooks")
243)]
244#[non_exhaustive]
245#[serde(rename_all = "camelCase")]
246pub struct NewPinnedNotebook {
247    /// The ID of the notebook that is being pinned.
248    #[builder(setter(into))]
249    pub notebook_id: Base64Uuid,
250}
251
252#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize, TypedBuilder)]
253#[non_exhaustive]
254#[serde(rename_all = "camelCase")]
255pub struct Trigger {
256    #[builder(setter(into))]
257    pub id: Base64Uuid,
258
259    #[builder(setter(into))]
260    pub title: String,
261
262    #[builder(setter(into))]
263    pub template_id: Base64Uuid,
264
265    #[builder(default, setter(into, strip_option))]
266    pub secret_key: Option<String>,
267
268    #[builder(default, setter(into, strip_option))]
269    pub default_arguments: Option<Map<String, Value>>,
270
271    #[builder(setter(into))]
272    pub created_at: Timestamp,
273
274    #[builder(setter(into))]
275    pub updated_at: Timestamp,
276}
277
278pub type TemplateExpandPayload = Map<String, Value>;
279
280#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize, TypedBuilder)]
281#[non_exhaustive]
282#[serde(rename_all = "camelCase")]
283pub struct TriggerInvokeResponse {
284    #[builder(setter(into))]
285    pub notebook_title: String,
286
287    #[builder(setter(into))]
288    pub notebook_id: Base64Uuid,
289
290    pub notebook_url: Url,
291}
292
293#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize, TypedBuilder)]
294#[cfg_attr(
295    feature = "fp-bindgen",
296    derive(Serializable),
297    fp(rust_module = "fiberplane_models::notebooks")
298)]
299#[non_exhaustive]
300#[serde(rename_all = "camelCase")]
301pub struct NotebookSummary {
302    #[builder(setter(into))]
303    pub id: Base64Uuid,
304
305    #[builder(setter(into))]
306    pub workspace_id: Base64Uuid,
307
308    #[builder(setter(into))]
309    pub created_at: Timestamp,
310
311    #[builder(setter(into))]
312    pub updated_at: Timestamp,
313
314    #[builder(setter(into))]
315    pub title: String,
316
317    pub visibility: NotebookVisibility,
318
319    pub created_by: CreatedBy,
320
321    #[builder(default)]
322    pub labels: Vec<Label>,
323}
324
325/// Notebook search parameters
326#[derive(Debug, Clone, Default, PartialEq, Eq, Deserialize, Serialize, TypedBuilder)]
327#[cfg_attr(
328    feature = "fp-bindgen",
329    derive(Serializable),
330    fp(rust_module = "fiberplane_models::notebooks")
331)]
332#[non_exhaustive]
333#[serde(rename_all = "camelCase")]
334pub struct NotebookSearch {
335    #[builder(default, setter(into, strip_option))]
336    #[serde(default, skip_serializing_if = "Option::is_none")]
337    pub labels: Option<HashMap<String, Option<String>>>,
338
339    #[builder(default, setter(strip_option))]
340    #[serde(default, skip_serializing_if = "Option::is_none")]
341    pub relative_time: Option<RelativeTime>,
342
343    #[builder(default, setter(strip_option))]
344    #[serde(default, skip_serializing_if = "Option::is_none")]
345    pub view: Option<Name>,
346}
347
348#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize, TypedBuilder)]
349#[cfg_attr(
350    feature = "fp-bindgen",
351    derive(Serializable),
352    fp(rust_module = "fiberplane_models::notebooks")
353)]
354#[non_exhaustive]
355#[serde(rename_all = "camelCase")]
356pub struct TemplateSummary {
357    #[builder(setter(into))]
358    pub id: Base64Uuid,
359
360    pub name: Name,
361
362    #[builder(default, setter(into))]
363    pub description: String,
364
365    #[builder(setter(into))]
366    pub created_at: Timestamp,
367
368    #[builder(setter(into))]
369    pub updated_at: Timestamp,
370}
371
372#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize, TypedBuilder)]
373#[cfg_attr(
374    feature = "fp-bindgen",
375    derive(Serializable),
376    fp(rust_module = "fiberplane_models::notebooks")
377)]
378#[non_exhaustive]
379#[serde(rename_all = "camelCase")]
380pub struct NewTemplate {
381    pub name: Name,
382
383    #[builder(default, setter(into))]
384    pub description: String,
385
386    #[builder(setter(into))]
387    pub body: String,
388}
389
390impl NewTemplate {
391    pub fn new(name: Name, description: impl Into<String>, body: impl Into<String>) -> Self {
392        Self {
393            name,
394            description: description.into(),
395            body: body.into(),
396        }
397    }
398}
399
400#[derive(Clone, Debug, Default, Eq, PartialEq, Deserialize, Serialize, TypedBuilder)]
401#[cfg_attr(
402    feature = "fp-bindgen",
403    derive(Serializable),
404    fp(rust_module = "fiberplane_models::notebooks")
405)]
406#[non_exhaustive]
407#[serde(rename_all = "camelCase")]
408pub struct UpdateTemplate {
409    #[builder(default, setter(into, strip_option))]
410    pub description: Option<String>,
411
412    #[builder(default, setter(into, strip_option))]
413    pub body: Option<String>,
414}
415
416#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize, TypedBuilder)]
417#[non_exhaustive]
418#[serde(rename_all = "camelCase")]
419pub struct NewTrigger {
420    #[builder(setter(into))]
421    pub title: String,
422
423    pub template_name: Name,
424
425    #[builder(default, setter(into, strip_option))]
426    pub default_arguments: Option<Map<String, Value>>,
427}
428
429#[derive(Debug, Default, Clone, PartialEq, Eq, Deserialize, Serialize, TypedBuilder)]
430#[cfg_attr(
431    feature = "fp-bindgen",
432    derive(Serializable),
433    fp(rust_module = "fiberplane_models::notebooks")
434)]
435#[non_exhaustive]
436#[serde(rename_all = "camelCase")]
437/// The query string values that are associated with the notebook_cells_append
438/// endpoint.
439pub struct NotebookCellsAppendQuery {
440    /// Append the provided cells before this cell.
441    /// If the cell is not found it will return a 400. This parameter cannot
442    /// be used together with `after`.
443    #[builder(default, setter(into, strip_option))]
444    pub before: Option<String>,
445
446    /// Append the provided cells after this cell.
447    /// If the cell is not found it will return a 400. This parameter cannot
448    /// be used together with `before`.
449    #[builder(default, setter(into, strip_option))]
450    pub after: Option<String>,
451}