<commit_before>package urknall

import (
	"github.com/dynport/dgtk/goup"
	. "github.com/dynport/urknall/cmd"
)

func runlist(name string, pkg Package) *Runlist {
	return &Runlist{name: name, pkg: pkg}
}

func (h *Host) buildSystemRunlists() {
	if h.Hostname != "" {
		h.addSystemPackage("hostname",
			h.newHostPackage(
				"hostname localhost", // Set hostname to make sudo happy.
				&FileCommand{Path: "/etc/hostname", Content: h.Hostname},
				&FileCommand{Path: "/etc/hosts", Content: "127.0.0.1 {{ .Hostname }} localhost"},
				"hostname -F /etc/hostname"))
	}

	if len(h.Rules) > 0 {
		h.addSystemPackage("firewall",
			h.newHostPackage(
				InstallPackages("iptables", "ipset"),
				WriteAsset("/etc/network/if-pre-up.d/iptables", "fw_upstart.sh", "root", 0744),
				WriteAsset("/etc/iptables/ipsets", "fw_ipset.conf", "root", 0644),
				WriteAsset("/etc/iptables/rules_ipv4", "fw_rules_ipv4.conf", "root", 0644),
				WriteAsset("/etc/iptables/rules_ipv6", "fw_rules_ipv6.conf", "root", 0644),
				"modprobe iptable_filter && modprobe iptable_nat", // here to make sure next command succeeds.
				"IFACE={{ .Interface }} /etc/network/if-pre-up.d/iptables"))
	}

	if h.isDockerHost() {
		h.addSystemPackage("docker", &dockerPackage{Host: h})
	}
}

type hostPackage struct {
	*Host
	cmds []interface{}
}

func (h *Host) newHostPackage(cmds ...interface{}) *hostPackage {
	return &hostPackage{Host: h, cmds: cmds}
}

func (h *hostPackage) IsDockerHost() bool {
	return h.isDockerHost()
}

func (h *hostPackage) IsDockerBuildHost() bool {
	return h.isDockerHost()
}

func (h *hostPackage) Interface() string {
	return h.publicInterface()
}

func (hp *hostPackage) Package(rl *Runlist) {
	for i := range hp.cmds {
		rl.Add(hp.cmds[i])
	}
}

type dockerPackage struct {
	*Host
}

func (dp *dockerPackage) Package(rl *Runlist) {
	rl.Add(
		Or("grep universe /etc/apt/sources.list",
			And("sed 's/main$/main universe/' -i /etc/apt/sources.list",
				"apt-get update")))
	rl.Add(
		InstallPackages("curl", "build-essential", "git-core", "bsdtar", "lxc", "aufs-tools"))
	rl.Add(
		Or(
			installDockerKernelOnRaring(),
			installDockerKernelOnPrecise(),
			"exit 1"))

	rl.Add(dp.dockerBinary())
	rl.Add(&UpstartCommand{Upstart: dp.createUpstart()})
	rl.Add("start docker")

	if dp.Docker.WithRegistry {
		rl.Add(WaitForUnixSocket("/var/run/docker.sock", 10))
		rl.Add("docker run -d -p 0.0.0.0:5000:5000 stackbrew/registry")
	}
}

func installDockerKernelOnRaring() *ShellCommand {
	return And("lsb_release -c | grep raring",
		InstallPackages("linux-image-extra-$(uname -r)"))
}

func installDockerKernelOnPrecise() *ShellCommand {
	return And("lsb_release -c | grep precise",
		IfNot("-f /etc/apt/sources.list.d/precise-updates.list",
			And("echo 'deb http://archive.ubuntu.com/ubuntu precise-updates main' > /etc/apt/sources.list.d/precise-updates.list",
				"apt-get update -y")),
		"apt-get -o Dpkg::Options::='--force-confnew' install linux-generic-lts-raring -y")
}

func (dp *dockerPackage) dockerBinary() *DownloadCommand {
	baseUrl := "http://get.docker.io/builds/Linux/x86_64"

	if dp.dockerVersion() < "0.6.0" {
		panic("version lower than 0.6.0 not supported yet")
	}
	url := baseUrl + "/docker-" + dp.dockerVersion()
	return DownloadToFile(url, "/usr/local/bin/docker", "root", 0700)
}

func (dp *dockerPackage) createUpstart() *goup.Upstart {
	execCmd := "/usr/local/bin/docker -d -r -H unix:///var/run/docker.sock -H tcp://127.0.0.1:4243 2>&1 | logger -i -t docker"
	return &goup.Upstart{
		Name:          "docker",
		StartOnEvents: []string{"runlevel [2345]"},
		StopOnEvents:  []string{"runlevel [!2345]"},
		Exec:          execCmd,
	}
}
<commit_msg>make debconf non interactive<commit_after>package urknall

import (
	"github.com/dynport/dgtk/goup"
	. "github.com/dynport/urknall/cmd"
)

func runlist(name string, pkg Package) *Runlist {
	return &Runlist{name: name, pkg: pkg}
}

func (h *Host) buildSystemRunlists() {
	if h.Hostname != "" {
		h.addSystemPackage("hostname",
			h.newHostPackage(
				"hostname localhost", // Set hostname to make sudo happy.
				&FileCommand{Path: "/etc/hostname", Content: h.Hostname},
				&FileCommand{Path: "/etc/hosts", Content: "127.0.0.1 {{ .Hostname }} localhost"},
				"hostname -F /etc/hostname"))
	}

	if len(h.Rules) > 0 {
		h.addSystemPackage("firewall",
			h.newHostPackage(
				InstallPackages("iptables", "ipset"),
				WriteAsset("/etc/network/if-pre-up.d/iptables", "fw_upstart.sh", "root", 0744),
				WriteAsset("/etc/iptables/ipsets", "fw_ipset.conf", "root", 0644),
				WriteAsset("/etc/iptables/rules_ipv4", "fw_rules_ipv4.conf", "root", 0644),
				WriteAsset("/etc/iptables/rules_ipv6", "fw_rules_ipv6.conf", "root", 0644),
				"modprobe iptable_filter && modprobe iptable_nat", // here to make sure next command succeeds.
				"IFACE={{ .Interface }} /etc/network/if-pre-up.d/iptables"))
	}

	if h.isDockerHost() {
		h.addSystemPackage("docker", &dockerPackage{Host: h})
	}
}

type hostPackage struct {
	*Host
	cmds []interface{}
}

func (h *Host) newHostPackage(cmds ...interface{}) *hostPackage {
	return &hostPackage{Host: h, cmds: cmds}
}

func (h *hostPackage) IsDockerHost() bool {
	return h.isDockerHost()
}

func (h *hostPackage) IsDockerBuildHost() bool {
	return h.isDockerHost()
}

func (h *hostPackage) Interface() string {
	return h.publicInterface()
}

func (hp *hostPackage) Package(rl *Runlist) {
	for i := range hp.cmds {
		rl.Add(hp.cmds[i])
	}
}

type dockerPackage struct {
	*Host
}

func (dp *dockerPackage) Package(rl *Runlist) {
	rl.Add(
		Or("grep universe /etc/apt/sources.list",
			And("sed 's/main$/main universe/' -i /etc/apt/sources.list",
				"DEBIAN_FRONTEND=noninteractive apt-get update")))
	rl.Add(
		InstallPackages("curl", "build-essential", "git-core", "bsdtar", "lxc", "aufs-tools"))
	rl.Add(
		Or(
			installDockerKernelOnRaring(),
			installDockerKernelOnPrecise(),
			"exit 1"))

	rl.Add(dp.dockerBinary())
	rl.Add(&UpstartCommand{Upstart: dp.createUpstart()})
	rl.Add("start docker")

	if dp.Docker.WithRegistry {
		rl.Add(WaitForUnixSocket("/var/run/docker.sock", 10))
		rl.Add("docker run -d -p 0.0.0.0:5000:5000 stackbrew/registry")
	}
}

func installDockerKernelOnRaring() *ShellCommand {
	return And("lsb_release -c | grep raring",
		InstallPackages("linux-image-extra-$(uname -r)"))
}

func installDockerKernelOnPrecise() *ShellCommand {
	return And("lsb_release -c | grep precise",
		IfNot("-f /etc/apt/sources.list.d/precise-updates.list",
			And("echo 'deb http://archive.ubuntu.com/ubuntu precise-updates main' > /etc/apt/sources.list.d/precise-updates.list",
				"apt-get update -y")),
		"apt-get -o Dpkg::Options::='--force-confnew' install linux-generic-lts-raring -y")
}

func (dp *dockerPackage) dockerBinary() *DownloadCommand {
	baseUrl := "http://get.docker.io/builds/Linux/x86_64"

	if dp.dockerVersion() < "0.6.0" {
		panic("version lower than 0.6.0 not supported yet")
	}
	url := baseUrl + "/docker-" + dp.dockerVersion()
	return DownloadToFile(url, "/usr/local/bin/docker", "root", 0700)
}

func (dp *dockerPackage) createUpstart() *goup.Upstart {
	execCmd := "/usr/local/bin/docker -d -r -H unix:///var/run/docker.sock -H tcp://127.0.0.1:4243 2>&1 | logger -i -t docker"
	return &goup.Upstart{
		Name:          "docker",
		StartOnEvents: []string{"runlevel [2345]"},
		StopOnEvents:  []string{"runlevel [!2345]"},
		Exec:          execCmd,
	}
}
<|endoftext|>


<commit_before><commit_msg>iat claim for gentoken and gensubtoken output tokens<commit_after><|endoftext|>


<commit_before>package main

import (
	log "github.com/Sirupsen/logrus"
	"gopkg.in/alecthomas/kingpin.v2"
	"os"
	"fmt"
)

var (
	hostFlag = kingpin.Flag("host", "Set the host of Hoverfly").String()
	adminPortFlag = kingpin.Flag("admin-port", "Set the admin port of Hoverfly").String()
	proxyPortFlag = kingpin.Flag("proxy-port", "Set the admin port of Hoverfly").String()
	verboseFlag = kingpin.Flag("verbose", "Verbose mode.").Short('v').Bool()

	modeCommand = kingpin.Command("mode", "Get Hoverfly's current mode")
	modeNameArg = modeCommand.Arg("name", "Set Hoverfly's mode").String()

	startCommand = kingpin.Command("start", "Start a local instance of Hoverfly")
	startArg = startCommand.Arg("server type", "Choose the configuration of Hoverfly (proxy/webserver)").String()
	stopCommand = kingpin.Command("stop", "Stop a local instance of Hoverfly")

	exportCommand = kingpin.Command("export", "Exports data out of Hoverfly")
	exportNameArg = exportCommand.Arg("name", "Name of exported simulation").Required().String()

	importCommand = kingpin.Command("import", "Imports data into Hoverfly")
	importNameArg = importCommand.Arg("name", "Name of imported simulation").Required().String()

	pushCommand = kingpin.Command("push", "Pushes the data to SpectoLab")
	pushNameArg = pushCommand.Arg("name", "Name of exported simulation").Required().String()

	pullCommand = kingpin.Command("pull", "Pushes the data to SpectoLab")
	pullNameArg = pullCommand.Arg("name", "Name of imported simulation").Required().String()
	pullOverrideHostFlag = pullCommand.Flag("override-host", "Name of the host you want to virtualise").String()

	wipeCommand = kingpin.Command("wipe", "Wipe Hoverfly database")

	delaysCommand = kingpin.Command("delays", "Get per-host response delay config currently loaded into Hoverfly")
	delaysPathArg = delaysCommand.Arg("path", "Set per-host response delay config from JSON file").String()
)

func main() {
	kingpin.Parse()

	log.SetOutput(os.Stdout)
	if *verboseFlag {
		log.SetLevel(log.DebugLevel)
	}

	SetConfigurationDefaults()
	SetConfigurationPaths()

	config := GetConfig(*hostFlag, *adminPortFlag, *proxyPortFlag, "", "")

	hoverflyDirectory, err := NewHoverflyDirectory(config)
	handleIfError(err)

	cacheDirectory, err := createCacheDirectory(hoverflyDirectory)
	handleIfError(err)

	localCache := LocalCache{
		URI: cacheDirectory,
	}

	hoverfly := NewHoverfly(config)

	spectoLab := SpectoLab{
		Host: "https://lab.specto.io",
		APIKey: config.SpectoLabAPIKey,
	}

	switch kingpin.Parse() {
		case modeCommand.FullCommand():
			if *modeNameArg == "" || *modeNameArg == "status"{
				mode, err := hoverfly.GetMode()
				handleIfError(err)

				log.Info("Hoverfly is set to ", mode, " mode")
			} else {
				mode, err := hoverfly.SetMode(*modeNameArg)
				handleIfError(err)

				log.Info("Hoverfly has been set to ", mode, " mode")
			}

		case startCommand.FullCommand():
			if *startArg == "webserver" {
				err := hoverfly.startWithFlags(hoverflyDirectory, "-webserver")
				handleIfError(err)

				log.Info("Hoverfly is now running as a webserver")
			} else {
				err := hoverfly.start(hoverflyDirectory)
				handleIfError(err)

				log.Info("Hoverfly is now running")
			}

		case stopCommand.FullCommand():
			err := hoverfly.stop(hoverflyDirectory)
			handleIfError(err)

			log.Info("Hoverfly has been stopped")

		case exportCommand.FullCommand():
			simulation, err := NewSimulation(*exportNameArg)
			handleIfError(err)

			simulationData, err := hoverfly.ExportSimulation()
			handleIfError(err)

			err = localCache.WriteSimulation(simulation, simulationData)
			handleIfError(err)

			log.Info(simulation.String(), " exported successfully")

		case importCommand.FullCommand():
			simulation, err := NewSimulation(*importNameArg)
			handleIfError(err)

			simulationData, err := localCache.ReadSimulation(simulation)
			handleIfError(err)

			err = hoverfly.ImportSimulation(string(simulationData))
			handleIfError(err)

			log.Info(simulation.String(), " imported successfully")

		case pushCommand.FullCommand():
			err := spectoLab.CheckAPIKey()
			handleIfError(err)

			simulation, err := NewSimulation(*pushNameArg)
			handleIfError(err)

			simulationData, err := localCache.ReadSimulation(simulation)
			handleIfError(err)

			statusCode, err := spectoLab.UploadSimulation(simulation, simulationData)
			handleIfError(err)

			if statusCode {
				log.Info(simulation.String(), " has been pushed to the SpectoLab")
			}

		case pullCommand.FullCommand():
			err := spectoLab.CheckAPIKey()
			handleIfError(err)

			simulation, err := NewSimulation(*pullNameArg)
			handleIfError(err)

			simulationData, err := spectoLab.GetSimulation(simulation, *pullOverrideHostFlag)
			handleIfError(err)

			err = localCache.WriteSimulation(simulation, simulationData)
			handleIfError(err)

			log.Info(simulation.String(), " has been pulled from the SpectoLab")

		case wipeCommand.FullCommand():
			err := hoverfly.Wipe()
			handleIfError(err)

			log.Info("Hoverfly has been wiped")

		case delaysCommand.FullCommand():
			if *delaysPathArg == "" || *delaysPathArg == "status"{
				delays, err := hoverfly.GetDelays()
				handleIfError(err)
				for _, delay := range delays {
					fmt.Printf("%+v\n", delay)
				}
			} else {
				delays, err := hoverfly.SetDelays(*delaysPathArg)
				handleIfError(err)
				log.Info("Response delays set in Hoverfly: ")
				for _, delay := range delays {
					fmt.Printf("%+v\n", delay)
				}
			}
	}
}

