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
|
|
|
|
2024-10-23 08:55:19 -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-08-04 10:56:46 -04:00
|
|
|
db *database.DB
|
2024-08-01 01:01:08 -04:00
|
|
|
}
|
|
|
|
|
2024-08-04 10:56:46 -04:00
|
|
|
func NewDatabaseSink(db *database.DB) *DatabaseSink {
|
|
|
|
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
|
|
|
|
}
|
2024-10-23 08:55:19 -04:00
|
|
|
|
2024-11-06 20:47:10 -05:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2024-11-06 20:47:10 -05:00
|
|
|
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{
|
2024-11-06 20:47:10 -05:00
|
|
|
ID: call.ID,
|
2024-08-01 01:01:08 -04:00
|
|
|
Submitter: call.Submitter.Int32Ptr(),
|
|
|
|
System: call.System,
|
|
|
|
Talkgroup: call.Talkgroup,
|
2024-10-23 08:55:19 -04:00
|
|
|
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-08-11 13:46:43 -04:00
|
|
|
Duration: call.Duration.Int32Ptr(),
|
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,
|
|
|
|
}
|
|
|
|
}
|