• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mlange-42 / arche-model / 9075130965

14 May 2024 07:02AM CUT coverage: 98.379%. Remained the same
9075130965

push

github

web-flow
Fix latest heading in CHANGELOG (#63)

607 of 617 relevant lines covered (98.38%)

146.19 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

86.49
/reporter/csv.go
1
package reporter
2

3
import (
4
        "fmt"
5
        "os"
6
        "path/filepath"
7
        "strings"
8

9
        "github.com/mlange-42/arche-model/observer"
10
        "github.com/mlange-42/arche/ecs"
11
)
12

13
// CSV reporter.
14
//
15
// Writes one row to a CSV file per step.
16
type CSV struct {
17
        Observer       observer.Row // Observer to get data from.
18
        File           string       // Path to the output file.
19
        Sep            string       // Column separator. Default ",".
20
        UpdateInterval int          // Update interval in model ticks.
21
        file           *os.File
22
        header         []string
23
        builder        strings.Builder
24
        step           int64
25
}
26

27
// Initialize the system
28
func (s *CSV) Initialize(w *ecs.World) {
1✔
29
        s.Observer.Initialize(w)
1✔
30
        s.header = s.Observer.Header()
1✔
31
        if s.UpdateInterval == 0 {
2✔
32
                s.UpdateInterval = 1
1✔
33
        }
1✔
34
        if s.Sep == "" {
2✔
35
                s.Sep = ","
1✔
36
        }
1✔
37

38
        err := os.MkdirAll(filepath.Dir(s.File), os.ModePerm)
1✔
39
        if err != nil {
1✔
40
                panic(err)
×
41
        }
42

43
        s.file, err = os.Create(s.File)
1✔
44
        if err != nil {
1✔
45
                panic(err)
×
46
        }
47
        _, err = fmt.Fprintf(s.file, "t%s%s\n", s.Sep, strings.Join(s.header, s.Sep))
1✔
48
        if err != nil {
1✔
49
                panic(err)
×
50
        }
51

52
        s.step = 0
1✔
53
}
54

55
// Update the system
56
func (s *CSV) Update(w *ecs.World) {
100✔
57
        s.Observer.Update(w)
100✔
58
        if s.UpdateInterval == 0 || s.step%int64(s.UpdateInterval) == 0 {
200✔
59
                values := s.Observer.Values(w)
100✔
60
                s.builder.Reset()
100✔
61
                fmt.Fprintf(&s.builder, "%d%s", s.step, s.Sep)
100✔
62
                for i, v := range values {
400✔
63
                        fmt.Fprintf(&s.builder, "%f", v)
300✔
64
                        if i < len(values)-1 {
500✔
65
                                fmt.Fprint(&s.builder, s.Sep)
200✔
66
                        }
200✔
67
                }
68
                _, err := fmt.Fprintf(s.file, "%s\n", s.builder.String())
100✔
69
                if err != nil {
100✔
70
                        panic(err)
×
71
                }
72
        }
73
        s.step++
100✔
74
}
75

76
// Finalize the system
77
func (s *CSV) Finalize(w *ecs.World) {
1✔
78
        if err := s.file.Close(); err != nil {
1✔
79
                panic(err)
×
80
        }
81
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc