Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
dedc0b4
backend: Sortby slice to string
wilhelmguo Nov 29, 2018
5fb93f0
backend: base api pageSize and pageNo must bigger than zero
wilhelmguo Nov 29, 2018
50ca792
backend: add kubernetes resource dataselector for backend paging
wilhelmguo Nov 29, 2018
5c26fcd
backend: add kubernetes deployment list
wilhelmguo Nov 29, 2018
2d9c02b
backend: abstraction base controller
wilhelmguo Nov 30, 2018
2f75143
Merge branch 'master' of github.com:Qihoo360/wayne into feature/add_k…
wilhelmguo Nov 30, 2018
7277e57
backend: remove deployment debug logs
wilhelmguo Nov 30, 2018
8da44cd
Merge branch 'master' of github.com:Qihoo360/wayne into feature/add_k…
wilhelmguo Nov 30, 2018
e9176a7
backend: update auditlog model Action size 255 to 256
wilhelmguo Dec 5, 2018
f33fe16
backend: update kubernetes deployment cluster & namespace param to path
wilhelmguo Dec 5, 2018
451ebe4
backend: add get kube deployment api
wilhelmguo Dec 5, 2018
d601685
backend: get kubernetes list return DeploymentList Object
wilhelmguo Dec 5, 2018
03cb40e
frontend: move tpl-detail to shared
wilhelmguo Dec 5, 2018
699621f
frontend: add kubernetes deployment management
wilhelmguo Dec 5, 2018
c847355
merge master
wilhelmguo Dec 5, 2018
a6db772
backend: update kubernetes deployment createOrUpdate strategy
wilhelmguo Dec 5, 2018
de1bfa4
frontend: update deployment tpl generate rules
wilhelmguo Dec 5, 2018
42e4b03
frontend: move tpl-detail to shared
wilhelmguo Dec 5, 2018
4fe613b
backend: deal with Resources already exist error.
wilhelmguo Dec 6, 2018
319f604
frontend: add kubernetes deployment migration
wilhelmguo Dec 6, 2018
b84aed2
frontend: update lib version
wilhelmguo Dec 6, 2018
95b5bb9
frontend: migration deployment showcolumns key to lower case
wilhelmguo Dec 6, 2018
453de6b
Merge branch 'master' of github.com:Qihoo360/wayne into feature/add_k…
wilhelmguo Dec 6, 2018
de6e641
Merge branch 'master' into feature/add_kubernetes_deployment_migration
Dec 6, 2018
42dedf4
backend: remove unused newline
wilhelmguo Dec 6, 2018
07c8c93
Merge branch 'feature/add_kubernetes_deployment_migration' of https:/…
wilhelmguo Dec 6, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
backend: add kubernetes resource dataselector for backend paging
  • Loading branch information
wilhelmguo committed Nov 29, 2018
commit 50ca792d9aea650825f1f7ae83c21b59da571b63
61 changes: 61 additions & 0 deletions src/backend/resources/dataselector/comparabletypes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package dataselector

import (
"strings"
"time"
)

type StdComparableInt int

func (self StdComparableInt) Compare(otherV ComparableValue) int {
other := otherV.(StdComparableInt)
return intsCompare(int(self), int(other))
}

func (self StdComparableInt) Contains(otherV ComparableValue) bool {
return self.Compare(otherV) == 0
}

type StdComparableString string

func (self StdComparableString) Compare(otherV ComparableValue) int {
other := otherV.(StdComparableString)
return strings.Compare(string(self), string(other))
}

func (self StdComparableString) Contains(otherV ComparableValue) bool {
other := otherV.(StdComparableString)
return strings.Contains(string(self), string(other))
}

type StdComparableTime time.Time

func (self StdComparableTime) Compare(otherV ComparableValue) int {
other := otherV.(StdComparableTime)
return ints64Compare(time.Time(self).Unix(), time.Time(other).Unix())
}

func (self StdComparableTime) Contains(otherV ComparableValue) bool {
return self.Compare(otherV) == 0
}

// Compares self with other value. Returns 1 if other value is smaller,
// 0 if they are the same, -1 if other is larger.
func ints64Compare(a, b int64) int {
if a > b {
return 1
} else if a == b {
return 0
}
return -1
}

// Int comparison functions. Similar to strings.Compare.
func intsCompare(a, b int) int {
if a > b {
return 1
} else if a == b {
return 0
}
return -1
}
13 changes: 13 additions & 0 deletions src/backend/resources/dataselector/const.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package dataselector

// PropertyName is used to get the value of certain property of data cell.
// For example if we want to get the namespace of certain Deployment we can use DeploymentCell.GetProperty(NamespaceProperty)
type PropertyName string

// List of all property names supported by the UI.
const (
NameProperty PropertyName = "name"
CreationTimestampProperty PropertyName = "creationTimestamp"
NamespaceProperty PropertyName = "namespace"
StatusProperty PropertyName = "status"
)
123 changes: 123 additions & 0 deletions src/backend/resources/dataselector/dataselector.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package dataselector

