<commit_before>package sla

import (
	"fmt"
	"strconv"
)

const (
	SlaKeyRetentionHours = "retention.hours"
	SlaKeyRetentionBytes = "retention.bytes"
	SlaKeyPartitions     = "partitions"
	SlaKeyReplicas       = "replicas"
)

const (
	defaultRetentionBytes = -1     // unlimited
	defaultRetentionHours = 3 * 24 // 3 days
	defaultPartitions     = 1
	defaultReplicas       = 2
)

type TopicSla struct {
	RetentionHours float64
	RetentionBytes int
	Partitions     int
	Replicas       int
}

func DefaultSla() *TopicSla {
	return &TopicSla{
		RetentionBytes: -1,
		RetentionHours: defaultRetentionHours,
		Partitions:     defaultPartitions,
		Replicas:       defaultReplicas,
	}
}

func (this *TopicSla) IsDefault() bool {
	return this.Replicas == defaultReplicas &&
		this.Partitions == defaultPartitions &&
		this.RetentionBytes == defaultRetentionBytes &&
		this.RetentionHours == defaultRetentionHours
}

func (this *TopicSla) ParseRetentionHours(s string) error {
	if len(s) == 0 {
		return ErrEmptyArg
	}

	f, e := strconv.ParseFloat(s, 64)
	if e != nil {
		return ErrNotNumber
	}

	if f < 0 {
		return ErrNegative
	}

	this.RetentionHours = f

	return nil
}

// Dump the sla for kafka-topics.sh as arguments.
func (this *TopicSla) DumpForTopicsCli() []string {
	r := make([]string, 0)
	if this.Partitions != defaultPartitions && this.Partitions > 0 {
		r = append(r, fmt.Sprintf("--partitions %d", this.Partitions))
	}
	if this.Replicas != defaultReplicas && this.Replicas > 0 {
		r = append(r, fmt.Sprintf("--replication-factor %d", this.Replicas))
	}
	if this.RetentionBytes != defaultRetentionBytes && this.RetentionBytes > 0 {
		r = append(r, fmt.Sprintf("--config retention.bytes=%d", this.RetentionBytes))
	}
	if this.RetentionHours != defaultRetentionHours && this.RetentionHours > 0 {
		r = append(r, fmt.Sprintf("--config retention.ms=%d",
			int(this.RetentionHours*1000*3600)))
	}
	return r
}<commit_msg>validation of upper limit for SLA's<commit_after>package sla

import (
	"fmt"
	"strconv"
)

const (
	SlaKeyRetentionHours = "retention.hours"
	SlaKeyRetentionBytes = "retention.bytes"
	SlaKeyPartitions     = "partitions"
	SlaKeyReplicas       = "replicas"
)

const (
	defaultRetentionBytes = -1     // unlimited
	defaultRetentionHours = 3 * 24 // 3 days
	defaultPartitions     = 1
	defaultReplicas       = 2

	maxReplicas       = 3
	maxPartitions     = 20
	maxRetentionHours = 7 * 24
)

type TopicSla struct {
	RetentionHours float64
	RetentionBytes int
	Partitions     int
	Replicas       int
}

func DefaultSla() *TopicSla {
	return &TopicSla{
		RetentionBytes: -1,
		RetentionHours: defaultRetentionHours,
		Partitions:     defaultPartitions,
		Replicas:       defaultReplicas,
	}
}

func (this *TopicSla) IsDefault() bool {
	return this.Replicas == defaultReplicas &&
		this.Partitions == defaultPartitions &&
		this.RetentionBytes == defaultRetentionBytes &&
		this.RetentionHours == defaultRetentionHours
}

func (this *TopicSla) ParseRetentionHours(s string) error {
	if len(s) == 0 {
		return ErrEmptyArg
	}

	f, e := strconv.ParseFloat(s, 64)
	if e != nil {
		return ErrNotNumber
	}

	if f < 0 {
		return ErrNegative
	}

	this.RetentionHours = f

	return nil
}

// Dump the sla for kafka-topics.sh as arguments.
func (this *TopicSla) DumpForTopicsCli() []string {
	r := make([]string, 0)
	if this.Partitions != defaultPartitions && this.Partitions > 0 && this.Partitions <= maxPartitions {
		r = append(r, fmt.Sprintf("--partitions %d", this.Partitions))
	}
	if this.Replicas != defaultReplicas && this.Replicas > 0 && this.Replicas <= maxReplicas {
		r = append(r, fmt.Sprintf("--replication-factor %d", this.Replicas))
	}
	if this.RetentionBytes != defaultRetentionBytes && this.RetentionBytes > 0 {
		r = append(r, fmt.Sprintf("--config retention.bytes=%d", this.RetentionBytes))
	}
	if this.RetentionHours != defaultRetentionHours && this.RetentionHours > 0 && this.RetentionHours <= maxRetentionHours {
		r = append(r, fmt.Sprintf("--config retention.ms=%d",
			int(this.RetentionHours*1000*3600)))
	}
	return r
}
{'fraction_non_alphanumeric': 0.11968348170128586, 'fraction_numerical': 0.01483679525222552, 'mean_word_length': 3.885265700483092, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 11, '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': '2422676', 'n_tokens_mistral': 1539, 'n_tokens_neox': 1439, 'n_words': 320}