Skip to content

Commit

Permalink
Update services, limit text in status to 200 chars
Browse files Browse the repository at this point in the history
  • Loading branch information
airenas committed Dec 15, 2022
1 parent f602edc commit 3a8166c
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 10 deletions.
10 changes: 6 additions & 4 deletions internal/pkg/status/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,17 @@ const (
Working
// Completed - final step
Completed
// Failure - final failing step
Failure
// ServiceError - indicates service error
ServiceError
// ServiceError - indicates service error
NotFound
)

var (
statusName = map[Status]string{Uploaded: "UPLOADED", Completed: "COMPLETED",
Working: "Working", Failure: "Failure"}
Working: "Working", ServiceError: "SERVICE_ERROR", NotFound: "NOT_FOUND"}
nameStatus = map[string]Status{"UPLOADED": Uploaded, "COMPLETED": Completed,
"Working": Working, "Failure": Failure}
"Working": Working, "SERVICE_ERROR": ServiceError, "NOT_FOUND": NotFound}
)

func (st Status) String() string {
Expand Down
5 changes: 3 additions & 2 deletions internal/pkg/status/constants_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ func TestStatus_String(t *testing.T) {
{st: Uploaded, want: "UPLOADED"},
{st: Completed, want: "COMPLETED"},
{st: Working, want: "Working"},
{st: Failure, want: "Failure"},
{st: ServiceError, want: "SERVICE_ERROR"},
{st: NotFound, want: "NOT_FOUND"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand All @@ -34,7 +35,7 @@ func TestFrom(t *testing.T) {
{args: "olia", want: 0},
{args: "Working", want: Working},
{args: "UPLOADED", want: Uploaded},
{args: "Failure", want: Failure},
{args: "SERVICE_ERROR", want: ServiceError},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
4 changes: 3 additions & 1 deletion internal/pkg/statusservice/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/gorilla/websocket"

"github.com/airenas/roxy/internal/pkg/persistence"
"github.com/airenas/roxy/internal/pkg/status"
"github.com/airenas/roxy/internal/pkg/utils"

"github.com/airenas/go-app/pkg/goapp"
Expand Down Expand Up @@ -114,7 +115,8 @@ func statusHandler(data *Data) func(echo.Context) error {
}
var res result
if st == nil {
res = result{ID: id, Status: "NOT_FOUND", Error: "NOT_FOUND", ErrorCode: "NOT_FOUND"}
res = result{ID: id, Status: status.NotFound.String(), Error: fmt.Sprintf("Nežinomas ID: %s", id),
ErrorCode: status.NotFound.String()}

} else {
res = *mapStatus(st)
Expand Down
2 changes: 1 addition & 1 deletion internal/pkg/statusservice/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func Test_Status_Empty(t *testing.T) {
dbMock.On("LoadStatus", mock.Anything, mock.Anything).Return(nil, nil)
resp := testCode(t, req, http.StatusOK)
res := test.Decode[result](t, resp.Result())
assert.Equal(t, result{ID: "2", Status: "NOT_FOUND", Error: "NOT_FOUND", ErrorCode: "NOT_FOUND",
assert.Equal(t, result{ID: "2", Status: "NOT_FOUND", Error: "Nežinomas ID: 2", ErrorCode: "NOT_FOUND",
AvailableResults: nil}, res)
}

Expand Down
11 changes: 9 additions & 2 deletions internal/pkg/worker/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ func handleStatus(ctx context.Context, m *messages.StatusMessage, data *ServiceD
status.ErrorCode = utils.ToSQLStr(m.ErrorCode)
status.Progress.Int32 = int32(m.Progress)
status.Status = m.Status
status.RecognizedText = utils.ToSQLStr(m.RecognizedText)
status.RecognizedText = utils.ToSQLStr(limit(m.RecognizedText, 200))
if err := data.DB.UpdateStatus(ctx, status); err != nil {
return fmt.Errorf("can't save status: %w", err)
}
Expand All @@ -219,6 +219,13 @@ func handleStatus(ctx context.Context, m *messages.StatusMessage, data *ServiceD
return nil
}

func limit(s string, l int) string {
if len(s) > l {
return s[:l-3] + "..."
}
return s
}

func getInformFinishType(errStr string) string {
if errStr == "" {
return amessages.InformTypeFinished
Expand All @@ -238,7 +245,7 @@ func handleFailure(ctx context.Context, m *messages.ASRMessage, data *ServiceDat
goapp.Log.Info().Str("ID", m.ID).Msg("error set - ignore")
}
statusRec.Error = utils.ToSQLStr(m.Error)
statusRec.ErrorCode = utils.ToSQLStr(status.Failure.String())
statusRec.ErrorCode = utils.ToSQLStr(status.ServiceError.String())
if err := data.DB.UpdateStatus(ctx, statusRec); err != nil {
return fmt.Errorf("can't save status: %w", err)
}
Expand Down
21 changes: 21 additions & 0 deletions internal/pkg/worker/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,24 @@ func Test_prepareParams(t *testing.T) {
})
}
}

func Test_limit(t *testing.T) {
type args struct {
s string
l int
}
tests := []struct {
name string
args args
want string
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := limit(tt.args.s, tt.args.l); got != tt.want {
t.Errorf("limit() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit 3a8166c

Please sign in to comment.