make more readable
This commit is contained in:
@@ -29,7 +29,6 @@ func NewFileMetricsStore(path string, logger *slog.Logger) *FileMetricsStore {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *FileMetricsStore) Append(ctx context.Context, taskName string, run models.TaskRunMetrics) error {
|
func (s *FileMetricsStore) Append(ctx context.Context, taskName string, run models.TaskRunMetrics) error {
|
||||||
|
|
||||||
if s.path == "" {
|
if s.path == "" {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -46,14 +45,8 @@ func (s *FileMetricsStore) Append(ctx context.Context, taskName string, run mode
|
|||||||
Runs: []models.TaskRunMetrics{},
|
Runs: []models.TaskRunMetrics{},
|
||||||
}
|
}
|
||||||
|
|
||||||
if bytes, err := os.ReadFile(s.path); err == nil {
|
if err := readMetrics(&metrics, s.path); err != nil {
|
||||||
if len(bytes) > 0 {
|
return err
|
||||||
if err := json.Unmarshal(bytes, &metrics); err != nil {
|
|
||||||
return fmt.Errorf("parse metrics file: %w", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else if !os.IsNotExist(err) {
|
|
||||||
return fmt.Errorf("read metrics file: %w", err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if metrics.TaskName == "" {
|
if metrics.TaskName == "" {
|
||||||
@@ -74,14 +67,24 @@ func (s *FileMetricsStore) Append(ctx context.Context, taskName string, run mode
|
|||||||
return fmt.Errorf("write metrics file: %w", err)
|
return fmt.Errorf("write metrics file: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if s.logger != nil {
|
if s.logger == nil {
|
||||||
s.logger.Info(
|
return fmt.Errorf("logger is nil")
|
||||||
"task metrics persisted",
|
|
||||||
"path", s.path,
|
|
||||||
"task", taskName,
|
|
||||||
"run_id", run.RunID,
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
s.logger.Info("task metrics persisted", "path", s.path, "task", taskName, "run_id", run.RunID)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func readMetrics(metrics *models.TaskMetricsFile, path string) error {
|
||||||
|
if bytes, err := os.ReadFile(path); err == nil {
|
||||||
|
if len(bytes) > 0 {
|
||||||
|
if err := json.Unmarshal(bytes, metrics); err != nil {
|
||||||
|
return fmt.Errorf("failed to parse metrics file: %w", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if !os.IsNotExist(err) {
|
||||||
|
return fmt.Errorf("failed to read metrics file: %w", err)
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user