Skip to content

Commit 383a9c0

Browse files
ref(ps): use array instead of map for sorted types (#91)
1 parent 72235ec commit 383a9c0

2 files changed

Lines changed: 86 additions & 10 deletions

File tree

ps/ps.go

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@ package ps
44
import (
55
"encoding/json"
66
"fmt"
7+
"sort"
78

89
deis "github.com/deis/controller-sdk-go"
910
"github.com/deis/controller-sdk-go/api"
1011
)
1112

1213
// List lists an app's processes.
13-
func List(c *deis.Client, appID string, results int) ([]api.Pods, int, error) {
14+
func List(c *deis.Client, appID string, results int) (api.PodsList, int, error) {
1415
u := fmt.Sprintf("/v2/apps/%s/pods/", appID)
1516
body, count, reqErr := c.LimitedRequest(u, results)
1617
if reqErr != nil && !deis.IsErrAPIMismatch(reqErr) {
@@ -46,7 +47,7 @@ func Scale(c *deis.Client, appID string, targets map[string]int) error {
4647
// Restart restarts an app's processes. To restart all app processes, pass empty strings for
4748
// procType and name. To restart an specific process, pass an procType by leave name empty.
4849
// To restart a specific instance, pass a procType and a name.
49-
func Restart(c *deis.Client, appID string, procType string, name string) ([]api.Pods, error) {
50+
func Restart(c *deis.Client, appID string, procType string, name string) (api.PodsList, error) {
5051
u := fmt.Sprintf("/v2/apps/%s/pods/", appID)
5152

5253
if procType == "" {
@@ -74,13 +75,36 @@ func Restart(c *deis.Client, appID string, procType string, name string) ([]api.
7475
}
7576

7677
// ByType organizes processes of an app by process type.
77-
// The key will be the process name, and the array will be all the pods of that type.
78-
func ByType(processes []api.Pods) map[string][]api.Pods {
79-
psMap := make(map[string][]api.Pods)
78+
func ByType(processes api.PodsList) api.PodTypes {
79+
var pts api.PodTypes
80+
81+
for _, process := range processes {
82+
exists := false
83+
// Is processtype for process already exists, append to it.
84+
for i, pt := range pts {
85+
if pt.Type == process.Type {
86+
exists = true
87+
pts[i].PodsList = append(pts[i].PodsList, process)
88+
break
89+
}
90+
}
8091

81-
for _, ps := range processes {
82-
psMap[ps.Type] = append(psMap[ps.Type], ps)
92+
// Is processtype for process doesn't exist, create a new one
93+
if !exists {
94+
pts = append(pts, api.PodType{
95+
Type: process.Type,
96+
PodsList: api.PodsList{process},
97+
})
98+
}
8399
}
84100

85-
return psMap
101+
// Sort the pods alphabetically by name.
102+
for _, pt := range pts {
103+
sort.Sort(pt.PodsList)
104+
}
105+
106+
// Sort ProcessTypes alphabetically by process name
107+
sort.Sort(pts)
108+
109+
return pts
86110
}

ps/ps_test.go

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ func TestProcessesList(t *testing.T) {
125125

126126
started := time.Time{}
127127
started.UnmarshalText([]byte("2016-02-13T00:47:52"))
128-
expected := []api.Pods{
128+
expected := api.PodsList{
129129
{
130130
Release: "v2",
131131
Type: "web",
@@ -158,7 +158,7 @@ func TestProcessesList(t *testing.T) {
158158
type testExpected struct {
159159
Name string
160160
Type string
161-
Expected []api.Pods
161+
Expected api.PodsList
162162
}
163163

164164
func TestAppsRestart(t *testing.T) {
@@ -259,3 +259,55 @@ func TestScale(t *testing.T) {
259259
t.Fatal(err)
260260
}
261261
}
262+
263+
func TestByType(t *testing.T) {
264+
t.Parallel()
265+
266+
started := time.Time{}
267+
started.UnmarshalText([]byte("2016-02-13T00:47:52"))
268+
269+
expected := api.PodTypes{
270+
{
271+
Type: "abc",
272+
PodsList: api.PodsList{
273+
{Type: "abc", Name: "1", Started: started},
274+
{Type: "abc", Name: "2", Started: started},
275+
{Type: "abc", Name: "3", Started: started},
276+
},
277+
},
278+
{
279+
Type: "web",
280+
PodsList: api.PodsList{
281+
{Type: "web", Name: "test1", Started: started},
282+
{Type: "web", Name: "test2", Started: started},
283+
{Type: "web", Name: "test3", Started: started},
284+
},
285+
},
286+
{
287+
Type: "worker",
288+
PodsList: api.PodsList{
289+
{Type: "worker", Name: "a", Started: started},
290+
{Type: "worker", Name: "b", Started: started},
291+
{Type: "worker", Name: "c", Started: started},
292+
},
293+
},
294+
}
295+
296+
input := api.PodsList{
297+
{Type: "worker", Name: "c", Started: started},
298+
{Type: "abc", Name: "2", Started: started},
299+
{Type: "worker", Name: "b", Started: started},
300+
{Type: "web", Name: "test1", Started: started},
301+
{Type: "web", Name: "test3", Started: started},
302+
{Type: "abc", Name: "1", Started: started},
303+
{Type: "worker", Name: "a", Started: started},
304+
{Type: "abc", Name: "3", Started: started},
305+
{Type: "web", Name: "test2", Started: started},
306+
}
307+
308+
actual := ByType(input)
309+
310+
if !reflect.DeepEqual(actual, expected) {
311+
t.Errorf("Expected: %v, Got %v", expected, actual)
312+
}
313+
}

0 commit comments

Comments
 (0)