Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEAT][API] Create Avatarization Job #70

Open
youen opened this issue Mar 31, 2024 · 0 comments
Open

[FEAT][API] Create Avatarization Job #70

youen opened this issue Mar 31, 2024 · 0 comments

Comments

@youen
Copy link
Member

youen commented Mar 31, 2024

Description

Creates an Avatarization job, which can then be used to calculate metrics.

Request

Method

POST

URL

/jobs

Request Parameters

Parameter Description Required Example
kind Type of job, must be "Avatarization" Yes Avatarization
parameters The job parameters Yes { "metric": "metric-id" }

Request Body

JSON-encoded AvatarizationJobCreate object.

Result

Result Parameters

Parameter Description
id Id of the created job
kind Type of the created job
created_at Timestamp of the created job
status Status of the job. Possible values are "Queued", "Running", "Error", and "Done"
error_message Error message, if the job ended with an error
traceback Traceback, if the job ended with an error
result Result of the job, if it ended with a success

Result Body

JSON-encoded AvatarizationJob object.

Example Curl Request

Request

curl -X POST \
  http://localhost:8080/jobs \
  -H 'Content-Type: application/json' \
  -d '{
    "kind": "Avatarization",
    "parameters": {
      "metric": "metric-id"
    }
  }'

Curl Response

{
  "id": "job-id",
  "kind": "Avatarization",
  "created_at": "2023-03-08T16:31:35.886981+00:00",
  "status": "Queued",
  "error_message": null,
  "traceback": null,
  "result": null,
  "parameters": {
    "metric": "metric-id"
  },
  "current_progress": null
}

Go server

Struct

type CreateAvatarizationJobRequest struct {
	Kind        string                            `json:"kind"`
	Parameters  AvatarizationJobParametersCreate `json:"parameters"`
}

type AvatarizationJobParametersCreate struct {
	Metric string `json:"metric"`
}

Handler

func (s *Server) createAvatarizationJob(ctx context.Context, w http.ResponseWriter, r *http.Request) {
	var request CreateAvatarizationJobRequest
	if err := json.NewDecoder(r.Body).Decode(&request); err != nil {
		http.Error(w, err.Error(), http.StatusBadRequest)
		return
	}
	if request.Kind != "Avatarization" {
		http.Error(w, "Kind must be Avatarization", http.StatusBadRequest)
		return
	}

	job := &AvatarizationJob{
		ID:          uuid.New().String(),
		Kind:        request.Kind,
		CreatedAt:   time.Now(),
		Status:      "Queued",
		Parameters:  request.Parameters,
		CurrentProgress: &JobProgress{
			StartedAt:  time.Now(),
			Percentage: 0,
		},
	}
	if err := s.db.CreateAvatarizationJob(ctx, job); err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	go func() {
		s.calculateAvatarizationJob(ctx, job)
	}()

	json.NewEncoder(w).Encode(job)
}

func (s *Server) calculateAvatarizationJob(ctx context.Context, job *AvatarizationJob) {
	// Calculate metrics
	result := &AvatarizationResult{}

	// Update job status and result
	job.Status = "Done"
	job.Result = result
	job.CurrentProgress = nil
	if err := s.db.UpdateAvatarizationJob(ctx, job); err != nil {
		log.Println(err)
		return
	}
}
```.Write(b); err != nil {
		log.Printf("error writing response: %v", err)
	}
}

Links

Automated Issue Details

Dear visitor,

This issue has been automatically generated from the Octopize project avatar-python to make SIGO compatible. Please vote with a thumbs up or thumbs down to assess the quality of the automatic generation.

Best regards,
The SIGO Team

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant