You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
425 lines
15 KiB
425 lines
15 KiB
package app
|
|
|
|
import (
|
|
"fmt"
|
|
"reflect"
|
|
"strings"
|
|
"github.com/seaweedfs/seaweedfs/weed/admin/maintenance"
|
|
"github.com/seaweedfs/seaweedfs/weed/admin/config"
|
|
)
|
|
|
|
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, getMaintenanceFieldValue(data.Config, field.JSONName))
|
|
}
|
|
|
|
<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('/maintenance/config', {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
},
|
|
body: JSON.stringify(config)
|
|
})
|
|
.then(response => response.json())
|
|
.then(data => {
|
|
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
|
|
templ ConfigField(field *config.Field, value interface{}) {
|
|
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", convertSecondsToDisplayValue(value, field)) }
|
|
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 getDisplayUnit(value, field) == "minutes" {
|
|
selected
|
|
}
|
|
>
|
|
Minutes
|
|
</option>
|
|
<option
|
|
value="hours"
|
|
if getDisplayUnit(value, field) == "hours" {
|
|
selected
|
|
}
|
|
>
|
|
Hours
|
|
</option>
|
|
<option
|
|
value="days"
|
|
if getDisplayUnit(value, field) == "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 getBoolValue(value) {
|
|
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("%v", value) }
|
|
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 the template
|
|
func getBoolValue(value interface{}) bool {
|
|
if boolVal, ok := value.(bool); ok {
|
|
return boolVal
|
|
}
|
|
return false
|
|
}
|
|
|
|
func convertSecondsToDisplayValue(value interface{}, field *config.Field) float64 {
|
|
if intVal, ok := value.(int); ok {
|
|
if intVal == 0 {
|
|
return 0
|
|
}
|
|
|
|
// Check if it's evenly divisible by days
|
|
if intVal%(24*3600) == 0 {
|
|
return float64(intVal / (24 * 3600))
|
|
}
|
|
|
|
// Check if it's evenly divisible by hours
|
|
if intVal%3600 == 0 {
|
|
return float64(intVal / 3600)
|
|
}
|
|
|
|
// Default to minutes
|
|
return float64(intVal / 60)
|
|
}
|
|
return 0
|
|
}
|
|
|
|
func getDisplayUnit(value interface{}, field *config.Field) string {
|
|
if intVal, ok := value.(int); ok {
|
|
if intVal == 0 {
|
|
return "minutes"
|
|
}
|
|
|
|
// Check if it's evenly divisible by days
|
|
if intVal%(24*3600) == 0 {
|
|
return "days"
|
|
}
|
|
|
|
// Check if it's evenly divisible by hours
|
|
if intVal%3600 == 0 {
|
|
return "hours"
|
|
}
|
|
|
|
// Default to minutes
|
|
return "minutes"
|
|
}
|
|
return "minutes"
|
|
}
|
|
|
|
func getNumberStep(field *config.Field) string {
|
|
if field.Type == config.FieldTypeFloat {
|
|
return "0.01"
|
|
}
|
|
return "1"
|
|
}
|
|
|
|
func getMaintenanceFieldValue(config *maintenance.MaintenanceConfig, fieldName string) interface{} {
|
|
if config == nil {
|
|
return nil
|
|
}
|
|
|
|
// Use reflection to get the field value from the config struct
|
|
configValue := reflect.ValueOf(config)
|
|
if configValue.Kind() == reflect.Ptr {
|
|
configValue = configValue.Elem()
|
|
}
|
|
|
|
if configValue.Kind() != reflect.Struct {
|
|
return nil
|
|
}
|
|
|
|
configType := configValue.Type()
|
|
|
|
for i := 0; i < configValue.NumField(); i++ {
|
|
field := configValue.Field(i)
|
|
fieldType := configType.Field(i)
|
|
|
|
// Get JSON tag name
|
|
jsonTag := fieldType.Tag.Get("json")
|
|
if jsonTag == "" {
|
|
continue
|
|
}
|
|
|
|
// Remove options like ",omitempty"
|
|
if commaIdx := strings.Index(jsonTag, ","); commaIdx > 0 {
|
|
jsonTag = jsonTag[:commaIdx]
|
|
}
|
|
|
|
if jsonTag == fieldName {
|
|
return field.Interface()
|
|
}
|
|
}
|
|
|
|
// Handle nested Policy fields
|
|
if config.Policy != nil && fieldName == "global_max_concurrent" {
|
|
return config.Policy.GlobalMaxConcurrent
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// Helper function to convert schema to JSON for JavaScript
|
|
templ schemaToJSON(schema *maintenance.MaintenanceConfigSchema) {
|
|
{`{}`}
|
|
}
|