aboutsummaryrefslogtreecommitdiff
path: root/weed/admin/view/app/maintenance_config_schema.templ
blob: ee89cab64261e92f0e087c034026c2ab6a0809fd (plain)
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
package app

import (
    "fmt"
    "github.com/seaweedfs/seaweedfs/weed/admin/maintenance"
    "github.com/seaweedfs/seaweedfs/weed/admin/config"
    "github.com/seaweedfs/seaweedfs/weed/admin/view/components"
)

templ MaintenanceConfigSchema(data *maintenance.MaintenanceConfigData, schema *maintenance.MaintenanceConfigSchema) {
    <div class="container-fluid">
        <div class="row mb-4">
            <div class="col-12">
                <div class="d-flex justify-content-between align-items-center">
                    <h2 class="mb-0">
                        <i class="fas fa-cogs me-2"></i>
                        Maintenance Configuration
                    </h2>
                    <div class="btn-group">
                        <a href="/maintenance/tasks" class="btn btn-outline-primary">
                            <i class="fas fa-tasks me-1"></i>
                            View Tasks
                        </a>
                    </div>
                </div>
            </div>
        </div>

        <div class="row">
            <div class="col-12">
                <div class="card">
                    <div class="card-header">
                        <h5 class="mb-0">System Settings</h5>
                    </div>
                    <div class="card-body">
                        <form id="maintenanceConfigForm">
                            <!-- Dynamically render all schema fields in order -->
                            for _, field := range schema.Fields {
                                @ConfigField(field, data.Config)
                            }

                            <div class="d-flex gap-2">
                                <button type="button" class="btn btn-primary" onclick="saveConfiguration()">
                                    <i class="fas fa-save me-1"></i>
                                    Save Configuration
                                </button>
                                <button type="button" class="btn btn-secondary" onclick="resetToDefaults()">
                                    <i class="fas fa-undo me-1"></i>
                                    Reset to Defaults
                                </button>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>

        <!-- Task Configuration Cards -->
        <div class="row mt-4">
            <div class="col-md-4">
                <div class="card">
                    <div class="card-header">
                        <h5 class="mb-0">
                            <i class="fas fa-broom me-2"></i>
                            Volume Vacuum
                        </h5>
                    </div>
                    <div class="card-body">
                        <p class="card-text">Reclaims disk space by removing deleted files from volumes.</p>
                        <a href="/maintenance/config/vacuum" class="btn btn-primary">Configure</a>
                    </div>
                </div>
            </div>
            <div class="col-md-4">
                <div class="card">
                    <div class="card-header">
                        <h5 class="mb-0">
                            <i class="fas fa-balance-scale me-2"></i>
                            Volume Balance
                        </h5>
                    </div>
                    <div class="card-body">
                        <p class="card-text">Redistributes volumes across servers to optimize storage utilization.</p>
                        <a href="/maintenance/config/balance" class="btn btn-primary">Configure</a>
                    </div>
                </div>
            </div>
            <div class="col-md-4">
                <div class="card">
                    <div class="card-header">
                        <h5 class="mb-0">
                            <i class="fas fa-shield-alt me-2"></i>
                            Erasure Coding
                        </h5>
                    </div>
                    <div class="card-body">
                        <p class="card-text">Converts volumes to erasure coded format for improved durability.</p>
                        <a href="/maintenance/config/erasure_coding" class="btn btn-primary">Configure</a>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script>
        function saveConfiguration() {
            const form = document.getElementById('maintenanceConfigForm');
            const formData = new FormData(form);
            
            // Convert form data to JSON, handling interval fields specially
            const config = {};
            
            for (let [key, value] of formData.entries()) {
                if (key.endsWith('_value')) {
                    // This is an interval value part
                    const baseKey = key.replace('_value', '');
                    const unitKey = baseKey + '_unit';
                    const unitValue = formData.get(unitKey);
                    
                    if (unitValue) {
                        // Convert to seconds based on unit
                        const numValue = parseInt(value) || 0;
                        let seconds = numValue;
                        switch(unitValue) {
                            case 'minutes':
                                seconds = numValue * 60;
                                break;
                            case 'hours':
                                seconds = numValue * 3600;
                                break;
                            case 'days':
                                seconds = numValue * 24 * 3600;
                                break;
                        }
                        config[baseKey] = seconds;
                    }
                } else if (key.endsWith('_unit')) {
                    // Skip unit keys - they're handled with their corresponding value
                    continue;
                } else {
                    // Regular field
                    if (form.querySelector(`[name="${key}"]`).type === 'checkbox') {
                        config[key] = form.querySelector(`[name="${key}"]`).checked;
                    } else {
                        const numValue = parseFloat(value);
                        config[key] = isNaN(numValue) ? value : numValue;
                    }
                }
            }

            fetch('/api/maintenance/config', {
                method: 'PUT',
                headers: {
                    'Content-Type': 'application/json',
                },
                body: JSON.stringify(config)
            })
            .then(response => {
                if (response.status === 401) {
                    alert('Authentication required. Please log in first.');
                    window.location.href = '/login';
                    return;
                }
                return response.json();
            })
            .then(data => {
                if (!data) return; // Skip if redirected to login
                if (data.success) {
                    alert('Configuration saved successfully!');
                    location.reload();
                } else {
                    alert('Error saving configuration: ' + (data.error || 'Unknown error'));
                }
            })
            .catch(error => {
                console.error('Error:', error);
                alert('Error saving configuration: ' + error.message);
            });
        }

        function resetToDefaults() {
            if (confirm('Are you sure you want to reset to default configuration? This will overwrite your current settings.')) {
                fetch('/maintenance/config/defaults', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    }
                })
                .then(response => response.json())
                .then(data => {
                    if (data.success) {
                        alert('Configuration reset to defaults!');
                        location.reload();
                    } else {
                        alert('Error resetting configuration: ' + (data.error || 'Unknown error'));
                    }
                })
                .catch(error => {
                    console.error('Error:', error);
                    alert('Error resetting configuration: ' + error.message);
                });
            }
        }
    </script>
}

