fiberplane_models/notebooks/
mod.rs

1use crate::comments::UserSummary;
2use crate::data_sources::SelectedDataSources;
3pub use crate::labels::Label;
4use crate::timestamps::*;
5use base64uuid::Base64Uuid;
6#[cfg(feature = "fp-bindgen")]
7use fp_bindgen::prelude::Serializable;
8use serde::{Deserialize, Serialize};
9use serde_json::{Map, Value};
10use std::collections::HashMap;
11use strum_macros::Display;
12use time::OffsetDateTime;
13use typed_builder::TypedBuilder;
14use url::Url;
15
16mod cells;
17use crate::names::Name;
18use crate::views::RelativeTime;
19pub use cells::*;
20
21pub mod operations;
22
23/// A JSON object which may or may not contain well known keys.
24/// More information in the [RFC](https://www.notion.so/fiberplane/RFC-58-Front-matter-Specialization-Front-matter-a9b3b51614ee48a19ec416c02a9fd647)
25// this is on purpose a `Map<String, Value>` instead of a `Value` to disallow top level arrays
26pub type FrontMatter = Map<String, Value>;
27
28#[derive(Debug, Deserialize, Serialize, Clone, PartialEq, TypedBuilder)]
29#[cfg_attr(
30    feature = "fp-bindgen",
31    derive(Serializable),
32    fp(rust_module = "fiberplane_models::notebooks")
33)]
34#[non_exhaustive]
35#[serde(rename_all = "camelCase")]
36pub struct Notebook {
37    #[builder(default, setter(into))]
38    pub id: String,
39    #[builder(default)]
40    pub workspace_id: Base64Uuid,
41    #[builder(setter(into))]
42    pub created_at: Timestamp,
43    #[builder(setter(into))]
44    pub updated_at: Timestamp,
45    pub time_range: TimeRange,
46    #[builder(default, setter(into))]
47    pub title: String,
48    #[builder(default)]
49    pub cells: Vec<Cell>,
50    pub revision: u32,
51    pub visibility: NotebookVisibility,
52    #[builder(default)]
53    pub read_only: bool,
54    pub created_by: CreatedBy,
55
56    #[builder(default)]
57    #[serde(default)]
58    pub selected_data_sources: SelectedDataSources,
59
60    #[builder(default)]
61    #[serde(default)]
62    pub labels: Vec<Label>,
63
64    #[builder(default)]
65    #[serde(default)]
66    pub front_matter: FrontMatter,
67}
68
69#[derive(Debug, Deserialize, Serialize, Clone, PartialEq, TypedBuilder)]
70#[cfg_attr(
71    feature = "fp-bindgen",
72    derive(Serializable),
73    fp(rust_module = "fiberplane_models::notebooks")
74)]
75#[non_exhaustive]
76#[serde(rename_all = "camelCase")]
77pub struct NewNotebook {
78    #[builder(setter(into))]
79    pub title: String,
80    #[builder(default)]
81    pub cells: Vec<Cell>,
82    #[builder(setter(into))]
83    pub time_range: NewTimeRange,
84
85    #[builder(default)]
86    #[serde(default)]
87    pub selected_data_sources: SelectedDataSources,
88
89    #[builder(default)]
90    #[serde(default)]
91    pub labels: Vec<Label>,
92
93    #[builder(default)]
94    #[serde(default)]
95    pub front_matter: FrontMatter,
96}
97
98impl From<Notebook> for NewNotebook {
99    fn from(notebook: Notebook) -> Self {
100        NewNotebook {
101            title: notebook.title,
102            cells: notebook.cells,
103            time_range: notebook.time_range.into(),
104            selected_data_sources: notebook.selected_data_sources,
105            labels: notebook.labels,
106            front_matter: notebook.front_matter,
107        }
108    }
109}
110
111#[derive(Debug, Deserialize, Serialize, Clone, PartialEq, Eq)]
112#[cfg_attr(
113    feature = "fp-bindgen",
114    derive(Serializable),
115    fp(rust_module = "fiberplane_models::notebooks")
116)]
117#[non_exhaustive]
118#[serde(tag = "type", rename_all = "snake_case")]
119pub enum CreatedBy {
120    User(UserSummary),
121    Trigger(TriggerSummary),
122    Onboarding,
123    Unknown,
124}
125
126impl CreatedBy {
127    pub fn name(&self) -> String {
128        match self {
129            CreatedBy::User(user) => user.name.clone(),
130            CreatedBy::Trigger(trigger) => trigger.title.clone(),
131            CreatedBy::Onboarding => "Onboarding".to_string(),
132            CreatedBy::Unknown => String::from("Unknown"),
133        }
134    }
135}
136
137#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq, TypedBuilder)]
138#[cfg_attr(
139    feature = "fp-bindgen",
140    derive(Serializable),
141    fp(rust_module = "fiberplane_models::notebooks")
142)]
143#[non_exhaustive]
144#[serde(rename_all = "camelCase")]
145pub struct TriggerSummary {
146    pub id: Base64Uuid,
147    pub title: String,
148    pub template_id: Base64Uuid,
149    pub created_at: Timestamp,
150    pub updated_at: Timestamp,
151}
152
153#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, Serialize, Display)]
154#[cfg_attr(
155    feature = "fp-bindgen",
156    derive(Serializable),
157    fp(rust_module = "fiberplane_models::notebooks")
158)]
159#[non_exhaustive]
160#[serde(rename_all = "snake_case")]
161pub enum NotebookVisibility {
162    Private,
163    Public,
164}
165
166impl Default for NotebookVisibility {
167    fn default() -> Self {
168        Self::Private
169    }
170}
171
172#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, Serialize, TypedBuilder)]
173#[cfg_attr(
174    feature = "fp-bindgen",
175    derive(Serializable),
176    fp(rust_module = "fiberplane_models::notebooks")
177)]
178#[non_exhaustive]
179#[serde(rename_all = "camelCase")]
180pub struct NotebookPatch {
181    pub visibility: NotebookVisibility,
182}
183
184#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize, TypedBuilder)]
185#[cfg_attr(
186    feature = "fp-bindgen",
187    derive(Serializable),
188    fp(rust_module = "fiberplane_models::notebooks")
189)]
190#[non_exhaustive]
191#[serde(rename_all = "camelCase")]
192pub struct NotebookCopyDestination {
193    #[builder(setter(into))]
194    pub title: String,
195    pub workspace_id: Base64Uuid,
196}
197
198#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, Serialize, TypedBuilder)]
199#[cfg_attr(
200    feature = "fp-bindgen",
201    derive(Serializable),
202    fp(rust_module = "fiberplane_models::notebooks")
203)]
204#[non_exhaustive]
205#[serde(rename_all = "camelCase")]
206pub struct NewPinnedNotebook {
207    /// The ID of the notebook that is being pinned.
208    pub notebook_id: Base64Uuid,
209}
210
211#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize, TypedBuilder)]
212#[non_exhaustive]
213#[serde(rename_all = "camelCase")]
214pub struct Trigger {
215    pub id: Base64Uuid,
216    #[builder(setter(into))]
217    pub title: String,
218    pub template_id: Base64Uuid,
219    #[builder(default, setter(into, strip_option))]
220    pub secret_key: Option<String>,
221    #[builder(default, setter(strip_option))]
222    pub default_arguments: Option<Map<String, Value>>,
223    #[serde(with = "time::serde::rfc3339")]
224    pub created_at: OffsetDateTime,
225    #[serde(with = "time::serde::rfc3339")]
226    pub updated_at: OffsetDateTime,
227}
228
229pub type TemplateExpandPayload = Map<String, Value>;
230
231#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize, TypedBuilder)]
232#[non_exhaustive]
233#[serde(rename_all = "camelCase")]
234pub struct TriggerInvokeResponse {
235    pub notebook_title: String,
236    pub notebook_id: Base64Uuid,
237    pub notebook_url: Url,
238}
239
240#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize, TypedBuilder)]
241#[cfg_attr(
242    feature = "fp-bindgen",
243    derive(Serializable),
244    fp(rust_module = "fiberplane_models::notebooks")
245)]
246#[non_exhaustive]
247#[serde(rename_all = "camelCase")]
248pub struct NotebookSummary {
249    pub id: Base64Uuid,
250    pub workspace_id: Base64Uuid,
251    #[serde(with = "time::serde::rfc3339")]
252    pub created_at: OffsetDateTime,
253    #[serde(with = "time::serde::rfc3339")]
254    pub updated_at: OffsetDateTime,
255    pub title: String,
256    pub visibility: NotebookVisibility,
257    pub created_by: CreatedBy,
258    pub labels: Vec<Label>,
259}
260
261/// Notebook search parameters
262#[derive(Debug, Clone, Default, PartialEq, Eq, Deserialize, Serialize, TypedBuilder)]
263#[cfg_attr(
264    feature = "fp-bindgen",
265    derive(Serializable),
266    fp(rust_module = "fiberplane_models::notebooks")
267)]
268#[non_exhaustive]
269#[serde(rename_all = "camelCase")]
270pub struct NotebookSearch {
271    #[builder(default)]
272    #[serde(default, skip_serializing_if = "Option::is_none")]
273    pub labels: Option<HashMap<String, Option<String>>>,
274    #[builder(default)]
275    #[serde(default, skip_serializing_if = "Option::is_none")]
276    pub relative_time: Option<RelativeTime>,
277    #[builder(default)]
278    #[serde(default, skip_serializing_if = "Option::is_none")]
279    pub view: Option<Name>,
280}
281
282#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize, TypedBuilder)]
283#[cfg_attr(
284    feature = "fp-bindgen",
285    derive(Serializable),
286    fp(rust_module = "fiberplane_models::notebooks")
287)]
288#[non_exhaustive]
289#[serde(rename_all = "camelCase")]
290pub struct TemplateSummary {
291    pub id: Base64Uuid,
292    pub name: Name,
293    pub description: String,
294    #[serde(with = "time::serde::rfc3339")]
295    pub created_at: OffsetDateTime,
296    #[serde(with = "time::serde::rfc3339")]
297    pub updated_at: OffsetDateTime,
298}
299
300#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize, TypedBuilder)]
301#[cfg_attr(
302    feature = "fp-bindgen",
303    derive(Serializable),
304    fp(rust_module = "fiberplane_models::notebooks")
305)]
306#[non_exhaustive]
307#[serde(rename_all = "camelCase")]
308pub struct NewTemplate {
309    pub name: Name,
310    #[builder(setter(into))]
311    pub description: String,
312    #[builder(setter(into))]
313    pub body: String,
314}
315
316impl NewTemplate {
317    pub fn new(name: Name, description: impl Into<String>, body: impl Into<String>) -> Self {
318        Self {
319            name,
320            description: description.into(),
321            body: body.into(),
322        }
323    }
324}
325
326#[derive(Debug, Clone, 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 UpdateTemplate {
335    #[builder(default, setter(into))]
336    pub description: Option<String>,
337    #[builder(default, setter(into))]
338    pub body: Option<String>,
339}
340
341#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize, TypedBuilder)]
342#[non_exhaustive]
343#[serde(rename_all = "camelCase")]
344pub struct NewTrigger {
345    #[builder(setter(into))]
346    pub title: String,
347    pub template_name: Name,
348    #[builder(default, setter(into, strip_option))]
349    pub default_arguments: Option<Map<String, Value>>,
350}
351
352#[derive(Debug, Default, Clone, PartialEq, Eq, Deserialize, Serialize, TypedBuilder)]
353#[cfg_attr(
354    feature = "fp-bindgen",
355    derive(Serializable),
356    fp(rust_module = "fiberplane_models::notebooks")
357)]
358#[non_exhaustive]
359#[serde(rename_all = "camelCase")]
360/// The query string values that are associated with the notebook_cells_append
361/// endpoint.
362pub struct NotebookCellsAppendQuery {
363    /// Append the provided cells before this cell.
364    /// If the cell is not found it will return a 400. This parameter cannot
365    /// be used together with `after`.
366    pub before: Option<String>,
367
368    /// Append the provided cells after this cell.
369    /// If the cell is not found it will return a 400. This parameter cannot
370    /// be used together with `before`.
371    pub after: Option<String>,
372}