summaryrefslogtreecommitdiff
path: root/src/hub/src/spreadspace.org/sfive/s5typesStore.go
blob: a6ac3a9a6a823e4cea9e33675946c4f94c982903 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package sfive

import (
	"encoding/binary"
	"errors"
	"fmt"
	"strings"
	"time"
)

var (
	ErrNotFound = errors.New("not found")
)

// compared to JSON DTOs, DB types are flattened, and use key-relations instead of collections
// this is very much not normalized at all, because I'm too lazy to type

const (
	// sqlite table names
	dataUpdatesTn = "DataUpdates"
	hubInfoTn     = "HubInfo"

	// bolt bucket names
	sourcesFwdBn    = "SourcesFwd"
	sourcesRevBn    = "SourcesRev"
	clientDataBn    = "ClientData"
	userAgentsFwdBn = "UserAgentsFwd"
	userAgentsRevBn = "UserAgentsRev"
)

type hubInfoDb struct {
	Name  string
	Value string
}

// stored in sourcesRevBn
type streamIdDb struct {
	ContentId string `json:"c"`
	Format    string `json:"f"`
	Quality   string `json:"q"`
}

type sourceDb struct {
	Hostname string     `json:"h"`
	StreamId streamIdDb `json:"s"`
	Tags     []string   `json:"t"`
}

func (s sourceDb) String() string {
	return fmt.Sprintf("%s/%s/%s/%s/%s", s.Hostname, s.StreamId.ContentId, s.StreamId.Format, s.StreamId.Quality, strings.Join(s.Tags, ","))
}

func (s *SourceId) CopyFromSourceDb(v sourceDb) {
	s.Hostname = v.Hostname
	s.StreamId.ContentId = v.StreamId.ContentId
	s.StreamId.Format = v.StreamId.Format
	s.StreamId.Quality = v.StreamId.Quality
	s.Tags = v.Tags
}

// stored in clientDataBn
type clientDataDb struct {
	Ip          string `json:"ip"`
	UserAgentId int    `json:"ua"`
	BytesSent   uint   `json:"bs"`
}

// stored in dataUpdatesTn
// in DB, StatisticsData/DataUpdate is flattened compared to JSON DTOs
type dataUpdateDb struct {
	Id                    int
	SourceId              int // foreign key to sourcesTn
	SourceHubUuid         *string
	SourceHubDataUpdateId *int
	StartTime             int64 // time.Time
	Duration              int64 // duration in milliseconds
	ClientCount           uint
	BytesReceived         uint
	BytesSent             uint
}

func (s *StatisticsData) CopyFromDataUpdateDb(v dataUpdateDb, hubId string) {
	if v.SourceHubUuid == nil {
		s.SourceHubUuid = &hubId
	} else {
		s.SourceHubUuid = v.SourceHubUuid
	}
	if v.SourceHubDataUpdateId == nil {
		s.SourceHubDataUpdateId = &v.Id
	} else {
		s.SourceHubDataUpdateId = v.SourceHubDataUpdateId
	}

	s.StartTime = time.Unix(v.StartTime, 0)
	s.Duration = v.Duration
	s.Data.ClientCount = v.ClientCount
	s.Data.BytesReceived = v.BytesReceived
	s.Data.BytesSent = v.BytesSent
}

func itob(v int) []byte {
	b := make([]byte, 8)
	binary.BigEndian.PutUint64(b, uint64(v))
	return b
}

func btoi(b []byte) int {
	return int(binary.BigEndian.Uint64(b))
}