Skip to content

Commit 355518d

Browse files
fix(backend, logger): create boards with the same Allowed Vars
1 parent 0db2a88 commit 355518d

2 files changed

Lines changed: 85 additions & 62 deletions

File tree

backend/pkg/logger/data/logger.go

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ import (
1616
"github.com/HyperloopUPV-H8/h9-backend/pkg/transport/packet/data"
1717
)
1818

19+
type loggerAccess string
20+
1921
const (
2022
Name abstraction.LoggerName = "data"
2123
)
@@ -26,7 +28,7 @@ type Logger struct {
2628

2729
fileLock *sync.RWMutex
2830
// saveFiles is a map that contains the file of each value
29-
saveFiles map[data.ValueName]*file.CSV
31+
saveFiles map[loggerAccess]*file.CSV
3032
// allowedVars contains the full names (board/valueName) to be logged
3133
allowedVars map[string]struct{}
3234
}
@@ -40,7 +42,7 @@ func NewLogger() *Logger {
4042
logger := &Logger{
4143

4244
BaseLogger: loggerbase.NewBaseLogger(Name),
43-
saveFiles: make(map[data.ValueName]*file.CSV),
45+
saveFiles: make(map[loggerAccess]*file.CSV),
4446
fileLock: &sync.RWMutex{},
4547
allowedVars: nil, // no filter by default
4648
}
@@ -130,16 +132,19 @@ func (sublogger *Logger) getFile(valueName data.ValueName, board string) (*file.
130132
sublogger.fileLock.Lock()
131133
defer sublogger.fileLock.Unlock()
132134

135+
// Takes into account that several boards might have the same valueName,
136+
valueNameBoard := internalLoggerName(valueName, board)
137+
133138
// Check if the file already exists
134-
valueFile, ok := sublogger.saveFiles[valueName]
139+
valueFile, ok := sublogger.saveFiles[valueNameBoard]
135140
if ok {
136141
return valueFile, nil
137142
}
138143

139144
valueFileRaw, err := sublogger.createFile(valueName, board)
140-
sublogger.saveFiles[valueName] = file.NewCSV(valueFileRaw)
145+
sublogger.saveFiles[valueNameBoard] = file.NewCSV(valueFileRaw)
141146

142-
return sublogger.saveFiles[valueName], err
147+
return sublogger.saveFiles[valueNameBoard], err
143148
}
144149

145150
// override createFile from BaseLogger to add specific path
@@ -173,8 +178,14 @@ func (sublogger *Logger) Stop() error {
173178
}
174179
}
175180

176-
sublogger.saveFiles = make(map[data.ValueName]*file.CSV, len(sublogger.saveFiles))
181+
sublogger.saveFiles = make(map[loggerAccess]*file.CSV, len(sublogger.saveFiles))
177182

178183
return closeErr
179184
})
180185
}
186+
187+
// This function avoids conflict between two boards that have the same variable to represent
188+
// should be called only when using saveFiles
189+
func internalLoggerName(valueName data.ValueName, board string) loggerAccess {
190+
return loggerAccess(string(valueName) + "-" + string(board))
191+
}

backend/pkg/logger/data/logger_test.go

Lines changed: 68 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -414,72 +414,84 @@ func TestPushRecord(t *testing.T) {
414414
}
415415
})
416416

417-
//! TEST DOES NOT WORK IF WE MONITOR THE SAME VARIABLE ACROSS MULTIPLE BOARDS
418-
t.Run("With allowed variables (multiple boards)", func(t *testing.T) {
419-
logger := createLoggerForTest(t)
420-
logger.Start()
417+
t.Run("With allowed variables (multiple boards, different values Name)", func(t *testing.T) {
418+
multipleBoardsValueNames(t, []string{"def", "def2"})
419+
})
421420

422-
//variables
421+
// The same as before but the loggers have the same value Name
422+
t.Run("With allowed variables (multiple boards, same value Name)", func(t *testing.T) {
423423

424-
values := []string{"def", "def2"}
424+
multipleBoardsValueNames(t, []string{"def", "def"})
425425

426-
// values to log per board
427-
vals := map[string]float64{
428-
"Pikachu": 11.11,
429-
"Bulbasaur": 22.22,
430-
}
426+
})
431427

432-
// push one record per board using "def" and unique packet IDs
433-
var id abstraction.PacketId = 1
434-
for b, v := range vals {
435-
pkt := data.NewPacket(id).
436-
SetValue(data.ValueName(values[id-1]), data.NewNumericValue(v), true)
437-
id++
438-
439-
rec := Record{
440-
Packet: pkt,
441-
From: b,
442-
To: "Logger",
443-
Timestamp: time.Now(),
444-
}
428+
})
445429

446-
if err := logger.PushRecord(&rec); err != nil {
447-
t.Fatalf("PushRecord failed for board %s: %v", b, err)
448-
}
449-
}
430+
}
450431

