-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create build-queue.yaml for group (#84)
- Loading branch information
Showing
6 changed files
with
339 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
/* | ||
* Copyright (C) 2011-2021 Red Hat, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package dataset | ||
|
||
import ( | ||
"fmt" | ||
|
||
"gopkg.in/yaml.v2" | ||
) | ||
|
||
type DepGraph struct { | ||
Vertices map[string]interface{} `json:"vertices"` | ||
Edges []Edge `json:"edges"` | ||
} | ||
type Edge struct { | ||
Source string | ||
Target string | ||
} | ||
|
||
type BuildQueue struct { | ||
Builds []Build | ||
} | ||
|
||
func (bq *BuildQueue) append(b Build) { | ||
bq.Builds = append(bq.Builds, b) | ||
} | ||
|
||
type Build struct { | ||
Id string | ||
Items []string `yaml:"build"` | ||
} | ||
|
||
func (b *Build) append(item string) { | ||
b.Items = append(b.Items, item) | ||
} | ||
|
||
func getBuildQueueAsYaml(edges []Edge) string { | ||
fmt.Printf("Make build queue, edges.len: %d\n", len(edges)) | ||
bQueue := getBuildQueue(edges) | ||
d, _ := yaml.Marshal(&bQueue.Builds) | ||
var s = string(d) | ||
fmt.Println(s) | ||
return s | ||
} | ||
|
||
func getBuildQueue(edges []Edge) BuildQueue { | ||
var bQueue BuildQueue | ||
level := 1 | ||
roots := make(map[string]bool) | ||
for ok := true; ok; ok = (len(edges) > 0) { | ||
keys, values := getKeysAndValues(edges) | ||
s := make(map[string]bool) | ||
//fmt.Printf("Keys: %s\n", strings.Join(keys, " ")) | ||
//fmt.Printf("level %d:\n", level) | ||
for index, v := range edges { | ||
if !contains(keys, v.Target) { | ||
s[v.Target] = true | ||
if isRoot(v.Source, values) { | ||
roots[v.Source] = true | ||
} | ||
edges[index].Target = "" // mark it | ||
} | ||
} | ||
bQueue.append(makeBuild(level, s)) | ||
edges = cleanUp(edges) | ||
//fmt.Printf("After clean up, len: %d\n", len(edges)) | ||
level += 1 | ||
} | ||
//fmt.Printf("level %d:\n", level) | ||
bQueue.append(makeBuild(level, roots)) | ||
return bQueue | ||
} | ||
|
||
func makeBuild(level int, m map[string]bool) Build { | ||
var rb Build | ||
rb.Id = "id" + fmt.Sprint(level) | ||
for k := range m { | ||
rb.append(k) | ||
} | ||
return rb | ||
} | ||
|
||
// A source is root if it is not in targets | ||
func isRoot(source string, targets []string) bool { | ||
return !contains(targets, source) | ||
} | ||
|
||
func contains(s []string, e string) bool { | ||
for _, a := range s { | ||
if a == e { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
func getKeysAndValues(edges []Edge) ([]string, []string) { | ||
var keys []string | ||
var values []string | ||
for _, v := range edges { | ||
keys = append(keys, v.Source) | ||
values = append(values, v.Target) | ||
} | ||
return keys, values | ||
} | ||
|
||
// Remove edges with empty target | ||
func cleanUp(edges []Edge) []Edge { | ||
var ret []Edge | ||
for _, v := range edges { | ||
if v.Target != "" { | ||
ret = append(ret, v) | ||
} | ||
} | ||
return ret | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,175 @@ | ||
/* | ||
* Copyright (C) 2011-2021 Red Hat, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package dataset | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func Test_getBuildQueue(t *testing.T) { | ||
type args struct { | ||
edges []Edge | ||
} | ||
|
||
tests := []struct { | ||
name string | ||
args args | ||
want string | ||
}{ | ||
// TODO: Add test cases. | ||
{name: "test", args: args{edges}, want: "ok"}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got := getBuildQueue(tt.args.edges) | ||
size := len(got.Builds) | ||
if len(got.Builds) != 5 { | ||
t.Errorf("getBuildQueue(), size: %d, want: %d", size, 5) | ||
} | ||
var total int | ||
for _, b := range got.Builds { | ||
total += len(b.Items) | ||
} | ||
if total != 23 { | ||
t.Errorf("getBuildQueue(), total: %d, want: %d", total, 23) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
var edges = []Edge{ | ||
{ | ||
Source: "90440", | ||
Target: "90447", | ||
}, | ||
{ | ||
Source: "90439", | ||
Target: "90446", | ||
}, | ||
{ | ||
Source: "90440", | ||
Target: "90439", | ||
}, | ||
{ | ||
Source: "90450", | ||
Target: "90440", | ||
}, | ||
{ | ||
Source: "90452", | ||
Target: "90446", | ||
}, | ||
{ | ||
Source: "90451", | ||
Target: "90452", | ||
}, | ||
{ | ||
Source: "90455", | ||
Target: "90451", | ||
}, | ||
{ | ||
Source: "90444", | ||
Target: "90446", | ||
}, | ||
{ | ||
Source: "90441", | ||
Target: "90444", | ||
}, | ||
{ | ||
Source: "90441", | ||
Target: "90439", | ||
}, | ||
{ | ||
Source: "90453", | ||
Target: "90441", | ||
}, | ||
{ | ||
Source: "90434", | ||
Target: "90444", | ||
}, | ||
{ | ||
Source: "90453", | ||
Target: "90434", | ||
}, | ||
{ | ||
Source: "90453", | ||
Target: "90439", | ||
}, | ||
{ | ||
Source: "90450", | ||
Target: "90453", | ||
}, | ||
{ | ||
Source: "90445", | ||
Target: "90448", | ||
}, | ||
{ | ||
Source: "90450", | ||
Target: "90445", | ||
}, | ||
{ | ||
Source: "90442", | ||
Target: "90443", | ||
}, | ||
{ | ||
Source: "90442", | ||
Target: "90437", | ||
}, | ||
{ | ||
Source: "90436", | ||
Target: "90442", | ||
}, | ||
{ | ||
Source: "90436", | ||
Target: "90452", | ||
}, | ||
{ | ||
Source: "90436", | ||
Target: "90438", | ||
}, | ||
{ | ||
Source: "90438", | ||
Target: "90452", | ||
}, | ||
{ | ||
Source: "90454", | ||
Target: "90433", | ||
}, | ||
{ | ||
Source: "90455", | ||
Target: "90439", | ||
}, | ||
{ | ||
Source: "90435", | ||
Target: "90446", | ||
}, | ||
{ | ||
Source: "90454", | ||
Target: "90435", | ||
}, | ||
{ | ||
Source: "90454", | ||
Target: "90439", | ||
}, | ||
{ | ||
Source: "90436", | ||
Target: "90439", | ||
}, | ||
{ | ||
Source: "90436", | ||
Target: "90449", | ||
}, | ||
} |
Oops, something went wrong.