summaryrefslogtreecommitdiff
path: root/src/daq/s5proxy/src/s5proxy/stats.go
blob: b4eccaae597ea751ba87809a4bc8ed0ec9736720 (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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
//
// 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-2018 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 main

import (
	"encoding/json"
	"errors"
	"fmt"
	"net"
	"os"
	"strings"
	"time"
)

// TODO: this is basically a copy from src/hub....

const (
	ProtocolVersion = 2
)

type Stream struct {
	ContentID string `json:"content"`
	Format    string `json:"format"`
	Quality   string `json:"quality"`
}

type Source struct {
	Hostname string   `json:"hostname"`
	Stream   Stream   `json:"stream"`
	Tags     []string `json:"tags,omitempty"`
}

type GeoInfo struct {
	CountryName  string  `json:"country,omitempty"`
	CountryCode2 string  `json:"country-code2,omitempty"`
	RegionName   string  `json:"region,omitempty"`
	RegionCode   string  `json:"region-code,omitempty"`
	CityName     string  `json:"city,omitempty"`
	Latitude     float64 `json:"latitude,omitempty"`
	Longitude    float64 `json:"longitude,omitempty"`
}

type Client struct {
	IP        string `json:"ip"`
	Port      uint   `json:"port,omitempty"`
	UserAgent string `json:"user-agent,omitempty"`
	BytesSent uint   `json:"bytes-sent,omitempty"`
	GeoInfo
}

type UpdateData struct {
	ClientCount   uint     `json:"client-count"`
	BytesReceived uint     `json:"bytes-received,omitempty"`
	BytesSent     uint     `json:"bytes-sent"`
	Clients       []Client `json:"clients,omitempty"`
}

type Update struct {
	StartTime time.Time  `json:"start-time"`
	Duration  int64      `json:"duration-ms"`
	Data      UpdateData `json:"data"`
}

type Header struct {
	Version            uint   `json:"version,omitempty"` // omitempty is needed for data only messages and for REST API
	SourceHubUUID      string `json:"SourceHubUuid,omitempty"`
	SourceHubUpdateID  int    `json:"SourceHubUpdateId,omitempty"`
	ForwardHubUUID     string `json:"ForwardHubUuid,omitempty"`
	ForwardHubUpdateID int    `json:"ForwardHubUpdateId,omitempty"`
}

type UpdateFull struct {
	Header
	Source
	Update
}

type StatsWorker struct {
	stream  Stream
	current map[string]*Client
	trigger chan time.Time
	output  chan<- *UpdateFull
	input   chan *Client
}

func NewStatsWorker(stream Stream, updates chan<- *UpdateFull) (sw StatsWorker) {
	sw.stream = stream
	sw.current = make(map[string]*Client)
	sw.trigger = make(chan time.Time)
	sw.output = updates
	sw.input = make(chan *Client, 100)
	return
}

func (sw StatsWorker) Run() {
	for {
		select {
		case t := <-sw.trigger:
			if t.UnixNano() != 0 {
				upd := &UpdateFull{Source: Source{Stream: sw.stream}, Update: Update{StartTime: t}}
				upd.Data.ClientCount = uint(len(sw.current))
				for _, c := range sw.current {
					upd.Data.Clients = append(upd.Data.Clients, *c)
					upd.Data.BytesSent += c.BytesSent
				}
				select {
				case sw.output <- upd:
				default:
					s5l.Printf("STATS: worker(%v): writing to output channel would block, dropping data update...", sw.stream)
				}
			}
			sw.current = make(map[string]*Client)
		case upd := <-sw.input:
			if data, exists := sw.current[upd.IP]; exists {
				data.BytesSent += upd.BytesSent
			} else {
				sw.current[upd.IP] = upd
			}
		}
	}
}

type Stats struct {
	conf        *Config
	sock        net.Conn
	dataEncoder *json.Encoder
	updates     chan *UpdateFull
	workers     map[string]StatsWorker
}

func (s *Stats) GetUpdateChannel(url string) chan<- *Client {
	s5l.Printf("STATS: got new client for url: %s", url)
	for name, worker := range s.workers {
		if strings.Contains(url, name) {
			return worker.input
		}
	}
	return nil
}

func parseStreamDescriptionElement(desc, desctype string) (e []string, err error) {
	parts := strings.Split(desc, ",")
	for _, p := range parts {
		if p = strings.TrimSpace(p); p == "" {
			err = fmt.Errorf("invalid streamer description %s: '%s'", desctype, desc)
			return
		}
		e = append(e, p)
	}
	return
}

func parseStreamDescription(desc string) (c, f, q []string, err error) {
	parts := strings.Split(desc, "/")
	if len(parts) != 3 {
		err = fmt.Errorf("invalid streamer description: '%s'", desc)
		return
	}
	if c, err = parseStreamDescriptionElement(parts[0], "content"); err != nil {
		return
	}
	if f, err = parseStreamDescriptionElement(parts[1], "format"); err != nil {
		return
	}
	if q, err = parseStreamDescriptionElement(parts[2], "quality"); err != nil {
		return
	}
	return
}

func generateStreamName(format, c, f, q string) string {
	return os.Expand(format, func(k string) string {
		switch k {
		case "content":
			return c
		case "format":
			return f
		case "quality":
			return q
		default:
			return ""
		}
	})
}

func NewStats(conf *Config) (s *Stats, err error) {
	s = &Stats{conf: conf}
	if err = s.connectToHub(); err != nil {
		return
	}

	s.updates = make(chan *UpdateFull, 1000)
	s.workers = make(map[string]StatsWorker)
	var content, format, quality []string
	for _, match := range conf.SFive.Matches {
		if content, format, quality, err = parseStreamDescription(match.Streams); err != nil {
			return
		}
		for _, c := range content {
			for _, f := range format {
				for _, q := range quality {
					name := generateStreamName(match.Format, c, f, q)
					s.workers[name] = NewStatsWorker(Stream{c, f, q}, s.updates)
					s5l.Printf("STATS: adding streamer '%s'", name)
				}
			}
		}
	}
	if len(s.workers) < 1 {
		err = errors.New("no streamers defined")
		return
	}
	return
}

func (s *Stats) connectToHub() (err error) {
	if s.sock, err = net.DialTimeout("unixgram", s.conf.SFive.Sock, time.Second); err != nil {
		return
	}
	s5l.Printf("STATS: connected to sfive-hub on '%s'", s.conf.SFive.Sock)
	s.dataEncoder = json.NewEncoder(s.sock)
	return
}

func (s *Stats) sendUpdates() {
	for {
		upd := <-s.updates
		upd.Version = ProtocolVersion
		upd.Hostname = s.conf.SFive.Hostname
		upd.Tags = s.conf.SFive.Tags
		upd.Duration = int64(s.conf.SFive.Duration)
		if err := s.dataEncoder.Encode(upd); err != nil {
			nErr, ok := err.(*net.OpError)
			if !ok {
				s5l.Printf("STATS: json encode(): %s", err)
			} else {
				if !nErr.Temporary() {
					for {
						time.Sleep(5 * time.Second)
						s5l.Printf("STATS: attempting reconnect to hub")
						if err := s.connectToHub(); err == nil {
							break
						}
					}
				}
			}
		}
	}
}

func (s *Stats) Run() (err error) {
	for _, worker := range s.workers {
		go worker.Run()
	}

	d := time.Duration(s.conf.SFive.Duration) * time.Millisecond
	offset := d - (time.Duration(time.Now().UnixNano()) % d)
	s5l.Printf("STATS: waiting %v for the next duration boundary", offset)
	time.Sleep(offset)
	s5l.Printf("STATS: starting ticker")
	ticker := time.NewTicker(d)
	for _, worker := range s.workers {
		worker.trigger <- time.Unix(0, 0) // stats-worker will not send data but just reset
	}
	go s.sendUpdates()
	for {
		t := <-ticker.C
		t = t.Add(-1 * d)
		for _, worker := range s.workers {
			worker.trigger <- t
		}
	}
}