Commit bfc209e0 authored by brent's avatar brent

modify taskheat

parent d5c29184
......@@ -2922,6 +2922,8 @@ func initTypeInRedis() []models.TaskHeat {
OutPutJson: output,
AccessStatus: dbType.AccessStatus,
PublishStatus: dbType.PublishStatus,
EstimatExeTime: dbType.EstimatExeTime,
StartUpTime: dbType.StartUpTime,
}
response = append(response, retask)
}
......
......@@ -12,6 +12,8 @@ import (
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"github.com/beego/beego/orm"
"github.com/beego/beego/v2/core/logs"
beego "github.com/beego/beego/v2/server/web"
"io"
......@@ -227,6 +229,65 @@ func (server *UserController) UserInfo() {
server.respond(http.StatusOK, "", userInfo)
}
func (server *UserController) FreeCallCount() {
info, err := server.Check()
if err != nil {
server.respond(http.StatusUnauthorized, err.Error())
return
}
body := server.Ctx.Input.RequestBody
appRequest := models.AppRequest{}
_ = json.Unmarshal(body, &appRequest) //解析body中数据
logs.Debug("appRequest", appRequest, string(body))
if appRequest.Page == 0 {
appRequest.Page = 1
}
if appRequest.Size == 0 {
appRequest.Size = 10
}
offset := (appRequest.Page - 1) * appRequest.Size
countQB, _ := orm.NewQueryBuilder("mysql")
queryQB, _ := orm.NewQueryBuilder("mysql")
countQB.Select("count(*) AS total").
From("task_type")
queryQB.Select("id", "name", "api_path").
From("task_type")
if !(info.Role == 1 || info.Role == 2) {
countQB.And(fmt.Sprintf("user_id = '%d'", info.UserID))
queryQB.And(fmt.Sprintf("user_id = '%d'", info.UserID))
}
if appRequest.Keyword != "" {
keyword := "%" + appRequest.Keyword + "%"
countQB.And(fmt.Sprintf("name like '%s'", keyword))
queryQB.And(fmt.Sprintf("name like '%s'", keyword))
}
queryQB.Limit(int(appRequest.Size)).Offset(int(offset))
sql := countQB.String()
var total int64
_ = mysql.GetMysqlInstace().Ormer.Raw(sql).QueryRow(&total)
// 导出 SQL 语句
var taskTypes []*models.TaskType
sql = queryQB.String()
mysql.GetMysqlInstace().Ormer.Raw(sql).QueryRows(&taskTypes)
responseData := struct {
Total int64 `json:"total"`
Data interface{} `json:"data,omitempty"`
}{
Total: total,
Data: taskTypes,
}
server.respond(http.StatusOK, "", responseData)
}
func regisgerUser(user models.User) (*models.User, error) {
var err error
qs := mysql.GetMysqlInstace().Ormer.QueryTable("user")
......
package cronjob
import (
"ai_developer_admin/libs/mysql"
"ai_developer_admin/libs/postgres"
"ai_developer_admin/libs/redis"
"ai_developer_admin/libs/registry"
......@@ -15,6 +16,7 @@ import (
"github.com/robfig/cron/v3"
"sort"
"strconv"
"strings"
"time"
)
......@@ -33,38 +35,94 @@ func Start() {
//startRegistBackend()
}
func findTaskCount(counts []models.TaskCount, id string) int {
for _, value := range counts {
if strings.Compare(value.Type, id) == 0 {
count, _ := strconv.Atoi(value.Count)
return count
}
}
return 0
}
func startHeatKey() {
//spec := "0 0 * * * ?" //"@every 1h"
spec := "@every 1h" //"@every 1h"
//spec := "*/1 * * * * ?" //"@every 1h"
loopCronTask.AddFunc(spec, func() {
logs.Debug("loopCronTask")
sql := "SELECT type,count(type) FROM bills GROUP BY type ORDER BY count DESC;"
tasks, err := postgres.CountTasks(sql)
if err != nil {
qs := mysql.GetMysqlInstace().Ormer.QueryTable("task_type")
count, _ := qs.Count()
logs.Debug("types = ", count)
var types []*models.TaskType
if count > 0 {
qs.All(&types)
} else {
return
}
dataString, err := redis.GetDataToString(HeatKey)
sql := "SELECT type,count(type) FROM bills GROUP BY type ORDER BY count DESC;"
tasks, err := postgres.CountTasks(sql)
//if err != nil {
// return
//}
//dataString, err := redis.GetDataToString(HeatKey)
var response []*models.TaskHeat
if err != nil {
return
}
err = json.Unmarshal([]byte(dataString), &response)
if err != nil {
return
}
for _, value := range tasks {
taskId, err := strconv.Atoi(value.Type)
if err != nil {
continue
//if err != nil {
// return
//}
//err = json.Unmarshal([]byte(dataString), &response)
//if err != nil {
// return
//}
for _, dbType := range types {
var hardwareRequire interface{}
eer := json.Unmarshal([]byte(dbType.HardwareRequire), &hardwareRequire)
if eer != nil {
}
count, _ := strconv.Atoi(value.Count)
for _, taskType := range response {
if taskType.TaskId == taskId {
taskType.Count = int64(count)
var output interface{}
if dbType.Form != "" {
err := json.Unmarshal([]byte(dbType.Form), &output)
if err != nil {
logs.Debug("Form Unmarshal err")
}
}
heat := findTaskCount(tasks, strconv.Itoa(dbType.Id))
retask := models.TaskHeat{
TaskId: dbType.Id,
User: dbType.Username,
Pwd: dbType.Password,
Repository: dbType.ImageUrl,
SignUrl: dbType.SignUrl,
ImageName: dbType.ImageName,
ImageId: dbType.ImageId,
HardwareRequire: hardwareRequire,
Count: int64(heat),
Kind: dbType.Kind,
FileExpiresTime: strconv.Itoa(dbType.ResultFileExpires),
OutPutJson: output,
AccessStatus: dbType.AccessStatus,
PublishStatus: dbType.PublishStatus,
EstimatExeTime: dbType.EstimatExeTime,
StartUpTime: dbType.StartUpTime,
}
response = append(response, &retask)
}
//for _, value := range tasks {
// taskId, err := strconv.Atoi(value.Type)
// if err != nil {
// continue
// }
// count, _ := strconv.Atoi(value.Count)
// for _, taskType := range response {
// if taskType.TaskId == taskId {
// taskType.Count = int64(count)
// }
// }
//}
//for _, value := range tasks {
// taskId, err := strconv.Atoi(value.Type)
// if err != nil {
......
......@@ -104,6 +104,7 @@ type TaskType struct {
Unit string `json:"unit";orm:"column(unit)"`
Sort int `json:"sort";orm:"column(sort)"`
EstimatExeTime int `json:"estimat_exe_time";orm:"column(estimat_exe_time)"`
StartUpTime int `json:"start_up_time";orm:"column(start_up_time)"`
MaxExecTime int `json:"max_exec_time";orm:"column(max_exec_time)"`
CreatedTime time.Time `json:"created_time";orm:"column(created_time);type(datetime)"`
UpdatedTime time.Time `json:"updated_time";orm:"column(updated_time);type(datetime)"`
......@@ -147,6 +148,7 @@ type NewTaskType struct {
PublishStatus int `json:"publish_status,omitempty"`
Unit string `json:"unit";orm:"column(unit)"`
Sort int `json:"sort";orm:"column(sort)"`
StartUpTime int `json:"start_up_time";orm:"column(start_up_time)"`
EstimatExeTime int `json:"estimat_exe_time";orm:"column(estimat_exe_time)"`
MaxExecTime int `json:"max_exec_time";orm:"column(max_exec_time)"`
CreatedTime time.Time `json:"created_time";orm:"column(created_time);type(datetime)"`
......@@ -269,6 +271,8 @@ type TaskHeat struct {
OutPutJson interface{} `json:"out_put_json";orm:"column(out_put_json)"`
AccessStatus int `json:"access_status,omitempty"`
PublishStatus int `json:"publish_status,omitempty"`
EstimatExeTime int `json:"estimat_exe_time"`
StartUpTime int `json:"start_up_time"`
}
type AddTaskType struct {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment