/
draw.go
172 lines (150 loc) · 4.88 KB
/
draw.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package chring
import (
"context"
"fmt"
"image"
"image/png"
_ "image/png"
"io/ioutil"
"log"
"math"
"net/http"
"net/url"
"os"
"path/filepath"
"strconv"
"strings"
"github.com/llgcode/draw2d"
"github.com/llgcode/draw2d/draw2dimg"
"github.com/sethgrid/chring/simpledraw"
)
func (r *Ring) drawChart(w http.ResponseWriter, req *http.Request) {
m, err := url.ParseQuery(req.URL.RawQuery)
if err != nil {
log.Println(err)
}
dest := image.NewRGBA(image.Rect(0, 0, 500, 375))
fontFolder := FindInGOPATH(filepath.Join("resources/font"))
draw2d.SetFontFolder(fontFolder)
gc := simpledraw.Draw{draw2dimg.NewGraphicContext(dest)}
ring := simpledraw.NewCircle(340, 175, 150)
// TODO parse the legend first to determine the needed canvas size, then draw all the things
legend := &simpledraw.Legend{}
legend.Title = "Consistent Hash Ring"
legend.Caption = "Distribution Visualization"
legend.Elements = make([]simpledraw.LegendElement, 0)
gc.DrawCircle(ring)
// draw the elements in order you want them z-stacked visually, later elements will be on top
// ring manager invokes the drawChart method and passes in keys via context
ctx := req.Context()
keys, ok := ctx.Value("keys").([]string)
if ok {
for i, param := range keys {
square := 4
props := simpledraw.DefaultBasicProperties
props.Color = simpledraw.Pallate[(i+3)%len(simpledraw.Pallate)]
gc.DrawOnEdge(ring, hashAngle(r.Hasher(param)), square, 4, props)
legend.AppendElement(square, param, props)
}
}
for i, param := range m["key[]"] {
square := 4
props := simpledraw.DefaultBasicProperties
props.Color = simpledraw.Pallate[(i+3)%len(simpledraw.Pallate)]
gc.DrawOnEdge(ring, hashAngle(r.Hasher(param)), square, 4, props)
legend.AppendElement(square, param, props)
}
for i, n := range r.Nodes {
circle := 0
props := simpledraw.DefaultBasicProperties
props.Color = simpledraw.Pallate[i%len(simpledraw.Pallate)]
gc.DrawOnEdge(ring, hashAngle(n.HashID), circle, 12, props)
legend.PrependElement(circle, n.ID, props)
}
for i, param := range m["hashid[]"] {
triangle := 3
hashID, _ := strconv.Atoi(param)
props := simpledraw.DefaultBasicProperties
props.Color = simpledraw.Pallate[(i+5)%len(simpledraw.Pallate)]
gc.DrawOnEdge(ring, hashAngle(uint32(hashID)), triangle, 10, props)
hashStr := fmt.Sprintf("hash #%d", hashID)
legend.AppendElement(triangle, hashStr, props)
}
gc.DrawLegend(legend)
w.Header().Set("Content-Type", "image/png")
err = png.Encode(w, dest) //Encode writes the Image m to w in PNG format.
if err != nil {
fmt.Printf("Error rendering pie chart: %v\n", err)
}
}
func htmlHandler(htmlFile string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
path := FindInGOPATH(filepath.Join("resources", htmlFile))
html, err := ioutil.ReadFile(path)
if err != nil {
log.Println(err)
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("unable to serve " + htmlFile))
return
}
w.Write(html)
}
}
// ServeRing presents a web view into your consistent hash ring
func ServeRing(r *Ring, addr string) {
http.HandleFunc("/ring.png", r.drawChart)
http.HandleFunc("/", htmlHandler("ring.html"))
log.Fatal(http.ListenAndServe(addr, nil))
}
// ServeRingManager presents a web view into your consistent hash ring manager
func ServeRingManager(rm *RingManager, addr string) {
http.HandleFunc("/ring.png", addKeysToCtx(rm, rm.nodeRing.drawChart))
http.HandleFunc("/", htmlHandler("ringmanager.html"))
log.Fatal(http.ListenAndServe(addr, nil))
}
// addKeysToCtx
func addKeysToCtx(rm *RingManager, next http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
nodeList := rm.GetNodes()
ctx := r.Context()
var keys []string
for _, n := range rm.dataRing.Nodes {
if inList(n.ID, nodeList) {
continue
}
keys = append(keys, n.ID)
}
ctx = context.WithValue(ctx, "keys", keys)
req := r.WithContext(ctx)
next.ServeHTTP(w, req)
}
}
// inList is a simple helper to determine if a string slice contains a given string
func inList(s string, list []string) bool {
for _, e := range list {
if s == e {
return true
}
}
return false
}
// hashAngle is a helper to find the angle in radians of the hashID in uint32 space
func hashAngle(hashID uint32) float64 {
return 2 * math.Pi * float64(hashID) / float64(math.MaxUint32)
}
// FindInGOPATH searches through all GOPATHS and attempts to find the given file
// this is useful here because we want to find chring files but we can't know the relative import path
// as the importer could be a subpackage
func FindInGOPATH(filename string) string {
gopath := os.Getenv("GOPATH")
paths := strings.Split(gopath, ":")
for _, path := range paths {
seek := filepath.Join(path, "src", "github.com", "sethgrid", "chring", filename)
_, err := os.Stat(seek)
if err == nil {
return seek
}
}
// not found in any GOPATH, just return what came in
return filename
}