This repository has been archived by the owner on Oct 22, 2020. It is now read-only.
/
appcollection_handler.go
131 lines (107 loc) · 3.78 KB
/
appcollection_handler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
Copyright 2016 Pivotal
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 service
import (
"fmt"
"net/http"
"github.com/gorilla/mux"
"app-metrics-nozzle/usageevents"
"github.com/unrolled/render"
"strings"
"app-metrics-nozzle/domain"
"strconv"
)
func appAllHandler(formatter *render.Render) http.HandlerFunc {
return func(w http.ResponseWriter, req *http.Request) {
w.Header().Add("Access-Control-Allow-Origin", req.Header.Get("Origin"))
w.Header().Add("Access-Control-Allow-Methods", "GET")
formatter.JSON(w, http.StatusOK, usageevents.AppDetails)
}
}
func appOrgHandler(formatter *render.Render) http.HandlerFunc {
return func(w http.ResponseWriter, req *http.Request) {
w.Header().Add("Access-Control-Allow-Origin", req.Header.Get("Origin"))
w.Header().Add("Access-Control-Allow-Methods", "GET")
vars := mux.Vars(req)
org := vars["org"]
searchKey := fmt.Sprintf("%s/", org)
searchApps(searchKey, w, formatter)
}
}
func appSpaceHandler(formatter *render.Render) http.HandlerFunc {
return func(w http.ResponseWriter, req *http.Request) {
w.Header().Add("Access-Control-Allow-Origin", req.Header.Get("Origin"))
w.Header().Add("Access-Control-Allow-Methods", "GET")
vars := mux.Vars(req)
org := vars["org"]
space := vars["space"]
searchKey := fmt.Sprintf("%s/%s/", org, space)
searchApps(searchKey, w, formatter)
}
}
func searchApps(searchKey string, w http.ResponseWriter, formatter *render.Render) {
allAppDetails := usageevents.AppDetails
foundApps := make(map[string]domain.App)
for idx, appDetail := range allAppDetails {
if strings.HasPrefix(idx, searchKey) {
foundApps[idx] = appDetail
}
}
if 0 < len(foundApps) {
formatter.JSON(w, http.StatusOK, foundApps)
} else {
formatter.JSON(w, http.StatusNotFound, "No such app")
}
}
//New deep structure with all application details
func appInstanceHandler(formatter *render.Render) http.HandlerFunc {
return func(w http.ResponseWriter, req *http.Request) {
w.Header().Add("Access-Control-Allow-Origin", req.Header.Get("Origin"))
w.Header().Add("Access-Control-Allow-Methods", "GET")
vars := mux.Vars(req)
instanceIndex := vars["instance_id"]
app := vars["app"]
org := vars["org"]
space := vars["space"]
key := usageevents.GetMapKeyFromAppData(org, space, app)
stat, exists := usageevents.AppDetails[key]
if !exists {
formatter.JSON(w, http.StatusNotFound, "No such app")
}
instanceIdx, _ := strconv.ParseInt(instanceIndex, 10, 64)
if 0 <= instanceIdx && instanceIdx < int64(len(stat.Instances)) {
formatter.JSON(w, http.StatusOK, stat.Instances[instanceIdx])
} else {
formatter.JSON(w, http.StatusNotFound, "No such instance")
}
}
}
//New deep structure with all application details
func appHandler(formatter *render.Render) http.HandlerFunc {
return func(w http.ResponseWriter, req *http.Request) {
w.Header().Add("Access-Control-Allow-Origin", req.Header.Get("Origin"))
w.Header().Add("Access-Control-Allow-Methods", "GET")
vars := mux.Vars(req)
app := vars["app"]
org := vars["org"]
space := vars["space"]
key := usageevents.GetMapKeyFromAppData(org, space, app)
stat, exists := usageevents.AppDetails[key]
if exists {
//todo calc needed statistics before serving
formatter.JSON(w, http.StatusOK, stat)
} else {
formatter.JSON(w, http.StatusNotFound, "No such app")
}
}
}