-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackend.ex
More file actions
240 lines (170 loc) · 6.08 KB
/
Copy pathbackend.ex
File metadata and controls
240 lines (170 loc) · 6.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
defmodule Crucible.UI.Backend do
@moduledoc """
Behaviour defining the backend interface for Crucible UI feature module.
Host applications must implement this behaviour to provide data operations
for the Crucible UI components. This allows the UI to remain independent
of specific database implementations, Ecto repos, or business logic.
## Example Implementation
defmodule MyApp.CrucibleBackend do
@behaviour Crucible.UI.Backend
def list_experiments(opts \\\\ []) do
experiments = MyApp.Experiments.list_experiments()
{:ok, experiments}
end
def get_experiment(id) do
case MyApp.Experiments.get_experiment(id) do
nil -> {:error, :not_found}
experiment -> {:ok, experiment}
end
end
# ... implement other callbacks
end
## Usage in Router
experiment_routes "/experiments",
backend: MyApp.CrucibleBackend,
root_layout: {MyAppWeb.Layouts, :app}
"""
@type experiment :: map() | struct()
@type run :: map() | struct()
@type telemetry_event :: map() | struct()
@type statistics :: map() | struct()
@type opts :: keyword()
@type id :: term()
@type error :: term()
@doc """
Lists all experiments.
Backend implementations should return experiments as either Ecto structs or
plain maps. The UI components handle both transparently.
## Options
* `:status` - Filter by status (e.g., "pending", "running", "completed")
* `:limit` - Limit number of results
* `:order` - Order by field (default: `:inserted_at`)
## Returns
* `{:ok, [experiment()]}` - List of experiments
* `{:error, term()}` - Error tuple
"""
@callback list_experiments(opts :: opts()) :: {:ok, [experiment()]} | {:error, term()}
@doc """
Gets a single experiment by ID.
## Returns
* `{:ok, experiment()}` - The experiment
* `{:error, :not_found}` - Experiment not found
* `{:error, term()}` - Other error
"""
@callback get_experiment(id :: id()) :: {:ok, experiment()} | {:error, term()}
@doc """
Gets a single experiment with preloaded associations (runs, results).
## Returns
* `{:ok, experiment()}` - The experiment with associations
* `{:error, :not_found}` - Experiment not found
* `{:error, term()}` - Other error
"""
@callback get_experiment_with_associations(id :: id()) ::
{:ok, experiment()} | {:error, term()}
@doc """
Creates a new experiment.
## Parameters
* `attrs` - Attributes for the new experiment (name, description, config, etc.)
## Returns
* `{:ok, experiment()}` - The created experiment
* `{:error, changeset()}` - Validation errors
"""
@callback create_experiment(attrs :: map()) :: {:ok, experiment()} | {:error, term()}
@doc """
Updates an experiment.
## Parameters
* `id` - Experiment ID
* `attrs` - Attributes to update
## Returns
* `{:ok, experiment()}` - The updated experiment
* `{:error, changeset()}` - Validation errors
"""
@callback update_experiment(id :: id(), attrs :: map()) ::
{:ok, experiment()} | {:error, term()}
@doc """
Deletes an experiment.
## Returns
* `{:ok, experiment()}` - The deleted experiment
* `{:error, term()}` - Error tuple
"""
@callback delete_experiment(id :: id()) :: {:ok, experiment()} | {:error, term()}
@doc """
Starts an experiment (updates status to "running").
## Returns
* `{:ok, experiment()}` - The updated experiment
* `{:error, term()}` - Error tuple
"""
@callback start_experiment(id :: id()) :: {:ok, experiment()} | {:error, term()}
@doc """
Completes an experiment (updates status to "completed").
## Returns
* `{:ok, experiment()}` - The updated experiment
* `{:error, term()}` - Error tuple
"""
@callback complete_experiment(id :: id()) :: {:ok, experiment()} | {:error, term()}
@doc """
Lists runs for an experiment.
## Options
* `:status` - Filter by status
* `:limit` - Limit number of results
## Returns
* `{:ok, [run()]}` - List of runs
* `{:error, term()}` - Error tuple
"""
@callback list_runs(experiment_id :: id(), opts :: opts()) ::
{:ok, [run()]} | {:error, term()}
@doc """
Gets a single run by ID with associations (experiment, events).
## Returns
* `{:ok, run()}` - The run with associations
* `{:error, :not_found}` - Run not found
* `{:error, term()}` - Other error
"""
@callback get_run(id :: id()) :: {:ok, run()} | {:error, term()}
@doc """
Starts a run (updates status to "running").
## Returns
* `{:ok, run()}` - The updated run
* `{:error, term()}` - Error tuple
"""
@callback start_run(id :: id()) :: {:ok, run()} | {:error, term()}
@doc """
Completes a run (updates status to "completed").
## Returns
* `{:ok, run()}` - The updated run
* `{:error, term()}` - Error tuple
"""
@callback complete_run(id :: id()) :: {:ok, run()} | {:error, term()}
@doc """
Lists telemetry events for a run.
## Options
* `:limit` - Limit number of results (default: 100)
* `:event_type` - Filter by event type
## Returns
* `{:ok, [telemetry_event()]}` - List of events
* `{:error, term()}` - Error tuple
"""
@callback list_telemetry_events(run_id :: id(), opts :: opts()) ::
{:ok, [telemetry_event()]} | {:error, term()}
@doc """
Gets statistics for an experiment or run.
## Returns
* `{:ok, statistics()}` - Statistics data
* `{:error, term()}` - Error tuple
"""
@callback get_statistics(id :: id()) :: {:ok, statistics()} | {:error, term()}
@doc """
Gets aggregated statistics for the system (total experiments, runs, etc.).
## Returns
* `{:ok, map()}` - Aggregated statistics
"""
@callback get_system_statistics() :: {:ok, map()} | {:error, term()}
@doc """
Optional callback for custom PubSub topic for experiment updates.
If not implemented, defaults to "experiment:{id}".
## Returns
* `String.t()` - PubSub topic name
"""
@callback pubsub_topic(resource :: atom(), id :: id()) :: String.t()
@optional_callbacks pubsub_topic: 2
end