func handleIfError(err error) {
	if err != nil {
		log.Fatal(err.Error())
	}
}<commit_msg>The wipe command in hoverctl is now delete with an alias to wipe<commit_after>package main

import (
	log "github.com/Sirupsen/logrus"
	"gopkg.in/alecthomas/kingpin.v2"
	"os"
	"fmt"
)

var (
	hostFlag = kingpin.Flag("host", "Set the host of Hoverfly").String()
	adminPortFlag = kingpin.Flag("admin-port", "Set the admin port of Hoverfly").String()
	proxyPortFlag = kingpin.Flag("proxy-port", "Set the admin port of Hoverfly").String()
	verboseFlag = kingpin.Flag("verbose", "Verbose mode.").Short('v').Bool()

	modeCommand = kingpin.Command("mode", "Get Hoverfly's current mode")
	modeNameArg = modeCommand.Arg("name", "Set Hoverfly's mode").String()

	startCommand = kingpin.Command("start", "Start a local instance of Hoverfly")
	startArg = startCommand.Arg("server type", "Choose the configuration of Hoverfly (proxy/webserver)").String()
	stopCommand = kingpin.Command("stop", "Stop a local instance of Hoverfly")

	exportCommand = kingpin.Command("export", "Exports data out of Hoverfly")
	exportNameArg = exportCommand.Arg("name", "Name of exported simulation").Required().String()

	importCommand = kingpin.Command("import", "Imports data into Hoverfly")
	importNameArg = importCommand.Arg("name", "Name of imported simulation").Required().String()

	pushCommand = kingpin.Command("push", "Pushes the data to SpectoLab")
	pushNameArg = pushCommand.Arg("name", "Name of exported simulation").Required().String()

	pullCommand = kingpin.Command("pull", "Pushes the data to SpectoLab")
	pullNameArg = pullCommand.Arg("name", "Name of imported simulation").Required().String()
	pullOverrideHostFlag = pullCommand.Flag("override-host", "Name of the host you want to virtualise").String()

	deleteCommand = kingpin.Command("delete", "Delete test data from Hoverfly")

	delaysCommand = kingpin.Command("delays", "Get per-host response delay config currently loaded into Hoverfly")
	delaysPathArg = delaysCommand.Arg("path", "Set per-host response delay config from JSON file").String()
)

func main() {
	deleteCommand.Alias("wipe")

	kingpin.Parse()

	log.SetOutput(os.Stdout)
	if *verboseFlag {
		log.SetLevel(log.DebugLevel)
	}

	SetConfigurationDefaults()
	SetConfigurationPaths()

	config := GetConfig(*hostFlag, *adminPortFlag, *proxyPortFlag, "", "")

	hoverflyDirectory, err := NewHoverflyDirectory(config)
	handleIfError(err)

	cacheDirectory, err := createCacheDirectory(hoverflyDirectory)
	handleIfError(err)

	localCache := LocalCache{
		URI: cacheDirectory,
	}

	hoverfly := NewHoverfly(config)

	spectoLab := SpectoLab{
		Host: "https://lab.specto.io",
		APIKey: config.SpectoLabAPIKey,
	}

	switch kingpin.Parse() {
		case modeCommand.FullCommand():
			if *modeNameArg == "" || *modeNameArg == "status"{
				mode, err := hoverfly.GetMode()
				handleIfError(err)

				log.Info("Hoverfly is set to ", mode, " mode")
			} else {
				mode, err := hoverfly.SetMode(*modeNameArg)
				handleIfError(err)

				log.Info("Hoverfly has been set to ", mode, " mode")
			}

		case startCommand.FullCommand():
			if *startArg == "webserver" {
				err := hoverfly.startWithFlags(hoverflyDirectory, "-webserver")
				handleIfError(err)

				log.Info("Hoverfly is now running as a webserver")
			} else {
				err := hoverfly.start(hoverflyDirectory)
				handleIfError(err)

				log.Info("Hoverfly is now running")
			}

		case stopCommand.FullCommand():
			err := hoverfly.stop(hoverflyDirectory)
			handleIfError(err)

			log.Info("Hoverfly has been stopped")

		case exportCommand.FullCommand():
			simulation, err := NewSimulation(*exportNameArg)
			handleIfError(err)

			simulationData, err := hoverfly.ExportSimulation()
			handleIfError(err)

			err = localCache.WriteSimulation(simulation, simulationData)
			handleIfError(err)

			log.Info(simulation.String(), " exported successfully")

		case importCommand.FullCommand():
			simulation, err := NewSimulation(*importNameArg)
			handleIfError(err)

			simulationData, err := localCache.ReadSimulation(simulation)
			handleIfError(err)

			err = hoverfly.ImportSimulation(string(simulationData))
			handleIfError(err)

			log.Info(simulation.String(), " imported successfully")

		case pushCommand.FullCommand():
			err := spectoLab.CheckAPIKey()
			handleIfError(err)

			simulation, err := NewSimulation(*pushNameArg)
			handleIfError(err)

			simulationData, err := localCache.ReadSimulation(simulation)
			handleIfError(err)

			statusCode, err := spectoLab.UploadSimulation(simulation, simulationData)
			handleIfError(err)

			if statusCode {
				log.Info(simulation.String(), " has been pushed to the SpectoLab")
			}

		case pullCommand.FullCommand():
			err := spectoLab.CheckAPIKey()
			handleIfError(err)

			simulation, err := NewSimulation(*pullNameArg)
			handleIfError(err)

			simulationData, err := spectoLab.GetSimulation(simulation, *pullOverrideHostFlag)
			handleIfError(err)

			err = localCache.WriteSimulation(simulation, simulationData)
			handleIfError(err)

			log.Info(simulation.String(), " has been pulled from the SpectoLab")

		case deleteCommand.FullCommand():
			err := hoverfly.Wipe()
			handleIfError(err)

			log.Info("Hoverfly has been wiped")

		case delaysCommand.FullCommand():
			if *delaysPathArg == "" || *delaysPathArg == "status"{
				delays, err := hoverfly.GetDelays()
				handleIfError(err)
				for _, delay := range delays {
					fmt.Printf("%+v\n", delay)
				}
			} else {
				delays, err := hoverfly.SetDelays(*delaysPathArg)
				handleIfError(err)
				log.Info("Response delays set in Hoverfly: ")
				for _, delay := range delays {
					fmt.Printf("%+v\n", delay)
				}
			}
	}
}

func handleIfError(err error) {
	if err != nil {
		log.Fatal(err.Error())
	}
}<|endoftext|>


<commit_before>package main

import (
	"encoding/json"
	"github.com/julienschmidt/httprouter"
	"net"
	"net/http"
	"os"

	"github.com/NebulousLabs/Sia-Ant-Farm/ant"
)

type (
	// AntfarmConfig contains the fields to parse and use to create a sia-antfarm.
	AntfarmConfig struct {
		ListenAddress string
		AntConfigs    []ant.AntConfig
		AutoConnect   bool

		// ExternalFarms is a slice of net addresses representing the API addresses
		// of other antFarms to connect to.
		ExternalFarms []string
	}

	// antFarm defines the 'antfarm' type. antFarm orchestrates a collection of
	// ants and provides an API server to interact with them.
	antFarm struct {
		apiListener net.Listener

		// ants is a slice of Ants in this antfarm.
		ants []*ant.Ant

		// externalAnts is a slice of externally connected ants, that is, ants that
		// are connected to this antfarm but managed by another antfarm.
		externalAnts []*ant.Ant
		router       *httprouter.Router
	}
)

// createAntfarm creates a new antFarm given the supplied AntfarmConfig
func createAntfarm(config AntfarmConfig) (*antFarm, error) {
	// clear old antfarm data before creating an antfarm
	os.RemoveAll("./antfarm-data")
	os.MkdirAll("./antfarm-data", 0700)

	farm := &antFarm{}

	// start up each ant process with its jobs
	ants, err := startAnts(config.AntConfigs...)
	if err != nil {
		return nil, err
	}
	farm.ants = ants
	defer func() {
		if err != nil {
			farm.Close()
		}
	}()

	// if the AutoConnect flag is set, use connectAnts to bootstrap the network.
	if config.AutoConnect {
		if err = connectAnts(ants...); err != nil {
			return nil, err
		}
	}
	// connect the external antFarms
	for _, address := range config.ExternalFarms {
		if err = farm.connectExternalAntfarm(address); err != nil {
			return nil, err
		}
	}
	// start up the api server listener
	farm.apiListener, err = net.Listen("tcp", config.ListenAddress)
	if err != nil {
		return nil, err
	}

	// construct the router and serve the API.
	farm.router = httprouter.New()
	farm.router.GET("/ants", farm.getAnts)

	return farm, nil
}

// allAnts returns all ants, external and internal, associated with this
// antFarm.
func (af *antFarm) allAnts() []*ant.Ant {
	return append(af.ants, af.externalAnts...)
}

// connectExternalAntfarm connects the current antfarm to an external antfarm,
// using the antfarm api at externalAddress.
func (af *antFarm) connectExternalAntfarm(externalAddress string) error {
	res, err := http.DefaultClient.Get("http://" + externalAddress + "/ants")
	if err != nil {
		return err
	}
	defer res.Body.Close()

	var externalAnts []*ant.Ant
	err = json.NewDecoder(res.Body).Decode(&externalAnts)
	if err != nil {
		return err
	}
	af.externalAnts = append(af.externalAnts, externalAnts...)
	return connectAnts(af.allAnts()...)
}

// ServeAPI serves the antFarm's http API.
func (af *antFarm) ServeAPI() error {
	http.Serve(af.apiListener, af.router)
	return nil
}

// permanentSyncMonitor checks that all ants in the antFarm are on the same
// blockchain.
/*
func (af *antFarm) permanentSyncMonitor() {
	// Give 30 seconds for everything to start up.
	time.Sleep(time.Second * 30)

	// Every 20 seconds, list all consensus groups.
	for {
		time.Sleep(time.Second * 20)
		groups, err := antConsensusGroups(af.allAnts()...)
		if err != nil {
			log.Println("error checking sync status of antfarm: ", err)
			continue
		}
		if len(groups) == 1 {
			log.Println("Ants are synchronized.")
		} else {
			log.Println("Ants split into multiple groups, displaying")
			for i, group := range groups {
				if i != 0 {
					log.Println()
				}
				log.Println("Group ", i+1)
				for _, ant := range group {
					log.Println(ant.APIAddr)
				}
			}
		}
	}
}
*/

// getAnts is a http handler that returns the ants currently running on the
// antfarm.
func (af *antFarm) getAnts(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
	err := json.NewEncoder(w).Encode(af.ants)
	if err != nil {
		http.Error(w, "error encoding ants", 500)
	}
}

// Close signals all the ants to stop and waits for them to return.
func (af *antFarm) Close() error {
	if af.apiListener != nil {
		af.apiListener.Close()
	}
	for _, ant := range af.ants {
		ant.Close()
	}
	return nil
}
<commit_msg>re-enable permanentSyncMonitor<commit_after>package main

import (
	"encoding/json"
	"github.com/julienschmidt/httprouter"
	"log"
	"net"
	"net/http"
	"os"
	"time"

	"github.com/NebulousLabs/Sia-Ant-Farm/ant"
)

type (
	// AntfarmConfig contains the fields to parse and use to create a sia-antfarm.
	AntfarmConfig struct {
		ListenAddress string
		AntConfigs    []ant.AntConfig
		AutoConnect   bool

		// ExternalFarms is a slice of net addresses representing the API addresses
		// of other antFarms to connect to.
		ExternalFarms []string
	}

	// antFarm defines the 'antfarm' type. antFarm orchestrates a collection of
	// ants and provides an API server to interact with them.
	antFarm struct {
		apiListener net.Listener

		// ants is a slice of Ants in this antfarm.
		ants []*ant.Ant

		// externalAnts is a slice of externally connected ants, that is, ants that
		// are connected to this antfarm but managed by another antfarm.
		externalAnts []*ant.Ant
		router       *httprouter.Router
	}
)

// createAntfarm creates a new antFarm given the supplied AntfarmConfig
func createAntfarm(config AntfarmConfig) (*antFarm, error) {
	// clear old antfarm data before creating an antfarm
	os.RemoveAll("./antfarm-data")
	os.MkdirAll("./antfarm-data", 0700)

	farm := &antFarm{}

	// start up each ant process with its jobs
	ants, err := startAnts(config.AntConfigs...)
	if err != nil {
		return nil, err
	}
	farm.ants = ants
	defer func() {
		if err != nil {
			farm.Close()
		}
	}()

	// if the AutoConnect flag is set, use connectAnts to bootstrap the network.
	if config.AutoConnect {
		if err = connectAnts(ants...); err != nil {
			return nil, err
		}
	}
	// connect the external antFarms
	for _, address := range config.ExternalFarms {
		if err = farm.connectExternalAntfarm(address); err != nil {
			return nil, err
		}
	}
	// start up the api server listener
	farm.apiListener, err = net.Listen("tcp", config.ListenAddress)
	if err != nil {
		return nil, err
	}

	// start the sync monitor
	go farm.permanentSyncMonitor()

	// construct the router and serve the API.
	farm.router = httprouter.New()
	farm.router.GET("/ants", farm.getAnts)

	return farm, nil
}

