github.com/influxdata/influxdb/v2@v2.7.6/notebook.go (about) 1 package influxdb 2 3 import ( 4 "context" 5 "database/sql/driver" 6 "encoding/json" 7 "fmt" 8 "strings" 9 "time" 10 11 "github.com/influxdata/influxdb/v2/kit/platform" 12 "github.com/influxdata/influxdb/v2/kit/platform/errors" 13 ) 14 15 var ( 16 ErrOrgIDRequired = fieldRequiredError("OrgID") 17 ErrNameRequired = fieldRequiredError("Name") 18 ErrSpecRequired = fieldRequiredError("Spec") 19 ErrOffsetNegative = &errors.Error{ 20 Code: errors.EInvalid, 21 Msg: "offset cannot be negative", 22 } 23 ErrLimitLTEZero = &errors.Error{ 24 Code: errors.EInvalid, 25 Msg: "limit cannot be less-than or equal-to zero", 26 } 27 ErrNotebookNotFound = &errors.Error{ 28 Code: errors.ENotFound, 29 Msg: "notebook not found", 30 } 31 ) 32 33 func fieldRequiredError(field string) error { 34 return &errors.Error{ 35 Code: errors.EInvalid, 36 Msg: fmt.Sprintf("%s required", field), 37 } 38 } 39 40 // Notebook represents all visual and query data for a notebook. 41 type Notebook struct { 42 OrgID platform.ID `json:"orgID" db:"org_id"` 43 ID platform.ID `json:"id" db:"id"` 44 Name string `json:"name" db:"name"` 45 Spec NotebookSpec `json:"spec" db:"spec"` 46 CreatedAt time.Time `json:"createdAt" db:"created_at"` 47 UpdatedAt time.Time `json:"updatedAt" db:"updated_at"` 48 } 49 50 // NotebookSpec is an abitrary JSON object provided by the client. 51 type NotebookSpec map[string]interface{} 52 53 // Value implements the database/sql Valuer interface for adding NotebookSpecs to the database. 54 func (s NotebookSpec) Value() (driver.Value, error) { 55 spec, err := json.Marshal(s) 56 if err != nil { 57 return nil, err 58 } 59 60 return string(spec), nil 61 } 62 63 // Scan implements the database/sql Scanner interface for retrieving NotebookSpecs from the database. 64 func (s *NotebookSpec) Scan(value interface{}) error { 65 var spec NotebookSpec 66 if err := json.NewDecoder(strings.NewReader(value.(string))).Decode(&spec); err != nil { 67 return err 68 } 69 70 *s = spec 71 return nil 72 } 73 74 // NotebookService is the service contract for Notebooks. 75 type NotebookService interface { 76 GetNotebook(ctx context.Context, id platform.ID) (*Notebook, error) 77 CreateNotebook(ctx context.Context, create *NotebookReqBody) (*Notebook, error) 78 UpdateNotebook(ctx context.Context, id platform.ID, update *NotebookReqBody) (*Notebook, error) 79 DeleteNotebook(ctx context.Context, id platform.ID) error 80 ListNotebooks(ctx context.Context, filter NotebookListFilter) ([]*Notebook, error) 81 } 82 83 // NotebookListFilter is a selection filter for listing notebooks. 84 type NotebookListFilter struct { 85 OrgID platform.ID 86 Page Page 87 } 88 89 // Page contains pagination information 90 type Page struct { 91 Offset int 92 Limit int 93 } 94 95 // Validate validates the Page 96 func (p Page) Validate() error { 97 if p.Offset < 0 { 98 return ErrOffsetNegative 99 } 100 if p.Limit <= 0 { 101 return ErrLimitLTEZero 102 } 103 return nil 104 } 105 106 // NotebookReqBody contains fields for creating or updating notebooks. 107 type NotebookReqBody struct { 108 OrgID platform.ID `json:"orgID"` 109 Name string `json:"name"` 110 Spec NotebookSpec `json:"spec"` 111 } 112 113 // Validate validates the creation object 114 func (n NotebookReqBody) Validate() error { 115 if !n.OrgID.Valid() { 116 return ErrOrgIDRequired 117 } 118 if n.Name == "" { 119 return ErrNameRequired 120 } 121 if n.Spec == nil { 122 return ErrSpecRequired 123 } 124 return nil 125 }