<commit_before>package influxdb

import (
	"errors"
	"fmt"
	"github.com/influxdata/influxdb/client/v2"
	"github.com/loadimpact/speedboat/sampler"
	neturl "net/url"
	"sync"
)

type Output struct {
	Client   client.Client
	Database string

	currentBatch client.BatchPoints
	batchMutex   sync.Mutex
}

func New(conf client.HTTPConfig, db string) (*Output, error) {
	c, err := client.NewHTTPClient(conf)
	if err != nil {
		return nil, err
	}

	return &Output{
		Client:   c,
		Database: db,
	}, nil
}

func NewFromURL(url string) (*Output, error) {
	conf, db, err := parseURL(url)
	if err != nil {
		return nil, err
	}
	return New(conf, db)
}

func parseURL(url string) (conf client.HTTPConfig, db string, err error) {
	u, err := neturl.Parse(url)
	if err != nil {
		return conf, db, err
	}

	if u.Path == "" || u.Path == "/" {
		return conf, db, errors.New("No InfluxDB database specified")
	}
	db = u.Path[1:]

	conf.Addr = fmt.Sprintf("%s://%s", u.Scheme, u.Host)
	if u.User != nil {
		conf.Username = u.User.Username()
		conf.Password, _ = u.User.Password()
	}
	return conf, db, nil
}

func (o *Output) Write(m *sampler.Metric, e *sampler.Entry) error {
	o.batchMutex.Lock()
	defer o.batchMutex.Unlock()

	if o.currentBatch == nil {
		batch, err := client.NewBatchPoints(client.BatchPointsConfig{
			Database: o.Database,
		})
		if err != nil {
			return err
		}
		o.currentBatch = batch
	}

	tags := make(map[string]string)
	for key, value := range e.Fields {
		tags[key] = fmt.Sprint(value)
	}
	fields := map[string]interface{}{"value": e.Value}

	point, err := client.NewPoint(m.Name, tags, fields, e.Time)
	if err != nil {
		return err
	}
	o.currentBatch.AddPoint(point)
	return nil
}

func (o *Output) Commit() error {
	o.batchMutex.Lock()
	defer o.batchMutex.Unlock()

	err := o.Client.Write(o.currentBatch)
	o.currentBatch = nil
	return err
}<commit_msg>[fix] Prevent crashes when no batch is prepared<commit_after>package influxdb

import (
	"errors"
	"fmt"
	"github.com/influxdata/influxdb/client/v2"
	"github.com/loadimpact/speedboat/sampler"
	neturl "net/url"
	"sync"
)

type Output struct {
	Client   client.Client
	Database string

	currentBatch client.BatchPoints
	batchMutex   sync.Mutex
}

func New(conf client.HTTPConfig, db string) (*Output, error) {
	c, err := client.NewHTTPClient(conf)
	if err != nil {
		return nil, err
	}

	return &Output{
		Client:   c,
		Database: db,
	}, nil
}

func NewFromURL(url string) (*Output, error) {
	conf, db, err := parseURL(url)
	if err != nil {
		return nil, err
	}
	return New(conf, db)
}

func parseURL(url string) (conf client.HTTPConfig, db string, err error) {
	u, err := neturl.Parse(url)
	if err != nil {
		return conf, db, err
	}

	if u.Path == "" || u.Path == "/" {
		return conf, db, errors.New("No InfluxDB database specified")
	}
	db = u.Path[1:]

	conf.Addr = fmt.Sprintf("%s://%s", u.Scheme, u.Host)
	if u.User != nil {
		conf.Username = u.User.Username()
		conf.Password, _ = u.User.Password()
	}
	return conf, db, nil
}

func (o *Output) Write(m *sampler.Metric, e *sampler.Entry) error {
	o.batchMutex.Lock()
	defer o.batchMutex.Unlock()

	if o.currentBatch == nil {
		batch, err := client.NewBatchPoints(client.BatchPointsConfig{
			Database: o.Database,
		})
		if err != nil {
			return err
		}
		o.currentBatch = batch
	}

	tags := make(map[string]string)
	for key, value := range e.Fields {
		tags[key] = fmt.Sprint(value)
	}
	fields := map[string]interface{}{"value": e.Value}

	point, err := client.NewPoint(m.Name, tags, fields, e.Time)
	if err != nil {
		return err
	}
	o.currentBatch.AddPoint(point)
	return nil
}

func (o *Output) Commit() error {
	o.batchMutex.Lock()
	defer o.batchMutex.Unlock()

	if o.currentBatch == nil {
		return nil
	}

	err := o.Client.Write(o.currentBatch)
	o.currentBatch = nil
	return err
}
{'fraction_non_alphanumeric': 0.15074980268350435, 'fraction_numerical': 0.0010523546435148646, 'mean_word_length': 3.6029055690072638, 'pattern_counts': {'":': 2, '<': 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': '4484857', 'n_tokens_mistral': 1487, 'n_tokens_neox': 1430, 'n_words': 360}