<commit_before>package data

import (
	"time"

	"github.com/jinzhu/gorm"

	// DB adapters
	_ "github.com/lib/pq"
	_ "github.com/mattn/go-sqlite3"
)

var db gorm.DB

type Check struct {
	Id        int64
	URL       string `gorm:"column:url"`
	Status    int    // status code of last result
	Success   bool   // success status of last result
	CreatedAt time.Time
	UpdatedAt time.Time
}

type Result struct {
	Id        int64
	CreatedAt time.Time
	Status    int
	Success   bool
	IP        string `gorm:"column:ip"`
	CheckId   int64
}

type User struct {
	Id          int64
	CreatedAt   time.Time
	Name        string
	Email       string
	GitHubId    int64  `gorm:"column:github_id"`
	GitHubLogin string `gorm:"column:github_login"`
	OAuthToken  string `gorm:"column:oauth_token"`
}

func InitDatabase() (err error) {
	db, err = gorm.Open("sqlite3", "/tmp/goffee.db")
	if err != nil {
		return err
	}

	db.AutoMigrate(&Check{}, &Result{}, &User{})

	return nil
}

func Checks() ([]Check, error) {
	var checks []Check
	res := db.Find(&checks)
	return checks, res.Error
}

func (c *Check) Create() error {
	res := db.Create(c)
	return res.Error
}

func (c *Check) AddResult(r *Result) error {
	tx := db.Begin()

	r.CheckId = c.Id
	res := tx.Create(r)
	if res.Error != nil {
		tx.Rollback()
		return res.Error
	}

	c.Status = r.Status
	c.Success = r.Success
	res = tx.Save(c)
	if res.Error != nil {
		tx.Rollback()
		return res.Error
	}

	tx.Commit()
	return nil
}

func (c *Check) Results() ([]Result, error) {
	var results []Result
	res := db.Model(c).Related(&results)
	return results, res.Error
}

func (u *User) UpdateOrCreate() error {
	res := db.Where(User{GitHubId: u.GitHubId}).Assign(*u).FirstOrInit(u)
	if res.Error != nil {
		return res.Error
	}

	res = db.Save(u)
	return res.Error
}<commit_msg>Add data.FindUser<commit_after>package data

import (
	"time"

	"github.com/jinzhu/gorm"

	// DB adapters
	_ "github.com/lib/pq"
	_ "github.com/mattn/go-sqlite3"
)

var db gorm.DB

type Check struct {
	Id        int64
	URL       string `gorm:"column:url"`
	Status    int    // status code of last result
	Success   bool   // success status of last result
	CreatedAt time.Time
	UpdatedAt time.Time
}

type Result struct {
	Id        int64
	CreatedAt time.Time
	Status    int
	Success   bool
	IP        string `gorm:"column:ip"`
	CheckId   int64
}

type User struct {
	Id          int64
	CreatedAt   time.Time
	Name        string
	Email       string
	GitHubId    int64  `gorm:"column:github_id"`
	GitHubLogin string `gorm:"column:github_login"`
	OAuthToken  string `gorm:"column:oauth_token"`
}

func InitDatabase() (err error) {
	db, err = gorm.Open("sqlite3", "/tmp/goffee.db")
	if err != nil {
		return err
	}

	db.AutoMigrate(&Check{}, &Result{}, &User{})

	return nil
}

func Checks() ([]Check, error) {
	var checks []Check
	res := db.Find(&checks)
	return checks, res.Error
}

func (c *Check) Create() error {
	res := db.Create(c)
	return res.Error
}

func (c *Check) AddResult(r *Result) error {
	tx := db.Begin()

	r.CheckId = c.Id
	res := tx.Create(r)
	if res.Error != nil {
		tx.Rollback()
		return res.Error
	}

	c.Status = r.Status
	c.Success = r.Success
	res = tx.Save(c)
	if res.Error != nil {
		tx.Rollback()
		return res.Error
	}

	tx.Commit()
	return nil
}

func (c *Check) Results() ([]Result, error) {
	var results []Result
	res := db.Model(c).Related(&results)
	return results, res.Error
}

func (u *User) UpdateOrCreate() error {
	res := db.Where(User{GitHubId: u.GitHubId}).Assign(*u).FirstOrInit(u)
	if res.Error != nil {
		return res.Error
	}

	res = db.Save(u)
	return res.Error
}

func FindUser(id int64) (user User, err error) {
	res := db.First(&user, id)
	if res.Error != nil {
		return user, res.Error
	}
	return user, nil
}
{'fraction_non_alphanumeric': 0.13942948547054118, 'fraction_numerical': 0.00693148493735004, 'mean_word_length': 3.0871459694989105, 'pattern_counts': {'":': 0, '<': 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': '2699312', 'n_tokens_mistral': 1485, 'n_tokens_neox': 1427, 'n_words': 374}