Skip to content

Commit

Permalink
feat(artifact): Impled getting artifacts
Browse files Browse the repository at this point in the history
  • Loading branch information
breadrock1 committed Jun 18, 2024
1 parent 291b09f commit 433d543
Show file tree
Hide file tree
Showing 10 changed files with 744 additions and 408 deletions.
364 changes: 237 additions & 127 deletions docs/docs.go

Large diffs are not rendered by default.

408 changes: 237 additions & 171 deletions docs/swagger.json

Large diffs are not rendered by default.

283 changes: 177 additions & 106 deletions docs/swagger.yaml

Large diffs are not rendered by default.

24 changes: 24 additions & 0 deletions internal/ocr/artifacts/artifacts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package artifacts

type OcrArtifacts struct {
AllDocTypes DocTypes `json:"doc_types"`
}

type DocTypes struct {
TN OcrDocType `json:"tn"`
Smgs OcrDocType `json:"smgs"`
Conosament OcrDocType `json:"bill_of_landing"`
}

type OcrDocType struct {
Name string `json:"name"`
JsonName string `json:"json_name"`

Check warning on line 15 in internal/ocr/artifacts/artifacts.go

View workflow job for this annotation

GitHub Actions / golangci

var-naming: struct field JsonName should be JSONName (revive)
SampleFileName string `json:"sample_file_name"`
Artifacts []Arts `json:"artifacts"`
}

type Arts struct {
GroupName string `json:"name"`
GroupJsonName string `json:"json_name"`

Check warning on line 22 in internal/ocr/artifacts/artifacts.go

View workflow job for this annotation

GitHub Actions / golangci

var-naming: struct field GroupJsonName should be GroupJSONName (revive)
Type string `json:"type"`
}
5 changes: 5 additions & 0 deletions internal/ocr/assistant/assistant.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package assistant