// allAnts returns all ants, external and internal, associated with this
// antFarm.
func (af *antFarm) allAnts() []*ant.Ant {
	return append(af.ants, af.externalAnts...)
}

// connectExternalAntfarm connects the current antfarm to an external antfarm,
// using the antfarm api at externalAddress.
func (af *antFarm) connectExternalAntfarm(externalAddress string) error {
	res, err := http.DefaultClient.Get("http://" + externalAddress + "/ants")
	if err != nil {
		return err
	}
	defer res.Body.Close()

	var externalAnts []*ant.Ant
	err = json.NewDecoder(res.Body).Decode(&externalAnts)
	if err != nil {
		return err
	}
	af.externalAnts = append(af.externalAnts, externalAnts...)
	return connectAnts(af.allAnts()...)
}

// ServeAPI serves the antFarm's http API.
func (af *antFarm) ServeAPI() error {
	http.Serve(af.apiListener, af.router)
	return nil
}

// permanentSyncMonitor checks that all ants in the antFarm are on the same
// blockchain.
func (af *antFarm) permanentSyncMonitor() {
	// Give 30 seconds for everything to start up.
	time.Sleep(time.Second * 30)

	// Every 20 seconds, list all consensus groups.
	for {
		time.Sleep(time.Second * 20)
		groups, err := antConsensusGroups(af.allAnts()...)
		if err != nil {
			log.Println("error checking sync status of antfarm: ", err)
			continue
		}
		if len(groups) == 1 {
			log.Println("Ants are synchronized.")
		} else {
			log.Println("Ants split into multiple groups, displaying")
			for i, group := range groups {
				if i != 0 {
					log.Println()
				}
				log.Println("Group ", i+1)
				for _, a := range group {
					log.Println(a.APIAddr)
				}
			}
		}
	}
}

// getAnts is a http handler that returns the ants currently running on the
// antfarm.
func (af *antFarm) getAnts(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
	err := json.NewEncoder(w).Encode(af.ants)
	if err != nil {
		http.Error(w, "error encoding ants", 500)
	}
}

// Close signals all the ants to stop and waits for them to return.
func (af *antFarm) Close() error {
	if af.apiListener != nil {
		af.apiListener.Close()
	}
	for _, ant := range af.ants {
		ant.Close()
	}
	return nil
}
<|endoftext|>


<commit_before>package go_koans

func aboutChannels() {
	ch := make(chan string, 2)

	assert(len(ch) == __int__) // channels are like buffers

	ch <- "foo" // i mean, "metaphors are like similes"

	assert(len(ch) == __int__) // they can be queried for queued items

	assert(<-ch == __string__) // items can be popped out of them

	assert(len(ch) == __int__) // and len() always reflects the "current" queue status

	// the 'go' keyword runs a function-call in a new "goroutine"
	// which executes "concurrently" with the calling "goroutine"
	go func() {
		// your code goes here
	}()

	assert(__delete_me__) // we'll need to make room for the queue, or suffer deadlocks

	ch <- "bar"   // this send will succeed
	ch <- "quux"  // there's enough room for this send too
	ch <- "extra" // but the buffer only has two slots
}
<commit_msg>Channels Koan<commit_after>package go_koans

func aboutChannels() {
	ch := make(chan string, 2)

	assert(len(ch) == 0) // channels are like buffers

	ch <- "foo" // i mean, "metaphors are like similes"

	assert(len(ch) == 1) // they can be queried for queued items

	assert(<-ch == "foo") // items can be popped out of them

	assert(len(ch) == 0) // and len() always reflects the "current" queue status

	// the 'go' keyword runs a function-call in a new "goroutine"
	// which executes "concurrently" with the calling "goroutine"
	go func() {
		// your code goes here
		<- ch
	}()

	ch <- "bar"   // this send will succeed
	ch <- "quux"  // there's enough room for this send too
	ch <- "extra" // but the buffer only has two slots
}
<|endoftext|>


<commit_before><commit_msg>also set environment variables via setenv<commit_after><|endoftext|>


<commit_before>package fixchain

import (
	"log"
	"net/http"
	"sync"
	"sync/atomic"
	"time"

	"github.com/google/certificate-transparency/go/x509"
)

// FixAndLog contains a Fixer and a Logger, for all your fix-then-log-chain needs!
type FixAndLog struct {
	fixer  *Fixer
	chains chan []*x509.Certificate
	logger *Logger
	wg     sync.WaitGroup

	queued        uint32     // Whole chains queued - before checking cache.
	done          *lockedMap // Cache of chains that QueueAllCertsInChain() has already been called on.
	chainsQueued  uint32     // Chains queued total, including chains from intermediate certs.
	alreadyDone   uint32
	alreadyPosted uint32
	chainsSent    uint32
}

// QueueAllCertsInChain adds every cert in the chain and the chain to the queue
// to be fixed and logged.
func (fl *FixAndLog) QueueAllCertsInChain(chain []*x509.Certificate) {
	if chain != nil {
		atomic.AddUint32(&fl.queued, 1)
		atomic.AddUint32(&fl.chainsQueued, uint32(len(chain)))
		dchain := newDedupedChain(chain)
		// Caching check
		h := hashBag(dchain.certs)
		if fl.done.get(h) {
			atomic.AddUint32(&fl.alreadyDone, 1)
			return
		}
		fl.done.set(h, true)

		for _, cert := range dchain.certs {
			if fl.logger.IsPosted(cert) {
				atomic.AddUint32(&fl.alreadyPosted, 1)
				continue
			}
			fl.fixer.QueueChain(cert, dchain.certs, fl.logger.RootCerts())
			atomic.AddUint32(&fl.chainsSent, 1)
		}
	}
}

// QueueChain queues the given chain to be fixed wrt the roots of the logger
// contained in fl, and then logged to the Certificate Transparency log
// represented by the logger.  Note: chain is expected to be in the order of
// cert --> root.
func (fl *FixAndLog) QueueChain(chain []*x509.Certificate) {
	if chain != nil {
		if fl.logger.IsPosted(chain[0]) {
			return
		}
		fl.fixer.QueueChain(chain[0], chain, fl.logger.RootCerts())
		atomic.AddUint32(&fl.chainsSent, 1)
	}
}

// Wait waits for the all of the queued chains to complete being fixed and
// logged.
func (fl *FixAndLog) Wait() {
	fl.fixer.Wait()
	close(fl.chains)
	fl.wg.Wait()
	fl.logger.Wait()
}

// NewFixAndLog creates an object that will asynchronously fix any chains that
// are added to its queue, and then log them to the Certificate Transparency log
// found at the given url.  Any errors encountered along the way are pushed to
// the given errors channel.
func NewFixAndLog(fixerWorkerCount int, loggerWorkerCount int, errors chan<- *FixError, client *http.Client, logClient *http.Client, logURL string, limiter Limiter, logStats bool) *FixAndLog {
	chains := make(chan []*x509.Certificate)
	fl := &FixAndLog{
		fixer:  NewFixer(fixerWorkerCount, chains, errors, client, logStats),
		chains: chains,
		logger: NewLogger(loggerWorkerCount, logURL, errors, logClient, limiter, logStats),
		done:   newLockedMap(),
	}

	fl.wg.Add(1)
	go func() {
		for chain := range chains {
			fl.logger.QueueChain(chain)
		}
		fl.wg.Done()
	}()

	if logStats {
		t := time.NewTicker(time.Second)
		go func() {
			for _ = range t.C {
				log.Printf("fix-then-log: %d whole chains queued, %d whole chains already done, %d total chains queued, %d chains already posted, %d chains sent to fixer", fl.queued, fl.alreadyDone, fl.chainsQueued, fl.alreadyPosted, fl.chainsSent)
			}
		}()
	}

	return fl
}
<commit_msg>Improve stats comments and add additional inc.<commit_after>package fixchain

import (
	"log"
	"net/http"
	"sync"
	"sync/atomic"
	"time"

	"github.com/google/certificate-transparency/go/x509"
)

// FixAndLog contains a Fixer and a Logger, for all your fix-then-log-chain needs!
type FixAndLog struct {
	fixer  *Fixer
	chains chan []*x509.Certificate
	logger *Logger
	wg     sync.WaitGroup

	// Number of whole chains queued - before checking cache & adding chains for intermediate certs.
	queued uint32
	// Cache of chains that QueueAllCertsInChain() has already been called on.
	done *lockedMap
	// Number of whole chains submitted to the FixAndLog using QueueAllCertsInChain() (before adding chains for intermediate certs) that had previously been processed.
	alreadyDone uint32
	// Number of chains queued total, including chains from intermediate certs.
	// Note that in each chain there is len(chain) certs.  So when calling QueueAllCertsInChain(chain), len(chain) chains will actually be added to the queue.
	chainsQueued uint32
	// Number of chains whose leaf cert has already been posted to the log with a valid chain.
	alreadyPosted uint32
	// Number of chains sent on to the Fixer to begin fixing & logging!
	chainsSent uint32
}

// QueueAllCertsInChain adds every cert in the chain and the chain to the queue
// to be fixed and logged.
func (fl *FixAndLog) QueueAllCertsInChain(chain []*x509.Certificate) {
	if chain != nil {
		atomic.AddUint32(&fl.queued, 1)
		atomic.AddUint32(&fl.chainsQueued, uint32(len(chain)))
		dchain := newDedupedChain(chain)
		// Caching check
		h := hashBag(dchain.certs)
		if fl.done.get(h) {
			atomic.AddUint32(&fl.alreadyDone, 1)
			return
		}
		fl.done.set(h, true)

		for _, cert := range dchain.certs {
			if fl.logger.IsPosted(cert) {
				atomic.AddUint32(&fl.alreadyPosted, 1)
				continue
			}
			fl.fixer.QueueChain(cert, dchain.certs, fl.logger.RootCerts())
			atomic.AddUint32(&fl.chainsSent, 1)
		}
	}
}

// QueueChain queues the given chain to be fixed wrt the roots of the logger
// contained in fl, and then logged to the Certificate Transparency log
// represented by the logger.  Note: chain is expected to be in the order of
// cert --> root.
func (fl *FixAndLog) QueueChain(chain []*x509.Certificate) {
	if chain != nil {
		if fl.logger.IsPosted(chain[0]) {
			atomic.AddUint32(&fl.alreadyPosted, 1)
			return
		}
		fl.fixer.QueueChain(chain[0], chain, fl.logger.RootCerts())
		atomic.AddUint32(&fl.chainsSent, 1)
	}
}

// Wait waits for the all of the queued chains to complete being fixed and
// logged.
func (fl *FixAndLog) Wait() {
	fl.fixer.Wait()
	close(fl.chains)
	fl.wg.Wait()
	fl.logger.Wait()
}

// NewFixAndLog creates an object that will asynchronously fix any chains that
// are added to its queue, and then log them to the Certificate Transparency log
// found at the given url.  Any errors encountered along the way are pushed to
// the given errors channel.
func NewFixAndLog(fixerWorkerCount int, loggerWorkerCount int, errors chan<- *FixError, client *http.Client, logClient *http.Client, logURL string, limiter Limiter, logStats bool) *FixAndLog {
	chains := make(chan []*x509.Certificate)
	fl := &FixAndLog{
		fixer:  NewFixer(fixerWorkerCount, chains, errors, client, logStats),
		chains: chains,
		logger: NewLogger(loggerWorkerCount, logURL, errors, logClient, limiter, logStats),
		done:   newLockedMap(),
	}

	fl.wg.Add(1)
	go func() {
		for chain := range chains {
			fl.logger.QueueChain(chain)
		}
		fl.wg.Done()
	}()

	if logStats {
		t := time.NewTicker(time.Second)
		go func() {
			for _ = range t.C {
				log.Printf("fix-then-log: %d whole chains queued, %d whole chains already done, %d total chains queued, %d chains don't need posting (cache hits), %d chains sent to fixer", fl.queued, fl.alreadyDone, fl.chainsQueued, fl.alreadyPosted, fl.chainsSent)
			}
		}()
	}

	return fl
}
<|endoftext|>


<commit_before>package gusher

import (
	"fmt"
	"log"
	"net/http"
)

func (h *handler) auth() http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
		if req.Method != "POST" {
			w.WriteHeader(405)
			return
		}
		callback := req.PostFormValue("callback")
		socketId := req.PostFormValue("socket_id")
		channelName := req.PostFormValue("channel_name")
		if callback != "" {
			authJSON := auth(socketId, channelName)
			payload := fmt.Sprintf("%s({\"auth\":\"%s\"})", callback, authJSON)
			w.Header().Set("Content-Type", "application/javascript")
			log.Println(payload)
			fmt.Fprintf(w, payload)
		} else {
			log.Println("not JSONP")
		}
		//set authTransport to 'ajax' (default)
		//POST to /pusher/auth w/ socket_id and channel_name
		//set authTransport to 'jsonp', also set authEndpoint (default to /pusher/auth)
		//JSONP to /pusher/auth w/ socket_id, channel_name and callback
		//render :text => params[:callback] + "(" + auth.to_json + ")", :content_type => 'application/javascript'
		//return if authorized application/json
		//{"auth":"278d425bdf160c739803:afaed3695da2ffd16931f457e338e6c9f2921fa133ce7dac49f529792be6304c","channel_data":"{\"user_id\":10,\"user_info\":{\"name\":\"Mr. Pusher\"}}"}
		//otherwise 403 Forbidden plain text
	})
}
<commit_msg>refactor<commit_after>package gusher

import (
	"fmt"
	"net/http"
)

func (h *handler) auth() http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
		if req.Method != "POST" {
			w.WriteHeader(405)
			return
		}
		callback := req.PostFormValue("callback")
		socketId := req.PostFormValue("socket_id")
		channelName := req.PostFormValue("channel_name")

		authJSON := auth(socketId, channelName)

		header := "application/json"
		payload := fmt.Sprintf("{\"auth\":\"%s\"}", authJSON)

		if callback != "" {
			header = "application/javascript"
			payload = fmt.Sprintf("%s(%s)", callback, payload)
		}

		w.Header().Set("Content-Type", header)
		fmt.Fprintf(w, payload)
		//set authTransport to 'ajax' (default)
		//POST to /pusher/auth w/ socket_id and channel_name
		//set authTransport to 'jsonp', also set authEndpoint (default to /pusher/auth)
		//JSONP to /pusher/auth w/ socket_id, channel_name and callback
		//render :text => params[:callback] + "(" + auth.to_json + ")", :content_type => 'application/javascript'
		//return if authorized application/json
		//{"auth":"278d425bdf160c739803:afaed3695da2ffd16931f457e338e6c9f2921fa133ce7dac49f529792be6304c","channel_data":"{\"user_id\":10,\"user_info\":{\"name\":\"Mr. Pusher\"}}"}
		//otherwise 403 Forbidden plain text
	})
}
<|endoftext|>


<commit_before>package logutil

import (
	"sync"
	"time"

	log "github.com/golang/glog"
)

// ThrottledLogger will allow logging of messages but won't spam the
// logs.
type ThrottledLogger struct {
	// set at construction
	name        string
	maxInterval time.Duration

	// mu protects the following members
	mu           sync.Mutex
	lastlogTime  time.Time
	skippedCount int
}

// NewThrottledLogger will create a ThrottledLogger with the given
// name and throttling interval.
func NewThrottledLogger(name string, maxInterval time.Duration) *ThrottledLogger {
	return &ThrottledLogger{
		name:        name,
		maxInterval: maxInterval,
	}
}

type logFunc func(string, ...interface{})

func (tl *ThrottledLogger) log(logF logFunc, format string, v ...interface{}) {
	now := time.Now()

	tl.mu.Lock()
	defer tl.mu.Unlock()
	logWaitTime := tl.maxInterval - (now.Sub(tl.lastlogTime))
	if logWaitTime < 0 {
		tl.lastlogTime = now
		logF(tl.name+":"+format, v...)
		return
	}
	// If this is the first message to be skipped, start a goroutine
	// to log and reset skippedCount
	if tl.skippedCount == 0 {
		go func(d time.Duration) {
			time.Sleep(d)
			tl.mu.Lock()
			defer tl.mu.Unlock()
			logF("%v: skipped %v log messages", tl.name, tl.skippedCount)
			tl.skippedCount = 0
		}(logWaitTime)
	}
	tl.skippedCount++
}

// Infof logs an info if not throttled.
func (tl *ThrottledLogger) Infof(format string, v ...interface{}) {
	tl.log(log.Infof, format, v...)
}

// Warningf logs a warning if not throttled.
func (tl *ThrottledLogger) Warningf(format string, v ...interface{}) {
	tl.log(log.Warningf, format, v...)
}

// Errorf logs an error if not throttled.
func (tl *ThrottledLogger) Errorf(format string, v ...interface{}) {
	tl.log(log.Errorf, format, v...)
}
<commit_msg>Add hook points to throttled logger to allow for other logging calls to be used.<commit_after>package logutil

import (
	"sync"
	"time"

	log "github.com/golang/glog"
)

// ThrottledLogger will allow logging of messages but won't spam the
// logs.
type ThrottledLogger struct {
	// set at construction
	name        string
	maxInterval time.Duration

	// mu protects the following members
	mu           sync.Mutex
	lastlogTime  time.Time
	skippedCount int
}

// NewThrottledLogger will create a ThrottledLogger with the given
// name and throttling interval.
func NewThrottledLogger(name string, maxInterval time.Duration) *ThrottledLogger {
	return &ThrottledLogger{
		name:        name,
		maxInterval: maxInterval,
	}
}

type logFunc func(string, ...interface{})

var (
	infof    = log.Infof
	warningf = log.Warningf
	errorf   = log.Errorf
)

func (tl *ThrottledLogger) log(logF logFunc, format string, v ...interface{}) {
	now := time.Now()

	tl.mu.Lock()
	defer tl.mu.Unlock()
	logWaitTime := tl.maxInterval - (now.Sub(tl.lastlogTime))
	if logWaitTime < 0 {
		tl.lastlogTime = now
		logF(tl.name+":"+format, v...)
		return
	}
	// If this is the first message to be skipped, start a goroutine
	// to log and reset skippedCount
	if tl.skippedCount == 0 {
		go func(d time.Duration) {
			time.Sleep(d)
			tl.mu.Lock()
			defer tl.mu.Unlock()
			logF("%v: skipped %v log messages", tl.name, tl.skippedCount)
			tl.skippedCount = 0
		}(logWaitTime)
	}
	tl.skippedCount++
}

// Infof logs an info if not throttled.
func (tl *ThrottledLogger) Infof(format string, v ...interface{}) {
	tl.log(infof, format, v...)
}

// Warningf logs a warning if not throttled.
func (tl *ThrottledLogger) Warningf(format string, v ...interface{}) {
	tl.log(warningf, format, v...)
}

// Errorf logs an error if not throttled.
func (tl *ThrottledLogger) Errorf(format string, v ...interface{}) {
	tl.log(errorf, format, v...)
}
<|endoftext|>


<commit_before>// Copyright 2013 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// Package redirect provides hooks to register HTTP handlers that redirect old
// godoc paths to their new equivalents and assist in accessing the issue
// tracker, wiki, code review system, etc.
package redirect // import "golang.org/x/tools/godoc/redirect"

import (
	"fmt"
	"net/http"
	"os"
	"regexp"
	"strconv"
	"strings"
)

// Register registers HTTP handlers that redirect old godoc paths to their new
// equivalents and assist in accessing the issue tracker, wiki, code review
// system, etc. If mux is nil it uses http.DefaultServeMux.
func Register(mux *http.ServeMux) {
	if mux == nil {
		mux = http.DefaultServeMux
	}
	handlePathRedirects(mux, pkgRedirects, "/pkg/")
	handlePathRedirects(mux, cmdRedirects, "/cmd/")
	for prefix, redirect := range prefixHelpers {
		p := "/" + prefix + "/"
		mux.Handle(p, PrefixHandler(p, redirect))
	}
	for path, redirect := range redirects {
		mux.Handle(path, Handler(redirect))
	}
	// NB: /src/pkg (sans trailing slash) is the index of packages.
	mux.HandleFunc("/src/pkg/", srcPkgHandler)
	mux.HandleFunc("/cl/", clHandler)
	mux.HandleFunc("/change/", changeHandler)
}

func handlePathRedirects(mux *http.ServeMux, redirects map[string]string, prefix string) {
	for source, target := range redirects {
		h := Handler(prefix + target + "/")
		p := prefix + source
		mux.Handle(p, h)
		mux.Handle(p+"/", h)
	}
}

// Packages that were renamed between r60 and go1.
var pkgRedirects = map[string]string{
	"asn1":              "encoding/asn1",
	"big":               "math/big",
	"cmath":             "math/cmplx",
	"csv":               "encoding/csv",
	"exec":              "os/exec",
	"exp/template/html": "html/template",
	"gob":               "encoding/gob",
	"http":              "net/http",
	"http/cgi":          "net/http/cgi",
	"http/fcgi":         "net/http/fcgi",
	"http/httptest":     "net/http/httptest",
	"http/pprof":        "net/http/pprof",
	"json":              "encoding/json",
	"mail":              "net/mail",
	"rand":              "math/rand",
	"rpc":               "net/rpc",
	"rpc/jsonrpc":       "net/rpc/jsonrpc",
	"scanner":           "text/scanner",
	"smtp":              "net/smtp",
	"tabwriter":         "text/tabwriter",
	"template":          "text/template",
	"template/parse":    "text/template/parse",
	"url":               "net/url",
	"utf16":             "unicode/utf16",
	"utf8":              "unicode/utf8",
	"xml":               "encoding/xml",
}

// Commands that were renamed between r60 and go1.
var cmdRedirects = map[string]string{
	"gofix":     "fix",
	"goinstall": "go",
	"gopack":    "pack",
	"gotest":    "go",
	"govet":     "vet",
	"goyacc":    "yacc",
}

var redirects = map[string]string{
	"/blog":       "/blog/",
	"/build":      "http://build.golang.org",
	"/change":     "https://go.googlesource.com/go",
	"/cl":         "https://go-review.googlesource.com",
	"/cmd/godoc/": "http://godoc.org/golang.org/x/tools/cmd/godoc/",
	"/cmd/vet/":   "http://godoc.org/golang.org/x/tools/cmd/vet/",
	"/issue":      "https://github.com/golang/go/issues",
	"/issue/new":  "https://github.com/golang/go/issues/new",
	"/issues":     "https://github.com/golang/go/issues",
	"/issues/new": "https://github.com/golang/go/issues/new",
	"/play":       "http://play.golang.org",

	// In Go 1.2 the references page is part of /doc/.
	"/ref": "/doc/#references",
	// This next rule clobbers /ref/spec and /ref/mem.
	// TODO(adg): figure out what to do here, if anything.
	// "/ref/": "/doc/#references",

	// Be nice to people who are looking in the wrong place.
	"/doc/mem":  "/ref/mem",
	"/doc/spec": "/ref/spec",

	"/talks": "http://talks.golang.org",
	"/tour":  "http://tour.golang.org",
	"/wiki":  "https://github.com/golang/go/wiki",

	"/doc/articles/c_go_cgo.html":                    "/blog/c-go-cgo",
	"/doc/articles/concurrency_patterns.html":        "/blog/go-concurrency-patterns-timing-out-and",
	"/doc/articles/defer_panic_recover.html":         "/blog/defer-panic-and-recover",
	"/doc/articles/error_handling.html":              "/blog/error-handling-and-go",
	"/doc/articles/gobs_of_data.html":                "/blog/gobs-of-data",
	"/doc/articles/godoc_documenting_go_code.html":   "/blog/godoc-documenting-go-code",
	"/doc/articles/gos_declaration_syntax.html":      "/blog/gos-declaration-syntax",
	"/doc/articles/image_draw.html":                  "/blog/go-imagedraw-package",
	"/doc/articles/image_package.html":               "/blog/go-image-package",
	"/doc/articles/json_and_go.html":                 "/blog/json-and-go",
	"/doc/articles/json_rpc_tale_of_interfaces.html": "/blog/json-rpc-tale-of-interfaces",
	"/doc/articles/laws_of_reflection.html":          "/blog/laws-of-reflection",
	"/doc/articles/slices_usage_and_internals.html":  "/blog/go-slices-usage-and-internals",
	"/doc/go_for_cpp_programmers.html":               "/wiki/GoForCPPProgrammers",
	"/doc/go_tutorial.html":                          "http://tour.golang.org/",
}

var prefixHelpers = map[string]string{
	"issue":  "https://github.com/golang/go/issues/",
	"issues": "https://github.com/golang/go/issues/",
	"play":   "http://play.golang.org/",
	"talks":  "http://talks.golang.org/",
	"wiki":   "https://github.com/golang/go/wiki/",
}

func Handler(target string) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		http.Redirect(w, r, target, http.StatusMovedPermanently)
	})
}

var validId = regexp.MustCompile(`^[A-Za-z0-9-]*$`)

func PrefixHandler(prefix, baseURL string) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		if p := r.URL.Path; p == prefix {
			// redirect /prefix/ to /prefix
			http.Redirect(w, r, p[:len(p)-1], http.StatusFound)
			return
		}
		id := r.URL.Path[len(prefix):]
		if !validId.MatchString(id) {
			http.Error(w, "Not found", http.StatusNotFound)
			return
		}
		target := baseURL + id
		http.Redirect(w, r, target, http.StatusFound)
	})
}

// Redirect requests from the old "/src/pkg/foo" to the new "/src/foo".
// See http://golang.org/s/go14nopkg
func srcPkgHandler(w http.ResponseWriter, r *http.Request) {
	r.URL.Path = "/src/" + r.URL.Path[len("/src/pkg/"):]
	http.Redirect(w, r, r.URL.String(), http.StatusMovedPermanently)
}

func clHandler(w http.ResponseWriter, r *http.Request) {
	const prefix = "/cl/"
	if p := r.URL.Path; p == prefix {
		// redirect /prefix/ to /prefix
		http.Redirect(w, r, p[:len(p)-1], http.StatusFound)
		return
	}
	id := r.URL.Path[len(prefix):]
	// support /cl/152700045/, which is used in commit 0edafefc36.
	id = strings.TrimSuffix(id, "/")
	if !validId.MatchString(id) {
		http.Error(w, "Not found", http.StatusNotFound)
		return
	}
	target := ""
	// the first CL in rietveld is about 152046, so only treat the id as
	// a rietveld CL if it is larger than 150000.
	if n, err := strconv.Atoi(id); err == nil && n > 150000 {
		target = "https://codereview.appspot.com/" + id
	} else {
		target = "https://go-review.googlesource.com/r/" + id
	}
	http.Redirect(w, r, target, http.StatusFound)
}

var changeMap *hashMap

// LoadChangeMap loads the specified map of Mercurial to Git revisions,
// which is used by the /change/ handler to intelligently map old hg
// revisions to their new git equivalents.
// It should be called before calling Register.
// The file should remain open as long as the process is running.
// See the implementation of this package for details.
func LoadChangeMap(filename string) error {
	f, err := os.Open(filename)
	if err != nil {
		return err
	}
	m, err := newHashMap(f)
	if err != nil {
		return err
	}
	changeMap = m
	return nil
}

func changeHandler(w http.ResponseWriter, r *http.Request) {
	const prefix = "/change/"
	if p := r.URL.Path; p == prefix {
		// redirect /prefix/ to /prefix
		http.Redirect(w, r, p[:len(p)-1], http.StatusFound)
		return
	}
	hash := r.URL.Path[len(prefix):]
	target := "https://go.googlesource.com/go/+/" + hash
	if git := changeMap.Lookup(hash); git > 0 {
		target = fmt.Sprintf("https://go.googlesource.com/%v/+/%v", git.Repo(), git.Hash())
	}
	http.Redirect(w, r, target, http.StatusFound)
}
<commit_msg>cmd/godoc: remove obsolete vet redirect<commit_after>// Copyright 2013 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// Package redirect provides hooks to register HTTP handlers that redirect old
// godoc paths to their new equivalents and assist in accessing the issue
// tracker, wiki, code review system, etc.
package redirect // import "golang.org/x/tools/godoc/redirect"

import (
	"fmt"
	"net/http"
	"os"
	"regexp"
	"strconv"
	"strings"
)

// Register registers HTTP handlers that redirect old godoc paths to their new
// equivalents and assist in accessing the issue tracker, wiki, code review
// system, etc. If mux is nil it uses http.DefaultServeMux.
func Register(mux *http.ServeMux) {
	if mux == nil {
		mux = http.DefaultServeMux
	}
	handlePathRedirects(mux, pkgRedirects, "/pkg/")
	handlePathRedirects(mux, cmdRedirects, "/cmd/")
	for prefix, redirect := range prefixHelpers {
		p := "/" + prefix + "/"
		mux.Handle(p, PrefixHandler(p, redirect))
	}
	for path, redirect := range redirects {
		mux.Handle(path, Handler(redirect))
	}
	// NB: /src/pkg (sans trailing slash) is the index of packages.
	mux.HandleFunc("/src/pkg/", srcPkgHandler)
	mux.HandleFunc("/cl/", clHandler)
	mux.HandleFunc("/change/", changeHandler)
}

func handlePathRedirects(mux *http.ServeMux, redirects map[string]string, prefix string) {
	for source, target := range redirects {
		h := Handler(prefix + target + "/")
		p := prefix + source
		mux.Handle(p, h)
		mux.Handle(p+"/", h)
	}
}

// Packages that were renamed between r60 and go1.
var pkgRedirects = map[string]string{
	"asn1":              "encoding/asn1",
	"big":               "math/big",
	"cmath":             "math/cmplx",
	"csv":               "encoding/csv",
	"exec":              "os/exec",
	"exp/template/html": "html/template",
	"gob":               "encoding/gob",
	"http":              "net/http",
	"http/cgi":          "net/http/cgi",
	"http/fcgi":         "net/http/fcgi",
	"http/httptest":     "net/http/httptest",
	"http/pprof":        "net/http/pprof",
	"json":              "encoding/json",
	"mail":              "net/mail",
	"rand":              "math/rand",
	"rpc":               "net/rpc",
	"rpc/jsonrpc":       "net/rpc/jsonrpc",
	"scanner":           "text/scanner",
	"smtp":              "net/smtp",
	"tabwriter":         "text/tabwriter",
	"template":          "text/template",
	"template/parse":    "text/template/parse",
	"url":               "net/url",
	"utf16":             "unicode/utf16",
	"utf8":              "unicode/utf8",
	"xml":               "encoding/xml",
}

// Commands that were renamed between r60 and go1.
var cmdRedirects = map[string]string{
	"gofix":     "fix",
	"goinstall": "go",
	"gopack":    "pack",
	"gotest":    "go",
	"govet":     "vet",
	"goyacc":    "yacc",
}

var redirects = map[string]string{
	"/blog":       "/blog/",
	"/build":      "http://build.golang.org",
	"/change":     "https://go.googlesource.com/go",
	"/cl":         "https://go-review.googlesource.com",
	"/cmd/godoc/": "http://godoc.org/golang.org/x/tools/cmd/godoc/",
	"/issue":      "https://github.com/golang/go/issues",
	"/issue/new":  "https://github.com/golang/go/issues/new",
	"/issues":     "https://github.com/golang/go/issues",
	"/issues/new": "https://github.com/golang/go/issues/new",
	"/play":       "http://play.golang.org",

	// In Go 1.2 the references page is part of /doc/.
	"/ref": "/doc/#references",
	// This next rule clobbers /ref/spec and /ref/mem.
	// TODO(adg): figure out what to do here, if anything.
	// "/ref/": "/doc/#references",

	// Be nice to people who are looking in the wrong place.
	"/doc/mem":  "/ref/mem",
	"/doc/spec": "/ref/spec",

	"/talks": "http://talks.golang.org",
	"/tour":  "http://tour.golang.org",
	"/wiki":  "https://github.com/golang/go/wiki",

	"/doc/articles/c_go_cgo.html":                    "/blog/c-go-cgo",
	"/doc/articles/concurrency_patterns.html":        "/blog/go-concurrency-patterns-timing-out-and",
	"/doc/articles/defer_panic_recover.html":         "/blog/defer-panic-and-recover",
	"/doc/articles/error_handling.html":              "/blog/error-handling-and-go",
	"/doc/articles/gobs_of_data.html":                "/blog/gobs-of-data",
	"/doc/articles/godoc_documenting_go_code.html":   "/blog/godoc-documenting-go-code",
	"/doc/articles/gos_declaration_syntax.html":      "/blog/gos-declaration-syntax",
	"/doc/articles/image_draw.html":                  "/blog/go-imagedraw-package",
	"/doc/articles/image_package.html":               "/blog/go-image-package",
	"/doc/articles/json_and_go.html":                 "/blog/json-and-go",
	"/doc/articles/json_rpc_tale_of_interfaces.html": "/blog/json-rpc-tale-of-interfaces",
	"/doc/articles/laws_of_reflection.html":          "/blog/laws-of-reflection",
	"/doc/articles/slices_usage_and_internals.html":  "/blog/go-slices-usage-and-internals",
	"/doc/go_for_cpp_programmers.html":               "/wiki/GoForCPPProgrammers",
	"/doc/go_tutorial.html":                          "http://tour.golang.org/",
}

var prefixHelpers = map[string]string{
	"issue":  "https://github.com/golang/go/issues/",
	"issues": "https://github.com/golang/go/issues/",
	"play":   "http://play.golang.org/",
	"talks":  "http://talks.golang.org/",
	"wiki":   "https://github.com/golang/go/wiki/",
}

func Handler(target string) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		http.Redirect(w, r, target, http.StatusMovedPermanently)
	})
}

var validId = regexp.MustCompile(`^[A-Za-z0-9-]*$`)

func PrefixHandler(prefix, baseURL string) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		if p := r.URL.Path; p == prefix {
			// redirect /prefix/ to /prefix
			http.Redirect(w, r, p[:len(p)-1], http.StatusFound)
			return
		}
		id := r.URL.Path[len(prefix):]
		if !validId.MatchString(id) {
			http.Error(w, "Not found", http.StatusNotFound)
			return
		}
		target := baseURL + id
		http.Redirect(w, r, target, http.StatusFound)
	})
}

// Redirect requests from the old "/src/pkg/foo" to the new "/src/foo".
// See http://golang.org/s/go14nopkg
func srcPkgHandler(w http.ResponseWriter, r *http.Request) {
	r.URL.Path = "/src/" + r.URL.Path[len("/src/pkg/"):]
	http.Redirect(w, r, r.URL.String(), http.StatusMovedPermanently)
}

func clHandler(w http.ResponseWriter, r *http.Request) {
	const prefix = "/cl/"
	if p := r.URL.Path; p == prefix {
		// redirect /prefix/ to /prefix
		http.Redirect(w, r, p[:len(p)-1], http.StatusFound)
		return
	}
	id := r.URL.Path[len(prefix):]
	// support /cl/152700045/, which is used in commit 0edafefc36.
	id = strings.TrimSuffix(id, "/")
	if !validId.MatchString(id) {
		http.Error(w, "Not found", http.StatusNotFound)
		return
	}
	target := ""
	// the first CL in rietveld is about 152046, so only treat the id as
	// a rietveld CL if it is larger than 150000.
	if n, err := strconv.Atoi(id); err == nil && n > 150000 {
		target = "https://codereview.appspot.com/" + id
	} else {
		target = "https://go-review.googlesource.com/r/" + id
	}
	http.Redirect(w, r, target, http.StatusFound)
}

var changeMap *hashMap

// LoadChangeMap loads the specified map of Mercurial to Git revisions,
// which is used by the /change/ handler to intelligently map old hg
// revisions to their new git equivalents.
// It should be called before calling Register.
// The file should remain open as long as the process is running.
// See the implementation of this package for details.
func LoadChangeMap(filename string) error {
	f, err := os.Open(filename)
	if err != nil {
		return err
	}
	m, err := newHashMap(f)
	if err != nil {
		return err
	}
	changeMap = m
	return nil
}

func changeHandler(w http.ResponseWriter, r *http.Request) {
	const prefix = "/change/"
	if p := r.URL.Path; p == prefix {
		// redirect /prefix/ to /prefix
		http.Redirect(w, r, p[:len(p)-1], http.StatusFound)
		return
	}
	hash := r.URL.Path[len(prefix):]
	target := "https://go.googlesource.com/go/+/" + hash
	if git := changeMap.Lookup(hash); git > 0 {
		target = fmt.Sprintf("https://go.googlesource.com/%v/+/%v", git.Repo(), git.Hash())
	}
	http.Redirect(w, r, target, http.StatusFound)
}
<|endoftext|>


<commit_before>package dataProcess

import (
	"../autils"
	"../config"
	"bytes"
	"database/sql"
	"github.com/gin-gonic/gin"
	"net/http"
	"regexp"
	"strconv"
	"strings"
	"time"
)

type detailInfo struct {
	Domian       string `json:"domain"`
	TotalPv      string `json:"totalPv"`
	Pv           string `json:"pv"`
	PvRate       string `json:"pvRate"`
	EstPv        string `json:"estPv"`
	EstPvRate    string `json:"estPvRate"`
	PatternEstPv string `json:"patternEstPv"`
	Urls         string `json:"urls"`
	RecordUrl    string `json:"recordUrl"`
	RecordRate   string `json:"recordRate"`
	PassUrl      string `json:"passUrl"`
	PassRate     string `json:"passRate"`
	RelativeUrl  string `json:"relativeUrl"`
	EffectUrl    string `json:"effectUrl"`
	EffectPv     string `json:"effectPv"`
	IneffectUrl  string `json:"ineffectUrl"`
	IneffectPv   string `json:"ineffectPv"`
	ShieldUrl    string `json:"shieldUrl"`
}

type detailData struct {
	Columns []tStruct    `json:"columns"`
	Rows    []detailInfo `json:"rows"`
	Total   int          `json:"total"`
}

var dateTotal = map[string]int{}

// 获取流量信息
func GetSDetail(c *gin.Context, db *sql.DB) {

	ts := tStruct{}
	td := detailData{}

	var FieldIdMap = map[string]string{}

	for i, v := range config.Titles {
		ts.Name = v
		ts.TextAlign = "center"
		ts.Id = config.Ids[i]
		td.Columns = append(td.Columns, ts)

		FieldIdMap[config.Ids[i]] = config.Field[i]
	}

	start := c.Query("start")
	limit := c.Query("limit")
	sortKey := c.Query("sortKey")
	sortType := c.Query("sortType")
	field := FieldIdMap[sortKey]

	var startDate string
	theDay := time.Now().AddDate(0, 0, -3)
	startDate = autils.GetCurrentData(theDay)

	q, _ := c.Get("conditions")
	_, eDate := autils.AnaDate(q)
	if eDate != "" {
		startDate = eDate
	}

	dn := autils.AnaSelect(q)

	ch := make(chan int)
	if dateTotal[startDate] == 0 {
		go getTotal(db, startDate, ch)
	}

	var domain, totalPv, pv, pvRate, /*estPv, estPvRate, patternEstPv,*/
		urls, recordUrl, recordRate, passUrl, passRate, relativeUrl, effectUrl, effectPv, ineffectUrl, ineffectPv, shieldUrl string

	var sqlStr bytes.Buffer
	sqlStr.WriteString("select " + strings.Join(config.Field, ",") + " from site_detail where date = '")
	sqlStr.WriteString(startDate)
	sqlStr.WriteString("'")
	if strings.Contains(dn, ".") {
		sqlStr.WriteString("and domain = '" + dn + "' ")
	}

	field = autils.CheckSql(field)
	if field == "" {
		field = "total_pv"
	}

	// 排序类型转换
	match, _ := regexp.MatchString("_rate", field)
	if match {
		field = "cast(" + field + " as real)"
	}

	sqlStr.WriteString(" order by " + field + " ")

	sortType = autils.CheckSql(sortType)
	if sortType == "" {
		sortType = "desc"
	}
	sqlStr.WriteString(" " + sortType + "")

	_, err := strconv.Atoi(limit)
	if err == nil {
		sqlStr.WriteString(" limit " + limit + "")
	}

	_, err = strconv.Atoi(start)
	if err == nil {
		sqlStr.WriteString(" offset " + start + "")
	}

	sqls := sqlStr.String()
	rows, err := db.Query(sqls)

	autils.ErrHadle(err)
	di := detailInfo{}
	for rows.Next() {
		err := rows.Scan(&domain, &totalPv, &pv, &pvRate, /*&estPv, &estPvRate, &patternEstPv,*/
			&urls, &recordUrl, &recordRate, &passUrl, &passRate, &relativeUrl, &effectUrl, &effectPv, &ineffectUrl, &ineffectPv, &shieldUrl)
		autils.ErrHadle(err)
		di.Domian = domain
		di.TotalPv = totalPv
		di.Pv = pv
		di.PvRate = clearZero(pvRate) + "%"
		/*di.EstPvRate = estPv
		di.EstPvRate = estPvRate
		di.PatternEstPv = patternEstPv*/
		di.Urls = urls
		di.RecordUrl = recordUrl
		di.RecordRate = clearZero(recordRate) + "%"
		di.PassUrl = passUrl
		di.PassRate = clearZero(passRate) + "%"
		di.RelativeUrl = relativeUrl
		di.EffectUrl = effectUrl
		di.EffectPv = effectPv
		di.IneffectUrl = ineffectUrl
		di.IneffectPv = ineffectPv
		di.ShieldUrl = shieldUrl
		td.Rows = append(td.Rows, di)

	}

	count := 0
	if dateTotal[startDate] == 0 {
		count = <-ch
		dateTotal[startDate] = count
	} else {
		count = dateTotal[startDate]
	}

	td.Total = count
	if strings.Contains(dn, ".") {
		td.Total = 1
	}

	err = rows.Err()
	autils.ErrHadle(err)

	defer rows.Close()

	c.JSON(http.StatusOK, gin.H{
		"status": 0,
		"msg":    "ok",
		"data":   td,
	})
}

func clearZero(s string) string {
	if strings.Contains(s, "0.0") {
		return "0"
	}
	return s
}

func getTotal(db *sql.DB, date string, ch chan int) {
	rows, err := db.Query("select count(id) from site_detail where date = '" + date + "'")

	autils.ErrHadle(err)
	count := 0
	for rows.Next() {
		err := rows.Scan(&count)
		autils.ErrHadle(err)
	}

	err = rows.Err()
	autils.ErrHadle(err)

	defer rows.Close()

	ch <- count
}
<commit_msg>delete string convert.<commit_after>package dataProcess

import (
	"../autils"
	"../config"
	"bytes"
	"database/sql"
	"github.com/gin-gonic/gin"
	"net/http"
	"regexp"
	"strconv"
	"strings"
	"time"
)

type detailInfo struct {
	Domian       string `json:"domain"`
	TotalPv      string `json:"totalPv"`
	Pv           string `json:"pv"`
	PvRate       string `json:"pvRate"`
	EstPv        string `json:"estPv"`
	EstPvRate    string `json:"estPvRate"`
	PatternEstPv string `json:"patternEstPv"`
	Urls         string `json:"urls"`
	RecordUrl    string `json:"recordUrl"`
	RecordRate   string `json:"recordRate"`
	PassUrl      string `json:"passUrl"`
	PassRate     string `json:"passRate"`
	RelativeUrl  string `json:"relativeUrl"`
	EffectUrl    string `json:"effectUrl"`
	EffectPv     string `json:"effectPv"`
	IneffectUrl  string `json:"ineffectUrl"`
	IneffectPv   string `json:"ineffectPv"`
	ShieldUrl    string `json:"shieldUrl"`
}

type detailData struct {
	Columns []tStruct    `json:"columns"`
	Rows    []detailInfo `json:"rows"`
	Total   int          `json:"total"`
}

var dateTotal = map[string]int{}

// 获取流量信息
func GetSDetail(c *gin.Context, db *sql.DB) {

	ts := tStruct{}
	td := detailData{}

	var FieldIdMap = map[string]string{}

	for i, v := range config.Titles {
		ts.Name = v
		ts.TextAlign = "center"
		ts.Id = config.Ids[i]
		td.Columns = append(td.Columns, ts)

		FieldIdMap[config.Ids[i]] = config.Field[i]
	}

	start := c.Query("start")
	limit := c.Query("limit")
	sortKey := c.Query("sortKey")
	sortType := c.Query("sortType")
	field := FieldIdMap[sortKey]

	var startDate string
	theDay := time.Now().AddDate(0, 0, -3)
	startDate = autils.GetCurrentData(theDay)

	q, _ := c.Get("conditions")
	_, eDate := autils.AnaDate(q)
	if eDate != "" {
		startDate = eDate
	}

	dn := autils.AnaSelect(q)

	ch := make(chan int)
	if dateTotal[startDate] == 0 {
		go getTotal(db, startDate, ch)
	}

	var domain, totalPv, pv, pvRate, /*estPv, estPvRate, patternEstPv,*/
		urls, recordUrl, recordRate, passUrl, passRate, relativeUrl, effectUrl, effectPv, ineffectUrl, ineffectPv, shieldUrl string

	var sqlStr bytes.Buffer
	sqlStr.WriteString("select " + strings.Join(config.Field, ",") + " from site_detail where date = '")
	sqlStr.WriteString(startDate)
	sqlStr.WriteString("'")
	if strings.Contains(dn, ".") {
		sqlStr.WriteString("and domain = '" + dn + "' ")
	}

	field = autils.CheckSql(field)
	if field == "" {
		field = "total_pv"
	}

	// 排序类型转换
	/*
	match, _ := regexp.MatchString("_rate", field)
	if match {
		field = "cast(" + field + " as real)"
	}
	*/

	sqlStr.WriteString(" order by " + field + " ")

	sortType = autils.CheckSql(sortType)
	if sortType == "" {
		sortType = "desc"
	}
	sqlStr.WriteString(" " + sortType + "")

	_, err := strconv.Atoi(limit)
	if err == nil {
		sqlStr.WriteString(" limit " + limit + "")
	}

	_, err = strconv.Atoi(start)
	if err == nil {
		sqlStr.WriteString(" offset " + start + "")
	}

	sqls := sqlStr.String()
	rows, err := db.Query(sqls)

	autils.ErrHadle(err)
	di := detailInfo{}
	for rows.Next() {
		err := rows.Scan(&domain, &totalPv, &pv, &pvRate, /*&estPv, &estPvRate, &patternEstPv,*/
			&urls, &recordUrl, &recordRate, &passUrl, &passRate, &relativeUrl, &effectUrl, &effectPv, &ineffectUrl, &ineffectPv, &shieldUrl)
		autils.ErrHadle(err)
		di.Domian = domain
		di.TotalPv = totalPv
		di.Pv = pv
		di.PvRate = clearZero(pvRate) + "%"
		/*di.EstPvRate = estPv
		di.EstPvRate = estPvRate
		di.PatternEstPv = patternEstPv*/
		di.Urls = urls
		di.RecordUrl = recordUrl
		di.RecordRate = clearZero(recordRate) + "%"
		di.PassUrl = passUrl
		di.PassRate = clearZero(passRate) + "%"
		di.RelativeUrl = relativeUrl
		di.EffectUrl = effectUrl
		di.EffectPv = effectPv
		di.IneffectUrl = ineffectUrl
		di.IneffectPv = ineffectPv
		di.ShieldUrl = shieldUrl
		td.Rows = append(td.Rows, di)

	}

	count := 0
	if dateTotal[startDate] == 0 {
		count = <-ch
		dateTotal[startDate] = count
	} else {
		count = dateTotal[startDate]
	}

	td.Total = count
	if strings.Contains(dn, ".") {
		td.Total = 1
	}

	err = rows.Err()
	autils.ErrHadle(err)

	defer rows.Close()

	c.JSON(http.StatusOK, gin.H{
		"status": 0,
		"msg":    "ok",
		"data":   td,
	})
}

func clearZero(s string) string {
	if strings.Contains(s, "0.0") {
		return "0"
	}
	return s
}

func getTotal(db *sql.DB, date string, ch chan int) {
	rows, err := db.Query("select count(id) from site_detail where date = '" + date + "'")

	autils.ErrHadle(err)
	count := 0
	for rows.Next() {
		err := rows.Scan(&count)
		autils.ErrHadle(err)
	}

	err = rows.Err()
	autils.ErrHadle(err)

	defer rows.Close()

	ch <- count
}
<|endoftext|>


<commit_before>package mmm

import (
	"testing"
	"unsafe"
)

// -----------------------------------------------------------------------------

func TestBytes_BytesOf_bool(t *testing.T) {
	var v bool = true
	size := unsafe.Sizeof(v)
	bytes := make([]byte, size)

	if err := BytesOf(v, bytes); err != nil {
		t.Error(err)
	}

	rv := *((*bool)(unsafe.Pointer(&(bytes[0]))))
	if v != rv {
		t.Error("invalid bytes for bool")
	}
}

func TestBytes_BytesOf_int(t *testing.T) {
	var v int = 42
	size := unsafe.Sizeof(v)
	bytes := make([]byte, size)

	if err := BytesOf(v, bytes); err != nil {
		t.Error(err)
	}

	rv := *((*int)(unsafe.Pointer(&(bytes[0]))))
	if v != rv {
		t.Error("invalid bytes for int")
	}
}

func TestBytes_BytesOf_int8(t *testing.T) {
	var v int8 = 42
	size := unsafe.Sizeof(v)
	bytes := make([]byte, size)

	if err := BytesOf(v, bytes); err != nil {
		t.Error(err)
	}

	rv := *((*int8)(unsafe.Pointer(&(bytes[0]))))
	if v != rv {
		t.Error("invalid bytes for int8")
	}
}

func TestBytes_BytesOf_int16(t *testing.T) {
	var v int16 = 42
	size := unsafe.Sizeof(v)
	bytes := make([]byte, size)

	if err := BytesOf(v, bytes); err != nil {
		t.Error(err)
	}

	rv := *((*int16)(unsafe.Pointer(&(bytes[0]))))
	if v != rv {
		t.Error("invalid bytes for int16")
	}
}

func TestBytes_BytesOf_int32(t *testing.T) {
	var v int32 = 42
	size := unsafe.Sizeof(v)
	bytes := make([]byte, size)

	if err := BytesOf(v, bytes); err != nil {
		t.Error(err)
	}

	rv := *((*int32)(unsafe.Pointer(&(bytes[0]))))
	if v != rv {
		t.Error("invalid bytes for int32")
	}
}

func TestBytes_BytesOf_int64(t *testing.T) {
	var v int64 = 42
	size := unsafe.Sizeof(v)
	bytes := make([]byte, size)

	if err := BytesOf(v, bytes); err != nil {
		t.Error(err)
	}

	rv := *((*int64)(unsafe.Pointer(&(bytes[0]))))
	if v != rv {
		t.Error("invalid bytes for int64")
	}
}

func TestBytes_BytesOf_uint(t *testing.T) {
	var v uint = 42
	size := unsafe.Sizeof(v)
	bytes := make([]byte, size)

	if err := BytesOf(v, bytes); err != nil {
		t.Error(err)
	}

	rv := *((*uint)(unsafe.Pointer(&(bytes[0]))))
	if v != rv {
		t.Error("invalid bytes for uint")
	}
}

func TestBytes_BytesOf_uint8(t *testing.T) {
	var v uint8 = 42
	size := unsafe.Sizeof(v)
	bytes := make([]byte, size)

	if err := BytesOf(v, bytes); err != nil {
		t.Error(err)
	}

	rv := *((*uint8)(unsafe.Pointer(&(bytes[0]))))
	if v != rv {
		t.Error("invalid bytes for uint8")
	}
}

func TestBytes_BytesOf_uint16(t *testing.T) {
	var v uint16 = 42
	size := unsafe.Sizeof(v)
	bytes := make([]byte, size)

	if err := BytesOf(v, bytes); err != nil {
		t.Error(err)
	}

	rv := *((*uint16)(unsafe.Pointer(&(bytes[0]))))
	if v != rv {
		t.Error("invalid bytes for uint16")
	}
}

func TestBytes_BytesOf_uint32(t *testing.T) {
	var v uint32 = 42
	size := unsafe.Sizeof(v)
	bytes := make([]byte, size)

	if err := BytesOf(v, bytes); err != nil {
		t.Error(err)
	}

	rv := *((*uint32)(unsafe.Pointer(&(bytes[0]))))
	if v != rv {
		t.Error("invalid bytes for uint32")
	}
}

func TestBytes_BytesOf_uint64(t *testing.T) {
	var v uint64 = 42
	size := unsafe.Sizeof(v)
	bytes := make([]byte, size)

	if err := BytesOf(v, bytes); err != nil {
		t.Error(err)
	}

	rv := *((*uint64)(unsafe.Pointer(&(bytes[0]))))
	if v != rv {
		t.Error("invalid bytes for uint64")
	}
}

func TestBytes_BytesOf_uintptr(t *testing.T) {
	var v uintptr = 42
	size := unsafe.Sizeof(v)
	bytes := make([]byte, size)

	if err := BytesOf(v, bytes); err != nil {
		t.Error(err)
	}

	rv := *((*uintptr)(unsafe.Pointer(&(bytes[0]))))
	if v != rv {
		t.Error("invalid bytes for uintptr")
	}
}

func TestBytes_BytesOf_float32(t *testing.T) {
	var v float32 = 42.7
	size := unsafe.Sizeof(v)
	bytes := make([]byte, size)

	if err := BytesOf(v, bytes); err != nil {
		t.Error(err)
	}

	rv := *((*float32)(unsafe.Pointer(&(bytes[0]))))
	if v != rv {
		t.Error("invalid bytes for float32")
	}
}

func TestBytes_BytesOf_float64(t *testing.T) {
	var v float64 = 42.7
	size := unsafe.Sizeof(v)
	bytes := make([]byte, size)

	if err := BytesOf(v, bytes); err != nil {
		t.Error(err)
	}

	rv := *((*float64)(unsafe.Pointer(&(bytes[0]))))
	if v != rv {
		t.Error("invalid bytes for float64")
	}
}

func TestBytes_BytesOf_complex64(t *testing.T) {
	var v complex64 = 42.7
	size := unsafe.Sizeof(v)
	bytes := make([]byte, size)

	if err := BytesOf(v, bytes); err != nil {
		t.Error(err)
	}

	rv := *((*complex64)(unsafe.Pointer(&(bytes[0]))))
	if v != rv {
		t.Error("invalid bytes for complex64")
	}
}

func TestBytes_BytesOf_complex128(t *testing.T) {
	var v complex128 = 42.7
	size := unsafe.Sizeof(v)
	bytes := make([]byte, size)

	if err := BytesOf(v, bytes); err != nil {
		t.Error(err)
	}

	rv := *((*complex128)(unsafe.Pointer(&(bytes[0]))))
	if v != rv {
		t.Error("invalid bytes for complex128")
	}
}

func TestBytes_BytesOf_array_int_64(t *testing.T) {
	var v [64]int
	for i := range v {
		v[i] = i
	}

	size := unsafe.Sizeof(v)
	bytes := make([]byte, size)

	if err := BytesOf(v, bytes); err != nil {
		t.Error(err)
	}

	rv := *((*[64]int)(unsafe.Pointer(&(bytes[0]))))
	if v != rv {
		t.Error("invalid bytes for [64]int")
	}
}

func TestBytes_BytesOf_array_int_312(t *testing.T) {
	var v [312]int
	for i := range v {
		v[i] = i
	}

	size := unsafe.Sizeof(v)
	bytes := make([]byte, size)

	if err := BytesOf(v, bytes); err != nil {
		t.Error(err)
	}

	rv := *((*[312]int)(unsafe.Pointer(&(bytes[0]))))
	if v != rv {
		t.Error("invalid bytes for [312]int")
	}
}

