<commit_before>package db

import (
	log "github.com/macarrie/flemzerd/logging"
	. "github.com/macarrie/flemzerd/objects"

	"github.com/jinzhu/gorm"
	_ "github.com/jinzhu/gorm/dialects/sqlite"
)

var Client *gorm.DB
var DbPath = DB_PATH
var Session SessionData

func InitDb() {
	Client.AutoMigrate(&SessionData{}, &TvShow{}, &Episode{}, &Movie{}, &MediaIds{}, &Torrent{}, &DownloadingItem{})
}

func ResetDb() {
	Client.DropTable(&SessionData{})
	Client.DropTable(&TvShow{})
	Client.DropTable(&Episode{})
	Client.DropTable(&Movie{})
	Client.DropTable(&MediaIds{})
	Client.DropTable(&Torrent{})
	Client.DropTable(&DownloadingItem{})
	InitDb()
}

func Load() error {
	dbObj, err := gorm.Open("sqlite3", DbPath)
	if err != nil {
		log.Fatal("Cannot open db: ", err)
	}
	Client = dbObj.Set("gorm:auto_preload", true)

	InitDb()

	Client.First(&Session)

	log.Info("Connection to Db successful")
	return nil
}

func TorrentHasFailed(d DownloadingItem, t Torrent) bool {
	if !d.Downloading {
		return false
	}

	for _, torrent := range d.FailedTorrents {
		if t.TorrentId == torrent.TorrentId {
			return true
		}
	}

	return false
}

func GetTrackedMovies() ([]Movie, error) {
	var movies []Movie
	var retList []Movie
	Client.Order("created_at DESC").Find(&movies)

	for _, m := range movies {
		if !m.DownloadingItem.Downloading && !m.DownloadingItem.Downloaded {
			retList = append(retList, m)
		}
	}

	return retList, nil
}

func GetTrackedTvShows() ([]TvShow, error) {
	var tvShows []TvShow

	Client.Order("status").Order("name").Find(&tvShows)

	return tvShows, nil
}

func GetDownloadingEpisodes() ([]Episode, error) {
	var episodes []Episode
	var retList []Episode
	Client.Unscoped().Find(&episodes)

	for _, e := range episodes {
		if e.DownloadingItem.Downloading && !e.DownloadingItem.Downloaded {
			retList = append(retList, e)
		}
	}
	return retList, nil
}

func GetDownloadingMovies() ([]Movie, error) {
	var movies []Movie
	var retList []Movie
	Client.Find(&movies)

	for _, m := range movies {
		if m.DownloadingItem.Downloading && !m.DownloadingItem.Downloaded {
			retList = append(retList, m)
		}
	}

	return retList, nil
}

func GetDownloadedEpisodes() ([]Episode, error) {
	var episodes []Episode<commit_msg>Get pending items too when gettings downloading items from db<commit_after>package db

import (
	log "github.com/macarrie/flemzerd/logging"
	. "github.com/macarrie/flemzerd/objects"

	"github.com/jinzhu/gorm"
	_ "github.com/jinzhu/gorm/dialects/sqlite"
)

var Client *gorm.DB
var DbPath = DB_PATH
var Session SessionData

func InitDb() {
	Client.AutoMigrate(&SessionData{}, &TvShow{}, &Episode{}, &Movie{}, &MediaIds{}, &Torrent{}, &DownloadingItem{})
}

func ResetDb() {
	Client.DropTable(&SessionData{})
	Client.DropTable(&TvShow{})
	Client.DropTable(&Episode{})
	Client.DropTable(&Movie{})
	Client.DropTable(&MediaIds{})
	Client.DropTable(&Torrent{})
	Client.DropTable(&DownloadingItem{})
	InitDb()
}

func Load() error {
	dbObj, err := gorm.Open("sqlite3", DbPath)
	if err != nil {
		log.Fatal("Cannot open db: ", err)
	}
	Client = dbObj.Set("gorm:auto_preload", true)

	InitDb()

	Client.First(&Session)

	log.Info("Connection to Db successful")
	return nil
}

func TorrentHasFailed(d DownloadingItem, t Torrent) bool {
	if !d.Downloading {
		return false
	}

	for _, torrent := range d.FailedTorrents {
		if t.TorrentId == torrent.TorrentId {
			return true
		}
	}

	return false
}

func GetTrackedMovies() ([]Movie, error) {
	var movies []Movie
	var retList []Movie
	Client.Order("created_at DESC").Find(&movies)

	for _, m := range movies {
		if !m.DownloadingItem.Downloading && !m.DownloadingItem.Downloaded {
			retList = append(retList, m)
		}
	}

	return retList, nil
}

func GetTrackedTvShows() ([]TvShow, error) {
	var tvShows []TvShow

	Client.Order("status").Order("name").Find(&tvShows)

	return tvShows, nil
}

func GetDownloadingEpisodes() ([]Episode, error) {
	var episodes []Episode
	var retList []Episode
	Client.Unscoped().Find(&episodes).Order("id DESC")

	for _, e := range episodes {
		if (e.DownloadingItem.Downloading || e.DownloadingItem.Pending) && !e.DownloadingItem.Downloaded {
			retList = append(retList, e)
		}
	}
	return retList, nil
}

func GetDownloadingMovies() ([]Movie, error) {
	var movies []Movie
	var retList []Movie
	Client.Find(&movies).Order("id DESC")

	for _, m := range movies {
		if (m.DownloadingItem.Downloading || m.DownloadingItem.Pending) && !m.DownloadingItem.Downloaded {
			retList = append(retList, m)
		}
	}

	return retList, nil
}

func GetDownloadedEpisodes() ([]Episode, error) {
	var episodes []Episode
{'fraction_non_alphanumeric': 0.14932520679146713, 'fraction_numerical': 0.00043535045711797995, 'mean_word_length': 4.700992555831266, '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': 59, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2778538', 'n_tokens_mistral': 1760, 'n_tokens_neox': 1665, 'n_words': 358}