~humaid/ns2-trace-go

256ef909c16b52a88da19fc5582f1d84758d4dd9 — Humaid AlQassimi 1 year, 9 months ago 3f45f98
Allow users to download jitter data as json
2 files changed, 116 insertions(+), 72 deletions(-)

M main.go
M templates/jitter.tmpl
M main.go => main.go +113 -70
@@ 90,89 90,132 @@ func main() {
			ctx.Data["Analysis"] = analysis
			ctx.HTML(200, "analysis")
		})
		m.Get("/:from/:to/:type/:zoom", func(ctx *macaron.Context) {
			analysis, ok := Analysises[ctx.Params("id")]
			if !ok {
				ctx.PlainText(404, []byte("Analysis results does not exist"))
				return
			}
			var from, to int
			var pType string = ctx.Params("type")
			var err error
		m.Group("/:from/:to/:type", func() {
			m.Get("/data.json", func(ctx *macaron.Context) {
				analysis, ok := Analysises[ctx.Params("id")]
				if !ok {
					ctx.PlainText(404, []byte("Analysis results does not exist"))
					return
				}
				var from, to int
				var pType string = ctx.Params("type")
				var err error

				from, err = strconv.Atoi(ctx.Params("from"))
				if err != nil {
					ctx.PlainText(400, []byte("Malformed request parameters"))
					return
				}
				to, err = strconv.Atoi(ctx.Params("to"))
				if err != nil {
					ctx.PlainText(400, []byte("Malformed request parameters"))
					return
				}

			from, err = strconv.Atoi(ctx.Params("from"))
			if err != nil {
				ctx.PlainText(400, []byte("Malformed request parameters"))
				return
			}
			to, err = strconv.Atoi(ctx.Params("to"))
			if err != nil {
				ctx.PlainText(400, []byte("Malformed request parameters"))
				return
			}
				var st *JitterStat = nil
				for _, stat := range analysis.JitterStats {
					if stat.FromNode == from && stat.ToNode == to && stat.PacketType == pType {
						st = stat
					}
				}
				if st == nil {
					ctx.PlainText(404, []byte("The jitter stat requested does not exist"))
					return
				}
				var seqValues, jitterValues []float64

			var st *JitterStat = nil
			for _, stat := range analysis.JitterStats {
				if stat.FromNode == from && stat.ToNode == to && stat.PacketType == pType {
					st = stat
				for seq := range st.Jitter {
					seqValues = append(seqValues, float64(seq))
				}
			}
			if st == nil {
				ctx.PlainText(404, []byte("The jitter stat requested does not exist"))
				return
			}
				sort.Float64s(seqValues)

			ctx.Data["ID"] = ctx.Params("id")
			ctx.Data["Stat"] = st
				for v := range seqValues {
					jitterValues = append(jitterValues, st.Jitter[v])
				}
				ctx.JSON(200, [][]float64{seqValues, jitterValues})
			})
			m.Get("/:zoom", func(ctx *macaron.Context) {
				analysis, ok := Analysises[ctx.Params("id")]
				if !ok {
					ctx.PlainText(404, []byte("Analysis results does not exist"))
					return
				}
				var from, to int
				var pType string = ctx.Params("type")
				var err error

				from, err = strconv.Atoi(ctx.Params("from"))
				if err != nil {
					ctx.PlainText(400, []byte("Malformed request parameters"))
					return
				}
				to, err = strconv.Atoi(ctx.Params("to"))
				if err != nil {
					ctx.PlainText(400, []byte("Malformed request parameters"))
					return
				}

			var seqValues, jitterValues []float64
				var st *JitterStat = nil
				for _, stat := range analysis.JitterStats {
					if stat.FromNode == from && stat.ToNode == to && stat.PacketType == pType {
						st = stat
					}
				}
				if st == nil {
					ctx.PlainText(404, []byte("The jitter stat requested does not exist"))
					return
				}

			for seq := range st.Jitter {
				seqValues = append(seqValues, float64(seq))
			}
			sort.Float64s(seqValues)
				ctx.Data["ID"] = ctx.Params("id")
				ctx.Data["Stat"] = st

			for v := range seqValues {
				jitterValues = append(jitterValues, st.Jitter[v])
				//fmt.Printf("(%d - %f) ", v, st.Jitter[v])
			}
			//fmt.Println(jitterValues)

			graph := chart.Chart{
				XAxis: chart.XAxis{
					Name: "Sequence",
				},
				YAxis: chart.YAxis{
					Name: "Jitter",
				},
				Series: []chart.Series{
					chart.ContinuousSeries{
						Style: chart.Style{
							StrokeColor: drawing.ColorRed,
				var seqValues, jitterValues []float64

				for seq := range st.Jitter {
					seqValues = append(seqValues, float64(seq))
				}
				sort.Float64s(seqValues)

				for v := range seqValues {
					jitterValues = append(jitterValues, st.Jitter[v])
				}

				graph := chart.Chart{
					XAxis: chart.XAxis{
						Name: "Sequence",
					},
					YAxis: chart.YAxis{
						Name: "Jitter",
					},
					Series: []chart.Series{
						chart.ContinuousSeries{
							Style: chart.Style{
								StrokeColor: drawing.ColorRed,
							},
							XValues: seqValues,
							YValues: jitterValues,
						},
						XValues: seqValues,
						YValues: jitterValues,
					},
				},
			}
			if ctx.Params("zoom") == "zoom" {
				graph.YAxis.Range = &chart.ContinuousRange{
					Min: -0.5,
					Max: 0.5,
				}
				ctx.Data["IsZoom"] = true
			}
				if ctx.Params("zoom") == "zoom" {
					graph.YAxis.Range = &chart.ContinuousRange{
						Min: -0.5,
						Max: 0.5,
					}
					ctx.Data["IsZoom"] = true
				}

			var imgBuf bytes.Buffer
			err = graph.Render(chart.SVG, &imgBuf)
			if err != nil {
				ctx.PlainText(500, []byte(fmt.Sprintf("Failed to generate jitter graph: %s", err)))
				return
			}
				var imgBuf bytes.Buffer
				err = graph.Render(chart.SVG, &imgBuf)
				if err != nil {
					ctx.PlainText(500, []byte(fmt.Sprintf("Failed to generate jitter graph: %s", err)))
					return
				}

			ctx.Data["Graph"] = template.HTML(imgBuf.String())
				ctx.Data["Graph"] = template.HTML(imgBuf.String())

			ctx.HTML(200, "jitter")
				ctx.HTML(200, "jitter")
			})
		})
	})
	log.Println("Running on 0.0.0.0:" + port)

M templates/jitter.tmpl => templates/jitter.tmpl +3 -2
@@ 8,9 8,10 @@
	<br>
	{{ if .IsZoom}}
	<p><i>This is a zoomed-in view of the graph. <a href="full">Switch to full
	view</a>.</i></p>{{else}}
	view</a>.{{else}}
	<p><i>This is a full view of the graph. <a href="zoom">Switch to zoomed
	view</a>.</i></p>{{end}}
	view</a>.{{end}}
	Or <a href="data.json">download jitter data as a JSON</a>.</p>
	<br>
	<br>