nidus-sync/platform/publicreport/log.go
Eli Ribble 9b6cacda0e
Make signals include the object they are attached to (pool, report)
This means pushing the types into the common types module, which
required a refactor of a bunch of other libraries.
2026-03-21 01:19:36 +00:00

51 lines
1.5 KiB
Go

package publicreport
import (
"context"
"fmt"
"github.com/Gleipnir-Technology/bob"
"github.com/Gleipnir-Technology/bob/dialect/psql"
"github.com/Gleipnir-Technology/bob/dialect/psql/sm"
"github.com/Gleipnir-Technology/nidus-sync/db"
"github.com/Gleipnir-Technology/nidus-sync/platform/types"
"github.com/rs/zerolog/log"
"github.com/stephenafamo/scan"
)
func logEntriesByReportID(ctx context.Context, report_ids []int32) (map[int32][]types.LogEntry, error) {
results := make(map[int32][]types.LogEntry, len(report_ids))
for _, report_id := range report_ids {
results[report_id] = make([]types.LogEntry, 0)
}
rows, err := bob.All(ctx, db.PGInstance.BobDB, psql.Select(
sm.Columns(
"l.created",
"l.id",
"COALESCE(t.content, '') AS message",
"l.report_id",
"l.type_",
"l.user_id",
),
sm.From("publicreport.report_log").As("l"),
sm.LeftJoin("comms.email_log").As("e").OnEQ(
psql.Quote("l", "email_log_id"),
psql.Quote("e", "id"),
),
sm.LeftJoin("comms.text_log").As("t").OnEQ(
psql.Quote("l", "text_log_id"),
psql.Quote("t", "id"),
),
sm.Where(psql.Quote("l", "report_id").EQ(psql.Any(report_ids))),
sm.OrderBy(psql.Quote("l", "created")),
), scan.StructMapper[types.LogEntry]())
if err != nil {
return results, fmt.Errorf("query created: %w", err)
}
log.Debug().Int("len(report_ids)", len(report_ids)).Int("len(rows)", len(rows)).Msg("getting log entries")
for _, row := range rows {
results[row.ReportID] = append(results[row.ReportID], row)
}
return results, nil
}