import (
"bytes"
"doc-notifier/internal/ocr/artifacts"
"encoding/json"
"fmt"
"log"
Expand Down Expand Up @@ -73,3 +74,7 @@ func (s *Service) GetProcessingJobs() map[string]*processing.ProcessJob {
func (s *Service) GetProcessingJob(_ string) *processing.ProcessJob {
return nil
}

func (s *Service) GetArtifacts() *artifacts.OcrArtifacts {
return nil
}
24 changes: 24 additions & 0 deletions internal/ocr/logoper/logoper.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package logoper

import (
"bytes"
"doc-notifier/internal/ocr/artifacts"
"encoding/json"
"fmt"
"io"
Expand All @@ -19,6 +20,7 @@ import (

const CheckJobStatusTimeout = 5 * time.Second
const RecognitionURL = "/api/v3/text/create_extraction"
const TemplatesURL = "/api/v2/artifacts/doc_types"
const GetResultURL = "/api/v2/text/get"

type Service struct {
Expand Down Expand Up @@ -182,3 +184,25 @@ func (s *Service) clearSuccessfulTasks() {
}
s.mu.Unlock()
}

func (s *Service) GetArtifacts() *artifacts.OcrArtifacts {
var arts *artifacts.OcrArtifacts

targetURL := fmt.Sprintf("%s%s", s.Address, TemplatesURL)
response, err := http.Get(targetURL)

Check failure on line 192 in internal/ocr/logoper/logoper.go

View workflow job for this annotation

GitHub Actions / golangci

G107: Potential HTTP request made with variable url (gosec)
if err != nil {
log.Printf("Error while creating request: %s", err)
return arts
}

var respData []byte
if respData, err = io.ReadAll(response.Body); err != nil {
log.Printf("Failed while reading response reqBody: %s", err)
return arts
}

if err = json.Unmarshal(respData, &arts); err != nil {
log.Println(err)
}
return arts
}
2 changes: 2 additions & 0 deletions internal/ocr/public.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ocr

import (
"doc-notifier/internal/config"
"doc-notifier/internal/ocr/artifacts"
"doc-notifier/internal/ocr/assistant"
"doc-notifier/internal/ocr/logoper"
"doc-notifier/internal/ocr/raw"
Expand All @@ -14,6 +15,7 @@ type Service struct {

type Recognizer interface {
RecognizeFile(document *reader.Document) error
GetArtifacts() *artifacts.OcrArtifacts
}

func New(config *config.OcrConfig) *Service {
Expand Down
11 changes: 8 additions & 3 deletions internal/ocr/raw/raw.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package raw

import (
"doc-notifier/internal/ocr/artifacts"
"fmt"
"log"
"os"
Expand All @@ -16,7 +17,7 @@ func New() *Service {
return &Service{}
}

func (re *Service) RecognizeFile(document *reader.Document) error {
func (s *Service) RecognizeFile(document *reader.Document) error {
bytesData, err := os.ReadFile(document.DocumentPath)
if err != nil {
log.Println("Failed while reading file: ", err)
Expand All @@ -32,10 +33,14 @@ func (re *Service) RecognizeFile(document *reader.Document) error {
return nil
}

func (re *Service) GetProcessingJobs() map[string]*processing.ProcessJob {
func (s *Service) GetProcessingJobs() map[string]*processing.ProcessJob {
return make(map[string]*processing.ProcessJob)
}

func (re *Service) GetProcessingJob(jobId string) *processing.ProcessJob {
func (s *Service) GetProcessingJob(jobId string) *processing.ProcessJob {

Check warning on line 40 in internal/ocr/raw/raw.go

View workflow job for this annotation

GitHub Actions / golangci

unused-parameter: parameter 'jobId' seems to be unused, consider removing or renaming it as _ (revive)
return nil
}

func (s *Service) GetArtifacts() *artifacts.OcrArtifacts {
return nil
}
29 changes: 29 additions & 0 deletions internal/server/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func (s *Service) CreateFilesGroup() error {

group.POST("/upload", s.UploadFilesToUnrecognized)
//group.POST("/analyse", s.AnalyseFiles, timeoutMW)

Check failure on line 22 in internal/server/files.go

View workflow job for this annotation

GitHub Actions / golangci

commentFormatting: put a space between `//` and comment text (gocritic)
group.GET("/artifacts", s.GetArtifactsTemplate)
group.POST("/analyse", s.AnalyseFiles)
group.POST("/download", s.DownloadFile)
group.POST("/move", s.MoveFiles)
Expand Down Expand Up @@ -127,6 +128,34 @@ func (s *Service) GetUnrecognized(c echo.Context) error {
return c.JSON(200, s.watcher.Reader.GetAwaitDocuments())
}

// GetArtifactsTemplate
// @Summary Get available artifacts
// @Description Get available artifacts
// @ID files-artifacts
// @Tags files
// @Produce json
// @Param document_type query string true "document_type"
// @Success 200 {object} map[string]interface{} "Ok"
// @Failure 400 {object} BadRequestForm "Bad Request message"
// @Failure 503 {object} ServerErrorForm "Server does not available"
// @Router /watcher/files/artifacts [get]
func (s *Service) GetArtifactsTemplate(c echo.Context) error {
artifacts := s.watcher.Ocr.Ocr.GetArtifacts()

docType := c.QueryParam("document_type")
switch docType {
case "tn":
return c.JSON(200, artifacts.AllDocTypes.TN)
case "smgs":
return c.JSON(200, artifacts.AllDocTypes.Smgs)

case "conosament":
return c.JSON(200, artifacts.AllDocTypes.Conosament)
default:
return c.JSON(200, artifacts)
}
}

// MoveFiles
// @Summary Moving files to target directory
// @Description Moving files to target directory
Expand Down
2 changes: 1 addition & 1 deletion internal/server/folders.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ func (s *Service) RunWatchers(c echo.Context) error {
// @Accept multipart/form
// @Produce json
// @Param files formData file true "Files multipart form"
// @Success 200 {array} reader.DocumentPreview "Ok"
// @Success 200 {array} []reader.Document "Ok"
// @Failure 400 {object} BadRequestForm "Bad Request message"
// @Failure 503 {object} ServerErrorForm "Server does not available"
// @Router /watcher/folders/upload [post]
Expand Down

0 comments on commit 433d543

Please sign in to comment.