Exemple #1
0
func ApiHandler(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-Type", "text/json")
	w.Header().Set("Access-Control-Allow-Origin", "*")

	vars := mux.Vars(r)

	neighbors_lock.RLock()
	defer neighbors_lock.RUnlock()

	type ipasnResult struct {
		ASN    ASN
		ASPath ASPath `json:",omitempty"`
		Name   string `json:",omitempty"`
		Prefix string `json:",omitempty"`
	}

	switch vars["method"] {
	case "ipasn":
		_ = r.ParseForm()
		ip := net.ParseIP(r.Form.Get("ip"))

		if ip == nil {
			w.WriteHeader(400)
			fmt.Fprintln(w, "Bad IP address")
			return
		}

		result := make(map[string]*ipasnResult)

		for neighbor, data := range neighbors {
			data.lock.RLock()
			defer data.lock.RUnlock()

			node := data.FindNode(&ip)
			if node.Bits() > 0 {
				result[neighbor] = new(ipasnResult)
				route := node.Value.(*Route)
				r := result[neighbor]
				r.ASN = ASN(route.PrimaryASN)
				r.ASPath = route.ASPath
				r.Name = ""
				r.Prefix = nodeToIPNet(node).String()
			}
		}

		json, err := json.Marshal(map[string]interface{}{"result": result})
		if err != nil {
			w.WriteHeader(500)
			log.Println("Error generating json", err)
			fmt.Fprintln(w, "Could not generate JSON")
		}

		fmt.Fprint(w, string(json), "\n")

	default:
		w.WriteHeader(404)
		return
	}

}
Exemple #2
0
// serveTop prints a list of the top repository for each language.
func (h *Handler) serveTop(w http.ResponseWriter, r *http.Request) {
	// Retrieve the top repositories.
	m, err := h.Store.TopRepositories()
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	// Sort keys.
	keys := make([]string, 0, len(m))
	for k := range m {
		keys = append(keys, k)
	}
	sort.Strings(keys)

	w.Header().Set("content-type", "text/plain")

	// Print results.
	for _, k := range keys {
		r := m[k]
		fmt.Fprintf(w, "%s: %s - %s\n", k, r.Name(), r.Description)
	}
}
func (loc Locntrl) CreateLoc(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
	var re Request
	var r Response

	json.NewDecoder(r.Body).Decode(&re)
	coord := getLoc(re.Address + "+" + re.City + "+" + re.State + "+" + re.Zip)
	fmt.Println("Lat:", coord.Coordinate.Lat, "Long:", coord.Coordinate.Lang)

	r.Id = bson.NewObjectId()
	r.Add = re.Add
	r.City = re.City
	r.Name = re.Name
	r.Zip = re.Zip
	r.State = re.State
	r.Coordinate.Lang = coord.Coordinate.Lang
	r.Coordinate.Lat = coord.Coordinate.Lat
	loc.se.DB("jaspalgill").C("location").Insert(r)
	result, _ := json.Marshal(r)
	w.Header().Set("Content-Type", "application/json")
	w.WriteHeader(201)
	fmt.Fprintf(w, "%s", result)
}
Exemple #4
0
func Index(w http.ResponseWriter, r *http.Request) {
	keys := make([]string, modulus)
	for i := range keys {
		keys[i] = fmt.Sprintf(keyPart, i*distance)
	}

	c := context(r)
	items, err := memcache.GetMulti(c, keys)
	if err != nil {
		return
	}

	ars := AllRequestStats{}
	for _, v := range items {
		t := stats_part{}
		err := gob.NewDecoder(bytes.NewBuffer(v.Value)).Decode(&t)
		if err != nil {
			continue
		}
		r := RequestStats(t)
		ars = append(ars, &r)
	}
	sort.Sort(reverse{ars})

	requestById := make(map[int]*RequestStats, len(ars))
	idByRequest := make(map[*RequestStats]int, len(ars))
	requests := make(map[int]*StatByName)
	byRequest := make(map[int]map[string]cVal)
	for i, v := range ars {
		idx := i + 1
		requestById[idx] = v
		idByRequest[v] = idx
		requests[idx] = &StatByName{
			RequestStats: v,
		}
		byRequest[idx] = make(map[string]cVal)
	}

	requestByPath := make(map[string][]int)
	byCount := make(map[string]cVal)
	byRPC := make(map[SKey]cVal)
	for _, t := range ars {
		id := idByRequest[t]

		requestByPath[t.Path] = append(requestByPath[t.Path], id)

		for _, r := range t.RPCStats {
			rpc := r.Name()

			v := byRequest[id][rpc]
			v.count++
			v.cost += r.Cost
			byRequest[id][rpc] = v

			v = byCount[rpc]
			v.count++
			v.cost += r.Cost
			byCount[rpc] = v

			v = byRPC[SKey{rpc, t.Path}]
			v.count++
			v.cost += r.Cost
			byRPC[SKey{rpc, t.Path}] = v
		}
	}

	for k, v := range byRequest {
		stats := StatsByName{}
		for rpc, s := range v {
			stats = append(stats, &StatByName{
				Name:  rpc,
				Count: s.count,
				Cost:  s.cost,
			})
		}
		sort.Sort(reverse{stats})
		requests[k].SubStats = stats
	}

	statsByRPC := make(map[string]StatsByName)
	pathStats := make(map[string]StatsByName)
	for k, v := range byRPC {
		statsByRPC[k.a] = append(statsByRPC[k.a], &StatByName{
			Name:  k.b,
			Count: v.count,
			Cost:  v.cost,
		})
		pathStats[k.b] = append(pathStats[k.b], &StatByName{
			Name:  k.a,
			Count: v.count,
			Cost:  v.cost,
		})
	}
	for k, v := range statsByRPC {
		sort.Sort(reverse{v})
		statsByRPC[k] = v
	}

	pathStatsByCount := StatsByName{}
	for k, v := range pathStats {
		total := 0
		var cost int64
		for _, stat := range v {
			total += stat.Count
			cost += stat.Cost
		}
		sort.Sort(reverse{v})

		pathStatsByCount = append(pathStatsByCount, &StatByName{
			Name:       k,
			Count:      total,
			Cost:       cost,
			SubStats:   v,
			Requests:   len(requestByPath[k]),
			RecentReqs: requestByPath[k],
		})
	}
	sort.Sort(reverse{pathStatsByCount})

	allStatsByCount := StatsByName{}
	for k, v := range byCount {
		allStatsByCount = append(allStatsByCount, &StatByName{
			Name:     k,
			Count:    v.count,
			Cost:     v.cost,
			SubStats: statsByRPC[k],
		})
	}
	sort.Sort(reverse{allStatsByCount})

	v := struct {
		Env                 map[string]string
		Requests            map[int]*StatByName
		RequestStatsByCount map[int]*StatByName
		AllStatsByCount     StatsByName
		PathStatsByCount    StatsByName
	}{
		Env: map[string]string{
			"APPLICATION_ID": appengine.AppID(c),
		},
		Requests:         requests,
		AllStatsByCount:  allStatsByCount,
		PathStatsByCount: pathStatsByCount,
	}

	_ = templates.ExecuteTemplate(w, "main", v)
}
Exemple #5
0
func Details(w http.ResponseWriter, r *http.Request) {
	i, _ := strconv.Atoi(r.FormValue("time"))
	qtime := roundTime(i)
	key := fmt.Sprintf(keyFull, qtime)

	c := context(r)

	v := struct {
		Env             map[string]string
		Record          *RequestStats
		Header          http.Header
		AllStatsByCount StatsByName
		Real            time.Duration
	}{
		Env: map[string]string{
			"APPLICATION_ID": appengine.AppID(c),
		},
	}

	item, err := memcache.Get(c, key)
	if err != nil {
		templates.ExecuteTemplate(w, "details", v)
		return
	}

	full := stats_full{}
	err = gob.NewDecoder(bytes.NewBuffer(item.Value)).Decode(&full)
	if err != nil {
		templates.ExecuteTemplate(w, "details", v)
		return
	}

	byCount := make(map[string]cVal)
	durationCount := make(map[string]time.Duration)
	var _real time.Duration
	for _, r := range full.Stats.RPCStats {
		rpc := r.Name()

		// byCount
		if _, present := byCount[rpc]; !present {
			durationCount[rpc] = 0
		}
		v := byCount[rpc]
		v.count++
		v.cost += r.Cost
		byCount[rpc] = v
		durationCount[rpc] += r.Duration
		_real += r.Duration
	}

	allStatsByCount := StatsByName{}
	for k, v := range byCount {
		allStatsByCount = append(allStatsByCount, &StatByName{
			Name:     k,
			Count:    v.count,
			Cost:     v.cost,
			Duration: durationCount[k],
		})
	}
	sort.Sort(allStatsByCount)

	v.Record = full.Stats
	v.Header = full.Header
	v.AllStatsByCount = allStatsByCount
	v.Real = _real

	_ = templates.ExecuteTemplate(w, "details", v)
}