1
0
mirror of https://github.com/newnius/YAO-scheduler.git synced 2025-06-07 22:31:55 +00:00
YAO-scheduler/src/util.go

109 lines
3.2 KiB
Go
Raw Normal View History

2019-03-04 09:19:55 +00:00
package main
import (
"strconv"
2019-03-20 03:14:07 +00:00
"math/rand"
"time"
"io"
"net/http"
2020-05-24 13:07:02 +00:00
)
2019-03-04 09:19:55 +00:00
2019-10-24 12:25:59 +00:00
type Configuration struct {
KafkaBrokers []string `json:"kafkaBrokers"`
KafkaTopic string `json:"kafkaTopic"`
SchedulerPolicy string `json:"schedulerPolicy"`
}
2019-03-20 03:14:07 +00:00
type Job struct {
2019-07-12 07:12:51 +00:00
ID int `json:"id"`
Name string `json:"name"`
Tasks []Task `json:"tasks"`
Workspace string `json:"workspace"`
2019-08-01 02:42:37 +00:00
Group string `json:"group"`
2019-07-12 07:12:51 +00:00
Priority JobPriority `json:"priority"`
RunBefore int `json:"run_before"`
CreatedAt int `json:"created_at"`
UpdatedAt int `json:"updated_at"`
CreatedBy int `json:"created_by"`
2020-05-02 14:35:31 +00:00
Locality int `json:"locality"`
2019-07-12 07:12:51 +00:00
Status State `json:"status"`
2019-03-20 03:14:07 +00:00
}
type Task struct {
Name string `json:"name"`
2019-04-12 09:21:09 +00:00
Image string `json:"image"`
2019-03-20 03:14:07 +00:00
Cmd string `json:"cmd"`
NumberCPU int `json:"cpu_number"`
Memory int `json:"memory"`
NumberGPU int `json:"gpu_number"`
MemoryGPU int `json:"gpu_memory"`
2020-04-30 04:13:21 +00:00
IsPS bool `json:"is_ps"`
2020-04-30 04:17:35 +00:00
ModelGPU string `json:"gpu_model"`
2019-03-04 09:19:55 +00:00
}
2020-04-30 15:06:12 +00:00
type UtilGPUTimeSeries struct {
Time int `json:"time"`
Util int `json:"util"`
}
2020-04-12 17:30:25 +00:00
type OptimizerJobExecutionTime struct {
Pre int `json:"pre"`
Post int `json:"post"`
Total int `json:"total"`
2020-04-30 05:08:08 +00:00
Main int `json:"main"`
2020-04-12 17:30:25 +00:00
Version int `json:"version"`
}
2020-04-30 08:45:43 +00:00
type OptimizerUtilGPU struct {
Util int `json:"util"`
Version int `json:"version"`
}
2020-05-24 13:07:02 +00:00
type ResourceCount struct {
NumberGPU int
MemoryGPU int
CPU int
Memory int
2020-05-03 15:32:38 +00:00
}
2019-03-04 09:19:55 +00:00
func str2int(str string, defaultValue int) int {
i, err := strconv.Atoi(str)
if err == nil {
return i
}
return defaultValue
}
2019-03-20 03:14:07 +00:00
func getUA() string {
rand.Seed(time.Now().Unix())
UAs := []string{
"Mozilla/5.0 (X11; Linux i686; rv:64.0) Gecko/20100101 Firefox/64.0",
"Mozilla/5.0 (Windows NT 6.1; WOW64; rv:64.0) Gecko/20100101 Firefox/64.0",
"Mozilla/5.0 (X11; Linux i586; rv:63.0) Gecko/20100101 Firefox/63.0",
"Mozilla/5.0 (Windows NT 6.2; WOW64; rv:63.0) Gecko/20100101 Firefox/63.0",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:10.0) Gecko/20100101 Firefox/62.0",
"Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.13; ko; rv:1.9.1b2) Gecko/20081201 Firefox/60.0",
"Mozilla/5.0 (Windows NT 6.1; WOW64; rv:54.0) Gecko/20100101 Firefox/58.0",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML like Gecko) Chrome/51.0.2704.79 Safari/537.36 Edge/14.14931",
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.111 Safari/537.36",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_2) AppleWebKit/601.3.9 (KHTML, like Gecko) Version/9.0.2 Safari/601.3.9",
}
return UAs[rand.Intn(len(UAs))]
}
func doRequest(method string, url string, r io.Reader, contentType string, referer string) (*http.Response, error) {
client := &http.Client{}
req, err := http.NewRequest(method, url, r)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", contentType)
req.Header.Set("User-Agent", getUA())
req.Header.Set("Referer", referer)
resp, err := client.Do(req)
return resp, err
}