diff options
Diffstat (limited to 'src/hub/src/spreadspace.org/sfive/s5typesStore.go')
-rw-r--r-- | src/hub/src/spreadspace.org/sfive/s5typesStore.go | 99 |
1 files changed, 99 insertions, 0 deletions
diff --git a/src/hub/src/spreadspace.org/sfive/s5typesStore.go b/src/hub/src/spreadspace.org/sfive/s5typesStore.go new file mode 100644 index 0000000..60dc6e7 --- /dev/null +++ b/src/hub/src/spreadspace.org/sfive/s5typesStore.go @@ -0,0 +1,99 @@ +package sfive + +import "time" + +// 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 + +// table names +const ( + tagsTn = "Tags" + sourceTagsTn = "StreamToTagMap" + sourcesTn = "Sources" + clientdataUpdatesTn = "ClientDataUpdates" + dataUpdatesTn = "DataUpdates" +) + +// stored in tagsTn +type tagDb struct { + Id int + Name string +} + +// stored in sourceTagsTn +// Stream m:n Tag +type sourceTagsDb struct { + TagId int // foreign key to tagsTn + SourceId int // foreign key to sourcesTn +} + +// stored in sourcesTn +type sourceDb struct { + Id int + StreamId + SourceId +} + +// stored in clientdataUpdatesTn +// ClientData n:1 DataUpdate +type clientDataDb struct { + Id int + DataUpdatesId int // foreign key to dataUpdatesTn + ClientData +} + +// stored in dataUpdatesTn +// in DB, StatisticsData/DataUpdate is flattened compared to JSON DTOs +type dataUpdateDb struct { + Id int + SourceId int // foreign key to sourcesTn + StartTime int64 // time.Time + Duration int64 // time.Duration + ClientCount uint + BytesReceived uint + BytesSent uint +} + +func (self *SourceId) CopyFromSourceDb(value sourceDb) { + self.Version = value.Version + self.Hostname = value.Hostname + self.StreamId.ContentId = value.ContentId + self.StreamId.Format = value.Format + self.StreamId.Quality = value.Quality +} + +func (self *SourceId) CopyFromTagsDb(values []tagDb) { + tags := make([]string, len(values)) + for i := range values { + tags[i] = values[i].Name + } + self.Tags = tags +} + +func (self *StatisticsData) CopyFromDataUpdateDb(value dataUpdateDb) { + self.StartTime = time.Unix(value.StartTime, 0) + self.Duration = time.Duration(value.Duration) * time.Second + self.Data.ClientCount = value.ClientCount + self.Data.BytesReceived = value.BytesReceived + self.Data.BytesSent = value.BytesSent +} + +func (self *StatisticsData) CopyFromClientDataDb(values []clientDataDb) { + clients := make([]ClientData, len(values)) + for i := range values { + clients[i].Ip = values[i].Ip + clients[i].UserAgent = values[i].UserAgent + clients[i].BytesSent = values[i].BytesSent + } + self.Data.Clients = clients +} + +func cvtToApiStatisticsData( + source sourceDb, update dataUpdateDb, clients []clientDataDb, tags []tagDb) StatisticsData { + res := StatisticsData{} + res.CopyFromSourceDb(source) + res.CopyFromDataUpdateDb(update) + res.CopyFromClientDataDb(clients) + res.CopyFromTagsDb(tags) + return res +} |