summaryrefslogtreecommitdiff
path: root/src/hub/src/spreadspace.org/sfive/s5typesStore.go
blob: 67d38d1f1bd2de60f80c2e9b0c4a9bd32c6091fa (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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
//
// sfive
//
// sfive - spreadspace streaming statistics suite is a generic
// statistic collection tool for streaming server infrastuctures.
// The system collects and stores meta data like number of views
// and throughput from a number of streaming servers and stores
// it in a global data store.
// The data acquisition is designed to be generic and extensible in
// order to support different streaming software.
// sfive also contains tools and applications to filter and visualize
// live and recorded data.
//
//
// Copyright (C) 2014-2017 Christian Pointner <equinox@spreadspace.org>
//                         Markus Grüneis <gimpf@gimpf.org>
//
// This file is part of sfive.
//
// sfive is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 3
// as published by the Free Software Foundation.
//
// sfive is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with sfive. If not, see <http://www.gnu.org/licenses/>.
//

package sfive

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

var (
	ErrNotFound       = errors.New("date-update entry not found")
	ErrSourceNotFound = errors.New("source entry not found")
	ErrClientNotFound = errors.New("client entry not found")
	ErrReadOnly       = errors.New("store is in read-only mode")
	ErrIndexCorrupt   = errors.New("the search index seems to be corrupt")
)

const (
	// bucket names
	hubInfoBn       = "HubInfo"
	latestUpdatesBn = "LatestUpdates"
	hubUUIDsFwdBn   = "HubUUIDsFwd"
	hubUUIDsRevBn   = "HubUUIDsRev"
	updatesBn       = "Updates"
	sourcesFwdBn    = "SourcesFwd"
	sourcesRevBn    = "SourcesRev"
	clientsFwdBn    = "ClientsFwd"
	clientsRevBn    = "ClientsRev"
	clientDataBn    = "ClientData"
	userAgentsFwdBn = "UserAgentsFwd"
	userAgentsRevBn = "UserAgentsRev"

	// index buckets
	timestampsIdxBn = "_timestamps"

	// well-known keys
	hubUUIDKey      = "HubUUID"
	storeVersionKey = "Version"
)

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

type sourceDB struct {
	Hostname string   `json:"h"`
	Stream   streamDB `json:"s"`
	Tags     []string `json:"t"`
}

func NewSourceDB(uf *UpdateFull) *sourceDB {
	return &sourceDB{
		Hostname: uf.Source.Hostname,
		Stream: streamDB{
			ContentID: uf.Source.Stream.ContentID,
			Format:    uf.Source.Stream.Format,
			Quality:   uf.Source.Stream.Quality,
		},
		Tags: uf.Source.Tags,
	}
}

func (s *sourceDB) Slug() string {
	return fmt.Sprintf("%s/%s/%s/%s/%s", s.Hostname, s.Stream.ContentID, s.Stream.Format, s.Stream.Quality, strings.Join(s.Tags, ","))
}

func (s *Source) CopyFromSourceDB(sdb *sourceDB) {
	s.Hostname = sdb.Hostname
	s.Stream.ContentID = sdb.Stream.ContentID
	s.Stream.Format = sdb.Stream.Format
	s.Stream.Quality = sdb.Stream.Quality
	s.Tags = sdb.Tags
}

type clientDB struct {
	IP           string  `json:"ip"`
	Port         uint    `json:"po,omitempty"`
	CountryName  string  `json:"cn,omitempty"`
	CountryCode2 string  `json:"cc,omitempty"`
	RegionName   string  `json:"rn,omitempty"`
	RegionCode   string  `json:"rc,omitempty"`
	CityName     string  `json:"ct,omitempty"`
	Latitude     float64 `json:"lat,omitempty"`
	Longitude    float64 `json:"lon,omitempty"`
}

func NewClientDB(c *Client) *clientDB {
	return &clientDB{
		IP:           c.IP,
		Port:         c.Port,
		CountryName:  c.CountryName,
		CountryCode2: c.CountryCode2,
		RegionName:   c.RegionName,
		RegionCode:   c.RegionCode,
		CityName:     c.CityName,
		Latitude:     c.Latitude,
		Longitude:    c.Longitude,
	}
}

func (c *clientDB) Slug() string {
	return fmt.Sprintf("%s-%d/%s/%s/%s/%f,%f", c.IP, c.Port, c.CountryCode2, c.RegionCode, c.CityName, c.Latitude, c.Longitude)
}

func (c *Client) CopyFromClientDB(cdb *clientDB) {
	c.IP = cdb.IP
	c.Port = cdb.Port
	c.CountryName = cdb.CountryName
	c.CountryCode2 = cdb.CountryCode2
	c.RegionName = cdb.RegionName
	c.RegionCode = cdb.RegionCode
	c.CityName = cdb.CityName
	c.Latitude = cdb.Latitude
	c.Longitude = cdb.Longitude
}

type clientDataDB struct {
	ClientID    int64 `json:"ci"`
	UserAgentID int64 `json:"ua"`
	BytesSent   uint  `json:"bs"`
}

type updateDB struct {
	SourceHubID       int64 `json:"h,omitempty"`
	SourceHubUpdateID int64 `json:"hi,omitempty"`
	SourceID          int64 `json:"si"`
	StartTime         int64 `json:"st"` // unix timestamp in milliseconds
	Duration          int64 `json:"du"` // duration in milliseconds
	ClientCount       uint  `json:"cc,omitempty"`
	BytesReceived     uint  `json:"br,omitempty"`
	BytesSent         uint  `json:"bs,omitempty"`
}

func NewUpdateDB(uf *UpdateFull) *updateDB {
	return &updateDB{
		-1,
		uf.SourceHubUpdateID,
		-1,
		int64(uf.StartTime.Unix()*1000) + int64(uf.StartTime.Nanosecond()/1000000),
		uf.Duration,
		uf.Data.ClientCount,
		uf.Data.BytesReceived,
		uf.Data.BytesSent,
	}
}

func (uf *UpdateFull) CopyFromUpdateDB(udb *updateDB, srcHubUUID, hubUUID string, id int64) {
	if srcHubUUID == "" {
		uf.SourceHubUUID = hubUUID
		uf.SourceHubUpdateID = id
	} else {
		uf.SourceHubUUID = srcHubUUID
		uf.SourceHubUpdateID = udb.SourceHubUpdateID
		uf.ForwardHubUUID = hubUUID
		uf.ForwardHubUpdateID = id
	}

	uf.StartTime = time.Unix((udb.StartTime / 1000), (udb.StartTime%1000)*1000000)
	uf.Duration = udb.Duration
	uf.Data.ClientCount = udb.ClientCount
	uf.Data.BytesReceived = udb.BytesReceived
	uf.Data.BytesSent = udb.BytesSent
}

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

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