Skip to content

Commit

Permalink
refactor(targets): same style across targets (Cloudbox#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
l3uddz authored Jul 28, 2020
1 parent a53ef3f commit 99c7be4
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 67 deletions.
32 changes: 11 additions & 21 deletions targets/plex/datastore.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,46 +8,37 @@ import (
_ "github.com/mattn/go-sqlite3"
)

func NewDatastore(path string) (*Datastore, error) {
func NewDatastore(path string) (*datastore, error) {
db, err := sql.Open("sqlite3", path)
if err != nil {
return nil, fmt.Errorf("could not open database: %v", err)
}

return &Datastore{db: db}, nil
return &datastore{db: db}, nil
}

type Datastore struct {
type datastore struct {
db *sql.DB
}

type LibraryType int

const (
Movie LibraryType = 1
TV LibraryType = 2
Music LibraryType = 8
)

type Library struct {
type library struct {
ID int
Type LibraryType
Name string
Path string
}

func (d *Datastore) Libraries() ([]Library, error) {
func (d *datastore) Libraries() ([]library, error) {
rows, err := d.db.Query(sqlSelectLibraries)
if err != nil {
return nil, fmt.Errorf("select libraries: %v", err)
}

defer rows.Close()

libraries := make([]Library, 0)
libraries := make([]library, 0)
for rows.Next() {
l := Library{}
if err := rows.Scan(&l.ID, &l.Type, &l.Name, &l.Path); err != nil {
l := library{}
if err := rows.Scan(&l.ID, &l.Name, &l.Path); err != nil {
return nil, fmt.Errorf("scan library row: %v", err)
}

Expand All @@ -57,15 +48,15 @@ func (d *Datastore) Libraries() ([]Library, error) {
return libraries, nil
}

type MediaPart struct {
type mediaPart struct {
ID int
DirectoryID int
File string
Size uint64
}

func (d *Datastore) MediaPartByFile(path string) (*MediaPart, error) {
mp := new(MediaPart)
func (d *datastore) MediaPartByFile(path string) (*mediaPart, error) {
mp := new(mediaPart)

row := d.db.QueryRow(sqlSelectMediaPart, path)
err := row.Scan(&mp.ID, &mp.DirectoryID, &mp.File, &mp.Size)
Expand All @@ -76,7 +67,6 @@ const (
sqlSelectLibraries = `
SELECT
ls.id,
ls.section_type,
ls.name,
sl.root_path
FROM
Expand Down
24 changes: 9 additions & 15 deletions targets/plex/datastore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
_ "github.com/mattn/go-sqlite3"
)

func setupTest(t *testing.T) *Datastore {
func setupTest(t *testing.T) *datastore {
t.Helper()

ds, err := NewDatastore(":memory:")
Expand All @@ -26,7 +26,7 @@ func setupTest(t *testing.T) *Datastore {
return ds
}

func setupDatabase(t *testing.T, ds *Datastore, paths []string) {
func setupDatabase(t *testing.T, ds *datastore, paths []string) {
if len(paths) == 0 {
return
}
Expand All @@ -45,7 +45,7 @@ func setupDatabase(t *testing.T, ds *Datastore, paths []string) {

func TestDatastore_Media(t *testing.T) {
type expect struct {
item *MediaPart
item *mediaPart
err error
}
type test struct {
Expand All @@ -59,7 +59,7 @@ func TestDatastore_Media(t *testing.T) {
sql: []string{"test_data/media_schema.sql", "test_data/media_data_1.sql"},
item: "/data/Movies/10 Cloverfield Lane (2016)/10 Cloverfield Lane (2016) - Remux-1080p.x264.TrueHD-SiDMUX.mkv",
want: expect{
item: &MediaPart{
item: &mediaPart{
ID: 83073,
DirectoryID: 5,
File: "/data/Movies/10 Cloverfield Lane (2016)/10 Cloverfield Lane (2016) - Remux-1080p.x264.TrueHD-SiDMUX.mkv",
Expand Down Expand Up @@ -100,7 +100,7 @@ func TestDatastore_Media(t *testing.T) {
if !reflect.DeepEqual(mp, tc.want.item) {
t.Log(mp)
t.Log(tc.want)
t.Errorf("MediaPart does not match")
t.Errorf("mediaPart does not match")
}
}
}
Expand All @@ -109,23 +109,21 @@ func TestDatastore_Libraries(t *testing.T) {
type test struct {
sql []string
size int
want []Library
want []library
}

tests := []test{
{
sql: []string{"test_data/libraries_schema.sql", "test_data/libraries_data_1.sql"},
size: 2,
want: []Library{
want: []library{
{
ID: 1,
Type: Movie,
Name: "Movies",
Path: "/data/Movies",
},
{
ID: 2,
Type: TV,
Name: "TV",
Path: "/data/TV",
},
Expand All @@ -134,28 +132,24 @@ func TestDatastore_Libraries(t *testing.T) {
{
sql: []string{"test_data/libraries_data_2.sql"},
size: 4,
want: []Library{
want: []library{
{
ID: 1,
Type: Movie,
Name: "Movies",
Path: "/data/Movies",
},
{
ID: 2,
Type: TV,
Name: "TV",
Path: "/data/TV",
},
{
ID: 10,
Type: Music,
Name: "Music",
Path: "/data/Music",
},
{
ID: 12,
Type: Movie,
Name: "4K - Movies",
Path: "/data/4K/Movies",
},
Expand All @@ -176,7 +170,7 @@ func TestDatastore_Libraries(t *testing.T) {
}

if len(libraries) != tc.size {
t.Fatalf("Library counts do not match, expected: %d, got: %d", tc.size, len(libraries))
t.Fatalf("library counts do not match, expected: %d, got: %d", tc.size, len(libraries))
}

if !reflect.DeepEqual(libraries, tc.want) {
Expand Down
14 changes: 7 additions & 7 deletions targets/plex/plex.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ type Config struct {
type target struct {
url string
token string
libraries []Library
libraries []library

log zerolog.Logger
rewrite autoscan.Rewriter
store *Datastore
store *datastore
}

func New(c Config) (*target, error) {
Expand All @@ -39,20 +39,20 @@ func New(c Config) (*target, error) {
return nil, err
}

lc := autoscan.GetLogger(c.Verbosity).With().
l := autoscan.GetLogger(c.Verbosity).With().
Str("target", "plex").
Str("target_url", c.URL).Logger()
Str("url", c.URL).Logger()

lc.Debug().
l.Debug().
Interface("libraries", libraries).
Msgf("Retrieved %d libraries", len(libraries))
Msg("Retrieved libraries")

return &target{
url: c.URL,
token: c.Token,
libraries: libraries,

log: lc,
log: l,
rewrite: rewriter,
store: store,
}, nil
Expand Down
45 changes: 21 additions & 24 deletions targets/plex/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ func (t target) Scan(scans []autoscan.Scan) error {
// check for at-least one missing/changed file
process := false
for _, s := range scans {
fp := t.rewrite(filepath.Join(s.Folder, s.File))
targetFilePath := t.rewrite(filepath.Join(s.Folder, s.File))

pf, err := t.store.MediaPartByFile(fp)
targetFile, err := t.store.MediaPartByFile(targetFilePath)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
// trigger file not found in target
// local file not found in target
t.log.Debug().
Str("target_path", fp).
Msg("At least one scan does not exist in target")
Str("path", targetFilePath).
Msg("At least one local file did not exist in target datastore")

process = true
break
Expand All @@ -40,48 +40,45 @@ func (t target) Scan(scans []autoscan.Scan) error {
return fmt.Errorf("could not check plex datastore: %v: %w", err, autoscan.ErrFatal)
}

// trigger file was found in target
if pf.Size != s.Size {
// trigger file did not match in target
// local file was found in target
if targetFile.Size != s.Size {
// local file did not match in target
t.log.Debug().
Str("target_path", fp).
Uint64("target_size", pf.Size).
Uint64("trigger_size", s.Size).
Msg("Trigger file size does not match target file")
Str("path", targetFilePath).
Uint64("target_size", targetFile.Size).
Uint64("local_size", s.Size).
Msg("Local file size does not match in target datastore")

process = true
break
}
}

if !process {
// all scan task files existed in target
// all local task files existed in target
t.log.Debug().
Interface("scans", scans).
Msg("All trigger files existed within target")
Msg("All local files exist in target")
return nil
}

s := scans[0]
scanFolder := t.rewrite(s.Folder)
scanFolder := t.rewrite(scans[0].Folder)

// determine library for this scan
lib, err := t.getScanLibrary(scanFolder)
if err != nil {
t.log.Warn().
Err(err).
Int("target_retries", s.Retries).
Msg("No target library found")
return fmt.Errorf("%v: %w", err, autoscan.ErrRetryScan)
}

slog := t.log.With().
Str("target_path", scanFolder).
Str("target_library", lib.Name).
Int("target_retries", s.Retries).
l := t.log.With().
Str("path", scanFolder).
Str("library", lib.Name).
Logger()

slog.Debug().Msg("Sending scan request")
l.Trace().Msg("Sending scan request")

// create request
reqURL := autoscan.JoinURL(t.url, "library", "sections", strconv.Itoa(lib.ID), "refresh")
Expand Down Expand Up @@ -114,11 +111,11 @@ func (t target) Scan(scans []autoscan.Scan) error {
return fmt.Errorf("%v: failed validating scan request response: %w", res.Status, autoscan.ErrTargetUnavailable)
}

slog.Info().Msg("Scan requested")
l.Info().Msg("Scan queued")
return nil
}

func (t target) getScanLibrary(folder string) (*Library, error) {
func (t target) getScanLibrary(folder string) (*library, error) {
for _, l := range t.libraries {
if strings.HasPrefix(folder, l.Path) {
return &l, nil
Expand Down

0 comments on commit 99c7be4

Please sign in to comment.