Backend: Move capture package to pkg/

Signed-off-by: Michael Mayer <michael@liquidbytes.net>
This commit is contained in:
Michael Mayer 2020-01-12 14:25:58 +01:00
parent f8a45b14d9
commit 3e4e72b00f
13 changed files with 32 additions and 55 deletions

View file

@ -1,14 +0,0 @@
/*
This package contains profiling functions, mainly for testing.
Additional information can be found in our Developer Guide:
https://github.com/photoprism/photoprism/wiki
*/
package capture
import (
"github.com/photoprism/photoprism/internal/event"
)
var log = event.Log

View file

@ -1,19 +0,0 @@
package capture
import (
"bytes"
"os"
"testing"
"github.com/sirupsen/logrus"
)
var logBuffer bytes.Buffer
func TestMain(m *testing.M) {
log = logrus.StandardLogger()
log.Out = &logBuffer
log.SetLevel(logrus.DebugLevel)
code := m.Run()
os.Exit(code)
}

View file

@ -1,10 +0,0 @@
package capture
import (
"time"
)
func Time(start time.Time, name string) {
elapsed := time.Since(start)
log.Debugf("%s [%s]", name, elapsed)
}

View file

@ -3,7 +3,7 @@ package commands
import (
"testing"
"github.com/photoprism/photoprism/internal/capture"
"github.com/photoprism/photoprism/pkg/capture"
"github.com/photoprism/photoprism/internal/config"
"github.com/stretchr/testify/assert"
)

View file

@ -14,7 +14,7 @@ import (
"github.com/disintegration/imaging"
"github.com/djherbis/times"
"github.com/photoprism/photoprism/internal/capture"
"github.com/photoprism/photoprism/pkg/capture"
"github.com/photoprism/photoprism/internal/entity"
"github.com/photoprism/photoprism/pkg/fs"
"github.com/photoprism/photoprism/internal/meta"
@ -715,7 +715,7 @@ func (m *MediaFile) Resample(path string, typeName string) (img image.Image, err
}
func (m *MediaFile) CreateDefaultThumbnails(thumbPath string, force bool) (err error) {
defer capture.Time(time.Now(), fmt.Sprintf("thumbs: created for \"%s\"", m.Filename()))
defer log.Debug(capture.Time(time.Now(), fmt.Sprintf("thumbs: created for \"%s\"", m.Filename())))
hash := m.Hash()

View file

@ -5,7 +5,7 @@ import (
"strings"
"time"
"github.com/photoprism/photoprism/internal/capture"
"github.com/photoprism/photoprism/pkg/capture"
"github.com/photoprism/photoprism/internal/entity"
"github.com/photoprism/photoprism/internal/form"
)
@ -54,7 +54,7 @@ func (s *Repo) Albums(f form.AlbumSearch) (results []AlbumResult, err error) {
return results, err
}
defer capture.Time(time.Now(), fmt.Sprintf("search: %+v", f))
defer log.Debug(capture.Time(time.Now(), fmt.Sprintf("search: %+v", f)))
q := s.db.NewScope(nil).DB()

View file

@ -6,7 +6,7 @@ import (
"time"
"github.com/gosimple/slug"
"github.com/photoprism/photoprism/internal/capture"
"github.com/photoprism/photoprism/pkg/capture"
"github.com/photoprism/photoprism/internal/entity"
"github.com/photoprism/photoprism/internal/form"
)
@ -91,7 +91,7 @@ func (s *Repo) Labels(f form.LabelSearch) (results []LabelResult, err error) {
return results, err
}
defer capture.Time(time.Now(), fmt.Sprintf("search: %+v", f))
defer log.Debug(capture.Time(time.Now(), fmt.Sprintf("search: %+v", f)))
q := s.db.NewScope(nil).DB()

View file

@ -6,7 +6,7 @@ import (
"time"
"github.com/gosimple/slug"
"github.com/photoprism/photoprism/internal/capture"
"github.com/photoprism/photoprism/pkg/capture"
"github.com/photoprism/photoprism/internal/entity"
"github.com/photoprism/photoprism/internal/form"
)
@ -103,7 +103,7 @@ func (s *Repo) Photos(f form.PhotoSearch) (results []PhotoResult, err error) {
return results, err
}
defer capture.Time(time.Now(), fmt.Sprintf("search: %+v", f))
defer log.Debug(capture.Time(time.Now(), fmt.Sprintf("search: %+v", f)))
q := s.db.NewScope(nil).DB()

8
pkg/capture/capture.go Normal file
View file

@ -0,0 +1,8 @@
/*
Package capture provides profiling functions for testing and debugging.
Additional information can be found in our Developer Guide:
https://github.com/photoprism/photoprism/wiki
*/
package capture

View file

@ -6,7 +6,7 @@ import (
"os"
)
// Returns output to stdout and stderr for testing
// Output returns output to stdout and stderr for testing.
func Output(f func()) string {
r, w, err := os.Pipe()
if err != nil {

12
pkg/capture/time.go Normal file
View file

@ -0,0 +1,12 @@
package capture
import (
"fmt"
"time"
)
// Time returns the input string with the time elapsed added.
func Time(start time.Time, label string) string {
elapsed := time.Since(start)
return fmt.Sprintf("%s [%s]", label, elapsed)
}

View file

@ -11,6 +11,6 @@ import (
func TestTime(t *testing.T) {
start := time.Now()
time.Sleep(1 * time.Millisecond)
Time(start, fmt.Sprintf("%s", "Successful test"))
assert.Contains(t, logBuffer.String(), "Successful test [")
result := Time(start, fmt.Sprintf("%s", "Successful test"))
assert.Contains(t, result, "Successful test [")
}