Skip to content
This repository has been archived by the owner on Jun 14, 2018. It is now read-only.

Commit

Permalink
Decoupled presentation model from internal model
Browse files Browse the repository at this point in the history
  • Loading branch information
carlosroman committed Mar 1, 2017
1 parent 4ff888d commit 053e06a
Show file tree
Hide file tree
Showing 6 changed files with 203 additions and 126 deletions.
99 changes: 88 additions & 11 deletions model.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
)

type job struct {
sync.RWMutex
JobID string `json:"jobID"`
ConceptType string `json:"conceptType"`
IDs []string `json:"IDToTID,omitempty"`
Expand All @@ -28,18 +27,96 @@ type createJobRequest struct {
IDS []string `json:"ids"`
}

func (theJob *job) updateStatus(status string) {
theJob.Lock()
theJob.Status = status
theJob.Unlock()
type internalJob struct {
sync.RWMutex
jobID string
conceptType string
ids []string
url string
gtgURL string
throttle int
count uint64
progress uint64
status string
failedIDs []string
}

func (j *internalJob) updateStatus(status string) {
j.Lock()
j.status = status
j.Unlock()
}

func (j *internalJob) getProgress() uint64 {
return atomic.LoadUint64(&j.progress)
}

func (j *internalJob) setProgress(i uint64) {
atomic.StoreUint64(&j.progress, i)
}

func (j *internalJob) getStatus() string {
j.RLock()
defer j.RUnlock()
return j.status
}

func (j *internalJob) updateCount(count uint64) {
j.Lock()
j.count = count
j.Unlock()
}

func (j *internalJob) getCount() uint64 {
return atomic.LoadUint64(&j.count)
}

func (theJob *job) updateCount(count uint64) {
theJob.Lock()
theJob.Count = count
theJob.Unlock()
func (j *internalJob) appendFailedID(id string) {
j.Lock()
j.failedIDs = append(j.failedIDs, id)
j.Unlock()
}

func (theJob *job) incrementProgress() {
atomic.AddUint64(&theJob.Progress, 1)
func (j *internalJob) getFailedIDs() []string {
j.RLock()
defer j.RUnlock()
return j.failedIDs
}

func (j *internalJob) incrementProgress() {
atomic.AddUint64(&j.progress, 1)
}

func (j *internalJob) getJobFiltered() *job {
j.RLock()
defer j.RUnlock()
return &job{
JobID: j.jobID,
ConceptType: j.conceptType,
Count: j.getCount(),
Progress: j.getProgress(),
Status: j.status,
Throttle: j.throttle,
URL: j.url,
GtgURL: j.gtgURL,
}

}

func (j *internalJob) getJob() *job {
j.RLock()
defer j.RUnlock()
return &job{
JobID: j.jobID,
ConceptType: j.conceptType,
IDs: j.ids,
Count: j.getCount(),
Progress: j.getProgress(),
Status: j.status,
Throttle: j.throttle,
URL: j.url,
GtgURL: j.gtgURL,
FailedIDs: j.failedIDs,
}

}
15 changes: 3 additions & 12 deletions publish_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func (h publishHandler) createJob(w http.ResponseWriter, r *http.Request) {
type shortJob struct {
JobID string `json:"jobID"`
}
sj := shortJob{JobID: theJob.JobID}
sj := shortJob{JobID: theJob.jobID}
enc := json.NewEncoder(w)
err = enc.Encode(sj)
if err != nil {
Expand All @@ -77,18 +77,9 @@ func (h publishHandler) status(w http.ResponseWriter, r *http.Request) {
}
var filteredJob *job
if _, ok := r.URL.Query()["full"]; !ok {
filteredJob = &job{
JobID: theJob.JobID,
ConceptType: theJob.ConceptType,
Count: theJob.Count,
Progress: theJob.Progress,
Status: theJob.Status,
Throttle: theJob.Throttle,
URL: theJob.URL,
GtgURL: theJob.GtgURL,
}
filteredJob = theJob.getJobFiltered()
} else {
filteredJob = theJob
filteredJob = theJob.getJob()
}
w.Header().Add("Content-Type", "application/json")
enc := json.NewEncoder(w)
Expand Down
30 changes: 15 additions & 15 deletions publish_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func TestHandlerCreateJob(t *testing.T) {
tests := []struct {
name string
httpBody string
createJobF func(ids []string, baseURL string, gtgURL string, throttle int) (*job, error)
createJobF func(ids []string, baseURL string, gtgURL string, throttle int) (*internalJob, error)
expectedStatus int
}{
{
Expand All @@ -35,15 +35,15 @@ func TestHandlerCreateJob(t *testing.T) {
{
name: "missing fields",
httpBody: `{"concept":"topics", "url":"/__topics-transformer/transformers/topics"}`,
createJobF: func(ids []string, baseURL string, gtgURL string, throttle int) (*job, error) {
return &job{JobID: "1"}, nil
createJobF: func(ids []string, baseURL string, gtgURL string, throttle int) (*internalJob, error) {
return &internalJob{jobID: "1"}, nil
},
expectedStatus: http.StatusCreated,
},
{
name: "error at subsequent call",
httpBody: `{"concept":"topics", "url":"/__topics-transformer/transformers/topics", "throttle": 100}`,
createJobF: func(ids []string, baseURL string, gtgURL string, throttle int) (*job, error) {
createJobF: func(ids []string, baseURL string, gtgURL string, throttle int) (*internalJob, error) {
return nil, errors.New("error creating job because of something")
},
expectedStatus: http.StatusBadRequest,
Expand All @@ -57,7 +57,7 @@ func TestHandlerCreateJob(t *testing.T) {
}
var pubService publisher = mockedPublisher{
createJobF: test.createJobF,
runJobF: func(theJob *job, authorization string) {},
runJobF: func(theJob *internalJob, authorization string) {},
}
pubHandler := newPublishHandler(&pubService)
recorder := httptest.NewRecorder()
Expand All @@ -79,21 +79,21 @@ func TestHandlerStatus(t *testing.T) {
tests := []struct {
name string
jobID string
getJobF func(string) (*job, error)
getJobF func(string) (*internalJob, error)
expectedStatus int
}{
{
name: "normal case",
jobID: "1",
getJobF: func(jobID string) (*job, error) {
return &job{JobID: "1"}, nil
getJobF: func(jobID string) (*internalJob, error) {
return &internalJob{jobID: "1"}, nil
},
expectedStatus: http.StatusOK,
},
{
name: "not found",
jobID: "1",
getJobF: func(jobID string) (*job, error) {
getJobF: func(jobID string) (*internalJob, error) {
return nil, newNotFoundError("1")
},
expectedStatus: http.StatusNotFound,
Expand Down Expand Up @@ -212,26 +212,26 @@ func TestHandlerDeleteJob(t *testing.T) {
}

type mockedPublisher struct {
createJobF func(ids []string, baseURL string, gtgURL string, throttle int) (*job, error)
getJobF func(jobID string) (*job, error)
createJobF func(ids []string, baseURL string, gtgURL string, throttle int) (*internalJob, error)
getJobF func(jobID string) (*internalJob, error)
getJobIdsF func() []string
runJobF func(theJob *job, authorization string)
runJobF func(theJob *internalJob, authorization string)
deleteJobF func(jobID string) error
}

func (p mockedPublisher) createJob(conceptType string, ids []string, baseURL string, gtgURL string, throttle int) (*job, error) {
func (p mockedPublisher) createJob(conceptType string, ids []string, baseURL string, gtgURL string, throttle int) (*internalJob, error) {
return p.createJobF(ids, baseURL, baseURL+"__gtg", throttle)
}

func (p mockedPublisher) getJob(jobID string) (*job, error) {
func (p mockedPublisher) getJob(jobID string) (*internalJob, error) {
return p.getJobF(jobID)
}

func (p mockedPublisher) getJobIds() []string {
return p.getJobIdsF()
}

func (p mockedPublisher) runJob(theJob *job, authorization string) {
func (p mockedPublisher) runJob(theJob *internalJob, authorization string) {
p.runJobF(theJob, authorization)
}

Expand Down

0 comments on commit 053e06a

Please sign in to comment.