<commit_before>package main

import (
	"bytes"
	"encoding/gob"
	"io/ioutil"
)

// HA HA, joke's on you! ENTIRE DB IS FILE!
const filename = "sparkledb.gob"

type SparkleDatabase struct {
	Sparkles     []Sparkle
	MostReceived []Leader
	MostGiven    []Leader
}

func (sparkledb *SparkleDatabase) Save() {
	// Persist the database to file
	var data bytes.Buffer
	contents := gob.NewEncoder(&data)
	err := contents.Encode(sparkledb)
	if err != nil {
		panic(err)
	}

	err = ioutil.WriteFile(filename, data.Bytes(), 0644)
	if err != nil {
		panic(err)
	}
}

func LoadDB() SparkleDatabase {
	// Load the database from a file
	n, err := ioutil.ReadFile(filename)
	// Create a bytes.Buffer
	p := bytes.NewBuffer(n)
	dec := gob.NewDecoder(p)

	var sparkleDB SparkleDatabase
	err = dec.Decode(&sparkleDB)

	if err != nil {
		panic(err)
	}

	return sparkleDB
}

func (sparkledb *SparkleDatabase) AddSparkle(sparkle Sparkle) {
	// Add a sparkle to the database
	sparkledb.Sparkles = append(sparkledb.Sparkles, sparkle)
	giver := Leader{Name: sparkle.Sparkler, Score: 1}
	receiver := Leader{Name: sparkle.Sparklee, Score: 1}

	receiver_found := false
	for k, v := range sparkledb.MostReceived {
		if v.Name == sparkle.Sparklee {
			receiver_found = true
			sparkledb.MostReceived[k].Score++
		}
	}

	if !receiver_found {
		sparkledb.MostReceived = append(sparkledb.MostReceived, receiver)
	}

	giver_found := false
	for k, v := range sparkledb.MostGiven {
		if v.Name == sparkle.Sparkler {
			giver_found = true
			sparkledb.MostGiven[k].Score++
		}
	}

	if !giver_found {
		sparkledb.MostGiven = append(sparkledb.MostGiven, giver)
	}

	// After the sparkle has been added, save the data file
	sparkledb.Save()
}

func (sparkledb *SparkleDatabase) TopUsers(n int) []Leader {
	// Get the top N leaders and return them
	leaders := make([]Leader, n)

	return leaders
}

func (db *SparkleDatabase) SparklesForUser(user string) []Sparkle {<commit_msg>Sorted top users<commit_after>package main

import (
	"bytes"
	"encoding/gob"
	"io/ioutil"
	"sort"
)

// HA HA, joke's on you! ENTIRE DB IS FILE!
const filename = "sparkledb.gob"

type SparkleDatabase struct {
	Sparkles     []Sparkle
	MostReceived []Leader
	MostGiven    []Leader
}

func (sparkledb *SparkleDatabase) Save() {
	// Persist the database to file
	var data bytes.Buffer
	contents := gob.NewEncoder(&data)
	err := contents.Encode(sparkledb)
	if err != nil {
		panic(err)
	}

	err = ioutil.WriteFile(filename, data.Bytes(), 0644)
	if err != nil {
		panic(err)
	}
}

func LoadDB() SparkleDatabase {
	// Load the database from a file
	n, err := ioutil.ReadFile(filename)
	// Create a bytes.Buffer
	p := bytes.NewBuffer(n)
	dec := gob.NewDecoder(p)

	var sparkleDB SparkleDatabase
	err = dec.Decode(&sparkleDB)

	if err != nil {
		panic(err)
	}

	return sparkleDB
}

func (sparkledb *SparkleDatabase) AddSparkle(sparkle Sparkle) {
	// Add a sparkle to the database
	sparkledb.Sparkles = append(sparkledb.Sparkles, sparkle)
	giver := Leader{Name: sparkle.Sparkler, Score: 1}
	receiver := Leader{Name: sparkle.Sparklee, Score: 1}

	// Add the receiver's data
	receiver_found := false
	for k, v := range sparkledb.MostReceived {
		if v.Name == sparkle.Sparklee {
			receiver_found = true
			sparkledb.MostReceived[k].Score++
		}
	}

	// Add the receiver if not already there
	if !receiver_found {
		sparkledb.MostReceived = append(sparkledb.MostReceived, receiver)
	}

	// Add the giver's data
	giver_found := false
	for k, v := range sparkledb.MostGiven {
		if v.Name == sparkle.Sparkler {
			giver_found = true
			sparkledb.MostGiven[k].Score++
		}
	}

	// Add the giver if not already there
	if !giver_found {
		sparkledb.MostGiven = append(sparkledb.MostGiven, giver)
	}

	// After the sparkle has been added, save the data file
	sparkledb.Save()
}

type ByScore []Leader

func (a ByScore) Len() int           { return len(a) }
func (a ByScore) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }
func (a ByScore) Less(i, j int) bool { return a[i].Score < a[j].Score }

func (sparkledb *SparkleDatabase) TopUsers(n int) []Leader {
	// Get the top N leaders and return them
	sort.Sort(ByScore(sparkledb.MostReceived))
	return sparkledb.MostReceived
}

func (db *SparkleDatabase) SparklesForUser(user string) []Sparkle {
{'fraction_non_alphanumeric': 0.12173913043478261, 'fraction_numerical': 0.00282021151586369, 'mean_word_length': 3.864, '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': '7256396', 'n_tokens_mistral': 1691, 'n_tokens_neox': 1570, 'n_words': 415}