<commit_before>package hub

import (
	"encoding/base64"
	"encoding/json"
	"fmt"
	"io"
	"log"
	"math/rand"
	"net/http"
	"os"
	"sync"
	"sync/atomic"
	"time"

	"code.google.com/p/go.net/websocket"
)

const (
	defaultBufferSize         = 128
	defaultHeartbeat          = time.Second * 60
	defaultHeartbeatGracetime = time.Second * 5
	defaultSessionTimeout     = time.Second * 180
	idLength                  = 16
	defaultLoglevel           = 1
)

func init() {
	rand.Seed(time.Now().UnixNano())
}

type Authorizer func(uri, token string, write bool) (authorized bool, err error)

func prettify(obj interface{}) string {
	b, err := json.MarshalIndent(obj, "", "  ")
	if err != nil {
		panic(err)
	}
	return string(b)
}

type Server struct {
	heartbeat          time.Duration
	heartbeatGracetime time.Duration
	sessionTimeout     time.Duration
	sessions           map[string]*Session
	subscriptions      map[string]map[string]*Session
	subscribers        map[string]map[string]bool
	lock               *sync.RWMutex
	loglevel           int
	bufferSize         int
	logger             *log.Logger
	authorizer         Authorizer
}

func NewServer() *Server {
	return &Server{
		heartbeat:          defaultHeartbeat,
		heartbeatGracetime: defaultHeartbeatGracetime,
		bufferSize:         defaultBufferSize,
		sessionTimeout:     defaultSessionTimeout,
		loglevel:           defaultLoglevel,
		sessions:           map[string]*Session{},
		subscriptions:      map[string]map[string]*Session{},
		subscribers:        map[string]map[string]bool{},
		lock:               &sync.RWMutex{},
		logger:             log.New(os.Stdout, "pusher: ", 0),
	}
}

type PusherSessionStats map[string]int
type PusherStats struct {
	Sessions      map[string]PusherSessionStats            `json:"sessions"`
	Subscriptions map[string]map[string]PusherSessionStats `json:"subscriptions"`
	Subscribers   []string                                 `json:"subscribers"`
}

func (self *Server) Stats() PusherStats {
	stats := PusherStats{
		Sessions:      map[string]PusherSessionStats{},
		Subscriptions: map[string]map[string]PusherSessionStats{},
		Subscribers:   []string{},
	}
	for k, session := range self.sessions {
		stats.Sessions[k] = PusherSessionStats{}
		stats.Sessions[k]["output"] = len(session.output)
		stats.Sessions[k]["input"] = len(session.input)
	}
	for subscription := range self.subscriptions {
		stats.Subscriptions[subscription] = map[string]PusherSessionStats{}
		for k, session := range self.subscriptions[subscription] {
			stats.Subscriptions[subscription][k] = PusherSessionStats{}
			stats.Subscriptions[subscription][k]["output"] = len(session.output)
			stats.Subscriptions[subscription][k]["input"] = len(session.input)
		}
	}
	for subscriber := range self.subscribers {
		stats.Subscribers = append(stats.Subscribers, subscriber)
	}
	return stats
}

func (self *Server) Loglevel(i int) *Server {
	self.loglevel = i
	return self
}

func (self *Server) Logger(l *log.Logger) *Server {
	self.logger = l
	return self
}

func (self *Server) Fatalf(fmt string, i ...interface{}) {
	self.logger.Printf("FATAL: "+fmt, i...)
}
<commit_msg>added number of connections to stats<commit_after>package hub

import (
	"encoding/base64"
	"encoding/json"
	"fmt"
	"io"
	"log"
	"math/rand"
	"net/http"
	"os"
	"sync"
	"sync/atomic"
	"time"

	"code.google.com/p/go.net/websocket"
)

const (
	defaultBufferSize         = 128
	defaultHeartbeat          = time.Second * 60
	defaultHeartbeatGracetime = time.Second * 5
	defaultSessionTimeout     = time.Second * 180
	idLength                  = 16
	defaultLoglevel           = 1
)

func init() {
	rand.Seed(time.Now().UnixNano())
}

type Authorizer func(uri, token string, write bool) (authorized bool, err error)

func prettify(obj interface{}) string {
	b, err := json.MarshalIndent(obj, "", "  ")
	if err != nil {
		panic(err)
	}
	return string(b)
}

type Server struct {
	heartbeat          time.Duration
	heartbeatGracetime time.Duration
	sessionTimeout     time.Duration
	sessions           map[string]*Session
	subscriptions      map[string]map[string]*Session
	subscribers        map[string]map[string]bool
	lock               *sync.RWMutex
	loglevel           int
	bufferSize         int
	logger             *log.Logger
	authorizer         Authorizer
}

func NewServer() *Server {
	return &Server{
		heartbeat:          defaultHeartbeat,
		heartbeatGracetime: defaultHeartbeatGracetime,
		bufferSize:         defaultBufferSize,
		sessionTimeout:     defaultSessionTimeout,
		loglevel:           defaultLoglevel,
		sessions:           map[string]*Session{},
		subscriptions:      map[string]map[string]*Session{},
		subscribers:        map[string]map[string]bool{},
		lock:               &sync.RWMutex{},
		logger:             log.New(os.Stdout, "pusher: ", 0),
	}
}

type PusherSessionStats map[string]int
type PusherStats struct {
	Sessions      map[string]PusherSessionStats            `json:"sessions"`
	Subscriptions map[string]map[string]PusherSessionStats `json:"subscriptions"`
	Subscribers   []string                                 `json:"subscribers"`
}

func (self *Server) Stats() PusherStats {
	stats := PusherStats{
		Sessions:      map[string]PusherSessionStats{},
		Subscriptions: map[string]map[string]PusherSessionStats{},
		Subscribers:   []string{},
	}
	for k, session := range self.sessions {
		stats.Sessions[k] = PusherSessionStats{}
		stats.Sessions[k]["output"] = len(session.output)
		stats.Sessions[k]["input"] = len(session.input)
		stats.Sessions[k]["connections"] = int(session.connections)
	}
	for subscription := range self.subscriptions {
		stats.Subscriptions[subscription] = map[string]PusherSessionStats{}
		for k, session := range self.subscriptions[subscription] {
			stats.Subscriptions[subscription][k] = PusherSessionStats{}
			stats.Subscriptions[subscription][k]["output"] = len(session.output)
			stats.Subscriptions[subscription][k]["input"] = len(session.input)
			stats.Subscriptions[subscription][k]["connections"] = int(session.connections)
		}
	}
	for subscriber := range self.subscribers {
		stats.Subscribers = append(stats.Subscribers, subscriber)
	}
	return stats
}

func (self *Server) Loglevel(i int) *Server {
	self.loglevel = i
	return self
}

func (self *Server) Logger(l *log.Logger) *Server {
	self.logger = l
	return self
}

func (self *Server) Fatalf(fmt string, i ...interface{}) {
	self.logger.Printf("FATAL: "+fmt, i...)
}

{'fraction_non_alphanumeric': 0.12369527963857299, 'fraction_numerical': 0.0046736251752609445, 'mean_word_length': 3.442906574394464, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1613618', 'n_tokens_mistral': 2157, 'n_tokens_neox': 2092, 'n_words': 419}