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

mlange-42 / arche-model / 9209952348

23 May 2024 02:34PM CUT coverage: 98.452% (+0.06%) from 98.395%
9209952348

push

github

web-flow
Add `reporter.TableCallback` for direct retrieval of table observer output in Go code (#66)

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

636 of 646 relevant lines covered (98.45%)

139.8 hits per line

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

87.5
/reporter/csv_snaphot.go
1
package reporter
2

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

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

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

27
// Initialize the system
28
func (s *SnapshotCSV) 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

35
        if s.Sep == "" {
2✔
36
                s.Sep = ","
1✔
37
        }
1✔
38

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

44
        s.step = 0
1✔
45
}
46

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

62
                _, err = fmt.Fprintf(file, "%s\n", strings.Join(s.header, s.Sep))
100✔
63
                if err != nil {
100✔
64
                        panic(err)
×
65
                }
66

67
                values := s.Observer.Values(w)
100✔
68
                s.builder.Reset()
100✔
69
                for _, row := range values {
400✔
70
                        for i, v := range row {
1,200✔
71
                                fmt.Fprint(&s.builder, strconv.FormatFloat(v, 'f', -1, 64))
900✔
72
                                if i < len(row)-1 {
1,500✔
73
                                        fmt.Fprint(&s.builder, s.Sep)
600✔
74
                                }
600✔
75
                        }
76
                        fmt.Fprint(&s.builder, "\n")
300✔
77
                }
78
                _, err = fmt.Fprint(file, s.builder.String())
100✔
79
                if err != nil {
100✔
80
                        panic(err)
×
81
                }
82
        }
83
        s.step++
100✔
84
}
85

86
// Finalize the system
87
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