<commit_before>package models

import (
	"errors"
	"github.com/fatih/structs"
	"github.com/mitchellh/mapstructure"
)

type Consumer struct {
	id       int
	Username string `json:"username"`
	ApiKey   string `json:"apikey"`
}

// NewConsumer creates new consumer. It generates an apikey if none is given.
func NewConsumer(username, apiKey string) (*Consumer, error) {
	usernameExists := FindConsumerBy("Username", username)
	apikeyExists := FindConsumerBy("ApiKey", apiKey)
	if usernameExists != nil {
		return nil, errors.New("Given username already exists")
	}
	if apiKey != "" && apikeyExists != nil {
		return nil, errors.New("Given apikey already exists")
	}

	if apiKey == "" {
		var keyExists *Consumer
		for {
			apiKey = newUuid()
			keyExists = FindConsumerBy("ApiKey", apiKey)
			if keyExists == nil {
				break
			}
		}
	}
	return &Consumer{
		id:       0,
		Username: username,
		ApiKey:   apiKey,
	}, nil
}

// GetConsumerFromInterface converts an map[string]interface{} (from tiedot) to a Consumer struct
func GetConsumerFromInterface(id int, c map[string]interface{}) *Consumer {
	var consumer Consumer
	if err := mapstructure.Decode(c, &consumer); err != nil {
		return nil
	}

	consumer.id = id
	return &consumer
}

// UpdateFromForm update consumer from form values
func (c *Consumer) UpdateFromForm(form map[string][]string) {
	for k, v := range form {
		switch k {
		case "username":
			c.Username = v[0]
		case "apikey":
			c.ApiKey = v[0]
		}
	}
}

// FindAllConsumers returns all consumers
func FindAllConsumers() []*Consumer {
	consumers := store.Use("Consumers")
	var consumersList []*Consumer = make([]*Consumer, 0)<commit_msg>Add RateLimit field to consumer model<commit_after>package models

import (
	"errors"
	"github.com/fatih/structs"
	"github.com/mitchellh/mapstructure"
)

type Consumer struct {
	id        int
	Username  string         `json:"username"`
	ApiKey    string         `json:"apikey"`
	RateLimit map[string]int `json:"ratelimit"`
}

// NewConsumer creates new consumer. It generates an apikey if none is given.
func NewConsumer(username, apiKey string) (*Consumer, error) {
	usernameExists := FindConsumerBy("Username", username)
	apikeyExists := FindConsumerBy("ApiKey", apiKey)
	if usernameExists != nil {
		return nil, errors.New("Given username already exists")
	}
	if apiKey != "" && apikeyExists != nil {
		return nil, errors.New("Given apikey already exists")
	}

	if apiKey == "" {
		var keyExists *Consumer
		for {
			apiKey = newUuid()
			keyExists = FindConsumerBy("ApiKey", apiKey)
			if keyExists == nil {
				break
			}
		}
	}
	return &Consumer{
		id:        0,
		Username:  username,
		ApiKey:    apiKey,
		RateLimit: make(map[string]int, 0),
	}, nil
}

// GetConsumerFromInterface converts an map[string]interface{} (from tiedot) to a Consumer struct
func GetConsumerFromInterface(id int, c map[string]interface{}) *Consumer {
	var consumer Consumer
	if err := mapstructure.Decode(c, &consumer); err != nil {
		return nil
	}

	consumer.id = id
	return &consumer
}

// UpdateFromForm update consumer from form values
func (c *Consumer) UpdateFromForm(form map[string][]string) {
	for k, v := range form {
		switch k {
		case "username":
			c.Username = v[0]
		case "apikey":
			c.ApiKey = v[0]
		}
	}
}

// FindAllConsumers returns all consumers
func FindAllConsumers() []*Consumer {
	consumers := store.Use("Consumers")
	var consumersList []*Consumer = make([]*Consumer, 0)
{'fraction_non_alphanumeric': 0.1240174672489083, 'fraction_numerical': 0.0026200873362445414, 'mean_word_length': 3.9869375907111757, 'pattern_counts': {'":': 4, '<': 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': '5141200', 'n_tokens_mistral': 1180, 'n_tokens_neox': 1094, 'n_words': 320}