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

mlange-42 / arche-model / 9060592980

13 May 2024 09:32AM CUT coverage: 98.314%. Remained the same
9060592980

push

github

web-flow
Fix typo in error message, upgrade to Arche v0.12.0 (#60)

1 of 1 new or added line in 1 file covered. (100.0%)

583 of 593 relevant lines covered (98.31%)

138.48 hits per line

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

85.29
/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.Sep == "" {
2✔
32
                s.Sep = ","
1✔
33
        }
1✔
34

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

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

49
        s.step = 0
1✔
50
}
51

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

73
// Finalize the system
74
func (s *CSV) Finalize(w *ecs.World) {
1✔
75
        if err := s.file.Close(); err != nil {
1✔
76
                panic(err)
×
77
        }
78
}
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