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

mlange-42 / arche-model / 9075085581

14 May 2024 06:58AM CUT coverage: 98.379%. Remained the same
9075085581

push

github

web-flow
Update changelog, fix docstings (#62)

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

87.5
/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
        if s.UpdateInterval == 0 {
2✔
31
                s.UpdateInterval = 1
1✔
32
        }
1✔
33

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

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

43
        s.step = 0
1✔
44
}
45

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

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

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

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