451-
// esperar a que terminen las escrituras asíncronas
452-
time.Sleep(2 * time.Second)
432+
// Test Multiple boards with the valueNames got from input
453433

454-
i := 0
434+
func multipleBoardsValueNames(t *testing.T, values []string) {
455435

456-
// comprobar ficheros y valores
457-
for b, v := range vals {
458-
filePath := filepath.Join("logger", loggerHandler.Timestamp.Format(loggerHandler.TimestampFormat), "data", strings.ToUpper(b), values[i]+".csv")
459-
i++
460-
if _, err := os.Stat(filePath); err != nil {
461-
t.Fatalf("expected file %s to exist, but got error: %v", filePath, err)
462-
}
436+
logger := createLoggerForTest(t)
437+
logger.Start()
463438

464-
bts, err := os.ReadFile(filePath)
465-
if err != nil {
466-
t.Fatalf("could not read file %s: %v", filePath, err)
467-
}
468-
lines := strings.Split(strings.TrimSpace(string(bts)), "\n")
469-
last := strings.TrimSpace(lines[len(lines)-1])
470-
fields := strings.Split(last, ",")
471-
if len(fields) < 1 {
472-
t.Fatalf("unexpected CSV content in %s: %q", filePath, last)
473-
}
474-
got := fields[len(fields)-1]
475-
want := strconv.FormatFloat(v, 'f', -1, 64)
476-
if got != want {
477-
t.Fatalf("unexpected value in %s: got %q, want %q", filePath, got, want)
478-
}
479-
}
480-
})
481-
})
439+
//variables
440+
441+
// values to log per board
442+
vals := map[string]float64{
443+
"Pikachu": 11.11,
444+
"Bulbasaur": 22.22,
445+
}
446+
447+
// push one record per board using "def" and unique packet IDs
448+
var id abstraction.PacketId = 1
449+
for b, v := range vals {
450+
pkt := data.NewPacket(id).
451+
SetValue(data.ValueName(values[id-1]), data.NewNumericValue(v), true)
452+
id++
482453

454+
rec := Record{
455+
Packet: pkt,
456+
From: b,
457+
To: "Logger",
458+
Timestamp: time.Now(),
459+
}
460+
461+
if err := logger.PushRecord(&rec); err != nil {
462+
t.Fatalf("PushRecord failed for board %s: %v", b, err)
463+
}
464+
}
465+
466+
// esperar a que terminen las escrituras asíncronas
467+
time.Sleep(2 * time.Second)
468+
469+
i := 0
470+
471+
// comprobar ficheros y valores
472+
for b, v := range vals {
473+
filePath := filepath.Join("logger", loggerHandler.Timestamp.Format(loggerHandler.TimestampFormat), "data", strings.ToUpper(b), values[i]+".csv")
474+
i++
475+
if _, err := os.Stat(filePath); err != nil {
476+
t.Fatalf("expected file %s to exist, but got error: %v", filePath, err)
477+
}
478+
479+
bts, err := os.ReadFile(filePath)
480+
if err != nil {
481+
t.Fatalf("could not read file %s: %v", filePath, err)
482+
}
483+
lines := strings.Split(strings.TrimSpace(string(bts)), "\n")
484+
last := strings.TrimSpace(lines[len(lines)-1])
485+
fields := strings.Split(last, ",")
486+
if len(fields) < 1 {
487+
t.Fatalf("unexpected CSV content in %s: %q", filePath, last)
488+
}
489+
got := fields[len(fields)-1]
490+
want := strconv.FormatFloat(v, 'f', -1, 64)
491+
if got != want {
492+
t.Fatalf("unexpected value in %s: got %q, want %q", filePath, got, want)
493+
}
494+
}
483495
}
484496

485497
/*************

0 commit comments

Comments
 (0)