func TestBytes_BytesOf_array_int_array_27_518(t *testing.T) {
	var v [27][518]int
	for i := range v {
		for j := range v[i] {
			v[i][j] = i + i*j
		}
	}

	size := unsafe.Sizeof(v)
	bytes := make([]byte, size)

	if err := BytesOf(v, bytes); err != nil {
		t.Error(err)
	}

	rv := *((*[27][518]int)(unsafe.Pointer(&(bytes[0]))))
	if v != rv {
		t.Error("invalid bytes for [27][518]int")
	}
}
<commit_msg>added TestBytes_BytesOf_struct_small<commit_after>package mmm

import (
	"testing"
	"unsafe"
)

// -----------------------------------------------------------------------------

func TestBytes_BytesOf_bool(t *testing.T) {
	var v bool = true
	size := unsafe.Sizeof(v)
	bytes := make([]byte, size)

	if err := BytesOf(v, bytes); err != nil {
		t.Error(err)
	}

	rv := *((*bool)(unsafe.Pointer(&(bytes[0]))))
	if v != rv {
		t.Error("invalid bytes for bool")
	}
}

func TestBytes_BytesOf_int(t *testing.T) {
	var v int = 42
	size := unsafe.Sizeof(v)
	bytes := make([]byte, size)

	if err := BytesOf(v, bytes); err != nil {
		t.Error(err)
	}

	rv := *((*int)(unsafe.Pointer(&(bytes[0]))))
	if v != rv {
		t.Error("invalid bytes for int")
	}
}

func TestBytes_BytesOf_int8(t *testing.T) {
	var v int8 = 42
	size := unsafe.Sizeof(v)
	bytes := make([]byte, size)

	if err := BytesOf(v, bytes); err != nil {
		t.Error(err)
	}

	rv := *((*int8)(unsafe.Pointer(&(bytes[0]))))
	if v != rv {
		t.Error("invalid bytes for int8")
	}
}

func TestBytes_BytesOf_int16(t *testing.T) {
	var v int16 = 42
	size := unsafe.Sizeof(v)
	bytes := make([]byte, size)

	if err := BytesOf(v, bytes); err != nil {
		t.Error(err)
	}

	rv := *((*int16)(unsafe.Pointer(&(bytes[0]))))
	if v != rv {
		t.Error("invalid bytes for int16")
	}
}

func TestBytes_BytesOf_int32(t *testing.T) {
	var v int32 = 42
	size := unsafe.Sizeof(v)
	bytes := make([]byte, size)

	if err := BytesOf(v, bytes); err != nil {
		t.Error(err)
	}

	rv := *((*int32)(unsafe.Pointer(&(bytes[0]))))
	if v != rv {
		t.Error("invalid bytes for int32")
	}
}

func TestBytes_BytesOf_int64(t *testing.T) {
	var v int64 = 42
	size := unsafe.Sizeof(v)
	bytes := make([]byte, size)

	if err := BytesOf(v, bytes); err != nil {
		t.Error(err)
	}

	rv := *((*int64)(unsafe.Pointer(&(bytes[0]))))
	if v != rv {
		t.Error("invalid bytes for int64")
	}
}

func TestBytes_BytesOf_uint(t *testing.T) {
	var v uint = 42
	size := unsafe.Sizeof(v)
	bytes := make([]byte, size)

	if err := BytesOf(v, bytes); err != nil {
		t.Error(err)
	}

	rv := *((*uint)(unsafe.Pointer(&(bytes[0]))))
	if v != rv {
		t.Error("invalid bytes for uint")
	}
}

func TestBytes_BytesOf_uint8(t *testing.T) {
	var v uint8 = 42
	size := unsafe.Sizeof(v)
	bytes := make([]byte, size)

	if err := BytesOf(v, bytes); err != nil {
		t.Error(err)
	}

	rv := *((*uint8)(unsafe.Pointer(&(bytes[0]))))
	if v != rv {
		t.Error("invalid bytes for uint8")
	}
}

func TestBytes_BytesOf_uint16(t *testing.T) {
	var v uint16 = 42
	size := unsafe.Sizeof(v)
	bytes := make([]byte, size)

	if err := BytesOf(v, bytes); err != nil {
		t.Error(err)
	}

	rv := *((*uint16)(unsafe.Pointer(&(bytes[0]))))
	if v != rv {
		t.Error("invalid bytes for uint16")
	}
}

func TestBytes_BytesOf_uint32(t *testing.T) {
	var v uint32 = 42
	size := unsafe.Sizeof(v)
	bytes := make([]byte, size)

	if err := BytesOf(v, bytes); err != nil {
		t.Error(err)
	}

	rv := *((*uint32)(unsafe.Pointer(&(bytes[0]))))
	if v != rv {
		t.Error("invalid bytes for uint32")
	}
}

func TestBytes_BytesOf_uint64(t *testing.T) {
	var v uint64 = 42
	size := unsafe.Sizeof(v)
	bytes := make([]byte, size)

	if err := BytesOf(v, bytes); err != nil {
		t.Error(err)
	}

	rv := *((*uint64)(unsafe.Pointer(&(bytes[0]))))
	if v != rv {
		t.Error("invalid bytes for uint64")
	}
}

func TestBytes_BytesOf_uintptr(t *testing.T) {
	var v uintptr = 42
	size := unsafe.Sizeof(v)
	bytes := make([]byte, size)

	if err := BytesOf(v, bytes); err != nil {
		t.Error(err)
	}

	rv := *((*uintptr)(unsafe.Pointer(&(bytes[0]))))
	if v != rv {
		t.Error("invalid bytes for uintptr")
	}
}

func TestBytes_BytesOf_float32(t *testing.T) {
	var v float32 = 42.7
	size := unsafe.Sizeof(v)
	bytes := make([]byte, size)

	if err := BytesOf(v, bytes); err != nil {
		t.Error(err)
	}

	rv := *((*float32)(unsafe.Pointer(&(bytes[0]))))
	if v != rv {
		t.Error("invalid bytes for float32")
	}
}

func TestBytes_BytesOf_float64(t *testing.T) {
	var v float64 = 42.7
	size := unsafe.Sizeof(v)
	bytes := make([]byte, size)

	if err := BytesOf(v, bytes); err != nil {
		t.Error(err)
	}

	rv := *((*float64)(unsafe.Pointer(&(bytes[0]))))
	if v != rv {
		t.Error("invalid bytes for float64")
	}
}

func TestBytes_BytesOf_complex64(t *testing.T) {
	var v complex64 = 42.7
	size := unsafe.Sizeof(v)
	bytes := make([]byte, size)

	if err := BytesOf(v, bytes); err != nil {
		t.Error(err)
	}

	rv := *((*complex64)(unsafe.Pointer(&(bytes[0]))))
	if v != rv {
		t.Error("invalid bytes for complex64")
	}
}

func TestBytes_BytesOf_complex128(t *testing.T) {
	var v complex128 = 42.7
	size := unsafe.Sizeof(v)
	bytes := make([]byte, size)

	if err := BytesOf(v, bytes); err != nil {
		t.Error(err)
	}

	rv := *((*complex128)(unsafe.Pointer(&(bytes[0]))))
	if v != rv {
		t.Error("invalid bytes for complex128")
	}
}

func TestBytes_BytesOf_array_int_64(t *testing.T) {
	var v [64]int
	for i := range v {
		v[i] = i
	}

	size := unsafe.Sizeof(v)
	bytes := make([]byte, size)

	if err := BytesOf(v, bytes); err != nil {
		t.Error(err)
	}

	rv := *((*[64]int)(unsafe.Pointer(&(bytes[0]))))
	if v != rv {
		t.Error("invalid bytes for [64]int")
	}
}

func TestBytes_BytesOf_array_int_312(t *testing.T) {
	var v [312]int
	for i := range v {
		v[i] = i
	}

	size := unsafe.Sizeof(v)
	bytes := make([]byte, size)

	if err := BytesOf(v, bytes); err != nil {
		t.Error(err)
	}

	rv := *((*[312]int)(unsafe.Pointer(&(bytes[0]))))
	if v != rv {
		t.Error("invalid bytes for [312]int")
	}
}

func TestBytes_BytesOf_array_int_array_27_518(t *testing.T) {
	var v [27][518]int
	for i := range v {
		for j := range v[i] {
			v[i][j] = i + i*j
		}
	}

	size := unsafe.Sizeof(v)
	bytes := make([]byte, size)

	if err := BytesOf(v, bytes); err != nil {
		t.Error(err)
	}

	rv := *((*[27][518]int)(unsafe.Pointer(&(bytes[0]))))
	if v != rv {
		t.Error("invalid bytes for [27][518]int")
	}
}

func TestBytes_BytesOf_struct_small(t *testing.T) {
	type small struct {
		a, b complex128
		c    int8
		z    [13]bool
	}
	v := small{a: 66.6, b: 42.7, c: 3, z: [13]bool{true}}

	size := unsafe.Sizeof(v)
	bytes := make([]byte, size)

	if err := BytesOf(v, bytes); err != nil {
		t.Error(err)
	}

	rv := *((*small)(unsafe.Pointer(&(bytes[0]))))
	if v != rv {
		t.Error("invalid bytes for struct{small}")
	}
}
<|endoftext|>


<commit_before>package goutils

import (
	"testing"

	. "github.com/smartystreets/goconvey/convey"
)

func TestDecode(t *testing.T) {
	Convey("Decode", t, func() {
		var str string
		err := Decode([]byte{5, 12, 0, 2, 'a', 'b'}, &str)
		So(err, ShouldEqual, nil)
		So(str, ShouldEqual, "ab")
	})
}

func TestEncode(t *testing.T) {
	Convey("Encode", t, func() {
		bs, _ := Encode("ab")
		So(bs, ShouldResemble, []byte{5, 12, 0, 2, 'a', 'b'})
	})
}
<commit_msg>bytes test<commit_after>package goutils

import (
	"testing"

	. "github.com/smartystreets/goconvey/convey"
)

func TestDecode(t *testing.T) {
	Convey("Decode", t, func() {
		var str string
		err := Decode([]byte{5, 12, 0, 2, 'a', 'b'}, &str)
		So(err, ShouldEqual, nil)
		So(str, ShouldEqual, "ab")
		Convey("Error", func() {
			var nstr string
			err := Decode(nil, &nstr)
			So(err.Error(), ShouldEqual, "EOF")
			So(nstr, ShouldEqual, "")
		})
	})
}

func TestEncode(t *testing.T) {
	Convey("Encode", t, func() {
		bs, _ := Encode("ab")
		So(bs, ShouldResemble, []byte{5, 12, 0, 2, 'a', 'b'})
	})
}
<|endoftext|>


