|
- package scheduler
-
- import (
- "gitlink.org.cn/jcce-pcm/pcm-coordinator/api/internal/types"
- "gorm.io/gorm"
- "math/rand"
- "time"
- )
-
- type scheduleService interface {
- getNewStructForDb(task *types.TaskInfo, participantIds []int64) (interface{}, error)
- }
-
- func MatchLabels(dbEngin *gorm.DB, task *types.TaskInfo) ([]int64, error) {
- var ids []int64
- count := 0
- for key := range task.MatchLabels {
- var participantId []int64
- dbEngin.Raw("select participant_id from sc_participant_label_info where `key` = ? and value = ?", key, task.MatchLabels[key]).Scan(&participantId)
- if count == 0 {
- ids = participantId
- }
- if len(participantId) == 0 || len(ids) == 0 {
- return nil, nil
- }
- ids = intersect(ids, participantId)
- count++
- }
- return micsSlice(ids, 1), nil
- }
-
- // 求交集
- func intersect(slice1, slice2 []int64) []int64 {
- m := make(map[int64]int)
- nn := make([]int64, 0)
- for _, v := range slice1 {
- m[v]++
- }
-
- for _, v := range slice2 {
- times, _ := m[v]
- if times == 1 {
- nn = append(nn, v)
- }
- }
- return nn
- }
-
- func micsSlice(origin []int64, count int) []int64 {
- tmpOrigin := make([]int64, len(origin))
- copy(tmpOrigin, origin)
- //一定要seed
- rand.Seed(time.Now().Unix())
- rand.Shuffle(len(tmpOrigin), func(i int, j int) {
- tmpOrigin[i], tmpOrigin[j] = tmpOrigin[j], tmpOrigin[i]
- })
-
- result := make([]int64, 0, count)
- for index, value := range tmpOrigin {
- if index == count {
- break
- }
- result = append(result, value)
- }
- return result
- }
|