aboutsummaryrefslogtreecommitdiff
path: root/weed/admin/handlers/maintenance_handlers.go
blob: 4b1f9138701c17afd4be6bf47c9bef3575457220 (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
382
383
package handlers

import (
	"net/http"
	"time"

	"github.com/gin-gonic/gin"
	"github.com/seaweedfs/seaweedfs/weed/admin/dash"
	"github.com/seaweedfs/seaweedfs/weed/admin/maintenance"
	"github.com/seaweedfs/seaweedfs/weed/admin/view/app"
	"github.com/seaweedfs/seaweedfs/weed/admin/view/components"
	"github.com/seaweedfs/seaweedfs/weed/admin/view/layout"
	"github.com/seaweedfs/seaweedfs/weed/worker/tasks"
	"github.com/seaweedfs/seaweedfs/weed/worker/types"
)

// MaintenanceHandlers handles maintenance-related HTTP requests
type MaintenanceHandlers struct {
	adminServer *dash.AdminServer
}

// NewMaintenanceHandlers creates a new instance of MaintenanceHandlers
func NewMaintenanceHandlers(adminServer *dash.AdminServer) *MaintenanceHandlers {
	return &MaintenanceHandlers{
		adminServer: adminServer,
	}
}

// ShowMaintenanceQueue displays the maintenance queue page
func (h *MaintenanceHandlers) ShowMaintenanceQueue(c *gin.Context) {
	data, err := h.getMaintenanceQueueData()
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}

	// Render HTML template
	c.Header("Content-Type", "text/html")
	maintenanceComponent := app.MaintenanceQueue(data)
	layoutComponent := layout.Layout(c, maintenanceComponent)
	err = layoutComponent.Render(c.Request.Context(), c.Writer)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to render template: " + err.Error()})
		return
	}
}

// ShowMaintenanceWorkers displays the maintenance workers page
func (h *MaintenanceHandlers) ShowMaintenanceWorkers(c *gin.Context) {
	workersData, err := h.adminServer.GetMaintenanceWorkersData()
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}

	// Render HTML template
	c.Header("Content-Type", "text/html")
	workersComponent := app.MaintenanceWorkers(workersData)
	layoutComponent := layout.Layout(c, workersComponent)
	err = layoutComponent.Render(c.Request.Context(), c.Writer)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to render template: " + err.Error()})
		return
	}
}

// ShowMaintenanceConfig displays the maintenance configuration page
func (h *MaintenanceHandlers) ShowMaintenanceConfig(c *gin.Context) {
	config, err := h.getMaintenanceConfig()
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}

	// Render HTML template
	c.Header("Content-Type", "text/html")
	configComponent := app.MaintenanceConfig(config)
	layoutComponent := layout.Layout(c, configComponent)
	err = layoutComponent.Render(c.Request.Context(), c.Writer)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to render template: " + err.Error()})
		return
	}
}

// ShowTaskConfig displays the configuration page for a specific task type
func (h *MaintenanceHandlers) ShowTaskConfig(c *gin.Context) {
	taskTypeName := c.Param("taskType")

	// Get the task type
	taskType := maintenance.GetMaintenanceTaskType(taskTypeName)
	if taskType == "" {
		c.JSON(http.StatusNotFound, gin.H{"error": "Task type not found"})
		return
	}

	// Get the UI provider for this task type
	uiRegistry := tasks.GetGlobalUIRegistry()
	typesRegistry := tasks.GetGlobalTypesRegistry()

	var provider types.TaskUIProvider
	for workerTaskType := range typesRegistry.GetAllDetectors() {
		if string(workerTaskType) == string(taskType) {
			provider = uiRegistry.GetProvider(workerTaskType)
			break
		}
	}

	if provider == nil {
		c.JSON(http.StatusNotFound, gin.H{"error": "UI provider not found for task type"})
		return
	}

	// Try to get templ UI provider first - temporarily disabled
	// templUIProvider := getTemplUIProvider(taskType)
	var configSections []components.ConfigSectionData

	// Temporarily disabled templ UI provider
	// if templUIProvider != nil {
	//	// Use the new templ-based UI provider
	//	currentConfig := templUIProvider.GetCurrentConfig()
	//	sections, err := templUIProvider.RenderConfigSections(currentConfig)
	//	if err != nil {
	//		c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to render configuration sections: " + err.Error()})
	//		return
	//	}
	//	configSections = sections
	// } else {
	// Fallback to basic configuration for providers that haven't been migrated yet
	configSections = []components.ConfigSectionData{
		{
			Title:       "Configuration Settings",
			Icon:        "fas fa-cogs",
			Description: "Configure task detection and scheduling parameters",
			Fields: []interface{}{
				components.CheckboxFieldData{
					FormFieldData: components.FormFieldData{
						Name:        "enabled",
						Label:       "Enable Task",
						Description: "Whether this task type should be enabled",
					},
					Checked: true,
				},
				components.NumberFieldData{
					FormFieldData: components.FormFieldData{
						Name:        "max_concurrent",
						Label:       "Max Concurrent Tasks",
						Description: "Maximum number of concurrent tasks",
						Required:    true,
					},
					Value: 2,
					Step:  "1",
					Min:   floatPtr(1),
				},
				components.DurationFieldData{
					FormFieldData: components.FormFieldData{
						Name:        "scan_interval",
						Label:       "Scan Interval",
						Description: "How often to scan for tasks",
						Required:    true,
					},
					Value: "30m",
				},
			},
		},
	}
	// } // End of disabled templ UI provider else block

	// Create task configuration data using templ components
	configData := &app.TaskConfigTemplData{
		TaskType:       taskType,
		TaskName:       provider.GetDisplayName(),
		TaskIcon:       provider.GetIcon(),
		Description:    provider.GetDescription(),
		ConfigSections: configSections,
	}

	// Render HTML template using templ components
	c.Header("Content-Type", "text/html")
	taskConfigComponent := app.TaskConfigTempl(configData)
	layoutComponent := layout.Layout(c, taskConfigComponent)
	err := layoutComponent.Render(c.Request.Context(), c.Writer)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to render template: " + err.Error()})
		return
	}
}

// UpdateTaskConfig updates configuration for a specific task type
func (h *MaintenanceHandlers) UpdateTaskConfig(c *gin.Context) {
	taskTypeName := c.Param("taskType")

	// Get the task type
	taskType := maintenance.GetMaintenanceTaskType(taskTypeName)
	if taskType == "" {
		c.JSON(http.StatusNotFound, gin.H{"error": "Task type not found"})
		return
	}

	// Try to get templ UI provider first - temporarily disabled
	// templUIProvider := getTemplUIProvider(taskType)

	// Parse form data
	err := c.Request.ParseForm()
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": "Failed to parse form data: " + err.Error()})
		return
	}

	// Convert form data to map
	formData := make(map[string][]string)
	for key, values := range c.Request.PostForm {
		formData[key] = values
	}

	var config interface{}

	// Temporarily disabled templ UI provider
	// if templUIProvider != nil {
	//	// Use the new templ-based UI provider
	//	config, err = templUIProvider.ParseConfigForm(formData)
	//	if err != nil {
	//		c.JSON(http.StatusBadRequest, gin.H{"error": "Failed to parse configuration: " + err.Error()})
	//		return
	//	}
	//	// Apply configuration using templ provider
	//	err = templUIProvider.ApplyConfig(config)
	//	if err != nil {
	//		c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to apply configuration: " + err.Error()})
	//		return
	//	}
	// } else {
	// Fallback to old UI provider for tasks that haven't been migrated yet
	// Fallback to old UI provider for tasks that haven't been migrated yet
	uiRegistry := tasks.GetGlobalUIRegistry()
	typesRegistry := tasks.GetGlobalTypesRegistry()

	var provider types.TaskUIProvider
	for workerTaskType := range typesRegistry.GetAllDetectors() {
		if string(workerTaskType) == string(taskType) {
			provider = uiRegistry.GetProvider(workerTaskType)
			break
		}
	}

	if provider == nil {
		c.JSON(http.StatusNotFound, gin.H{"error": "UI provider not found for task type"})
		return
	}

	// Parse configuration from form using old provider
	config, err = provider.ParseConfigForm(formData)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": "Failed to parse configuration: " + err.Error()})
		return
	}

	// Apply configuration using old provider
	err = provider.ApplyConfig(config)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to apply configuration: " + err.Error()})
		return
	}
	// } // End of disabled templ UI provider else block

	// Redirect back to task configuration page
	c.Redirect(http.StatusSeeOther, "/maintenance/config/"+taskTypeName)
}

// UpdateMaintenanceConfig updates maintenance configuration from form
func (h *MaintenanceHandlers) UpdateMaintenanceConfig(c *gin.Context) {
	var config maintenance.MaintenanceConfig
	if err := c.ShouldBind(&config); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	err := h.updateMaintenanceConfig(&config)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}

	c.Redirect(http.StatusSeeOther, "/maintenance/config")
}

// Helper methods that delegate to AdminServer

func (h *MaintenanceHandlers) getMaintenanceQueueData() (*maintenance.MaintenanceQueueData, error) {
	tasks, err := h.getMaintenanceTasks()
	if err != nil {
		return nil, err
	}

	workers, err := h.getMaintenanceWorkers()
	if err != nil {
		return nil, err
	}

	stats, err := h.getMaintenanceQueueStats()
	if err != nil {
		return nil, err
	}

	return &maintenance.MaintenanceQueueData{
		Tasks:       tasks,
		Workers:     workers,
		Stats:       stats,
		LastUpdated: time.Now(),
	}, nil
}

func (h *MaintenanceHandlers) getMaintenanceQueueStats() (*maintenance.QueueStats, error) {
	// This would integrate with the maintenance queue to get real statistics
	// For now, return mock data
	return &maintenance.QueueStats{
		PendingTasks:   5,
		RunningTasks:   2,
		CompletedToday: 15,
		FailedToday:    1,
		TotalTasks:     23,
	}, nil
}

func (h *MaintenanceHandlers) getMaintenanceTasks() ([]*maintenance.MaintenanceTask, error) {
	// This would integrate with the maintenance queue to get real tasks
	// For now, return mock data
	return []*maintenance.MaintenanceTask{}, nil
}

func (h *MaintenanceHandlers) getMaintenanceWorkers() ([]*maintenance.MaintenanceWorker, error) {
	// This would integrate with the maintenance system to get real workers
	// For now, return mock data
	return []*maintenance.MaintenanceWorker{}, nil
}

func (h *MaintenanceHandlers) getMaintenanceConfig() (*maintenance.MaintenanceConfigData, error) {
	// Delegate to AdminServer's real persistence method
	return h.adminServer.GetMaintenanceConfigData()
}

func (h *MaintenanceHandlers) updateMaintenanceConfig(config *maintenance.MaintenanceConfig) error {
	// Delegate to AdminServer's real persistence method
	return h.adminServer.UpdateMaintenanceConfigData(config)
}

// floatPtr is a helper function to create float64 pointers
func floatPtr(f float64) *float64 {
	return &f
}

// Global templ UI registry - temporarily disabled
// var globalTemplUIRegistry *types.UITemplRegistry

// initTemplUIRegistry initializes the global templ UI registry - temporarily disabled
func initTemplUIRegistry() {
	// Temporarily disabled due to missing types
	// if globalTemplUIRegistry == nil {
	//	globalTemplUIRegistry = types.NewUITemplRegistry()
	//	// Register vacuum templ UI provider using shared instances
	//	vacuumDetector, vacuumScheduler := vacuum.GetSharedInstances()
	//	vacuum.RegisterUITempl(globalTemplUIRegistry, vacuumDetector, vacuumScheduler)
	//	// Register erasure coding templ UI provider using shared instances
	//	erasureCodingDetector, erasureCodingScheduler := erasure_coding.GetSharedInstances()
	//	erasure_coding.RegisterUITempl(globalTemplUIRegistry, erasureCodingDetector, erasureCodingScheduler)
	//	// Register balance templ UI provider using shared instances
	//	balanceDetector, balanceScheduler := balance.GetSharedInstances()
	//	balance.RegisterUITempl(globalTemplUIRegistry, balanceDetector, balanceScheduler)
	// }
}

// getTemplUIProvider gets the templ UI provider for a task type - temporarily disabled
func getTemplUIProvider(taskType maintenance.MaintenanceTaskType) interface{} {
	// initTemplUIRegistry()
	// Convert maintenance task type to worker task type
	// typesRegistry := tasks.GetGlobalTypesRegistry()
	// for workerTaskType := range typesRegistry.GetAllDetectors() {
	//	if string(workerTaskType) == string(taskType) {
	//		return globalTemplUIRegistry.GetProvider(workerTaskType)
	//	}
	// }
	return nil
}