import (
"sort"
"strings"
"time"

"github.com/Qihoo360/wayne/src/backend/common"
"github.com/Qihoo360/wayne/src/backend/models"
)

// GenericDataCell describes the interface of the data cell that contains all the necessary methods needed to perform
// complex data selection
// GenericDataSelect takes a list of these interfaces and performs selection operation.
// Therefore as long as the list is composed of GenericDataCells you can perform any data selection!
type DataCell interface {
// GetPropertyAtIndex returns the property of this data cell.
// Value returned has to have Compare method which is required by Sort functionality of DataSelect.
GetProperty(PropertyName) ComparableValue
}

// ComparableValue hold any value that can be compared to its own kind.
type ComparableValue interface {
// Compares self with other value. Returns 1 if other value is smaller, 0 if they are the same, -1 if other is larger.
Compare(ComparableValue) int
// Returns true if self value contains or is equal to other value, false otherwise.
Contains(ComparableValue) bool
}

// SelectableData contains all the required data to perform data selection.
// It implements sort.Interface so its sortable under sort.Sort
// You can use its Select method to get selected GenericDataCell list.
type DataSelector struct {
// GenericDataList hold generic data cells that are being selected.
GenericDataList []DataCell
// DataSelectQuery holds instructions for data select.
DataSelectQuery *common.QueryParam
}

// Implementation of sort.Interface so that we can use built-in sort function (sort.Sort) for sorting SelectableData

// Len returns the length of data inside SelectableData.
func (self DataSelector) Len() int { return len(self.GenericDataList) }

// Swap swaps 2 indices inside SelectableData.
func (self DataSelector) Swap(i, j int) {
self.GenericDataList[i], self.GenericDataList[j] = self.GenericDataList[j], self.GenericDataList[i]
}

// Less compares 2 indices inside SelectableData and returns true if first index is larger.
func (self DataSelector) Less(i, j int) bool {
sort := self.DataSelectQuery.Sortby
if sort != "" {
asc := true
if strings.Index(sort, "-") == 0 {
asc = false
sort = sort[1:]
}
a := self.GenericDataList[i].GetProperty(PropertyName(sort))
b := self.GenericDataList[j].GetProperty(PropertyName(sort))
// ignore sort completely if property name not found
if a == nil || b == nil {
return false
}
cmp := a.Compare(b)
if cmp == 0 { // values are the same. Just return
return false
} else { // values different
return (cmp == -1 && asc) || (cmp == 1 && !asc)
}
}
return false
}

// Sort sorts the data inside as instructed by DataSelectQuery and returns itself to allow method chaining.
func (self *DataSelector) Sort() *DataSelector {
sort.Sort(*self)
return self
}

// Filter the data inside as instructed by DataSelectQuery and returns itself to allow method chaining.
func (self *DataSelector) Filter() *DataSelector {
filteredList := []DataCell{}

for _, c := range self.GenericDataList {
matches := true
for key, value := range self.DataSelectQuery.Query {
// TODO now string default use contains condition, may be support labels and other types?
if strings.Contains(key, models.ListFilterExprSep) {
keySplit := strings.Split(key, models.ListFilterExprSep)
key = keySplit[0]
}
v := c.GetProperty(PropertyName(key))
if v == nil {
matches = false
continue
}
if !v.Contains(ParseToComparableValue(value)) {
matches = false
continue
}
}
if matches {
filteredList = append(filteredList, c)
}
}

self.GenericDataList = filteredList
return self
}

func ParseToComparableValue(value interface{}) ComparableValue {
switch value.(type) {
case string:
return StdComparableString(value.(string))
case int:
return StdComparableInt(value.(int))
case time.Time:
return StdComparableTime(value.(time.Time))
default:
return nil
}
}
32 changes: 32 additions & 0 deletions src/backend/resources/dataselector/page.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package dataselector

import (
"github.com/Qihoo360/wayne/src/backend/common"
"github.com/Qihoo360/wayne/src/backend/util/logs"
)

// GenericDataSelect takes a list of GenericDataCells and DataSelectQuery and returns selected data as instructed by dsQuery.
func DataSelectPage(dataList []DataCell, q *common.QueryParam) *common.Page {
SelectableData := DataSelector{
GenericDataList: dataList,
DataSelectQuery: q,
}
// Pipeline is Filter -> Sort -> Paginate
filtered := SelectableData.Filter().Sort()
logs.Error(len(filtered.GenericDataList), q.Offset(), q.Limit())
filteredTotal := len(filtered.GenericDataList)

// slice start and end point
start := q.Offset()
end := start + q.Limit()
if start > int64(filteredTotal) {
start = int64(filteredTotal)
}
if end > int64(filteredTotal) {
end = int64(filteredTotal)
}

pagedList := filtered.GenericDataList[start:end]

return q.NewPage(int64(filteredTotal), pagedList)
}