<commit_before>/*
 * Copyright 2019 Dgraph Labs, Inc. and Contributors
 *
 * 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 ristretto

import (
	"fmt"
	"testing"

	"github.com/dgraph-io/ristretto/bench/sim"
)

const (
	NUM_COUNTERS = 256
	BUFFER_ITEMS = NUM_COUNTERS / 4
	SAMPLE_ITEMS = NUM_COUNTERS * 8
	ZIPF_V       = 1.01
	ZIPF_S       = 2
)

func newCache(config *Config, p PolicyCreator) *Cache {
	if config.MaxCost == 0 && config.NumCounters != 0 {
		config.MaxCost = config.NumCounters
	}
	data := newStore()
	policy := p(config.NumCounters, config.MaxCost)
	if config.Log {
		policy = NewRecorder(policy, data)
	}
	return &Cache{
		data:   data,
		policy: policy,
		buffer: newRingBuffer(ringLossy, &ringConfig{
			Consumer: policy,
			Capacity: config.BufferItems,
		}),
	}
}

func BenchmarkCache(b *testing.B) {
	c := newCache(&Config{
		NumCounters: NUM_COUNTERS,
		BufferItems: BUFFER_ITEMS,
		Log:         false,
	}, newPolicy)
	c.Set("1", 1, 1)
	b.SetBytes(1)
	b.ResetTimer()
	b.RunParallel(func(pb *testing.PB) {
		for pb.Next() {
			c.Get("1")
		}
	})
}

func GenerateCacheTest(p PolicyCreator, k sim.Simulator) func(*testing.T) {
	return func(t *testing.T) {
		// create the cache with the provided policy and constant params
		cache := newCache(&Config{
			NumCounters: NUM_COUNTERS,
			BufferItems: BUFFER_ITEMS,
			Log:         true,
		}, p)
		// must iterate through SAMPLE_ITEMS because it's fixed and should be
		// much larger than the MAX_ITEMS
		for i := 0; i < SAMPLE_ITEMS; i++ {
			// generate a key from the simulator
			key, err := k()
			if err != nil {
				panic(err)
			}
			// must be a set operation for hit ratio logging
			cache.Set(fmt.Sprintf("%d", key), i, 1)
		}
		// stats is the hit ratio stats for the cache instance
		stats := cache.Log()
		// log the hit ratio
		t.Logf("------------------- %d%%\n", uint64(stats.Ratio()*100))
	}
}

type (
	policyTest struct {
		label   string
		creator PolicyCreator
	}
	accessTest struct {
		label  string
		access sim.Simulator
	}
)

func TestCache(t *testing.T) {
	// policies is a slice of all policies to test (see policy.go)
	policies := []policyTest{
		{"clairvoyant", newClairvoyant},
		{"    default", newPolicy},
	}
	// accesses is a slice of all access distributions to test (see sim package)
	accesses := []accessTest{
		{"uniform    ", sim.NewUniform(SAMPLE_ITEMS)},
		{"zipfian    ", sim.NewZipfian(ZIPF_V, ZIPF_S, SAMPLE_ITEMS)},
	}
	for _, access := range accesses {
		for _, policy := range policies {
			t.Logf("%s-%s", policy.label, access.label)
			GenerateCacheTest(policy.creator, access.access)(t)
		}
	}
}

func TestCacheBasic(t *testing.T) {
	c := NewCache(&Config{
		NumCounters: 4,
		BufferItems: 1,
	})
	if added := c.Set("1", 1, 1); !added {
		t.Fatal("set error")
	}
	if value := c.Get("1"); value.(int) != 1 {
		t.Fatal("get error")
	}
}

func TestCacheOnEvict(t *testing.T) {
	v := make([]uint64, 0)
	c := NewCache(&Config{
		NumCounters: 4,
		BufferItems: 1,
		OnEvict: func(key uint64) {
			v = append(v, key)
		},
	})
	for i := 0; i < 16; i++ {
		c.Set(fmt.Sprintf("%d", i), i, 1)
	}
	if len(v) != 12 {
		t.Fatal("onevict callback error")
	}
}

func TestCacheSetGet(t *testing.T) {
	c := NewCache(&Config{
		NumCounters: 4,
		BufferItems: 4,
	})
	for i := 0; i < 16; i++ {
		key := fmt.Sprintf("%d", i)
		if added := c.Set(key, i, 1); added {
			value := c.Get(key)
			if value == nil || value.(int) != i {
				t.Fatal("set/get error")
			}
		}
	}
}

func TestCacheSize(t *testing.T) {
	c := NewCache(&Config{
		NumCounters: 16,
		MaxCost:     16 * 4,
		BufferItems: 1,
	})
	for i := 0; i < 8; i++ {
		c.Set(fmt.Sprintf("%d", i), i, 4)
		if c.policy.Cap() < 0 {
			t.Fatal("size overflow")
		}
	}
}
<commit_msg>Remove onevict callback tests<commit_after>/*
 * Copyright 2019 Dgraph Labs, Inc. and Contributors
 *
 * 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 ristretto

import (
	"fmt"
	"testing"

	"github.com/dgraph-io/ristretto/bench/sim"
)

const (
	NUM_COUNTERS = 256
	BUFFER_ITEMS = NUM_COUNTERS / 4
	SAMPLE_ITEMS = NUM_COUNTERS * 8
	ZIPF_V       = 1.01
	ZIPF_S       = 2
)

func newCache(config *Config, p PolicyCreator) *Cache {
	if config.MaxCost == 0 && config.NumCounters != 0 {
		config.MaxCost = config.NumCounters
	}
	data := newStore()
	policy := p(config.NumCounters, config.MaxCost)
	if config.Log {
		policy = NewRecorder(policy, data)
	}
	return &Cache{
		data:   data,
		policy: policy,
		buffer: newRingBuffer(ringLossy, &ringConfig{
			Consumer: policy,
			Capacity: config.BufferItems,
		}),
	}
}

func BenchmarkCache(b *testing.B) {
	c := newCache(&Config{
		NumCounters: NUM_COUNTERS,
		BufferItems: BUFFER_ITEMS,
		Log:         false,
	}, newPolicy)
	c.Set("1", 1, 1)
	b.SetBytes(1)
	b.ResetTimer()
	b.RunParallel(func(pb *testing.PB) {
		for pb.Next() {
			c.Get("1")
		}
	})
}

func GenerateCacheTest(p PolicyCreator, k sim.Simulator) func(*testing.T) {
	return func(t *testing.T) {
		// create the cache with the provided policy and constant params
		cache := newCache(&Config{
			NumCounters: NUM_COUNTERS,
			BufferItems: BUFFER_ITEMS,
			Log:         true,
		}, p)
		// must iterate through SAMPLE_ITEMS because it's fixed and should be
		// much larger than the MAX_ITEMS
		for i := 0; i < SAMPLE_ITEMS; i++ {
			// generate a key from the simulator
			key, err := k()
			if err != nil {
				panic(err)
			}
			// must be a set operation for hit ratio logging
			cache.Set(fmt.Sprintf("%d", key), i, 1)
		}
		// stats is the hit ratio stats for the cache instance
		stats := cache.Log()
		// log the hit ratio
		t.Logf("------------------- %d%%\n", uint64(stats.Ratio()*100))
	}
}

type (
	policyTest struct {
		label   string
		creator PolicyCreator
	}
	accessTest struct {
		label  string
		access sim.Simulator
	}
)

func TestCache(t *testing.T) {
	// policies is a slice of all policies to test (see policy.go)
	policies := []policyTest{
		{"clairvoyant", newClairvoyant},
		{"    default", newPolicy},
	}
	// accesses is a slice of all access distributions to test (see sim package)
	accesses := []accessTest{
		{"uniform    ", sim.NewUniform(SAMPLE_ITEMS)},
		{"zipfian    ", sim.NewZipfian(ZIPF_V, ZIPF_S, SAMPLE_ITEMS)},
	}
	for _, access := range accesses {
		for _, policy := range policies {
			t.Logf("%s-%s", policy.label, access.label)
			GenerateCacheTest(policy.creator, access.access)(t)
		}
	}
}

func TestCacheBasic(t *testing.T) {
	c := NewCache(&Config{
		NumCounters: 4,
		BufferItems: 1,
	})
	if added := c.Set("1", 1, 1); !added {
		t.Fatal("set error")
	}
	if value := c.Get("1"); value.(int) != 1 {
		t.Fatal("get error")
	}
}

func TestCacheSetGet(t *testing.T) {
	c := NewCache(&Config{
		NumCounters: 4,
		BufferItems: 4,
	})
	for i := 0; i < 16; i++ {
		key := fmt.Sprintf("%d", i)
		if added := c.Set(key, i, 1); added {
			value := c.Get(key)
			if value == nil || value.(int) != i {
				t.Fatal("set/get error")
			}
		}
	}
}

func TestCacheSize(t *testing.T) {
	c := NewCache(&Config{
		NumCounters: 16,
		MaxCost:     16 * 4,
		BufferItems: 1,
	})
	for i := 0; i < 8; i++ {
		c.Set(fmt.Sprintf("%d", i), i, 4)
		if c.policy.Cap() < 0 {
			t.Fatal("size overflow")
		}
	}
}
<|endoftext|>


<commit_before>package sdees

import (
	"encoding/json"
	"fmt"
	"log"
	"net/http"
	"strconv"
	"strings"
	"time"
)

type GithubJson struct {
	URL             string `json:"url"`
	AssetsURL       string `json:"assets_url"`
	UploadURL       string `json:"upload_url"`
	HTMLURL         string `json:"html_url"`
	ID              int    `json:"id"`
	TagName         string `json:"tag_name"`
	TargetCommitish string `json:"target_commitish"`
	Name            string `json:"name"`
	Draft           bool   `json:"draft"`
	Author          struct {
		Login             string `json:"login"`
		ID                int    `json:"id"`
		AvatarURL         string `json:"avatar_url"`
		GravatarID        string `json:"gravatar_id"`
		URL               string `json:"url"`
		HTMLURL           string `json:"html_url"`
		FollowersURL      string `json:"followers_url"`
		FollowingURL      string `json:"following_url"`
		GistsURL          string `json:"gists_url"`
		StarredURL        string `json:"starred_url"`
		SubscriptionsURL  string `json:"subscriptions_url"`
		OrganizationsURL  string `json:"organizations_url"`
		ReposURL          string `json:"repos_url"`
		EventsURL         string `json:"events_url"`
		ReceivedEventsURL string `json:"received_events_url"`
		Type              string `json:"type"`
		SiteAdmin         bool   `json:"site_admin"`
	} `json:"author"`
	Prerelease  bool      `json:"prerelease"`
	CreatedAt   time.Time `json:"created_at"`
	PublishedAt time.Time `json:"published_at"`
	Assets      []struct {
		URL      string `json:"url"`
		ID       int    `json:"id"`
		Name     string `json:"name"`
		Label    string `json:"label"`
		Uploader struct {
			Login             string `json:"login"`
			ID                int    `json:"id"`
			AvatarURL         string `json:"avatar_url"`
			GravatarID        string `json:"gravatar_id"`
			URL               string `json:"url"`
			HTMLURL           string `json:"html_url"`
			FollowersURL      string `json:"followers_url"`
			FollowingURL      string `json:"following_url"`
			GistsURL          string `json:"gists_url"`
			StarredURL        string `json:"starred_url"`
			SubscriptionsURL  string `json:"subscriptions_url"`
			OrganizationsURL  string `json:"organizations_url"`
			ReposURL          string `json:"repos_url"`
			EventsURL         string `json:"events_url"`
			ReceivedEventsURL string `json:"received_events_url"`
			Type              string `json:"type"`
			SiteAdmin         bool   `json:"site_admin"`
		} `json:"uploader"`
		ContentType        string    `json:"content_type"`
		State              string    `json:"state"`
		Size               int       `json:"size"`
		DownloadCount      int       `json:"download_count"`
		CreatedAt          time.Time `json:"created_at"`
		UpdatedAt          time.Time `json:"updated_at"`
		BrowserDownloadURL string    `json:"browser_download_url"`
	} `json:"assets"`
	TarballURL string `json:"tarball_url"`
	ZipballURL string `json:"zipball_url"`
	Body       string `json:"body"`
}

func CheckNewVersion(version string, os string) {
	url := "https://api.github.com/repos/schollz/sdees/releases/latest"
	r, err := http.Get(url)
	if err != nil {
		log.Fatal(err)
	}
	defer r.Body.Close()
	var j GithubJson
	err = json.NewDecoder(r.Body).Decode(&j)
	if err != nil {
		log.Fatal(err)
	}
	newVersion := j.TagName
	versions := strings.Split(newVersion, ".")
	if len(versions) != 3 {
		return
	}
	majorMinorWeb := []int{}
	for i := 0; i < 3; i++ {
		i, _ := strconv.Atoi(versions[i])
		majorMinorWeb = append(majorMinorWeb, i)
	}
	fmt.Println(majorMinorWeb)

	versions = strings.Split(version, ".")
	if len(versions) != 3 {
		return
	}
	majorMinor := []int{}
	for i := 0; i < 3; i++ {
		i, _ := strconv.Atoi(versions[i])
		majorMinor = append(majorMinor, i)
	}
	fmt.Println(majorMinor)

	for i := range majorMinor {
		if majorMinor[i] > majorMinorWeb[i] {
			fmt.Println("New version available")
			break
		}
	}

	fmt.Println(version, os)

}
<commit_msg>New version works<commit_after>package sdees

import (
	"encoding/json"
	"fmt"
	"log"
	"net/http"
	"strconv"
	"strings"
	"time"
)

type GithubJson struct {
	URL             string `json:"url"`
	AssetsURL       string `json:"assets_url"`
	UploadURL       string `json:"upload_url"`
	HTMLURL         string `json:"html_url"`
	ID              int    `json:"id"`
	TagName         string `json:"tag_name"`
	TargetCommitish string `json:"target_commitish"`
	Name            string `json:"name"`
	Draft           bool   `json:"draft"`
	Author          struct {
		Login             string `json:"login"`
		ID                int    `json:"id"`
		AvatarURL         string `json:"avatar_url"`
		GravatarID        string `json:"gravatar_id"`
		URL               string `json:"url"`
		HTMLURL           string `json:"html_url"`
		FollowersURL      string `json:"followers_url"`
		FollowingURL      string `json:"following_url"`
		GistsURL          string `json:"gists_url"`
		StarredURL        string `json:"starred_url"`
		SubscriptionsURL  string `json:"subscriptions_url"`
		OrganizationsURL  string `json:"organizations_url"`
		ReposURL          string `json:"repos_url"`
		EventsURL         string `json:"events_url"`
		ReceivedEventsURL string `json:"received_events_url"`
		Type              string `json:"type"`
		SiteAdmin         bool   `json:"site_admin"`
	} `json:"author"`
	Prerelease  bool      `json:"prerelease"`
	CreatedAt   time.Time `json:"created_at"`
	PublishedAt time.Time `json:"published_at"`
	Assets      []struct {
		URL      string `json:"url"`
		ID       int    `json:"id"`
		Name     string `json:"name"`
		Label    string `json:"label"`
		Uploader struct {
			Login             string `json:"login"`
			ID                int    `json:"id"`
			AvatarURL         string `json:"avatar_url"`
			GravatarID        string `json:"gravatar_id"`
			URL               string `json:"url"`
			HTMLURL           string `json:"html_url"`
			FollowersURL      string `json:"followers_url"`
			FollowingURL      string `json:"following_url"`
			GistsURL          string `json:"gists_url"`
			StarredURL        string `json:"starred_url"`
			SubscriptionsURL  string `json:"subscriptions_url"`
			OrganizationsURL  string `json:"organizations_url"`
			ReposURL          string `json:"repos_url"`
			EventsURL         string `json:"events_url"`
			ReceivedEventsURL string `json:"received_events_url"`
			Type              string `json:"type"`
			SiteAdmin         bool   `json:"site_admin"`
		} `json:"uploader"`
		ContentType        string    `json:"content_type"`
		State              string    `json:"state"`
		Size               int       `json:"size"`
		DownloadCount      int       `json:"download_count"`
		CreatedAt          time.Time `json:"created_at"`
		UpdatedAt          time.Time `json:"updated_at"`
		BrowserDownloadURL string    `json:"browser_download_url"`
	} `json:"assets"`
	TarballURL string `json:"tarball_url"`
	ZipballURL string `json:"zipball_url"`
	Body       string `json:"body"`
}

func CheckNewVersion(version string, os string) {
	url := "https://api.github.com/repos/schollz/sdees/releases/latest"
	r, err := http.Get(url)
	if err != nil {
		log.Fatal(err)
	}
	defer r.Body.Close()
	var j GithubJson
	err = json.NewDecoder(r.Body).Decode(&j)
	if err != nil {
		log.Fatal(err)
	}
	newVersion := j.TagName
	versions := strings.Split(newVersion, ".")
	if len(versions) != 3 {
		return
	}
	majorMinorWeb := []int{}
	for i := 0; i < 3; i++ {
		i, _ := strconv.Atoi(versions[i])
		majorMinorWeb = append(majorMinorWeb, i)
	}
	fmt.Println(majorMinorWeb)

	versions = strings.Split(version, ".")
	if len(versions) != 3 {
		return
	}
	majorMinor := []int{}
	for i := 0; i < 3; i++ {
		i, _ := strconv.Atoi(versions[i])
		majorMinor = append(majorMinor, i)
	}
	fmt.Println(majorMinor)

	newVersionAvailable := false
	for i := range majorMinor {
		if majorMinor[i] > majorMinorWeb[i] {
			break
		}
		newVersionAvailable = true
	}

	fmt.Println(version, os, newVersionAvailable)

}
<|endoftext|>