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

mlange-42 / arche-model / 4731229459

18 Apr 2023 10:20AM CUT coverage: 96.368% (+0.2%) from 96.144%
4731229459

push

github

GitHub
Single grid coords & MatrixToGrid observer (#38)

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

398 of 413 relevant lines covered (96.37%)

127.65 hits per line

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

86.84
/reporter/csv_snaphot.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
// SnapshotCSV reporter.
14
//
15
// Writes a CSV file per step.
16
type SnapshotCSV struct {
17
        Observer       observer.Table // Observer to get data from.
18
        FilePattern    string         // File path and pattern for output files, like out/foo-%06d.csv
19
        Sep            string         // Column separator. Default ",".
20
        UpdateInterval int            // Update interval in model ticks.
21
        header         []string
22
        builder        strings.Builder
23
        step           int64
24
}
25

26
// Initialize the system
27
func (s *SnapshotCSV) Initialize(w *ecs.World) {
1✔
28
        s.Observer.Initialize(w)
1✔
29
        s.header = s.Observer.Header()
1✔
30

1✔
31
        if s.Sep == "" {
2✔
32
                s.Sep = ","
1✔
33
        }
1✔
34

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

40
        s.step = 0
1✔
41
}
42

43
// Update the system
44
func (s *SnapshotCSV) Update(w *ecs.World) {
100✔
45
        s.Observer.Update(w)
100✔
46
        if s.UpdateInterval == 0 || s.step%int64(s.UpdateInterval) == 0 {
110✔
47
                file, err := os.Create(fmt.Sprintf(s.FilePattern, s.step))
10✔
48
                if err != nil {
10✔
49
                        panic(err)
×
50
                }
51
                defer func() {
20✔
52
                        err := file.Close()
10✔
53
                        if err != nil {
10✔
54
                                panic(err)
×
55
                        }
56
                }()
57

58
                _, err = fmt.Fprintf(file, "%s\n", strings.Join(s.header, s.Sep))
10✔
59
                if err != nil {
10✔
60
                        panic(err)
×
61
                }
62

63
                values := s.Observer.Values(w)
10✔
64
                s.builder.Reset()
10✔
65
                for _, row := range values {
40✔
66
                        for i, v := range row {
120✔
67
                                fmt.Fprintf(&s.builder, "%f", v)
90✔
68
                                if i < len(row)-1 {
150✔
69
                                        fmt.Fprint(&s.builder, s.Sep)
60✔
70
                                }
60✔
71
                        }
72
                        fmt.Fprint(&s.builder, "\n")
30✔
73
                }
74
                _, err = fmt.Fprint(file, s.builder.String())
10✔
75
                if err != nil {
10✔
76
                        panic(err)
×
77
                }
78
        }
79
        s.step++
100✔
80
}
81

82
// Finalize the system
83
func (s *SnapshotCSV) Finalize(w *ecs.World) {}
1✔
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