74 lines
2.5 KiB
Go
74 lines
2.5 KiB
Go
|
|
package database
|
||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"fmt"
|
||
|
|
|
||
|
|
"modelRT/constants"
|
||
|
|
"modelRT/model"
|
||
|
|
modelsql "modelRT/sql"
|
||
|
|
|
||
|
|
"gorm.io/gorm"
|
||
|
|
)
|
||
|
|
|
||
|
|
// QueryMeasurementInitializationRecords loads every measurement that can be
|
||
|
|
// addressed through the seven-part, four-part, and two-part token forms.
|
||
|
|
func QueryMeasurementInitializationRecords(ctx context.Context, db *gorm.DB) ([]model.MeasurementInitializationRecord, error) {
|
||
|
|
if db == nil {
|
||
|
|
return nil, fmt.Errorf("postgres client is nil")
|
||
|
|
}
|
||
|
|
|
||
|
|
var records []model.MeasurementInitializationRecord
|
||
|
|
if err := db.WithContext(ctx).
|
||
|
|
Raw(compactMeasurementSQL(modelsql.MeasurementInitializationRows)).
|
||
|
|
Scan(&records).Error; err != nil {
|
||
|
|
return nil, fmt.Errorf("query measurement initialization records: %w", err)
|
||
|
|
}
|
||
|
|
if err := validateMeasurementInitializationRecords(records); err != nil {
|
||
|
|
return nil, fmt.Errorf("validate measurement initialization records: %w", err)
|
||
|
|
}
|
||
|
|
return records, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func validateMeasurementInitializationRecords(records []model.MeasurementInitializationRecord) error {
|
||
|
|
for _, record := range records {
|
||
|
|
if record.MeasurementID <= 0 {
|
||
|
|
return fmt.Errorf("measurement %q has invalid id %d", record.MeasurementTag, record.MeasurementID)
|
||
|
|
}
|
||
|
|
if record.ComponentUUID == "" {
|
||
|
|
return fmt.Errorf("measurement %q has empty component uuid", record.MeasurementTag)
|
||
|
|
}
|
||
|
|
if record.GridTag == "" ||
|
||
|
|
record.ZoneTag == "" ||
|
||
|
|
record.StationTag == "" ||
|
||
|
|
record.ComponentNSPath == "" ||
|
||
|
|
record.ComponentTag == "" ||
|
||
|
|
record.MeasurementTag == "" {
|
||
|
|
return fmt.Errorf("measurement %d contains an empty data-object token segment", record.MeasurementID)
|
||
|
|
}
|
||
|
|
if record.MeasurementMode != constants.MeasurementModeManual &&
|
||
|
|
record.MeasurementMode != constants.MeasurementModeAutomatic {
|
||
|
|
return fmt.Errorf(
|
||
|
|
"measurement %q mode must be %d or %d, got %d",
|
||
|
|
record.MeasurementTag,
|
||
|
|
constants.MeasurementModeManual,
|
||
|
|
constants.MeasurementModeAutomatic,
|
||
|
|
record.MeasurementMode,
|
||
|
|
)
|
||
|
|
}
|
||
|
|
if _, err := model.MeasurementTypeString(record.MeasurementType); err != nil {
|
||
|
|
return fmt.Errorf("measurement %q: %w", record.MeasurementTag, err)
|
||
|
|
}
|
||
|
|
if record.MeasurementDataSource == nil {
|
||
|
|
return fmt.Errorf("measurement %q has null data_source", record.MeasurementTag)
|
||
|
|
}
|
||
|
|
if record.MeasurementEventPlan == nil {
|
||
|
|
return fmt.Errorf("measurement %q has null event_plan", record.MeasurementTag)
|
||
|
|
}
|
||
|
|
if record.MeasurementBinding == nil {
|
||
|
|
return fmt.Errorf("measurement %q has null binding", record.MeasurementTag)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return nil
|
||
|
|
}
|