<commit_before>// Copyright © 2017 The Things Network
// Use of this source code is governed by the MIT license that can be found in the LICENSE file.

package monitorclient

import (
	"context"
	"io"
	"net"
	"strings"
	"sync"
	"sync/atomic"
	"time"

	"github.com/TheThingsNetwork/go-utils/backoff"
	"github.com/TheThingsNetwork/go-utils/grpc/rpclog"
	"github.com/TheThingsNetwork/go-utils/grpc/streambuffer"
	"github.com/TheThingsNetwork/go-utils/log"
	"github.com/TheThingsNetwork/ttn/api/broker"
	"github.com/TheThingsNetwork/ttn/api/gateway"
	"github.com/TheThingsNetwork/ttn/api/handler"
	"github.com/TheThingsNetwork/ttn/api/monitor"
	"github.com/TheThingsNetwork/ttn/api/networkserver"
	"github.com/TheThingsNetwork/ttn/api/pool"
	"github.com/TheThingsNetwork/ttn/api/router"
	"github.com/mwitkow/go-grpc-middleware"
	"google.golang.org/grpc"
	"google.golang.org/grpc/codes"
	"google.golang.org/grpc/credentials"
)

// DefaultBufferSize sets the default buffer size per stream
var DefaultBufferSize = 10

// MonitorOption changes something in the MonitorClient
type MonitorOption func(m *MonitorClient)

// WithConn adds the given conns to the MonitorClient
func WithConn(name string, conn *grpc.ClientConn) MonitorOption {
	return func(m *MonitorClient) {
		m.clients[name] = monitor.NewMonitorClient(conn)
	}
}

// WithServer [DEPRECATED] connects to the given server and adds it to the MonitorClient
// Instead of using WithServer, you should set up the gRPC connection externally and use
// WithConn to add it to the MonitorClient.
func WithServer(name, addr string, opts ...grpc.DialOption) MonitorOption {
	if len(opts) == 0 {
		opts = append(opts,
			grpc.WithUnaryInterceptor(grpc_middleware.ChainUnaryClient(
				rpclog.UnaryClientInterceptor(nil),
			)),
			grpc.WithStreamInterceptor(grpc_middleware.ChainStreamClient(
				rpclog.StreamClientInterceptor(nil),
			)),
		)
		if strings.HasSuffix(name, "-tls") {
			netHost, _, _ := net.SplitHostPort(addr)
			creds := credentials.NewTLS(pool.TLSConfig(netHost))
			opts = append(opts, grpc.WithTransportCredentials(creds))
		} else {
			opts = append(opts, grpc.WithInsecure())
		}
	}
	return func(m *MonitorClient) {
		conn, err := grpc.Dial(addr, opts...)
		if err != nil {
			return
		}
		m.clients[name] = monitor.NewMonitorClient(conn)
	}
}

// NewMonitorClient returns a new MonitorClient<commit_msg>Make Monitor conns secure by default<commit_after>// Copyright © 2017 The Things Network
// Use of this source code is governed by the MIT license that can be found in the LICENSE file.

package monitorclient

import (
	"context"
	"io"
	"net"
	"strings"
	"sync"
	"sync/atomic"
	"time"

	"github.com/TheThingsNetwork/go-utils/backoff"
	"github.com/TheThingsNetwork/go-utils/grpc/rpclog"
	"github.com/TheThingsNetwork/go-utils/grpc/streambuffer"
	"github.com/TheThingsNetwork/go-utils/log"
	"github.com/TheThingsNetwork/ttn/api/broker"
	"github.com/TheThingsNetwork/ttn/api/gateway"
	"github.com/TheThingsNetwork/ttn/api/handler"
	"github.com/TheThingsNetwork/ttn/api/monitor"
	"github.com/TheThingsNetwork/ttn/api/networkserver"
	"github.com/TheThingsNetwork/ttn/api/pool"
	"github.com/TheThingsNetwork/ttn/api/router"
	"github.com/mwitkow/go-grpc-middleware"
	"google.golang.org/grpc"
	"google.golang.org/grpc/codes"
	"google.golang.org/grpc/credentials"
)

// DefaultBufferSize sets the default buffer size per stream
var DefaultBufferSize = 10

// MonitorOption changes something in the MonitorClient
type MonitorOption func(m *MonitorClient)

// WithConn adds the given conns to the MonitorClient
func WithConn(name string, conn *grpc.ClientConn) MonitorOption {
	return func(m *MonitorClient) {
		m.clients[name] = monitor.NewMonitorClient(conn)
	}
}

// WithServer [DEPRECATED] connects to the given server and adds it to the MonitorClient
// Instead of using WithServer, you should set up the gRPC connection externally and use
// WithConn to add it to the MonitorClient.
func WithServer(name, addr string, opts ...grpc.DialOption) MonitorOption {
	if len(opts) == 0 {
		opts = append(opts,
			grpc.WithUnaryInterceptor(grpc_middleware.ChainUnaryClient(
				rpclog.UnaryClientInterceptor(nil),
			)),
			grpc.WithStreamInterceptor(grpc_middleware.ChainStreamClient(
				rpclog.StreamClientInterceptor(nil),
			)),
		)
		if strings.HasSuffix(name, "-insecure") {
			opts = append(opts, grpc.WithInsecure())
		} else {
			netHost, _, _ := net.SplitHostPort(addr)
			creds := credentials.NewTLS(pool.TLSConfig(netHost))
			opts = append(opts, grpc.WithTransportCredentials(creds))
		}
	}
	return func(m *MonitorClient) {
		conn, err := grpc.Dial(addr, opts...)
		if err != nil {
			return
		}
		m.clients[name] = monitor.NewMonitorClient(conn)
	}
}

// NewMonitorClient returns a new MonitorClient
{'fraction_non_alphanumeric': 0.1165129924559933, 'fraction_numerical': 0.002933780385582565, 'mean_word_length': 4.575934579439252, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5431382', 'n_tokens_mistral': 1708, 'n_tokens_neox': 1613, 'n_words': 350}