Skip to content

Commit

Permalink
Merge pull request #9 from brotherlogic/report_missing
Browse files Browse the repository at this point in the history
Added GetMissing method
  • Loading branch information
brotherlogic committed Jan 31, 2018
2 parents 8eacb5f + 39780b7 commit 42ef9c1
Show file tree
Hide file tree
Showing 5 changed files with 200 additions and 11 deletions.
33 changes: 32 additions & 1 deletion cdprocessor.go
Original file line number Diff line number Diff line change
@@ -1,26 +1,55 @@
package main

import (
"context"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"strconv"
"strings"
"time"

"github.com/brotherlogic/goserver"
"google.golang.org/grpc"

pbcdp "github.com/brotherlogic/cdprocessor/proto"
pbg "github.com/brotherlogic/goserver/proto"
"github.com/brotherlogic/goserver/utils"
pbrc "github.com/brotherlogic/recordcollection/proto"
)

type io interface {
readDir() ([]os.FileInfo, error)
convert(name string) (int32, error)
}

type rc interface {
get(filter *pbrc.Record) (*pbrc.GetRecordsResponse, error)
}

type prodRc struct{}

func (rc *prodRc) get(filter *pbrc.Record) (*pbrc.GetRecordsResponse, error) {
host, port, err := utils.Resolve("recordcollection")

if err != nil {
return &pbrc.GetRecordsResponse{}, err
}

conn, err := grpc.Dial(host+":"+strconv.Itoa(int(port)), grpc.WithInsecure())
defer conn.Close()
if err != nil {
return &pbrc.GetRecordsResponse{}, err
}

ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
client := pbrc.NewRecordCollectionServiceClient(conn)
return client.GetRecords(ctx, &pbrc.GetRecordsRequest{Filter: filter})
}

type prodIo struct {
dir string
}
Expand Down Expand Up @@ -49,12 +78,14 @@ func (i *prodIo) convert(name string) (int32, error) {
type Server struct {
*goserver.GoServer
io io
rc rc
}

// Init builds the server
func Init(dir string) *Server {
s := &Server{GoServer: &goserver.GoServer{},
io: &prodIo{dir: dir}}
io: &prodIo{dir: dir},
rc: &prodRc{}}
return s
}

Expand Down
31 changes: 31 additions & 0 deletions cdprocessorapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"golang.org/x/net/context"

pbcdp "github.com/brotherlogic/cdprocessor/proto"
pbgd "github.com/brotherlogic/godiscogs"
pbrc "github.com/brotherlogic/recordcollection/proto"
)

//GetRipped returns the ripped cds
Expand All @@ -29,3 +31,32 @@ func (s *Server) GetRipped(ctx context.Context, req *pbcdp.GetRippedRequest) (*p

return resp, nil
}

//GetMissing gets the missing rips
func (s *Server) GetMissing(ctx context.Context, req *pbcdp.GetMissingRequest) (*pbcdp.GetMissingResponse, error) {
resp := &pbcdp.GetMissingResponse{}

missing, err := s.rc.get(&pbrc.Record{Release: &pbgd.Release{FolderId: 242018}})
if err != nil {
return resp, err
}

ripped, err := s.GetRipped(ctx, &pbcdp.GetRippedRequest{})
if err != nil {
return resp, err
}

for _, r := range missing.Records {
found := false
for _, ri := range ripped.GetRippedIds() {
if ri == r.GetRelease().Id {
found = true
}
}
if !found {
resp.Missing = append(resp.GetMissing(), r)
}
}

return resp, nil
}
50 changes: 50 additions & 0 deletions cdprocessorapi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,24 @@ import (
"testing"

pbcdp "github.com/brotherlogic/cdprocessor/proto"
pbgd "github.com/brotherlogic/godiscogs"
pbrc "github.com/brotherlogic/recordcollection/proto"
)

type testRc struct {
failGet bool
}

func (rc *testRc) get(filter *pbrc.Record) (*pbrc.GetRecordsResponse, error) {
if rc.failGet {
return &pbrc.GetRecordsResponse{}, fmt.Errorf("Built to fail")
}
return &pbrc.GetRecordsResponse{Records: []*pbrc.Record{
&pbrc.Record{Release: &pbgd.Release{Id: 12345}},
&pbrc.Record{Release: &pbgd.Release{Id: 12346}},
}}, nil
}

type testIo struct {
dir string
failRead bool
Expand Down Expand Up @@ -77,3 +93,37 @@ func TestGetFailConvert(t *testing.T) {
t.Fatalf("Bad read did not fail: %v", err)
}
}

func TestGetMissing(t *testing.T) {
s := Init("testdata")
s.io = &testIo{dir: "testdata"}
s.rc = &testRc{}
missing, err := s.GetMissing(context.Background(), &pbcdp.GetMissingRequest{})
if err != nil {
t.Fatalf("Error getting missing: %v", err)
}

if len(missing.GetMissing()) != 1 || missing.GetMissing()[0].GetRelease().Id != 12346 {
t.Errorf("Rips reported missing: %v", missing)
}
}

func TestGetMissingFailGet(t *testing.T) {
s := Init("testdata")
s.io = &testIo{dir: "testdata"}
s.rc = &testRc{failGet: true}
missing, err := s.GetMissing(context.Background(), &pbcdp.GetMissingRequest{})
if err == nil {
t.Fatalf("Should have failed: %v", missing)
}
}

func TestGetMissingFailGetRipped(t *testing.T) {
s := Init("testdata")
s.io = &testIo{dir: "testdata", failConv: true}
s.rc = &testRc{}
missing, err := s.GetMissing(context.Background(), &pbcdp.GetMissingRequest{})
if err == nil {
t.Fatalf("Should have failed: %v", missing)
}
}
88 changes: 78 additions & 10 deletions proto/cdprocessor.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions proto/cdprocessor.proto
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,21 @@ syntax = "proto3";

package cdprocessor;

import "github.com/brotherlogic/recordcollection/proto/recordcollection.proto";

message GetRippedRequest {}

message GetRippedResponse {
repeated int32 ripped_ids = 1;
}

message GetMissingRequest {}

message GetMissingResponse {
repeated recordcollection.Record missing = 1;
}

service CDProcessor {
rpc GetRipped (GetRippedRequest) returns (GetRippedResponse);
rpc GetMissing (GetMissingRequest) returns (GetMissingResponse);
}

0 comments on commit 42ef9c1

Please sign in to comment.