Files
scroll/prover-stats-api/internal/controller/prover_task.go
Lawliet-Chan e3b451c641 feat(prover-stats-api): add prover stats API (#635)
Co-authored-by: xinran chen <lawliet@xinran-m1x.local>
Co-authored-by: georgehao <haohongfan@gmail.com>
Co-authored-by: HAOYUatHZ <37070449+HAOYUatHZ@users.noreply.github.com>
2023-07-20 15:33:29 +08:00

139 lines
4.4 KiB
Go

package controller
import (
"fmt"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
ctype "scroll-tech/common/types"
"scroll-tech/common/types/message"
"scroll-tech/prover-stats-api/internal/logic"
"scroll-tech/prover-stats-api/internal/types"
)
// ProverTaskController provides API controller.
type ProverTaskController struct {
logic *logic.ProverTaskLogic
}
// NewProverTaskController provides a ProverTask instance.
func NewProverTaskController(db *gorm.DB) *ProverTaskController {
return &ProverTaskController{
logic: logic.NewProverTaskLogic(db),
}
}
// ProverTasks godoc
// @Summary get all the prover task by prover public key
// @Description get all the prover task by prover public key
// @Tags prover_task
// @Accept plain
// @Produce plain
// @Param pubkey query string true "prover public key"
// @Param page query int true "page"
// @Param page_size query int true "page_size"
// @Param Authorization header string false "Bearer license"
// @Success 200 {array} types.ProverTaskSchema
// @Router /api/prover_task/v1/tasks [get]
func (c *ProverTaskController) ProverTasks(ctx *gin.Context) {
var pp types.ProverTasksPaginationParameter
if err := ctx.ShouldBind(&pp); err != nil {
nerr := fmt.Errorf("parameter invalid, err:%w", err)
types.RenderJSON(ctx, types.ErrParameterInvalidNo, nerr, nil)
return
}
tasks, err := c.logic.GetTasksByProver(ctx, pp.PublicKey, pp.Page, pp.PageSize)
if err != nil {
nerr := fmt.Errorf("controller.ProverTasks err:%w", err)
types.RenderJSON(ctx, types.ErrProverTaskFailure, nerr, nil)
return
}
var proverTaskSchemas []types.ProverTaskSchema
for _, task := range tasks {
proverTaskSchema := types.ProverTaskSchema{
TaskID: task.TaskID,
ProverName: task.ProverName,
TaskType: message.ProofType(task.TaskType).String(),
ProvingStatus: ctype.ProvingStatus(task.ProvingStatus).String(),
Reward: task.Reward.String(),
CreatedAt: task.CreatedAt,
}
proverTaskSchemas = append(proverTaskSchemas, proverTaskSchema)
}
types.RenderJSON(ctx, types.Success, nil, proverTaskSchemas)
}
// GetTotalRewards godoc
// @Summary give the total rewards of a prover
// @Description get uint64 by prover public key
// @Tags prover_task
// @Accept plain
// @Produce plain
// @Param pubkey path string true "prover public key"
// @Param Authorization header string false "Bearer license"
// @Success 200 {object} types.ProverTotalRewardsSchema
// @Router /api/prover_task/v1/total_rewards [get]
func (c *ProverTaskController) GetTotalRewards(ctx *gin.Context) {
var pp types.ProverTotalRewardsParameter
if err := ctx.ShouldBind(&pp); err != nil {
nerr := fmt.Errorf("parameter invalid, err:%w", err)
types.RenderJSON(ctx, types.ErrParameterInvalidNo, nerr, nil)
return
}
rewards, err := c.logic.GetTotalRewards(ctx, pp.PublicKey)
if err != nil {
nerr := fmt.Errorf("controller.GetTotalRewards, err:%w", err)
types.RenderJSON(ctx, types.ErrProverTotalRewardFailure, nerr, nil)
return
}
resp := types.ProverTotalRewardsSchema{
Rewards: rewards.String(),
}
types.RenderJSON(ctx, types.Success, nil, resp)
}
// GetTask godoc
// @Summary give the specific prover task
// @Description get prover task by task id
// @Tags prover_task
// @Accept plain
// @Produce plain
// @Param task_id path string true "prover task hash"
// @Param Authorization header string false "Bearer license"
// @Success 200 {object} types.ProverTaskSchema
// @Router /api/prover_task/v1/task [get]
func (c *ProverTaskController) GetTask(ctx *gin.Context) {
var pp types.ProverTaskParameter
if err := ctx.ShouldBind(&pp); err != nil {
nerr := fmt.Errorf("parameter invalid, err:%w", err)
types.RenderJSON(ctx, types.ErrParameterInvalidNo, nerr, nil)
return
}
task, err := c.logic.GetTask(ctx, pp.TaskID)
if err != nil {
nerr := fmt.Errorf("controller.GetTask, err:%w", err)
types.RenderJSON(ctx, types.ErrProverTotalRewardFailure, nerr, nil)
return
}
schema := types.ProverTaskSchema{
TaskID: task.TaskID,
ProverName: task.ProverName,
TaskType: message.ProofType(task.TaskType).String(),
ProvingStatus: ctype.ProvingStatus(task.ProvingStatus).String(),
Reward: task.Reward.String(),
CreatedAt: task.CreatedAt,
}
types.RenderJSON(ctx, types.Success, nil, schema)
}