// ConfigField renders a single configuration field based on schema with typed value lookup
templ ConfigField(field *config.Field, config *maintenance.MaintenanceConfig) {
    if field.InputType == "interval" {
        <!-- Interval field with number input + unit dropdown -->
        <div class="mb-3">
            <label for={ field.JSONName } class="form-label">
                { field.DisplayName }
                if field.Required {
                    <span class="text-danger">*</span>
                }
            </label>
            <div class="input-group">
                <input 
                    type="number" 
                    class="form-control" 
                    id={ field.JSONName + "_value" } 
                    name={ field.JSONName + "_value" } 
                    value={ fmt.Sprintf("%.0f", components.ConvertInt32SecondsToDisplayValue(getMaintenanceInt32Field(config, field.JSONName))) }
                    step="1"
                    min="1"
                    if field.Required {
                        required
                    }
                />
                <select 
                    class="form-select" 
                    id={ field.JSONName + "_unit" } 
                    name={ field.JSONName + "_unit" }
                    style="max-width: 120px;"
                    if field.Required {
                        required
                    }
                >
                    <option 
                        value="minutes"
                        if components.GetInt32DisplayUnit(getMaintenanceInt32Field(config, field.JSONName)) == "minutes" {
                            selected
                        }
                    >
                        Minutes
                    </option>
                    <option 
                        value="hours"
                        if components.GetInt32DisplayUnit(getMaintenanceInt32Field(config, field.JSONName)) == "hours" {
                            selected
                        }
                    >
                        Hours
                    </option>
                    <option 
                        value="days"
                        if components.GetInt32DisplayUnit(getMaintenanceInt32Field(config, field.JSONName)) == "days" {
                            selected
                        }
                    >
                        Days
                    </option>
                </select>
            </div>
            if field.Description != "" {
                <div class="form-text text-muted">{ field.Description }</div>
            }
        </div>
    } else if field.InputType == "checkbox" {
        <!-- Checkbox field -->
        <div class="mb-3">
            <div class="form-check form-switch">
                <input 
                    class="form-check-input"
                    type="checkbox" 
                    id={ field.JSONName }
                    name={ field.JSONName }
                    if getMaintenanceBoolField(config, field.JSONName) {
                        checked
                    }
                />
                <label class="form-check-label" for={ field.JSONName }>
                    <strong>{ field.DisplayName }</strong>
                </label>
            </div>
            if field.Description != "" {
                <div class="form-text text-muted">{ field.Description }</div>
            }
        </div>
    } else {
        <!-- Number field -->
        <div class="mb-3">
            <label for={ field.JSONName } class="form-label">
                { field.DisplayName }
                if field.Required {
                    <span class="text-danger">*</span>
                }
            </label>
            <input 
                type="number" 
                class="form-control" 
                id={ field.JSONName }
                name={ field.JSONName }
                value={ fmt.Sprintf("%d", getMaintenanceInt32Field(config, field.JSONName)) }
                placeholder={ field.Placeholder }
                if field.MinValue != nil {
                    min={ fmt.Sprintf("%v", field.MinValue) }
                }
                if field.MaxValue != nil {
                    max={ fmt.Sprintf("%v", field.MaxValue) }
                }
                step={ getNumberStep(field) }
                if field.Required {
                    required
                }
            />
            if field.Description != "" {
                <div class="form-text text-muted">{ field.Description }</div>
            }
        </div>
    }
}

// Helper functions for form field types

func getNumberStep(field *config.Field) string {
    if field.Type == config.FieldTypeFloat {
        return "0.01"
    }
    return "1"
}

// Typed field getters for MaintenanceConfig - no interface{} needed
func getMaintenanceInt32Field(config *maintenance.MaintenanceConfig, fieldName string) int32 {
    if config == nil {
        return 0
    }
    
    switch fieldName {
    case "scan_interval_seconds":
        return config.ScanIntervalSeconds
    case "worker_timeout_seconds":
        return config.WorkerTimeoutSeconds
    case "task_timeout_seconds":
        return config.TaskTimeoutSeconds
    case "retry_delay_seconds":
        return config.RetryDelaySeconds
    case "max_retries":
        return config.MaxRetries
    case "cleanup_interval_seconds":
        return config.CleanupIntervalSeconds
    case "task_retention_seconds":
        return config.TaskRetentionSeconds
    case "global_max_concurrent":
        if config.Policy != nil {
            return config.Policy.GlobalMaxConcurrent
        }
        return 0
    default:
        return 0
    }
}

func getMaintenanceBoolField(config *maintenance.MaintenanceConfig, fieldName string) bool {
    if config == nil {
        return false
    }
    
    switch fieldName {
    case "enabled":
        return config.Enabled
    default:
        return false
    }
}

// Helper function to convert schema to JSON for JavaScript
templ schemaToJSON(schema *maintenance.MaintenanceConfigSchema) {
    {`{}`}
}