stillbox/pkg/sinks/database.go

63 lines
1.5 KiB
Go
Raw Normal View History

2024-08-01 01:01:08 -04:00
package sinks
import (
"context"
"fmt"
"dynatron.me/x/stillbox/internal/common"
2024-08-05 18:11:31 -04:00
"dynatron.me/x/stillbox/pkg/calls"
2024-11-03 07:19:03 -05:00
"dynatron.me/x/stillbox/pkg/database"
2024-08-01 01:01:08 -04:00
"github.com/jackc/pgx/v5/pgtype"
2024-08-01 01:01:08 -04:00
"github.com/rs/zerolog/log"
)
type DatabaseSink struct {
2024-11-15 12:18:32 -05:00
db database.DB
2024-08-01 01:01:08 -04:00
}
2024-11-15 12:18:32 -05:00
func NewDatabaseSink(db database.DB) *DatabaseSink {
2024-08-04 10:56:46 -04:00
return &DatabaseSink{db: db}
2024-08-01 01:01:08 -04:00
}
func (s *DatabaseSink) Call(ctx context.Context, call *calls.Call) error {
2024-08-18 08:44:44 -04:00
if !call.ShouldStore() {
log.Debug().Str("call", call.String()).Msg("received dontStore call")
return nil
}
err := s.db.AddCall(ctx, s.toAddCallParams(call))
2024-08-01 01:01:08 -04:00
if err != nil {
return fmt.Errorf("add call: %w", err)
}
log.Debug().Str("id", call.ID.String()).Int("system", call.System).Int("tgid", call.Talkgroup).Msg("stored")
2024-08-01 01:01:08 -04:00
return nil
}
func (s *DatabaseSink) SinkType() string {
return "database"
}
func (s *DatabaseSink) toAddCallParams(call *calls.Call) database.AddCallParams {
return database.AddCallParams{
ID: call.ID,
2024-08-01 01:01:08 -04:00
Submitter: call.Submitter.Int32Ptr(),
System: call.System,
Talkgroup: call.Talkgroup,
CallDate: pgtype.Timestamptz{Time: call.DateTime, Valid: true},
2024-08-01 01:01:08 -04:00
AudioName: common.PtrOrNull(call.AudioName),
AudioBlob: call.Audio,
AudioType: common.PtrOrNull(call.AudioType),
2024-11-06 20:55:48 -05:00
Duration: call.Duration.MsInt32Ptr(),
2024-08-01 01:01:08 -04:00
Frequency: call.Frequency,
Frequencies: call.Frequencies,
Patches: call.Patches,
TgLabel: call.TalkgroupLabel,
2024-08-23 14:28:47 -04:00
TgAlphaTag: call.TGAlphaTag,
2024-08-01 01:01:08 -04:00
TgGroup: call.TalkgroupGroup,
Source: call.Source,
}
}