<commit_before>package server

import (
	"encoding/json"
	"github.com/olivere/elastic"
	"log"
	"reflect"
	"strings"
)

const (
	TYPE string = "irc"
)

type ElasticHistory struct {
	host             string
	client           *elastic.Client
	index            string
	incomingMessages chan *Message
}

func NewElasticHistory(host string, index string) *ElasticHistory {
	var err error

	history := new(ElasticHistory)
	history.client, err = elastic.NewClient(elastic.SetURL(host))
	history.incomingMessages = make(chan *Message)
	history.index = strings.Replace(index, "#", "", -1)

	if err != nil {
		log.Panicf("Failed to connect to elasticsearch : %e", err)
	}

	go history.incomingLoop()

	return history
}

func (history *ElasticHistory) GetLastMessages(count int) ([]byte, error) {
	result, err := history.client.Search().
		Index(history.index).
		Type(TYPE).
		Sort("date", true).
		From(0).Size(count).
		Do()

	if err != nil {
		return nil, err
	}

	log.Printf("Fetched %d entries\n", result.TotalHits())

	resultSlice := make([]Message, result.TotalHits(), result.TotalHits())
	var tmpMessage Message

	for i, m := range result.Each(reflect.TypeOf(tmpMessage)) {
		resultSlice[i] = m.(Message)
	}

	return json.Marshal(resultSlice)
}<commit_msg>Fixed - correctly fetch latest irc entries<commit_after>package server

import (
	"encoding/json"
	"github.com/olivere/elastic"
	"log"
	"reflect"
	"strings"
)

const (
	TYPE string = "irc"
)

type ElasticHistory struct {
	host             string
	client           *elastic.Client
	index            string
	incomingMessages chan *Message
}

func NewElasticHistory(host string, index string) *ElasticHistory {
	var err error

	history := new(ElasticHistory)
	history.client, err = elastic.NewClient(elastic.SetURL(host))
	history.incomingMessages = make(chan *Message)
	history.index = strings.Replace(index, "#", "", -1)

	if err != nil {
		log.Panicf("Failed to connect to elasticsearch : %e", err)
	}

	go history.incomingLoop()

	return history
}

func (history *ElasticHistory) GetLastMessages(count int) ([]byte, error) {
	result, err := history.client.Search().
		Index(history.index).
		Type(TYPE).
		Sort("date", false).
		From(0).Size(count).
		Do()

	if err != nil {
		return nil, err
	}

	resultCnt := len(result.Hits.Hits)

	log.Printf("Fetched %d entries\n", resultCnt)

	resultSlice := make([]Message, resultCnt, resultCnt)
	var tmpMessage Message

	for i, m := range result.Each(reflect.TypeOf(tmpMessage)) {
		resultSlice[i] = m.(Message)
	}

	reverseHistory(resultSlice)

	return json.Marshal(resultSlice)
}

func reverseHistory(result []Message) {
	for i, j := 0, len(result)-1; i < j; i, j = i+1, j-1 {
		tmp := result[i]
		result[i] = result[j]
		result[j] = tmp
	}
}
{'fraction_non_alphanumeric': 0.13910186199342825, 'fraction_numerical': 0.0029207740051113546, 'mean_word_length': 3.892857142857143, 'pattern_counts': {'":': 0, '<': 4, '<?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': '6841317', 'n_tokens_mistral': 984, 'n_tokens_neox': 932, 'n_words': 228}