Skip to content

Commit

Permalink
fielddiff, fulldiff working with a shared method getdiff. Added new s…
Browse files Browse the repository at this point in the history
…creenshot. Added len not equal check in the compareMaps
  • Loading branch information
djarotech committed Aug 31, 2018
1 parent a31cb70 commit 74ef1b2
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 22 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,10 +177,10 @@ kubectl.sh get --raw "/apis/kubeprovenance.cloudark.io/v1/namespaces/default/pos
kubectl.sh get --raw "/apis/kubeprovenance.cloudark.io/v1/namespaces/default/postgreses/client25/diff?start=1&end=2&field=databases"
```

5) Get diff of the field users for a Postgres custom resource instance between version 1 and version 2
5) Get diff of the field users for a Postgres custom resource instance between version 1 and version 3

```
kubectl.sh get --raw "/apis/kubeprovenance.cloudark.io/v1/namespaces/default/postgreses/client25/diff?start=1&end=2&field=users"
kubectl.sh get --raw "/apis/kubeprovenance.cloudark.io/v1/namespaces/default/postgreses/client25/diff?start=1&end=3&field=users"
```

6) Find out in which version the user 'pallavi' was given password 'pass123'
Expand Down Expand Up @@ -239,10 +239,10 @@ kubectl get --raw "/apis/kubeprovenance.cloudark.io/v1/namespaces/default/postgr
![alt text](https://github.com/cloud-ark/kubeprovenance/raw/master/docs/getfielddiff.png)


5) Get diff of the field users for a Postgres custom resource instance between version 1 and version 2
5) Get diff of the field users for a Postgres custom resource instance between version 1 and version 3

```
kubectl get --raw "/apis/kubeprovenance.cloudark.io/v1/namespaces/default/postgreses/client25/diff?start=1&end=2&field=users"
kubectl get --raw "/apis/kubeprovenance.cloudark.io/v1/namespaces/default/postgreses/client25/diff?start=1&end=3&field=users"
```

![alt text](https://github.com/cloud-ark/kubeprovenance/raw/master/docs/usersfielddiff.png)
Expand Down
Binary file modified docs/usersfielddiff.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
44 changes: 26 additions & 18 deletions pkg/provenance/provenance.go
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,7 @@ func handleCompositeFields(vSliceMap []map[string]string, mapRelationships map[s
}
return false
}
//Method that returns true if all the elements in boolSlice are True
func all(boolSlice []bool) bool{
allTrue := true
for _, b := range boolSlice{
Expand All @@ -416,7 +417,14 @@ func all(boolSlice []bool) bool{
}
return allTrue
}
//Method that compares the elements within 2 mapSlices.
//Each map must have a corresponding map
func compareMaps(mapSlice1, mapSlice2 []map[string]string) bool {
//little trick so that I loop through the bigger map slice,
if len(mapSlice2) != len(mapSlice1){
return false
}

foundMatches := make([]bool, 0)
for i := 0; i < len(mapSlice1); i++ {
mapleft := mapSlice1[i]
Expand Down Expand Up @@ -455,7 +463,7 @@ func (o ObjectLineage) FullDiff(vNumStart, vNumEnd int) string {
for attribute, data1 := range sp1.AttributeToData {
data2, ok := sp2.AttributeToData[attribute] //check if the attribute even exists
if ok {
getDiff(b, attribute, data1, data2, vNumStart, vNumEnd)
getDiff(&b, attribute, data1, data2, vNumStart, vNumEnd)
} else { //for the case where a key exists in spec 1 that doesn't exist in spec 2
fmt.Fprintf(&b, "Found diff on attribute %s:\n", attribute)
fmt.Fprintf(&b, "\tVersion %d: %s\n", vNumStart, data1)
Expand All @@ -472,20 +480,20 @@ func (o ObjectLineage) FullDiff(vNumStart, vNumEnd int) string {
}
return b.String()
}
func getDiff(b strings.Builder, fieldName string, data1, data2 interface{}, vNumStart, vNumEnd int) string{
func getDiff(b *strings.Builder, fieldName string, data1, data2 interface{}, vNumStart, vNumEnd int) string{
str1, ok1 := data1.(string)
str2, ok2 := data2.(string)
if ok1 && ok2 && str1 != str2 {
fmt.Fprintf(&b, "Found diff on attribute %s:\n", fieldName)
fmt.Fprintf(&b, "\tVersion %d: %s\n", vNumStart, data1)
fmt.Fprintf(&b, "\tVersion %d: %s\n", vNumEnd, data2)
fmt.Fprintf(b, "Found diff on attribute %s:\n", fieldName)
fmt.Fprintf(b, "\tVersion %d: %s\n", vNumStart, data1)
fmt.Fprintf(b, "\tVersion %d: %s\n", vNumEnd, data2)
}
int1, ok1 := data1.(int)
int2, ok2 := data2.(int)
if ok1 && ok2 && int1 != int2 {
fmt.Fprintf(&b, "Found diff on attribute %s:\n", fieldName)
fmt.Fprintf(&b, "\tVersion %d: %s\n", vNumStart, data1)
fmt.Fprintf(&b, "\tVersion %d: %s\n", vNumEnd, data2)
fmt.Fprintf(b, "Found diff on attribute %s:\n", fieldName)
fmt.Fprintf(b, "\tVersion %d: %s\n", vNumStart, data1)
fmt.Fprintf(b, "\tVersion %d: %s\n", vNumEnd, data2)
}
strArray1, ok1 := data1.([]string)
strArray2, ok2 := data2.([]string)
Expand All @@ -498,24 +506,24 @@ func getDiff(b strings.Builder, fieldName string, data1, data2 interface{}, vNum
}
}
if !found { // if an element does not have a match in the next version
fmt.Fprintf(&b, "Found diff on attribute %s:\n", fieldName)
fmt.Fprintf(&b, "\tVersion %d: %s\n", vNumStart, strArray1)
fmt.Fprintf(&b, "\tVersion %d: %s\n", vNumEnd, strArray2)
fmt.Fprintf(b, "Found diff on attribute %s:\n", fieldName)
fmt.Fprintf(b, "\tVersion %d: %s\n", vNumStart, strArray1)
fmt.Fprintf(b, "\tVersion %d: %s\n", vNumEnd, strArray2)
}
}
}
strMap1, ok1 := data1.([]map[string]string)
strMap2, ok2 := data2.([]map[string]string)
if ok1 && ok2 {
if len(strMap1) != len(strMap2) {
fmt.Fprintf(&b, "Found diff on attribute %s:\n", fieldName)
fmt.Fprintf(&b, "\tVersion %d: %s\n", vNumStart, strMap1)
fmt.Fprintf(&b, "\tVersion %d: %s\n", vNumEnd, strMap2)
fmt.Fprintf(b, "Found diff on attribute %s:\n", fieldName)
fmt.Fprintf(b, "\tVersion %d: %s\n", vNumStart, strMap1)
fmt.Fprintf(b, "\tVersion %d: %s\n", vNumEnd, strMap2)
}
if !compareMaps(strMap1,strMap2){
fmt.Fprintf(&b, "Found diff on attribute %s:\n", fieldName)
fmt.Fprintf(&b, "\tVersion %d: %s\n", vNumStart, strMap1)
fmt.Fprintf(&b, "\tVersion %d: %s\n", vNumEnd, strMap2)
fmt.Fprintf(b, "Found diff on attribute %s:\n", fieldName)
fmt.Fprintf(b, "\tVersion %d: %s\n", vNumStart, strMap1)
fmt.Fprintf(b, "\tVersion %d: %s\n", vNumEnd, strMap2)
}
}

Expand All @@ -528,7 +536,7 @@ func (o ObjectLineage) FieldDiff(fieldName string, vNumStart, vNumEnd int) strin
data2, ok2 := o[vNumEnd].AttributeToData[fieldName]
switch {
case ok1 && ok2:
return getDiff(b, fieldName,data1, data2, vNumStart, vNumEnd)
return getDiff(&b, fieldName, data1, data2, vNumStart, vNumEnd)
case !ok1 && ok2:
fmt.Fprintf(&b, "Found diff on attribute %s:\n", fieldName)
fmt.Fprintf(&b, "\tVersion %d: %s\n", vNumStart, "No attribute found.")
Expand Down

0 comments on commit 74ef1b2

Please sign in to comment.