<commit_before>package main

import (
	"bytes"
	"encoding/json"
	"fmt"
	"net/http"
	"strings"
	"text/template"

	"github.com/gorilla/mux"
	. "github.com/weaveworks/weave/common"
	"github.com/weaveworks/weave/ipam"
	"github.com/weaveworks/weave/mesh"
	"github.com/weaveworks/weave/nameserver"
	"github.com/weaveworks/weave/net/address"
	weave "github.com/weaveworks/weave/router"
)

var rootTemplate = template.New("root").Funcs(map[string]interface{}{
	"countDNSEntries": func(entries []nameserver.EntryStatus) int {
		count := 0
		for _, entry := range entries {
			if entry.Tombstone == 0 {
				count++
			}
		}
		return count
	},
	"upstreamServers": func(servers []string) string {
		if len(servers) == 0 {
			return "none"
		}
		return strings.Join(servers, ", ")
	},
	"printIPAMRanges": func(status ipam.Status) string {
		var buffer bytes.Buffer

		type stats struct {
			Ranges    []address.Range
			Addresses []address.Address
			Reachable bool
		}

		peerStats := make(map[mesh.PeerName]*stats)
		getOrCreateStats := func(peer mesh.PeerName) *stats {
			s, found := peerStats[peer]
			if !found {
				s = &stats{Reachable: status.IsKnownPeer(peer)}
				peerStats[peer] = s
			}
			return s
		}

		for _, entry := range status.Ring.Entries {
			stats := getOrCreateStats(entry.Peer)
			stats.Addresses = append(stats.Addresses, entry.Token)
		}

		for peer, ranges := range status.Ring.OwnedRangesByPeer() {
			stats := getOrCreateStats(peer)
			stats.Ranges = append(stats.Ranges, ranges...)
		}

		appendAddresses := func(peer mesh.PeerName, nickName string, reachable bool, addresses []address.Address, ranges []address.Range) {
			reachableStr := ""
			if !reachable {
				reachableStr = "- unreachable!"
			}
			ipsInRange := 0
			for _, chunk := range ranges {
				ipsInRange += int(chunk.Size())
			}
			percentageRanges := float32(ipsInRange) * 100.0 / float32(status.RangeNumIPs)

			displayName := peer.String() + "(" + nickName + ")"
			fmt.Fprintf(&buffer, "%20s: %8d IPs (%04.1f%% of total) - used: %d %s\n",
				displayName, ipsInRange, percentageRanges, len(addresses), reachableStr)
		}

		// print the local addresses
		if ourStats := peerStats[status.OurName]; ourStats != nil {
			appendAddresses(status.OurName, "local", true, ourStats.Addresses, ourStats.Ranges)
		}

		// and then the rest
		for peer, stats := range peerStats {
			if peer != status.OurName {
				nickname, found := status.Nicknames[peer]
				if !found {
					nickname = peer.String()
				}
				appendAddresses(peer, nickname, stats.Reachable, stats.Addresses, stats.Ranges)
			}
		}

		return buffer.String()
	},
	"printConnectionCounts": func(conns []mesh.LocalConnectionStatus) string {
		counts := make(map[string]int)
		for _, conn := range conns {
			counts[conn.State]++<commit_msg>Remove counting of ring tokens, since it isn't useful to end-user. Previously the count of tokens was claimed to be a count of addresses in use, which is incorrect. Also rename `appendAddresses` to `appendOwned` since it doesn't involve addresses.<commit_after>package main

import (
	"bytes"
	"encoding/json"
	"fmt"
	"net/http"
	"strings"
	"text/template"

	"github.com/gorilla/mux"
	. "github.com/weaveworks/weave/common"
	"github.com/weaveworks/weave/ipam"
	"github.com/weaveworks/weave/mesh"
	"github.com/weaveworks/weave/nameserver"
	"github.com/weaveworks/weave/net/address"
	weave "github.com/weaveworks/weave/router"
)

var rootTemplate = template.New("root").Funcs(map[string]interface{}{
	"countDNSEntries": func(entries []nameserver.EntryStatus) int {
		count := 0
		for _, entry := range entries {
			if entry.Tombstone == 0 {
				count++
			}
		}
		return count
	},
	"upstreamServers": func(servers []string) string {
		if len(servers) == 0 {
			return "none"
		}
		return strings.Join(servers, ", ")
	},
	"printIPAMRanges": func(status ipam.Status) string {
		var buffer bytes.Buffer

		type stats struct {
			Ranges    []address.Range
			Reachable bool
		}

		peerStats := make(map[mesh.PeerName]*stats)

		for peer, ranges := range status.Ring.OwnedRangesByPeer() {
			s, found := peerStats[peer]
			if !found {
				s = &stats{Reachable: status.IsKnownPeer(peer)}
				peerStats[peer] = s
			}
			s.Ranges = append(s.Ranges, ranges...)
		}

		appendOwned := func(peer mesh.PeerName, nickName string, reachable bool, ranges []address.Range) {
			reachableStr := ""
			if !reachable {
				reachableStr = "- unreachable!"
			}
			ipsInRange := 0
			for _, chunk := range ranges {
				ipsInRange += int(chunk.Size())
			}
			percentageRanges := float32(ipsInRange) * 100.0 / float32(status.RangeNumIPs)

			displayName := peer.String() + "(" + nickName + ")"
			fmt.Fprintf(&buffer, "%20s: %8d IPs (%04.1f%% of total) %s\n",
				displayName, ipsInRange, percentageRanges, reachableStr)
		}

		// print the local addresses
		if ourStats := peerStats[status.OurName]; ourStats != nil {
			appendOwned(status.OurName, "local", true, ourStats.Ranges)
		}

		// and then the rest
		for peer, stats := range peerStats {
			if peer != status.OurName {
				nickname, found := status.Nicknames[peer]
				if !found {
					nickname = peer.String()
				}
				appendOwned(peer, nickname, stats.Reachable, stats.Ranges)
			}
		}

		return buffer.String()
	},
	"printConnectionCounts": func(conns []mesh.LocalConnectionStatus) string {
		counts := make(map[string]int)
		for _, conn := range conns {
			counts[conn.State]++
{'fraction_non_alphanumeric': 0.1409805735430157, 'fraction_numerical': 0.0066604995374653095, 'mean_word_length': 3.6927083333333335, 'pattern_counts': {'":': 8, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7348960', 'n_tokens_mistral': 2135, 'n_tokens_neox': 1862, 'n_words': 405}