<commit_before>/*
Copyright 2014 The Kubernetes Authors.

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 portforward

import (
	"context"
	"fmt"
	"net/http"
	"net/url"
	"os"
	"os/signal"
	"strconv"
	"strings"
	"time"

	"github.com/spf13/cobra"

	corev1 "k8s.io/api/core/v1"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/cli-runtime/pkg/genericclioptions"
	"k8s.io/client-go/kubernetes/scheme"
	corev1client "k8s.io/client-go/kubernetes/typed/core/v1"
	restclient "k8s.io/client-go/rest"
	"k8s.io/client-go/tools/portforward"
	"k8s.io/client-go/transport/spdy"
	cmdutil "k8s.io/kubectl/pkg/cmd/util"
	"k8s.io/kubectl/pkg/polymorphichelpers"
	"k8s.io/kubectl/pkg/util"
	"k8s.io/kubectl/pkg/util/i18n"
	"k8s.io/kubectl/pkg/util/templates"
)

// PortForwardOptions contains all the options for running the port-forward cli command.
type PortForwardOptions struct {
	Namespace     string
	PodName       string
	RESTClient    *restclient.RESTClient
	Config        *restclient.Config
	PodClient     corev1client.PodsGetter
	Address       []string
	Ports         []string
	PortForwarder portForwarder
	StopChannel   chan struct{}
	ReadyChannel  chan struct{}
}

var (
	portforwardLong = templates.LongDesc(i18n.T(`
                Forward one or more local ports to a pod. This command requires the node to have 'socat' installed.

                Use resource type/name such as deployment/mydeployment to select a pod. Resource type defaults to 'pod' if omitted.

                If there are multiple pods matching the criteria, a pod will be selected automatically. The
                forwarding session ends when the selected pod terminates, and rerun of the command is needed
                to resume forwarding.`))

	portforwardExample = templates.Examples(i18n.T(`
		# Listen on ports 5000 and 6000 locally, forwarding data to/from ports 5000 and 6000 in the pod
		kubectl port-forward pod/mypod 5000 6000

		# Listen on ports 5000 and 6000 locally, forwarding data to/from ports 5000 and 6000 in a pod selected by the deployment
		kubectl port-forward deployment/mydeployment 5000 6000

		# Listen on ports 5000 and 6000 locally, forwarding data to/from ports 5000 and 6000 in a pod selected by the service
		kubectl port-forward service/myservice 5000 6000

		# Listen on port 8888 locally, forwarding to 5000 in the pod
		kubectl port-forward pod/mypod 8888:5000

		# Listen on port 8888 on all addresses, forwarding to 5000 in the pod
		kubectl port-forward --address 0.0.0.0 pod/mypod 8888:5000

		# Listen on port 8888 on localhost and selected IP, forwarding to 5000 in the pod
		kubectl port-forward --address localhost,10.19.21.23 pod/mypod 8888:5000

		# Listen on a random port locally, forwarding to 5000 in the pod
		kubectl port-forward pod/mypod :5000`))
)

const (
	// Amount of time to wait until at least one pod is running
	defaultPodPortForwardWaitTimeout = 60 * time.Second
)

func NewCmdPortForward(f cmdutil.Factory, streams genericclioptions.IOStreams) *cobra.Command {
	opts := &PortForwardOptions{
		PortForwarder: &defaultPortForwarder{
			IOStreams: streams,
		},
	}
	cmd := &cobra.Command{
		Use:                   "port-forward TYPE/NAME [options] [LOCAL_PORT:]REMOTE_PORT [...[LOCAL_PORT_N:]REMOTE_PORT_N]",
		DisableFlagsInUseLine: true,
		Short:                 i18n.T("Forward one or more local ports to a pod"),
		Long:                  portforwardLong,
		Example:               portforwardExample,
		Run: func(cmd *cobra.Command, args []string) {
			if err := opts.Complete(f, cmd, args); err != nil {
				cmdutil.CheckErr(err)
			}
			if err := opts.Validate(); err != nil {
				cmdutil.CheckErr(cmdutil.UsageErrorf(cmd, "%v", err.Error()))
			}
			if err := opts.RunPortForward(); err != nil {
				cmdutil.CheckErr(err)
			}
		},
	}
	cmdutil.AddPodRunningTimeoutFlag(cmd, defaultPodPortForwardWaitTimeout)
	cmd.Flags().StringSliceVar(&opts.Address, "address", []string{"localhost"}, "Addresses to listen on (comma separated). Only accepts IP addresses or localhost as a value. When localhost is supplied, kubectl will try to bind on both 127.0.0.1 and ::1 and will fail if neither of these addresses are available to bind.")
	// TODO support UID
	return cmd
}

type portForwarder interface {
	ForwardPorts(method string, url *url.URL, opts PortForwardOptions) error
}

type defaultPortForwarder struct {
	genericclioptions.IOStreams
}

func (f *defaultPortForwarder) ForwardPorts(method string, url *url.URL, opts PortForwardOptions) error {
	transport, upgrader, err := spdy.RoundTripperFor(opts.Config)
	if err != nil {
		return err
	}
	dialer := spdy.NewDialer(upgrader, &http.Client{Transport: transport}, method, url)
	fw, err := portforward.NewOnAddresses(dialer, opts.Address, opts.Ports, opts.StopChannel, opts.ReadyChannel, f.Out, f.ErrOut)
	if err != nil {
		return err
	}
	return fw.ForwardPorts()
}

// splitPort splits port string which is in form of [LOCAL PORT]:REMOTE PORT
// and returns local and remote ports separately
func splitPort(port string) (local, remote string) {
	parts := strings.Split(port, ":")
	if len(parts) == 2 {
		return parts[0], parts[1]
	}

	return parts[0], parts[0]
}

// Translates service port to target port
// It rewrites ports as needed if the Service port declares targetPort.
// It returns an error when a named targetPort can't find a match in the pod, or the Service did not declare
// the port.
func translateServicePortToTargetPort(ports []string, svc corev1.Service, pod corev1.Pod) ([]string, error) {
	var translated []string
	for _, port := range ports {
		localPort, remotePort := splitPort(port)

		portnum, err := strconv.Atoi(remotePort)
		if err != nil {
			svcPort, err := util.LookupServicePortNumberByName(svc, remotePort)
			if err != nil {
				return nil, err
			}
			portnum = int(svcPort)

			if localPort == remotePort {
				localPort = strconv.Itoa(portnum)
			}
		}
		containerPort, err := util.LookupContainerPortNumberByServicePort(svc, pod, int32(portnum))
		if err != nil {
			// can't resolve a named port, or Service did not declare this port, return an error
			return nil, err
		}

		// should fail when localPort is empty (=> use random local port)
		localportnum, err := strconv.Atoi(localPort)

		if int32(portnum) != containerPort || localPort == "" || (int32(localportnum) != containerPort && err == nil) {
			translated = append(translated, fmt.Sprintf("%s:%d", localPort, containerPort))
		} else {
			translated = append(translated, fmt.Sprintf("%d", containerPort))
		}
	}
	return translated, nil
}

// convertPodNamedPortToNumber converts named ports into port numbers
// It returns an error when a named port can't be found in the pod containers
func convertPodNamedPortToNumber(ports []string, pod corev1.Pod) ([]string, error) {
	var converted []string
	for _, port := range ports {
		localPort, remotePort := splitPort(port)

		containerPortStr := remotePort
		_, err := strconv.Atoi(remotePort)
		if err != nil {
			containerPort, err := util.LookupContainerPortNumberByName(pod, remotePort)
			if err != nil {
				return nil, err
			}

			containerPortStr = strconv.Itoa(int(containerPort))
		}

		if localPort != remotePort {
			converted = append(converted, fmt.Sprintf("%s:%s", localPort, containerPortStr))
		} else {
			converted = append(converted, containerPortStr)
		}
	}

	return converted, nil
}

// Complete completes all the required options for port-forward cmd.
func (o *PortForwardOptions) Complete(f cmdutil.Factory, cmd *cobra.Command, args []string) error {
	var err error
	if len(args) < 2 {
		return cmdutil.UsageErrorf(cmd, "TYPE/NAME and list of ports are required for port-forward")
	}

	o.Namespace, _, err = f.ToRawKubeConfigLoader().Namespace()
	if err != nil {
		return err
	}

	builder := f.NewBuilder().
		WithScheme(scheme.Scheme, scheme.Scheme.PrioritizedVersionsAllGroups()...).
		ContinueOnError().
		NamespaceParam(o.Namespace).DefaultNamespace()

	getPodTimeout, err := cmdutil.GetPodRunningTimeoutFlag(cmd)
	if err != nil {
		return cmdutil.UsageErrorf(cmd, err.Error())
	}

	resourceName := args[0]
	builder.ResourceNames("pods", resourceName)

	obj, err := builder.Do().Object()
	if err != nil {
		return err
	}

	forwardablePod, err := polymorphichelpers.AttachablePodForObjectFn(f, obj, getPodTimeout)
	if err != nil {
		return err
	}

	o.PodName = forwardablePod.Name

	// handle service port mapping to target port if needed
	switch t := obj.(type) {
	case *corev1.Service:
		o.Ports, err = translateServicePortToTargetPort(args[1:], *t, *forwardablePod)
		if err != nil {
			return err
		}
	default:
		o.Ports, err = convertPodNamedPortToNumber(args[1:], *forwardablePod)
		if err != nil {
			return err
		}
	}

	clientset, err := f.KubernetesClientSet()
	if err != nil {
		return err
	}

	o.PodClient = clientset.CoreV1()

	o.Config, err = f.ToRESTConfig()
	if err != nil {
		return err
	}
	o.RESTClient, err = f.RESTClient()
	if err != nil {
		return err
	}

	o.StopChannel = make(chan struct{}, 1)
	o.ReadyChannel = make(chan struct{})
	return nil
}

// Validate validates all the required options for port-forward cmd.
func (o PortForwardOptions) Validate() error {
	if len(o.PodName) == 0 {
		return fmt.Errorf("pod name or resource type/name must be specified")
	}

	if len(o.Ports) < 1 {
		return fmt.Errorf("at least 1 PORT is required for port-forward")
	}

	if o.PortForwarder == nil || o.PodClient == nil || o.RESTClient == nil || o.Config == nil {
		return fmt.Errorf("client, client config, restClient, and portforwarder must be provided")
	}
	return nil
}

// RunPortForward implements all the necessary functionality for port-forward cmd.
func (o PortForwardOptions) RunPortForward() error {
	pod, err := o.PodClient.Pods(o.Namespace).Get(context.TODO(), o.PodName, metav1.GetOptions{})
	if err != nil {
		return err
	}

	if pod.Status.Phase != corev1.PodRunning {
		return fmt.Errorf("unable to forward port because pod is not running. Current status=%v", pod.Status.Phase)
	}

	signals := make(chan os.Signal, 1)
	signal.Notify(signals, os.Interrupt)
	defer signal.Stop(signals)

	go func() {
		<-signals
		if o.StopChannel != nil {
			close(o.StopChannel)
		}
	}()

	req := o.RESTClient.Post().
		Resource("pods").
		Namespace(o.Namespace).
		Name(pod.Name).
		SubResource("portforward")

	return o.PortForwarder.ForwardPorts("POST", req.URL(), o)
}
<commit_msg>Simplify dual or single port logic<commit_after>/*
Copyright 2014 The Kubernetes Authors.

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 portforward

import (
	"context"
	"fmt"
	"net/http"
	"net/url"
	"os"
	"os/signal"
	"strconv"
	"strings"
	"time"

	"github.com/spf13/cobra"

	corev1 "k8s.io/api/core/v1"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/cli-runtime/pkg/genericclioptions"
	"k8s.io/client-go/kubernetes/scheme"
	corev1client "k8s.io/client-go/kubernetes/typed/core/v1"
	restclient "k8s.io/client-go/rest"
	"k8s.io/client-go/tools/portforward"
	"k8s.io/client-go/transport/spdy"
	cmdutil "k8s.io/kubectl/pkg/cmd/util"
	"k8s.io/kubectl/pkg/polymorphichelpers"
	"k8s.io/kubectl/pkg/util"
	"k8s.io/kubectl/pkg/util/i18n"
	"k8s.io/kubectl/pkg/util/templates"
)

// PortForwardOptions contains all the options for running the port-forward cli command.
type PortForwardOptions struct {
	Namespace     string
	PodName       string
	RESTClient    *restclient.RESTClient
	Config        *restclient.Config
	PodClient     corev1client.PodsGetter
	Address       []string
	Ports         []string
	PortForwarder portForwarder
	StopChannel   chan struct{}
	ReadyChannel  chan struct{}
}

var (
	portforwardLong = templates.LongDesc(i18n.T(`
                Forward one or more local ports to a pod. This command requires the node to have 'socat' installed.

                Use resource type/name such as deployment/mydeployment to select a pod. Resource type defaults to 'pod' if omitted.

                If there are multiple pods matching the criteria, a pod will be selected automatically. The
                forwarding session ends when the selected pod terminates, and rerun of the command is needed
                to resume forwarding.`))

	portforwardExample = templates.Examples(i18n.T(`
		# Listen on ports 5000 and 6000 locally, forwarding data to/from ports 5000 and 6000 in the pod
		kubectl port-forward pod/mypod 5000 6000

		# Listen on ports 5000 and 6000 locally, forwarding data to/from ports 5000 and 6000 in a pod selected by the deployment
		kubectl port-forward deployment/mydeployment 5000 6000

		# Listen on ports 5000 and 6000 locally, forwarding data to/from ports 5000 and 6000 in a pod selected by the service
		kubectl port-forward service/myservice 5000 6000

		# Listen on port 8888 locally, forwarding to 5000 in the pod
		kubectl port-forward pod/mypod 8888:5000

		# Listen on port 8888 on all addresses, forwarding to 5000 in the pod
		kubectl port-forward --address 0.0.0.0 pod/mypod 8888:5000

		# Listen on port 8888 on localhost and selected IP, forwarding to 5000 in the pod
		kubectl port-forward --address localhost,10.19.21.23 pod/mypod 8888:5000

		# Listen on a random port locally, forwarding to 5000 in the pod
		kubectl port-forward pod/mypod :5000`))
)

const (
	// Amount of time to wait until at least one pod is running
	defaultPodPortForwardWaitTimeout = 60 * time.Second
)

func NewCmdPortForward(f cmdutil.Factory, streams genericclioptions.IOStreams) *cobra.Command {
	opts := &PortForwardOptions{
		PortForwarder: &defaultPortForwarder{
			IOStreams: streams,
		},
	}
	cmd := &cobra.Command{
		Use:                   "port-forward TYPE/NAME [options] [LOCAL_PORT:]REMOTE_PORT [...[LOCAL_PORT_N:]REMOTE_PORT_N]",
		DisableFlagsInUseLine: true,
		Short:                 i18n.T("Forward one or more local ports to a pod"),
		Long:                  portforwardLong,
		Example:               portforwardExample,
		Run: func(cmd *cobra.Command, args []string) {
			if err := opts.Complete(f, cmd, args); err != nil {
				cmdutil.CheckErr(err)
			}
			if err := opts.Validate(); err != nil {
				cmdutil.CheckErr(cmdutil.UsageErrorf(cmd, "%v", err.Error()))
			}
			if err := opts.RunPortForward(); err != nil {
				cmdutil.CheckErr(err)
			}
		},
	}
	cmdutil.AddPodRunningTimeoutFlag(cmd, defaultPodPortForwardWaitTimeout)
	cmd.Flags().StringSliceVar(&opts.Address, "address", []string{"localhost"}, "Addresses to listen on (comma separated). Only accepts IP addresses or localhost as a value. When localhost is supplied, kubectl will try to bind on both 127.0.0.1 and ::1 and will fail if neither of these addresses are available to bind.")
	// TODO support UID
	return cmd
}

type portForwarder interface {
	ForwardPorts(method string, url *url.URL, opts PortForwardOptions) error
}

type defaultPortForwarder struct {
	genericclioptions.IOStreams
}

func (f *defaultPortForwarder) ForwardPorts(method string, url *url.URL, opts PortForwardOptions) error {
	transport, upgrader, err := spdy.RoundTripperFor(opts.Config)
	if err != nil {
		return err
	}
	dialer := spdy.NewDialer(upgrader, &http.Client{Transport: transport}, method, url)
	fw, err := portforward.NewOnAddresses(dialer, opts.Address, opts.Ports, opts.StopChannel, opts.ReadyChannel, f.Out, f.ErrOut)
	if err != nil {
		return err
	}
	return fw.ForwardPorts()
}

// splitPort splits port string which is in form of [LOCAL PORT]:REMOTE PORT
// and returns local and remote ports separately
func splitPort(port string) (local, remote string) {
	parts := strings.Split(port, ":")
	if len(parts) == 2 {
		return parts[0], parts[1]
	}

	return parts[0], parts[0]
}

// Translates service port to target port
// It rewrites ports as needed if the Service port declares targetPort.
// It returns an error when a named targetPort can't find a match in the pod, or the Service did not declare
// the port.
func translateServicePortToTargetPort(ports []string, svc corev1.Service, pod corev1.Pod) ([]string, error) {
	var translated []string
	for _, port := range ports {
		localPort, remotePort := splitPort(port)

		portnum, err := strconv.Atoi(remotePort)
		if err != nil {
			svcPort, err := util.LookupServicePortNumberByName(svc, remotePort)
			if err != nil {
				return nil, err
			}
			portnum = int(svcPort)

			if localPort == remotePort {
				localPort = strconv.Itoa(portnum)
			}
		}
		containerPort, err := util.LookupContainerPortNumberByServicePort(svc, pod, int32(portnum))
		if err != nil {
			// can't resolve a named port, or Service did not declare this port, return an error
			return nil, err
		}

		// convert the resolved target port back to a string
		remotePort = strconv.Itoa(int(containerPort))

		if localPort != remotePort {
			translated = append(translated, fmt.Sprintf("%s:%s", localPort, remotePort))
		} else {
			translated = append(translated, remotePort)
		}
	}
	return translated, nil
}

// convertPodNamedPortToNumber converts named ports into port numbers
// It returns an error when a named port can't be found in the pod containers
func convertPodNamedPortToNumber(ports []string, pod corev1.Pod) ([]string, error) {
	var converted []string
	for _, port := range ports {
		localPort, remotePort := splitPort(port)

		containerPortStr := remotePort
		_, err := strconv.Atoi(remotePort)
		if err != nil {
			containerPort, err := util.LookupContainerPortNumberByName(pod, remotePort)
			if err != nil {
				return nil, err
			}

			containerPortStr = strconv.Itoa(int(containerPort))
		}

		if localPort != remotePort {
			converted = append(converted, fmt.Sprintf("%s:%s", localPort, containerPortStr))
		} else {
			converted = append(converted, containerPortStr)
		}
	}

	return converted, nil
}

// Complete completes all the required options for port-forward cmd.
func (o *PortForwardOptions) Complete(f cmdutil.Factory, cmd *cobra.Command, args []string) error {
	var err error
	if len(args) < 2 {
		return cmdutil.UsageErrorf(cmd, "TYPE/NAME and list of ports are required for port-forward")
	}

	o.Namespace, _, err = f.ToRawKubeConfigLoader().Namespace()
	if err != nil {
		return err
	}

	builder := f.NewBuilder().
		WithScheme(scheme.Scheme, scheme.Scheme.PrioritizedVersionsAllGroups()...).
		ContinueOnError().
		NamespaceParam(o.Namespace).DefaultNamespace()

	getPodTimeout, err := cmdutil.GetPodRunningTimeoutFlag(cmd)
	if err != nil {
		return cmdutil.UsageErrorf(cmd, err.Error())
	}

	resourceName := args[0]
	builder.ResourceNames("pods", resourceName)

	obj, err := builder.Do().Object()
	if err != nil {
		return err
	}

	forwardablePod, err := polymorphichelpers.AttachablePodForObjectFn(f, obj, getPodTimeout)
	if err != nil {
		return err
	}

	o.PodName = forwardablePod.Name

	// handle service port mapping to target port if needed
	switch t := obj.(type) {
	case *corev1.Service:
		o.Ports, err = translateServicePortToTargetPort(args[1:], *t, *forwardablePod)
		if err != nil {
			return err
		}
	default:
		o.Ports, err = convertPodNamedPortToNumber(args[1:], *forwardablePod)
		if err != nil {
			return err
		}
	}

	clientset, err := f.KubernetesClientSet()
	if err != nil {
		return err
	}

	o.PodClient = clientset.CoreV1()

	o.Config, err = f.ToRESTConfig()
	if err != nil {
		return err
	}
	o.RESTClient, err = f.RESTClient()
	if err != nil {
		return err
	}

	o.StopChannel = make(chan struct{}, 1)
	o.ReadyChannel = make(chan struct{})
	return nil
}

// Validate validates all the required options for port-forward cmd.
func (o PortForwardOptions) Validate() error {
	if len(o.PodName) == 0 {
		return fmt.Errorf("pod name or resource type/name must be specified")
	}

	if len(o.Ports) < 1 {
		return fmt.Errorf("at least 1 PORT is required for port-forward")
	}

	if o.PortForwarder == nil || o.PodClient == nil || o.RESTClient == nil || o.Config == nil {
		return fmt.Errorf("client, client config, restClient, and portforwarder must be provided")
	}
	return nil
}

// RunPortForward implements all the necessary functionality for port-forward cmd.
func (o PortForwardOptions) RunPortForward() error {
	pod, err := o.PodClient.Pods(o.Namespace).Get(context.TODO(), o.PodName, metav1.GetOptions{})
	if err != nil {
		return err
	}

	if pod.Status.Phase != corev1.PodRunning {
		return fmt.Errorf("unable to forward port because pod is not running. Current status=%v", pod.Status.Phase)
	}

	signals := make(chan os.Signal, 1)
	signal.Notify(signals, os.Interrupt)
	defer signal.Stop(signals)

	go func() {
		<-signals
		if o.StopChannel != nil {
			close(o.StopChannel)
		}
	}()

	req := o.RESTClient.Post().
		Resource("pods").
		Namespace(o.Namespace).
		Name(pod.Name).
		SubResource("portforward")

	return o.PortForwarder.ForwardPorts("POST", req.URL(), o)
}
<|endoftext|>


<commit_before>package cluster

import (
	"github.com/smancke/guble/protocol"

	log "github.com/Sirupsen/logrus"
	"github.com/hashicorp/memberlist"

	"errors"
	"fmt"
	"io/ioutil"
)

// Config is a struct used by the local node when creating and running the guble cluster
type Config struct {
	ID                   int
	Host                 string
	Port                 int
	Remotes              []string
	HealthScoreThreshold int
}

type MessageHandler interface {
	HandleMessage(message *protocol.Message) error
}

// Cluster is a struct for managing the `local view` of the guble cluster, as seen by a node.
type Cluster struct {
	// Pointer to a Config struct, based on which the Cluster node is created and runs.
	Config *Config

	// MessageHandler is used for dispatching messages received by this node.
	// Should be set after the node is created with New(), and before Start().
	MessageHandler MessageHandler

	memberlist *memberlist.Memberlist
	broadcasts [][]byte
}

//New returns a new instance of the cluster, created using the given Config.
func New(config *Config) (*Cluster, error) {
	c := &Cluster{Config: config}

	memberlistConfig := memberlist.DefaultLANConfig()
	memberlistConfig.Name = fmt.Sprintf("%d", config.ID)
	memberlistConfig.BindAddr = config.Host
	memberlistConfig.BindPort = config.Port
	memberlistConfig.Events = &eventDelegate{}

	//TODO Cosmin temporarily disabling any logging from memberlist, we might want to enable it again using logrus?
	memberlistConfig.LogOutput = ioutil.Discard

	memberlist, err := memberlist.Create(memberlistConfig)
	if err != nil {
		logger.WithField("error", err).Error("Error when creating the internal memberlist of the cluster")
		return nil, err
	}
	c.memberlist = memberlist
	memberlistConfig.Delegate = c
	memberlistConfig.Conflict = c
	return c, nil
}

// Start the cluster module.
func (cluster *Cluster) Start() error {
	logger.WithField("remotes", cluster.Config.Remotes).Debug("Starting Cluster")
	if cluster.MessageHandler == nil {
		errorMessage := "There should be a valid MessageHandler already set-up"
		logger.Error(errorMessage)
		return errors.New(errorMessage)
	}
	num, err := cluster.memberlist.Join(cluster.Config.Remotes)
	if err != nil {
		logger.WithField("error", err).Error("Error when this node wanted to join the cluster")
		return err
	}
	if num == 0 {
		errorMessage := "No remote hosts were successfuly contacted when this node wanted to join the cluster"
		logger.Error(errorMessage)
		return errors.New(errorMessage)
	}
	return nil
}

// Stop the cluster module.
func (cluster *Cluster) Stop() error {
	return cluster.memberlist.Shutdown()
}

// Check returns a non-nil error if the health status of the cluster (as seen by this node) is not perfect.
func (cluster *Cluster) Check() error {
	if healthScore := cluster.memberlist.GetHealthScore(); healthScore > cluster.Config.HealthScoreThreshold {
		errorMessage := "Cluster Health Score is not perfect"
		logger.WithField("healthScore", healthScore).Error(errorMessage)
		return errors.New(errorMessage)
	}
	return nil
}

// NotifyMsg is invoked each time a message is received by this node of the cluster; it decodes and dispatches the messages.
func (cluster *Cluster) NotifyMsg(msg []byte) {
	logger.WithField("msgAsBytes", msg).Debug("NotifyMsg")

	cmsg, err := decode(msg)
	if err != nil {
		logger.WithField("err", err).Error("Decoding of cluster message failed")
		return
	}
	logger.WithFields(log.Fields{
		"senderNodeID": cmsg.NodeID,
		"type":         cmsg.Type,
		"body":         string(cmsg.Body),
	}).Debug("NotifyMsg: Received cluster message")

	if cluster.MessageHandler != nil && cmsg.Type == GUBLE_MESSAGE {
		pMessage, err := protocol.ParseMessage(cmsg.Body)
		if err != nil {
			logger.WithField("err", err).Error("Parsing of guble-message contained in cluster-message failed")
			return
		}
		cluster.MessageHandler.HandleMessage(pMessage)
	}
}

func (cluster *Cluster) GetBroadcasts(overhead, limit int) [][]byte {
	b := cluster.broadcasts
	cluster.broadcasts = nil
	return b
}

func (cluster *Cluster) NodeMeta(limit int) []byte {
	return nil
}

func (cluster *Cluster) LocalState(join bool) []byte {
	return nil
}

func (cluster *Cluster) MergeRemoteState(s []byte, join bool) {
}

func (cluster *Cluster) NotifyConflict(existing, other *memberlist.Node) {
	logger.WithFields(log.Fields{
		"existing": *existing,
		"other":    *other,
	}).Panic("NotifyConflict")
}

// BroadcastString broadcasts a string to all the other nodes in the guble cluster
func (cluster *Cluster) BroadcastString(sMessage *string) error {
	logger.WithField("string", sMessage).Debug("BroadcastString")
	cMessage := &message{
		NodeID: cluster.Config.ID,
		Type:   STRING_BODY_MESSAGE,
		Body:   []byte(*sMessage),
	}
	return cluster.broadcastClusterMessage(cMessage)
}

// BroadcastMessage broadcasts a guble-protocol-message to all the other nodes in the guble cluster
func (cluster *Cluster) BroadcastMessage(pMessage *protocol.Message) error {
	logger.WithField("message", pMessage).Debug("BroadcastMessage")
	cMessage := &message{
		NodeID: cluster.Config.ID,
		Type:   GUBLE_MESSAGE,
		Body:   pMessage.Bytes(),
	}
	return cluster.broadcastClusterMessage(cMessage)
}

func (cluster *Cluster) broadcastClusterMessage(cMessage *message) error {
	logger.WithField("clusterMessage", cMessage).Debug("broadcastClusterMessage")
	cMessageBytes, err := cMessage.encode()
	if err != nil {
		logger.WithField("err", err).Error("Could not encode and send clusterMessage")
		return err
	}
	logger.WithFields(log.Fields{
		"nodeId":                cMessage.NodeID,
		"clusterMessageAsBytes": cMessageBytes,
	}).Debug("broadcastClusterMessage bytes")

	for _, node := range cluster.memberlist.Members() {
		if cluster.memberlist.LocalNode().Name != node.Name {
			logger.WithField("nodeName", node.Name).Debug("Sending cluster-message to a node")
			err := cluster.memberlist.SendToTCP(node, cMessageBytes)
			if err != nil {
				logger.WithFields(log.Fields{
					"err":  err,
					"node": node,
				}).Error("Error sending cluster-message to node")
				return err
			}
		}
	}
	return nil
}
<commit_msg>minor<commit_after>package cluster

import (
	"github.com/smancke/guble/protocol"

	log "github.com/Sirupsen/logrus"
	"github.com/hashicorp/memberlist"

	"errors"
	"fmt"
	"io/ioutil"
)

// Config is a struct used by the local node when creating and running the guble cluster
type Config struct {
	ID                   int
	Host                 string
	Port                 int
	Remotes              []string
	HealthScoreThreshold int
}

type MessageHandler interface {
	HandleMessage(message *protocol.Message) error
}

// Cluster is a struct for managing the `local view` of the guble cluster, as seen by a node.
type Cluster struct {
	// Pointer to a Config struct, based on which the Cluster node is created and runs.
	Config *Config

	// MessageHandler is used for dispatching messages received by this node.
	// Should be set after the node is created with New(), and before Start().
	MessageHandler MessageHandler

	memberlist *memberlist.Memberlist
	broadcasts [][]byte
}

//New returns a new instance of the cluster, created using the given Config.
func New(config *Config) (*Cluster, error) {
	c := &Cluster{Config: config}

	memberlistConfig := memberlist.DefaultLANConfig()
	memberlistConfig.Name = fmt.Sprintf("%d", config.ID)
	memberlistConfig.BindAddr = config.Host
	memberlistConfig.BindPort = config.Port
	memberlistConfig.Events = &eventDelegate{}

	//TODO Cosmin temporarily disabling any logging from memberlist, we might want to enable it again using logrus?
	memberlistConfig.LogOutput = ioutil.Discard

	memberlist, err := memberlist.Create(memberlistConfig)
	if err != nil {
		logger.WithField("error", err).Error("Error when creating the internal memberlist of the cluster")
		return nil, err
	}
	c.memberlist = memberlist
	memberlistConfig.Delegate = c
	memberlistConfig.Conflict = c
	return c, nil
}

// Start the cluster module.
func (cluster *Cluster) Start() error {
	logger.WithField("remotes", cluster.Config.Remotes).Debug("Starting Cluster")
	if cluster.MessageHandler == nil {
		errorMessage := "There should be a valid MessageHandler already set-up"
		logger.Error(errorMessage)
		return errors.New(errorMessage)
	}
	num, err := cluster.memberlist.Join(cluster.Config.Remotes)
	if err != nil {
		logger.WithField("error", err).Error("Error when this node wanted to join the cluster")
		return err
	}
	if num == 0 {
		errorMessage := "No remote hosts were successfuly contacted when this node wanted to join the cluster"
		logger.Error(errorMessage)
		return errors.New(errorMessage)
	}
	return nil
}

// Stop the cluster module.
func (cluster *Cluster) Stop() error {
	return cluster.memberlist.Shutdown()
}

// Check returns a non-nil error if the health status of the cluster (as seen by this node) is not perfect.
func (cluster *Cluster) Check() error {
	if healthScore := cluster.memberlist.GetHealthScore(); healthScore > cluster.Config.HealthScoreThreshold {
		errorMessage := "Cluster Health Score is not perfect"
		logger.WithField("healthScore", healthScore).Error(errorMessage)
		return errors.New(errorMessage)
	}
	return nil
}

// NotifyMsg is invoked each time a message is received by this node of the cluster; it decodes and dispatches the messages.
func (cluster *Cluster) NotifyMsg(msg []byte) {
	logger.WithField("msgAsBytes", msg).Debug("NotifyMsg")

	cmsg, err := decode(msg)
	if err != nil {
		logger.WithField("err", err).Error("Decoding of cluster message failed")
		return
	}
	logger.WithFields(log.Fields{
		"senderNodeID": cmsg.NodeID,
		"type":         cmsg.Type,
		"body":         string(cmsg.Body),
	}).Debug("NotifyMsg: Received cluster message")

	if cluster.MessageHandler != nil && cmsg.Type == GUBLE_MESSAGE {
		pMessage, err := protocol.ParseMessage(cmsg.Body)
		if err != nil {
			logger.WithField("err", err).Error("Parsing of guble-message contained in cluster-message failed")
			return
		}
		cluster.MessageHandler.HandleMessage(pMessage)
	}
}

func (cluster *Cluster) GetBroadcasts(overhead, limit int) [][]byte {
	b := cluster.broadcasts
	cluster.broadcasts = nil
	return b
}

func (cluster *Cluster) NodeMeta(limit int) []byte {
	return nil
}

func (cluster *Cluster) LocalState(join bool) []byte {
	return nil
}

func (cluster *Cluster) MergeRemoteState(s []byte, join bool) {
}

func (cluster *Cluster) NotifyConflict(existing, other *memberlist.Node) {
	logger.WithFields(log.Fields{
		"existing": *existing,
		"other":    *other,
	}).Panic("NotifyConflict")
}

// BroadcastString broadcasts a string to all the other nodes in the guble cluster
func (cluster *Cluster) BroadcastString(sMessage *string) error {
	logger.WithField("string", sMessage).Debug("BroadcastString")
	cMessage := &message{
		NodeID: cluster.Config.ID,
		Type:   STRING_BODY_MESSAGE,
		Body:   []byte(*sMessage),
	}
	return cluster.broadcastClusterMessage(cMessage)
}

// BroadcastMessage broadcasts a guble-protocol-message to all the other nodes in the guble cluster
func (cluster *Cluster) BroadcastMessage(pMessage *protocol.Message) error {
	logger.WithField("message", pMessage).Debug("BroadcastMessage")
	cMessage := &message{
		NodeID: cluster.Config.ID,
		Type:   GUBLE_MESSAGE,
		Body:   pMessage.Bytes(),
	}
	return cluster.broadcastClusterMessage(cMessage)
}

func (cluster *Cluster) broadcastClusterMessage(cMessage *message) error {
	logger.WithField("clusterMessage", cMessage).Debug("broadcastClusterMessage")
	cMessageBytes, err := cMessage.encode()
	if err != nil {
		logger.WithField("err", err).Error("Could not encode and broadcast cluster-message")
		return err
	}
	logger.WithFields(log.Fields{
		"nodeId":                cMessage.NodeID,
		"clusterMessageAsBytes": cMessageBytes,
	}).Debug("broadcastClusterMessage bytes")

	for _, node := range cluster.memberlist.Members() {
		if cluster.memberlist.LocalNode().Name != node.Name {
			logger.WithField("nodeName", node.Name).Debug("Sending cluster-message to a node")
			err := cluster.memberlist.SendToTCP(node, cMessageBytes)
			if err != nil {
				logger.WithFields(log.Fields{
					"err":  err,
					"node": node,
				}).Error("Error sending cluster-message to node")
				return err
			}
		}
	}
	return nil
}
<|endoftext|>


<commit_before>package controllers

import (
  "fmt"
  "time"
  "github.com/revel/revel"
  "godisc/app/routes"
  "godisc/app/models"
)

type Threads struct {
  App
  *revel.Controller
}

func (c Threads) checkUser() revel.Result {
  if user := c.connected(); user == nil {
    c.Flash.Error("Please log in first")

    return c.Redirect(routes.App.Index())
  }

  return nil
}

// Index shows all threads
func (c Threads) Index() revel.Result {
  results, err := c.Txn.Select(models.Thread{},
    `SELECT * FROM Thread`)
  if err != nil {
    // TODO: Catch it
    panic(err)
  }

  var threads []*models.Thread
  for _, r := range results {
    t := r.(*models.Thread)
    threads = append(threads, t)
  }

  return c.Render(threads)
}

// ShowNew shows the form for new threads
func (c Threads) ShowNew() revel.Result {
  return c.Render()
}

// New creates a new thread
func (c Threads) New(topic string) revel.Result {
  // Validate
  c.Validation.Required(topic)
  c.Validation.MinSize(topic, 3)

  if c.Validation.HasErrors() {
    c.Validation.Keep()
    c.FlashParams()
    return c.Redirect(Threads.ShowNew)
  }

  // Create thread
  user := c.connected()
  thread := &models.Thread{
    0,
    user.UserId,
    topic,
    time.Now().Unix(),
    user,
  }
  err := c.Txn.Insert(thread)
  if err != nil {
    c.Flash.Error("An error occurred, sorry")
    fmt.Println(err)
  }

  return c.Redirect(Threads.Index)
}

func (c Threads) getThread(id int) *models.Thread {
  threads, err := c.Txn.Select(models.Thread{},
    `SELECT * FROM Thread WHERE ThreadId = ?`, id)
  // TODO: Get posts

  if err != nil {
    // TODO: User not found message
    panic(err)
  }

  // No user found
  if len(threads) == 0 {
    return nil
  }

  return threads[0].(*models.Thread)
}

func (c Threads) getPosts(threadId int) []*models.Post {
  posts, err := c.Txn.Select(models.Post{},
    `SELECT * FROM Post WHERE ThreadId = ?`, threadId)

  if err != nil {
    panic(err)
  }

  if len(posts) == 0 {
    return nil
  }

  tc := []*models.Post{}
  for _, post := range posts {
    tc = append(tc, post.(*models.Post))
  }

  return tc
}

func (c Threads) Show(id int) revel.Result {
  thread := c.getThread(id)
  posts := c.getPosts(id)

  return c.Render(thread, posts)
}

func (c Threads) Post(id int, body string) revel.Result {
  if len(body) > 0 {
    thread := c.getThread(id)
    user := c.connected()
    if thread != nil && user != nil {
      post := &models.Post{
        0,
        thread.ThreadId,
        user.UserId,
        body,
        time.Now().Unix(),
        thread,
        user,
      }

      err := c.Txn.Insert(post)
      if err != nil {
        c.Flash.Error("An error occurred, sorry")
        fmt.Println(err)
      }
    }
  }

  return c.Redirect(Threads.Index)
}
<commit_msg>Finish base functionality<commit_after>package controllers

import (
  "fmt"
  "time"
  "github.com/revel/revel"
  "godisc/app/routes"
  "godisc/app/models"
)

type Threads struct {
  App
  *revel.Controller
}

func (c Threads) checkUser() revel.Result {
  if user := c.connected(); user == nil {
    c.Flash.Error("Please log in first")

    return c.Redirect(routes.App.Index())
  }

  return nil
}

// Index shows all threads
func (c Threads) Index() revel.Result {
  results, err := c.Txn.Select(models.Thread{},
    `SELECT * FROM Thread`)
  if err != nil {
    // TODO: Catch it
    panic(err)
  }

  var threads []*models.Thread
  for _, r := range results {
    t := r.(*models.Thread)
    threads = append(threads, t)
  }

  return c.Render(threads)
}

// ShowNew shows the form for new threads
func (c Threads) ShowNew() revel.Result {
  return c.Render()
}

// New creates a new thread
func (c Threads) New(topic string) revel.Result {
  // Validate
  c.Validation.Required(topic)
  c.Validation.MinSize(topic, 3)

  if c.Validation.HasErrors() {
    c.Validation.Keep()
    c.FlashParams()
    return c.Redirect(Threads.ShowNew)
  }

  // Create thread
  user := c.connected()
  thread := &models.Thread{
    0,
    user.UserId,
    topic,
    time.Now().Unix(),
    user,
  }
  err := c.Txn.Insert(thread)
  if err != nil {
    c.Flash.Error("An error occurred, sorry")
    fmt.Println(err)
  }

  return c.Redirect("/threads/%d", thread.ThreadId)
}

func (c Threads) getThread(id int) *models.Thread {
  threads, err := c.Txn.Select(models.Thread{},
    `SELECT * FROM Thread WHERE ThreadId = ?`, id)
  // TODO: Get posts

  if err != nil {
    // TODO: User not found message
    panic(err)
  }

  // No user found
  if len(threads) == 0 {
    return nil
  }

  return threads[0].(*models.Thread)
}

func (c Threads) getPosts(threadId int) []*models.Post {
  posts, err := c.Txn.Select(models.Post{},
    `SELECT * FROM Post WHERE ThreadId = ?`, threadId)

  if err != nil {
    panic(err)
  }

  if len(posts) == 0 {
    return nil
  }

  tc := []*models.Post{}
  for _, post := range posts {
    tc = append(tc, post.(*models.Post))
  }

  return tc
}

func (c Threads) Show(id int) revel.Result {
  thread := c.getThread(id)
  posts := c.getPosts(id)

  return c.Render(thread, posts)
}

func (c Threads) Post(id int, body string) revel.Result {
  if len(body) > 0 {
    thread := c.getThread(id)
    user := c.connected()
    if thread != nil && user != nil {
      post := &models.Post{
        0,
        thread.ThreadId,
        user.UserId,
        body,
        time.Now().Unix(),
        thread,
        user,
      }

      err := c.Txn.Insert(post)
      if err != nil {
        c.Flash.Error("An error occurred, sorry")
        fmt.Println(err)
      }
    }

    return c.Redirect("/threads/%d", thread.ThreadId)
  }

  return c.Todo()
}
<|endoftext|>


<commit_before>package yorm

import (
	"database/sql"
	"errors"
	"fmt"
	"reflect"
	"time"
)

type querySetter struct {
	table   string
	dests   []interface{}
	columns []column
}

type sqlScanner interface {
	Scan(dest ...interface{}) error
}

//Query do a query operation.
func Query(i interface{}, query string, args ...interface{}) error {
	typ := reflect.TypeOf(i)
	if typ.Kind() != reflect.Ptr {
		return ErrNonPtr
	}
	typ = typ.Elem()
	var err error
	var stmt *sql.Stmt
	stmt, err = getStmt(query)
	if stmt == nil {
		return err
	}
	if typ.Kind() == reflect.Slice {
		rows, err := stmt.Query(args...)
		if rows == nil {
			return err
		}
		return queryList(i, rows)
	}
	return queryOne(i, stmt.QueryRow(args...))

}

func queryOne(i interface{}, row *sql.Row) error {
	if row == nil {
		return ErrIllegalParams
	}
	return convertAssignRow(i, row)
}
func queryList(i interface{}, rows *sql.Rows) error {
	if rows == nil {
		return ErrIllegalParams
	}
	return convertAssignRows(i, rows)
}

func newQuery(ri reflect.Value) *querySetter {
	if q, ok := tableMap[ri.Kind()]; ok {
		return q
	}
	if ri.Kind() != reflect.Ptr || ri.IsNil() {
		return nil
	}
	q := new(querySetter)
	defer func() {
		tableMap[ri.Kind()] = q
	}()
	table, cs := structToTable(reflect.Indirect(ri).Interface())
	q.table = table
	q.columns = cs
	q.dests = make([]interface{}, len(cs))
	for k, v := range cs {
		q.dests[k] = newPtrInterface(v.typ)
	}
	return q
}

func newPtrInterface(t reflect.Type) interface{} {
	k := t.Kind()
	var ti interface{}
	switch k {
	case reflect.Int:
		ti = new(int)
	case reflect.Int64:
		ti = new(int64)
	case reflect.String:
		ti = new(string)
	case reflect.Struct:
		switch t {
		case reflect.TypeOf(time.Time{}):
			ti = new(string)
		}
	}
	return ti
}

func convertAssignRows(i interface{}, rows *sql.Rows) error {
	typ := reflect.TypeOf(i)
	if typ.Kind() != reflect.Ptr {
		return ErrNonPtr
	}
	typ = typ.Elem()
	if typ.Kind() != reflect.Slice {
		return ErrNonSlice
	}
	typ = typ.Elem()
	var q *querySetter
	if typ.Kind() == reflect.Struct {
		q = newQuery(reflect.New(typ))
		if q == nil {
			return errors.New("q is not support")
		}
	}
	size := 0
	v := reflect.Indirect(reflect.ValueOf(i))
	ti := newPtrInterface(typ)
	for rows.Next() {
		if size >= v.Cap() {
			newCap := v.Cap()
			if newCap == 0 {
				newCap = 1
			} else {
				if newCap < 1024 {
					newCap += newCap
				} else {
					newCap += newCap / 4
				}
			}
			nv := reflect.MakeSlice(v.Type(), v.Len(), newCap)
			reflect.Copy(nv, v)
			v.Set(nv)
		}
		if size >= v.Len() {
			v.SetLen(size + 1)
		}
		st := reflect.New(typ)
		st = st.Elem()
		if q != nil {
			err := scanValue(rows, q, st)
			if err != nil {
				return err
			}
		} else {
			rows.Scan(ti)
			st.Set(reflect.ValueOf(ti).Elem())
		}
		v.Index(size).Set(st)
		size++
	}
	return nil
}
func convertAssignRow(i interface{}, row *sql.Row) error {
	typ := reflect.TypeOf(i)

	if typ.Kind() == reflect.Ptr && typ.Elem().Kind() != reflect.Struct {
		return row.Scan(i)
	}

	q := newQuery(reflect.ValueOf(i))
	if q == nil {
		return errors.New("nil struct")
	}
	st := reflect.ValueOf(i).Elem()
	return scanValue(row, q, st)
}

func scanValue(sc sqlScanner, q *querySetter, st reflect.Value) error {
	err := sc.Scan(q.dests...)
	if err != nil {
		return err
	}
	for idx, c := range q.columns {
		// different assign func here
		switch c.typ.Kind() {
		case reflect.Int:
			st.Field(c.fieldNum).SetInt(int64(*(q.dests[idx].(*int))))
		case reflect.Int64:
			st.Field(c.fieldNum).SetInt(int64(*(q.dests[idx].(*int64))))
		case reflect.String:
			st.Field(c.fieldNum).SetString(string(*(q.dests[idx].(*string))))
		case reflect.Struct:
			switch c.typ {
			case reflect.TypeOf(time.Time{}):
				timeStr := string(*(q.dests[idx].(*string)))
				fmt.Println(timeStr)
				var layout string
				if len(timeStr) == 10 {
					layout = "2006-01-02"
				}
				if len(timeStr) == 19 {
					layout = "2006-01-02 15:04:05"
				}
				timeTime, err := time.ParseInLocation(layout, timeStr, time.Local)
				if timeTime.IsZero() {
					return err
				}
				st.Field(c.fieldNum).Set(reflect.ValueOf(timeTime))
			}
		}
	}
	return nil
}
<commit_msg>添加时间类型的解析<commit_after>package yorm

import (
	"database/sql"
	"errors"
	"fmt"
	"reflect"
	"time"
)

type querySetter struct {
	table   string
	dests   []interface{}
	columns []column
}

type sqlScanner interface {
	Scan(dest ...interface{}) error
}

//Query do a query operation.
func Query(i interface{}, query string, args ...interface{}) error {
	typ := reflect.TypeOf(i)
	if typ.Kind() != reflect.Ptr {
		return ErrNonPtr
	}
	typ = typ.Elem()
	var err error
	var stmt *sql.Stmt
	stmt, err = getStmt(query)
	if stmt == nil {
		return err
	}
	if typ.Kind() == reflect.Slice {
		rows, err := stmt.Query(args...)
		if rows == nil {
			return err
		}
		return queryList(i, rows)
	}
	return queryOne(i, stmt.QueryRow(args...))

}

func queryOne(i interface{}, row *sql.Row) error {
	if row == nil {
		return ErrIllegalParams
	}
	return convertAssignRow(i, row)
}
func queryList(i interface{}, rows *sql.Rows) error {
	if rows == nil {
		return ErrIllegalParams
	}
	return convertAssignRows(i, rows)
}

func newQuery(ri reflect.Value) *querySetter {
	if q, ok := tableMap[ri.Kind()]; ok {
		return q
	}
	if ri.Kind() != reflect.Ptr || ri.IsNil() {
		return nil
	}
	q := new(querySetter)
	defer func() {
		tableMap[ri.Kind()] = q
	}()
	table, cs := structToTable(reflect.Indirect(ri).Interface())
	q.table = table
	q.columns = cs
	q.dests = make([]interface{}, len(cs))
	for k, v := range cs {
		q.dests[k] = newPtrInterface(v.typ)
	}
	return q
}

func newPtrInterface(t reflect.Type) interface{} {
	k := t.Kind()
	var ti interface{}
	switch k {
	case reflect.Int:
		ti = new(int)
	case reflect.Int64:
		ti = new(int64)
	case reflect.String:
		ti = new(string)
	case reflect.Struct:
		switch t {
		case reflect.TypeOf(time.Time{}):
			ti = new(string)
		}
	}
	return ti
}

func convertAssignRows(i interface{}, rows *sql.Rows) error {
	typ := reflect.TypeOf(i)
	if typ.Kind() != reflect.Ptr {
		return ErrNonPtr
	}
	typ = typ.Elem()
	if typ.Kind() != reflect.Slice {
		return ErrNonSlice
	}
	typ = typ.Elem()
	var q *querySetter
	if typ.Kind() == reflect.Struct {
		q = newQuery(reflect.New(typ))
		if q == nil {
			return errors.New("q is not support")
		}
	}
	size := 0
	v := reflect.Indirect(reflect.ValueOf(i))
	ti := newPtrInterface(typ)
	for rows.Next() {
		if size >= v.Cap() {
			newCap := v.Cap()
			if newCap == 0 {
				newCap = 1
			} else {
				if newCap < 1024 {
					newCap += newCap
				} else {
					newCap += newCap / 4
				}
			}
			nv := reflect.MakeSlice(v.Type(), v.Len(), newCap)
			reflect.Copy(nv, v)
			v.Set(nv)
		}
		if size >= v.Len() {
			v.SetLen(size + 1)
		}
		st := reflect.New(typ)
		st = st.Elem()
		if q != nil {
			err := scanValue(rows, q, st)
			if err != nil {
				return err
			}
		} else {
			rows.Scan(ti)
			st.Set(reflect.ValueOf(ti).Elem())
		}
		v.Index(size).Set(st)
		size++
	}
	return nil
}
func convertAssignRow(i interface{}, row *sql.Row) error {
	typ := reflect.TypeOf(i)

	if typ.Kind() == reflect.Ptr && typ.Elem().Kind() != reflect.Struct {
		return row.Scan(i)
	}

	q := newQuery(reflect.ValueOf(i))
	if q == nil {
		return errors.New("nil struct")
	}
	st := reflect.ValueOf(i).Elem()
	return scanValue(row, q, st)
}

func scanValue(sc sqlScanner, q *querySetter, st reflect.Value) error {
	err := sc.Scan(q.dests...)
	if err != nil {
		return err
	}
	for idx, c := range q.columns {
		// different assign func here
		switch c.typ.Kind() {
		case reflect.Int:
			st.Field(c.fieldNum).SetInt(int64(*(q.dests[idx].(*int))))
		case reflect.Int64:
			st.Field(c.fieldNum).SetInt(int64(*(q.dests[idx].(*int64))))
		case reflect.String:
			st.Field(c.fieldNum).SetString(string(*(q.dests[idx].(*string))))
		case reflect.Struct:
			switch c.typ {
			case reflect.TypeOf(time.Time{}):
				timeStr := string(*(q.dests[idx].(*string)))
				var layout string
				if len(timeStr) == 10 {
					layout = "2006-01-02"
				}
				if len(timeStr) == 19 {
					layout = "2006-01-02 15:04:05"
				}
				timeTime, err := time.ParseInLocation(layout, timeStr, time.Local)
				if timeTime.IsZero() {
					return err
				}
				st.Field(c.fieldNum).Set(reflect.ValueOf(timeTime))
			}
		}
	}
	return nil
}
<|endoftext|>


<commit_before>package logberry

import (
	"bytes"
	"fmt"
	"io"
	"reflect"
	"strings"
)

// D maps capture data to be logged as key/value pairs associated with
// Tasks or particular events.
type D map[string]interface{}

// CopyFrom populates a D from the given data object.  The following
// rules are applied:
//
//   data is nil                Nothing happens.
//
//   data is a struct           Each exposed field in data is set as a
//                              key/value in x, copying the value.
//
//   data is a map              Each key/value in data is set as a
//                              key/value in x.  fmt.Sprint is used to
//                              create a string key from the original
//                              map key.  The value stored is the
//                              original value, not a copy.
//
//   otherwise                  The value data is placed within a field
//                              keyed as "value" in x.  If the field
//                              already exists and is a single value,
//                              it is replaced with a list of both
//                              values.  Otherwise, if the field is
//                              already a list, data is appended to it.
//
// If data is a pointer or interface, the construction descends to the
// object it references.
//
// The modified host x is itself returned.
func (x D) CopyFrom(data interface{}) D {

	if data == nil {
		return x
	}

	var val = reflect.ValueOf(data)

	// Chain through any pointers or interfaces
	done := false
	for !done {
		switch val.Kind() {
		case reflect.Interface:
			fallthrough
		case reflect.Ptr:

			if val.IsNil() {
				return x
			}

			val = val.Elem()

		default:
			done = true
		}
	}

	// Apply the rules listed above
	switch val.Kind() {

	case reflect.Struct:

		var vtype = val.Type()
		var haspublic bool

		for i := 0; i < val.NumField(); i++ {
			var f = val.Field(i)
			if f.IsValid() && f.CanInterface() && !strings.Contains(vtype.Field(i).Tag.Get("logberry"), "quiet") {
				x[vtype.Field(i).Name] = f.Interface()
				haspublic = true
			}
		}

		// Special case: If the value is an error but has no accessible
		// fields, call its Error() function to get a text representation.
		if err, ok := data.(error); ok {
			if !haspublic {
				x["Error"] = err.Error()
			}
		}

	case reflect.Map:
		var vals = val.MapKeys()
		for _, k := range vals {
			v := val.MapIndex(k)
			if k.CanInterface() && v.CanInterface() {
				x[fmt.Sprint(k.Interface())] = v.Interface()
			}
		}

	default:
		var prev, find = x["value"]
		if find {
			switch prev.(type) {
			case []interface{}:
				x["value"] = append(prev.([]interface{}), data)

			default:
				x["value"] = []interface{}{prev, data}
			}
		} else {
			x["value"] = data
		}

		// end switch type
	}

	return x

}

// DAggregate returns a new D object populated from the given array
// using CopyFrom().  It is up to the caller to threadsafe and
// otherwise correctly share the D.  This may matter because the
// caller is using the same D object in another goroutine, or if a
// Root or OutputDriver buffers the event and logs it asynchronously.
func DAggregate(data []interface{}) D {

	if data == nil || len(data) == 0 {
		return D{}
	}

	var accum = D{}

	for _,d := range(data) {
		accum.CopyFrom(d)
	}

	return accum

	// end DAggregate
}

// String returns a text representation of the host D.  This is
// presented as a sequence of key=value pairs for arguably
// human-readable presentation.  To produce a JSON serialization,
// simply marshal on the D as usual.  It is equivalent to casting
// output from Text().
func (x D) String() string {
	return string(x.Text())
}

// Text returns a byte slice textual representation of the host D.
// This is presented as a sequence of key=value pairs for arguably
// human-readable presentation.  To produce a JSON serialization,
// simply marshal on the D as usual.
func (x D) Text() []byte {
	var buffer bytes.Buffer
	x.WriteTo(&buffer)
	return buffer.Bytes()
}

// WriteTo serializes the host D to the given io.Writer.  This is
// presented as a sequence of key=value pairs for arguably
// human-readable presentation.  To produce a JSON serialization,
// simply marshal on the D as usual.
func (x D) WriteTo(w io.Writer) error {
	return textrecurse(w, false, x)
}

func textrecurse(buffer io.Writer, wrap bool, data interface{}) error {

	var val = reflect.ValueOf(data)

	// Chain through any pointers or interfaces
	done := false
	for !done {
		switch val.Kind() {
		case reflect.Interface:
			fallthrough
		case reflect.Ptr:

			if val.IsNil() {
				return nil
			}

			val = val.Elem()

		default:
			done = true
		}
	}

	// Handle the possibilities
	switch val.Kind() {

	case reflect.Map:
		if wrap {
			_, e := fmt.Fprint(buffer, "{")
			if e != nil {
				return e
			}
		}

		var vals = val.MapKeys()
		for _, k := range vals {
			vval := val.MapIndex(k)
			if k.CanInterface() && vval.IsValid() && vval.CanInterface() {
				_, e := fmt.Fprintf(buffer, " %s=", k.Interface())
				if e != nil {
					return e
				}

				e = textrecurse(buffer, true, vval.Interface())
				if e != nil {
					return e
				}
			}
		}

		if wrap {
			_, e := fmt.Fprint(buffer, " }")
			if e != nil {
				return e
			}
		}

	case reflect.Struct:
		if wrap {
			_, e := fmt.Fprint(buffer, "{")
			if e != nil {
				return e
			}
		}

		var vtype = val.Type()
		var haspublic bool
		for i := 0; i < val.NumField(); i++ {
			var f = val.Field(i)
			if f.IsValid() && f.CanInterface() && !strings.Contains(vtype.Field(i).Tag.Get("logberry"), "quiet") {
				_, e := fmt.Fprintf(buffer, " %s=", vtype.Field(i).Name)
				if e != nil {
					return e
				}

				e = textrecurse(buffer, true, f.Interface())
				haspublic = true
				if e != nil {
					return e
				}
			}
		}

		if err, ok := data.(error); ok {
			if !haspublic {
				_, e := fmt.Fprintf(buffer, " Message=%q", err.Error())
				if e != nil {
					return e
				}
			} else if _, ok := err.(*Error); !ok {
				_, e := fmt.Fprintf(buffer, " Type=%T", err)
				if e != nil {
					return e
				}
			}
		}

		if wrap {
			_, e := fmt.Fprint(buffer, " }")
			if e != nil {
				return e
			}
		}

	case reflect.Array:
		fallthrough
	case reflect.Slice:
		_, e := fmt.Fprint(buffer, "[")
		if e != nil {
			return e
		}

		if val.Len() > 0 {
			v := val.Index(0)
			if v.CanInterface() {
				e := textrecurse(buffer, true, v.Interface())
				if e != nil {
					return e
				}
			} else {
				_, e := fmt.Fprint(buffer, "--")
				if e != nil {
					return e
				}
			}
		}

		for i := 1; i < val.Len(); i++ {
			_, e := fmt.Fprint(buffer, ", ")
			if e != nil {
				return e
			}

			v := val.Index(i)
			if v.CanInterface() {
				e = textrecurse(buffer, true, val.Index(i).Interface())
				if e != nil {
					return e
				}
			} else {
				_, e := fmt.Fprint(buffer, "--")
				if e != nil {
					return e
				}
			}
		}

		_, e = fmt.Fprint(buffer, " ]")
		if e != nil {
			return e
		}

	case reflect.String:
		_, e := fmt.Fprintf(buffer, "%q", val.String())
		if e != nil {
			return e
		}

	default:
		if val.IsValid() && val.CanInterface() {
			_, e := fmt.Fprintf(buffer, "%v", val.Interface())
			if e != nil {
				return e
			}
		} else {
			_, e := fmt.Fprintf(buffer, "--")
			if e != nil {
				return e
			}
		}

		// end switch type
	}

	return nil

}
<commit_msg>Deep copy.<commit_after>package logberry

import (
	"bytes"
	"fmt"
	"io"
	"reflect"
	"strings"
)

// D is a convenience type to be used for quickly structuring simple
// data to be logged.
type D map[string]interface{}

func rolldown(data interface{}) (reflect.Value,bool) {

	if data == nil {
		return reflect.Value{},true
	}

	val := reflect.ValueOf(data)

	// Chain through any pointers or interfaces
	done := false
	for !done {
		switch val.Kind() {
		case reflect.Interface:
			fallthrough
		case reflect.Ptr:

			if val.IsNil() {
				return reflect.Value{},true
			}

			val = val.Elem()

		default:
			done = true
		}
	}

	return val, false

}

// CopyFrom populates a D from the given data object.  The following
// rules are applied:
//
//   data is nil                Nothing happens.
//
//   data is a struct           Each exposed field in data is set as a
//                              key/value in x, copying the value.
//
//   data is a map              Each key/value in data is set as a
//                              key/value in x.  fmt.Sprint is used to
//                              create a string key from the original
//                              map key.  The value stored is the
//                              original value, not a copy.
//
//   otherwise                  The value data is placed within a field
//                              keyed as "value" in x.  If the field
//                              already exists and is a single value,
//                              it is replaced with a list of both
//                              values.  Otherwise, if the field is
//                              already a list, data is appended to it.
//
// If data is a pointer or interface, the construction descends to the
// object it references.
//
	// The modified host x is itself returned.

func recursecopy(data interface{}) interface{} {

	val,n := rolldown(data)
	if n {
		return nil
	}

	// Apply the rules listed above
	switch val.Kind() {

	case reflect.Struct:
		return (D{}).copystruct(val)

	case reflect.Map:
		return (D{}).copymap(val)

	default:
		return copydata(val)

		// end switch type
	}

	return nil

}

func (x D) copystruct(val reflect.Value) D {

	var vtype = val.Type()
	var haspublic bool

	for i := 0; i < val.NumField(); i++ {
		var f = val.Field(i)
		if f.IsValid() && f.CanInterface() && !strings.Contains(vtype.Field(i).Tag.Get("logberry"), "quiet") {
			x[vtype.Field(i).Name] = recursecopy(f.Interface())
			haspublic = true
		}
	}

	// Special case: If the value is an error but has no accessible
	// fields, call its Error() function to get a text representation.
	if !haspublic && val.CanInterface() {
		if err, ok := val.Interface().(error); ok {
			x["Error"] = err.Error()
		}
	}

	return x

}

func (x D) copymap(val reflect.Value) D {

	var vals = val.MapKeys()
	for _, k := range vals {
		v := val.MapIndex(k)
		if k.CanInterface() && v.CanInterface() {
			x[fmt.Sprint(k.Interface())] = recursecopy(v.Interface())
		}
	}

	return x

}

func copydata(val reflect.Value) interface{} {

	switch val.Kind() {
	case reflect.Array: fallthrough
	case reflect.Slice:
		arr := make([]interface{}, val.Len())

		for i := 0; i < val.Len(); i++ {
			arr[i] = val.Index(i)
		}

		return arr

	default:
		if val.CanInterface() {
			return val.Interface()
		}

	}

	return "--"

}

func (x D) CopyFrom(data interface{}) D {

	val,n := rolldown(data)
	if n {
		return x
	}

	switch val.Kind() {

	case reflect.Struct:
		x.copystruct(val)

	case reflect.Map:
		x.copymap(val)

	default:
		newval := copydata(val)
		prev, find := x["value"]

		if find {
			switch p := prev.(type) {
			case []interface{}:
				x["value"] = append(p, newval)

			default:
				x["value"] = []interface{}{p, newval}
			}
		} else {
			x["value"] = newval
		}

	}

	return x

}

// DAggregate returns a new D object populated from the given array
// using CopyFrom().  It is up to the caller to threadsafe and
// otherwise correctly share the D.  This may matter because the
// caller is using the same D object in another goroutine, or if a
// Root or OutputDriver buffers the event and logs it asynchronously.
func DAggregate(data []interface{}) D {

	if data == nil || len(data) == 0 {
		return D{}
	}

	var accum = D{}

	for _,d := range(data) {
		accum.CopyFrom(d)
	}

	return accum

	// end DAggregate
}

// String returns a text representation of the host D.  This is
// presented as a sequence of key=value pairs for arguably
// human-readable presentation.  To produce a JSON serialization,
// simply marshal on the D as usual.  It is equivalent to casting
// output from Text().
func (x D) String() string {
	return string(x.Text())
}

// Text returns a byte slice textual representation of the host D.
// This is presented as a sequence of key=value pairs for arguably
// human-readable presentation.  To produce a JSON serialization,
// simply marshal on the D as usual.
func (x D) Text() []byte {
	var buffer bytes.Buffer
	x.WriteTo(&buffer)
	return buffer.Bytes()
}

// WriteTo serializes the host D to the given io.Writer.  This is
// presented as a sequence of key=value pairs for arguably
// human-readable presentation.  To produce a JSON serialization,
// simply marshal on the D as usual.
func (x D) WriteTo(w io.Writer) error {
	return textrecurse(w, false, x)
}

func textrecurse(buffer io.Writer, wrap bool, data interface{}) error {

	var val = reflect.ValueOf(data)

	// Chain through any pointers or interfaces
	done := false
	for !done {
		switch val.Kind() {
		case reflect.Interface:
			fallthrough
		case reflect.Ptr:

			if val.IsNil() {
				return nil
			}

			val = val.Elem()

		default:
			done = true
		}
	}

	// Handle the possibilities
	switch val.Kind() {

	case reflect.Map:
		if wrap {
			_, e := fmt.Fprint(buffer, "{")
			if e != nil {
				return e
			}
		}

		var vals = val.MapKeys()
		for _, k := range vals {
			vval := val.MapIndex(k)
			if k.CanInterface() && vval.IsValid() && vval.CanInterface() {
				_, e := fmt.Fprintf(buffer, " %s=", k.Interface())
				if e != nil {
					return e
				}

				e = textrecurse(buffer, true, vval.Interface())
				if e != nil {
					return e
				}
			}
		}

		if wrap {
			_, e := fmt.Fprint(buffer, " }")
			if e != nil {
				return e
			}
		}

	case reflect.Struct:
		if wrap {
			_, e := fmt.Fprint(buffer, "{")
			if e != nil {
				return e
			}
		}

		var vtype = val.Type()
		var haspublic bool
		for i := 0; i < val.NumField(); i++ {
			var f = val.Field(i)
			if f.IsValid() && f.CanInterface() && !strings.Contains(vtype.Field(i).Tag.Get("logberry"), "quiet") {
				_, e := fmt.Fprintf(buffer, " %s=", vtype.Field(i).Name)
				if e != nil {
					return e
				}

				e = textrecurse(buffer, true, f.Interface())
				haspublic = true
				if e != nil {
					return e
				}
			}
		}

		if err, ok := data.(error); ok {
			if !haspublic {
				_, e := fmt.Fprintf(buffer, " Message=%q", err.Error())
				if e != nil {
					return e
				}
			} else if _, ok := err.(*Error); !ok {
				_, e := fmt.Fprintf(buffer, " Type=%T", err)
				if e != nil {
					return e
				}
			}
		}

		if wrap {
			_, e := fmt.Fprint(buffer, " }")
			if e != nil {
				return e
			}
		}

	case reflect.Array:
		fallthrough
	case reflect.Slice:
		_, e := fmt.Fprint(buffer, "[")
		if e != nil {
			return e
		}

		if val.Len() > 0 {
			v := val.Index(0)
			if v.CanInterface() {
				e := textrecurse(buffer, true, v.Interface())
				if e != nil {
					return e
				}
			} else {
				_, e := fmt.Fprint(buffer, "--")
				if e != nil {
					return e
				}
			}
		}

		for i := 1; i < val.Len(); i++ {
			_, e := fmt.Fprint(buffer, ", ")
			if e != nil {
				return e
			}

			v := val.Index(i)
			if v.CanInterface() {
				e = textrecurse(buffer, true, val.Index(i).Interface())
				if e != nil {
					return e
				}
			} else {
				_, e := fmt.Fprint(buffer, "--")
				if e != nil {
					return e
				}
			}
		}

		_, e = fmt.Fprint(buffer, " ]")
		if e != nil {
			return e
		}

	case reflect.String:
		_, e := fmt.Fprintf(buffer, "%q", val.String())
		if e != nil {
			return e
		}

	default:
		if val.IsValid() && val.CanInterface() {
			_, e := fmt.Fprintf(buffer, "%v", val.Interface())
			if e != nil {
				return e
			}
		} else {
			_, e := fmt.Fprintf(buffer, "--")
			if e != nil {
				return e
			}
		}

		// end switch type
	}

	return nil

}
<|endoftext|>


<commit_before>package handlers

import (
	"encoding/json"
	"fmt"
	"net/http"
	"os"
	"path/filepath"
	"strings"

	"github.com/consbio/mbtileserver/mbtiles"
)

// RootURL returns the root URL of the HTTP request. Optionally, a domain and a
// base path may be provided which will be used to construct the root URL if
// they are not empty. Otherwise the hostname will be determined from the
// request and the path will be empty.
func RootURL(r *http.Request, domain, path string) string {
	host := r.Host
	if len(domain) > 0 {
		host = domain
	}

	root := fmt.Sprintf("%s://%s", Scheme(r), host)
	if len(path) > 0 {
		root = fmt.Sprintf("%s/%s", root, path)
	}

	return root
}

// Scheme returns the underlying URL scheme of the original request.
func Scheme(r *http.Request) string {
	if r.TLS != nil {
		return "https"
	}
	if scheme := r.Header.Get("X-Forwarded-Proto"); scheme != "" {
		return scheme
	}
	if scheme := r.Header.Get("X-Forwarded-Protocol"); scheme != "" {
		return scheme
	}
	if ssl := r.Header.Get("X-Forwarded-Ssl"); ssl == "on" {
		return "https"
	}
	if scheme := r.Header.Get("X-Url-Scheme"); scheme != "" {
		return scheme
	}
	return "http"
}

type handlerFunc func(http.ResponseWriter, *http.Request) (int, error)

func wrapGetWithErrors(ef func(error), hf handlerFunc) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		if r.Method != "GET" {
			status := http.StatusMethodNotAllowed
			http.Error(w, http.StatusText(status), status)
			return
		}
		status, err := hf(w, r) // run the handlerFunc and obtain the return codes
		if err != nil && ef != nil {
			ef(err) // handle the error with the supplied function
		}
		// in case it's an error, write the status code for the requester
		if status >= 400 {
			http.Error(w, http.StatusText(status), status)
		}
	})
}

// ServiceInfo consists of two strings that contain the image type and a URL.
type ServiceInfo struct {
	ImageType string `json:"imageType"`
	URL       string `json:"url"`
}

// ServiceSet is the base type for the HTTP handlers which combines multiple
// mbtiles.DB tilesets.
type ServiceSet struct {
	tilesets map[string]*mbtiles.DB
	Domain   string
	Path     string
}

// New returns a new ServiceSet. Use AddDBOnPath to add a mbtiles file.
func New() *ServiceSet {
	return &ServiceSet{
		tilesets: make(map[string]*mbtiles.DB),
	}
}

// AddDBOnPath interprets filename as mbtiles file which is opened and which will be
// served under "/services/<urlPath>" by Handler(). The parameter urlPath may not be
// nil, otherwise an error is returned. In case the DB cannot be opened the returned
// error is non-nil.
func (s *ServiceSet) AddDBOnPath(filename string, urlPath string) error {
	var err error
	if urlPath == "" {
		return fmt.Errorf("path parameter may not be empty")
	}
	ts, err := mbtiles.NewDB(filename)
	if err != nil {
		return fmt.Errorf("could not open mbtiles file %q: %v", filename, err)
	}
	s.tilesets[urlPath] = ts
	return nil
}

// NewFromBaseDir returns a ServiceSet that combines all .mbtiles files under
// the directory at baseDir. The DBs will all be served under their relative paths
// to baseDir.
func NewFromBaseDir(baseDir string) (*ServiceSet, error) {
	var filenames []string
	err := filepath.Walk(baseDir, func(p string, info os.FileInfo, err error) error {
		if err != nil {
			return err
		}
		if ext := filepath.Ext(p); ext == ".mbtiles" {
			filenames = append(filenames, p)
		}
		return nil
	})
	if err != nil {
		return nil, fmt.Errorf("unable to scan tilesets: %v", err)
	}

	if len(filenames) == 0 {
		return nil, fmt.Errorf("no tilesets found in %q", baseDir)
	}

	s := New()

	for _, filename := range filenames {
		subpath, err := filepath.Rel(baseDir, filename)
		if err != nil {
			return nil, fmt.Errorf("unable to extract URL path for %q: %v", filename, err)
		}
		e := filepath.Ext(filename)
		p := filepath.ToSlash(subpath)
		id := strings.ToLower(p[:len(p)-len(e)])
		err = s.AddDBOnPath(filename, id)
		if err != nil {
			return nil, err
		}
	}
	return s, nil
}

// Len returns the number of tilesets in this ServiceSet
func (s *ServiceSet) Size() int {
	return len(s.tilesets)
}

// RootURL returns the root URL of the service. If s.Domain is non-empty, it
// will be used as the hostname. If s.Path is non-empty, it will be used as a
// prefix.
func (s *ServiceSet) RootURL(r *http.Request) string {
	return RootURL(r, s.Domain, s.Path)
}

func (s *ServiceSet) listServices(w http.ResponseWriter, r *http.Request) (int, error) {
	rootURL := fmt.Sprintf("%s%s", s.RootURL(r), r.URL)
	services := []ServiceInfo{}
	for id, tileset := range s.tilesets {
		services = append(services, ServiceInfo{
			ImageType: tileset.TileFormatString(),
			URL:       fmt.Sprintf("%s/%s", rootURL, id),
		})
	}
	bytes, err := json.Marshal(services)
	if err != nil {
		return 500, fmt.Errorf("cannot marshal services JSON: %v", err)
	}
	w.Header().Set("Content-Type", "application/json")
	_, err = w.Write(bytes)
	if err != nil {
		return 0, err
	}
	return 0, nil
}

func (s *ServiceSet) serviceInfo(db *mbtiles.DB) handlerFunc {
	return func(w http.ResponseWriter, r *http.Request) (int, error) {
		// TODO implement this
		return http.StatusNotImplemented, nil
	}
}

func (s *ServiceSet) serviceHTML(db *mbtiles.DB) handlerFunc {
	return func(w http.ResponseWriter, r *http.Request) (int, error) {
		// TODO implement this
		return http.StatusNotImplemented, nil
	}
}

func (s *ServiceSet) tiles(db *mbtiles.DB) handlerFunc {
	return func(w http.ResponseWriter, r *http.Request) (int, error) {
		// TODO implement this
		return http.StatusNotImplemented, nil
	}
}

// Handler returns a http.Handler that serves all endpoints of the ServiceSet.
// The function ef is called with any occuring error if it is non-nil, so it
// can be used for e.g. logging with logging facitilies of the caller.
func (s *ServiceSet) Handler(ef func(error)) http.Handler {
	m := http.NewServeMux()
	m.Handle("/services", wrapGetWithErrors(ef, s.listServices))
	for id, db := range s.tilesets {
		p := "/services/" + id
		m.Handle(p, wrapGetWithErrors(ef, s.serviceInfo(db)))
		m.Handle(p+"/map", wrapGetWithErrors(ef, s.serviceHTML(db)))
		m.Handle(p+"/tiles/", wrapGetWithErrors(ef, s.tiles(db)))
		// TODO arcgis handlers
		// p = "//arcgis/rest/services/" + id + "/MapServer"
		// m.Handle(p, wrapGetWithErrors(s.getArcGISService))
		// m.Handle(p + "/layers", wrapGetWithErrors(s.getArcGISLayers))
		// m.Handle(p + "/legend", wrapGetWithErrors(s.getArcGISLegend))
		// m.Handle(p + "/tile/", wrapGetWithErrors(s.getArcGISTile))
	}
	return m
}
<commit_msg>Implement service info handler<commit_after>package handlers

import (
	"encoding/json"
	"fmt"
	"net/http"
	"os"
	"path/filepath"
	"strings"

	"github.com/consbio/mbtileserver/mbtiles"
)

// RootURL returns the root URL of the HTTP request. Optionally, a domain and a
// base path may be provided which will be used to construct the root URL if
// they are not empty. Otherwise the hostname will be determined from the
// request and the path will be empty.
func RootURL(r *http.Request, domain, path string) string {
	host := r.Host
	if len(domain) > 0 {
		host = domain
	}

	root := fmt.Sprintf("%s://%s", Scheme(r), host)
	if len(path) > 0 {
		root = fmt.Sprintf("%s/%s", root, path)
	}

	return root
}

// Scheme returns the underlying URL scheme of the original request.
func Scheme(r *http.Request) string {
	if r.TLS != nil {
		return "https"
	}
	if scheme := r.Header.Get("X-Forwarded-Proto"); scheme != "" {
		return scheme
	}
	if scheme := r.Header.Get("X-Forwarded-Protocol"); scheme != "" {
		return scheme
	}
	if ssl := r.Header.Get("X-Forwarded-Ssl"); ssl == "on" {
		return "https"
	}
	if scheme := r.Header.Get("X-Url-Scheme"); scheme != "" {
		return scheme
	}
	return "http"
}

type handlerFunc func(http.ResponseWriter, *http.Request) (int, error)

func wrapGetWithErrors(ef func(error), hf handlerFunc) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		if r.Method != "GET" {
			status := http.StatusMethodNotAllowed
			http.Error(w, http.StatusText(status), status)
			return
		}
		status, err := hf(w, r) // run the handlerFunc and obtain the return codes
		if err != nil && ef != nil {
			ef(err) // handle the error with the supplied function
		}
		// in case it's an error, write the status code for the requester
		if status >= 400 {
			http.Error(w, http.StatusText(status), status)
		}
	})
}

// ServiceInfo consists of two strings that contain the image type and a URL.
type ServiceInfo struct {
	ImageType string `json:"imageType"`
	URL       string `json:"url"`
}

// ServiceSet is the base type for the HTTP handlers which combines multiple
// mbtiles.DB tilesets.
type ServiceSet struct {
	tilesets map[string]*mbtiles.DB
	Domain   string
	Path     string
}

// New returns a new ServiceSet. Use AddDBOnPath to add a mbtiles file.
func New() *ServiceSet {
	return &ServiceSet{
		tilesets: make(map[string]*mbtiles.DB),
	}
}

// AddDBOnPath interprets filename as mbtiles file which is opened and which will be
// served under "/services/<urlPath>" by Handler(). The parameter urlPath may not be
// nil, otherwise an error is returned. In case the DB cannot be opened the returned
// error is non-nil.
func (s *ServiceSet) AddDBOnPath(filename string, urlPath string) error {
	var err error
	if urlPath == "" {
		return fmt.Errorf("path parameter may not be empty")
	}
	ts, err := mbtiles.NewDB(filename)
	if err != nil {
		return fmt.Errorf("could not open mbtiles file %q: %v", filename, err)
	}
	s.tilesets[urlPath] = ts
	return nil
}

// NewFromBaseDir returns a ServiceSet that combines all .mbtiles files under
// the directory at baseDir. The DBs will all be served under their relative paths
// to baseDir.
func NewFromBaseDir(baseDir string) (*ServiceSet, error) {
	var filenames []string
	err := filepath.Walk(baseDir, func(p string, info os.FileInfo, err error) error {
		if err != nil {
			return err
		}
		if ext := filepath.Ext(p); ext == ".mbtiles" {
			filenames = append(filenames, p)
		}
		return nil
	})
	if err != nil {
		return nil, fmt.Errorf("unable to scan tilesets: %v", err)
	}

	if len(filenames) == 0 {
		return nil, fmt.Errorf("no tilesets found in %q", baseDir)
	}

	s := New()

	for _, filename := range filenames {
		subpath, err := filepath.Rel(baseDir, filename)
		if err != nil {
			return nil, fmt.Errorf("unable to extract URL path for %q: %v", filename, err)
		}
		e := filepath.Ext(filename)
		p := filepath.ToSlash(subpath)
		id := strings.ToLower(p[:len(p)-len(e)])
		err = s.AddDBOnPath(filename, id)
		if err != nil {
			return nil, err
		}
	}
	return s, nil
}

// Len returns the number of tilesets in this ServiceSet
func (s *ServiceSet) Size() int {
	return len(s.tilesets)
}

// RootURL returns the root URL of the service. If s.Domain is non-empty, it
// will be used as the hostname. If s.Path is non-empty, it will be used as a
// prefix.
func (s *ServiceSet) RootURL(r *http.Request) string {
	return RootURL(r, s.Domain, s.Path)
}

func (s *ServiceSet) listServices(w http.ResponseWriter, r *http.Request) (int, error) {
	rootURL := fmt.Sprintf("%s%s", s.RootURL(r), r.URL)
	services := []ServiceInfo{}
	for id, tileset := range s.tilesets {
		services = append(services, ServiceInfo{
			ImageType: tileset.TileFormatString(),
			URL:       fmt.Sprintf("%s/%s", rootURL, id),
		})
	}
	bytes, err := json.Marshal(services)
	if err != nil {
		return http.StatusInternalServerError, fmt.Errorf("cannot marshal services JSON: %v", err)
	}
	w.Header().Set("Content-Type", "application/json")
	_, err = w.Write(bytes)
	if err != nil {
		return 0, err
	}
	return 0, nil
}

func (s *ServiceSet) serviceInfo(id string, db *mbtiles.DB) handlerFunc {
	return func(w http.ResponseWriter, r *http.Request) (int, error) {
		svcURL := fmt.Sprintf("%s%s", s.RootURL(r), r.URL.Path)
		imgFormat := db.TileFormatString()
		out := map[string]interface{}{
			"tilejson": "2.1.0",
			"id":       id,
			"scheme":   "xyz",
			"format":   imgFormat,
			"tiles":    []string{fmt.Sprintf("%s/tiles/{z}/{x}/{y}.%s", svcURL, imgFormat)},
			"map":      fmt.Sprintf("%s/map", svcURL),
		}
		metadata, err := db.ReadMetadata()
		if err != nil {
			return http.StatusInternalServerError, err
		}
		for k, v := range metadata {
			switch k {
			// strip out values above
			case "tilejson", "id", "scheme", "format", "tiles", "map":
				continue

			// strip out values that are not supported or are overridden below
			case "grids", "interactivity", "modTime":
				continue

			// strip out values that come from TileMill but aren't useful here
			case "metatile", "scale", "autoscale", "_updated", "Layer", "Stylesheet":
				continue

			default:
				out[k] = v
			}
		}

		if db.HasUTFGrid() {
			out["grids"] = []string{fmt.Sprintf("%s/tiles/{z}/{x}/{y}.json", svcURL)}
		}
		bytes, err := json.Marshal(out)
		if err != nil {
			return http.StatusInternalServerError, fmt.Errorf("cannot marshal service info JSON: %v", err)
		}
		w.Header().Set("Content-Type", "application/json")
		_, err = w.Write(bytes)
		if err != nil {
			return 0, err
		}
		return 0, nil
	}
}

func (s *ServiceSet) serviceHTML(db *mbtiles.DB) handlerFunc {
	return func(w http.ResponseWriter, r *http.Request) (int, error) {
		// TODO implement this
		return http.StatusNotImplemented, nil
	}
}

func (s *ServiceSet) tiles(db *mbtiles.DB) handlerFunc {
	return func(w http.ResponseWriter, r *http.Request) (int, error) {
		// TODO implement this
		return http.StatusNotImplemented, nil
	}
}

// Handler returns a http.Handler that serves all endpoints of the ServiceSet.
// The function ef is called with any occuring error if it is non-nil, so it
// can be used for e.g. logging with logging facitilies of the caller.
func (s *ServiceSet) Handler(ef func(error)) http.Handler {
	m := http.NewServeMux()
	m.Handle("/services", wrapGetWithErrors(ef, s.listServices))
	for id, db := range s.tilesets {
		p := "/services/" + id
		m.Handle(p, wrapGetWithErrors(ef, s.serviceInfo(id, db)))
		m.Handle(p+"/map", wrapGetWithErrors(ef, s.serviceHTML(db)))
		m.Handle(p+"/tiles/", wrapGetWithErrors(ef, s.tiles(db)))
		// TODO arcgis handlers
		// p = "//arcgis/rest/services/" + id + "/MapServer"
		// m.Handle(p, wrapGetWithErrors(s.getArcGISService))
		// m.Handle(p + "/layers", wrapGetWithErrors(s.getArcGISLayers))
		// m.Handle(p + "/legend", wrapGetWithErrors(s.getArcGISLegend))
		// m.Handle(p + "/tile/", wrapGetWithErrors(s.getArcGISTile))
	}
	return m
}
<|endoftext|>


<commit_before>package handlers

import (
	"encoding/json"
	"fmt"
	"net/http"
	"path"
	"strconv"

	"github.com/zefer/gompd/mpd"
	"gopkg.in/airbrake/glog.v1"
)

type Playlister interface {
	Status() (mpd.Attrs, error)
	PlaylistInfo(int, int) ([]mpd.Attrs, error)
	Clear() error
	PlaylistLoad(string, int, int) error
	Add(string) error
	Play(int) error
}

type PlayListEntry struct {
	Pos  int    `json:"pos"`
	Name string `json:"name"`
}

func PlayListHandler(c Playlister) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		if r.Method == "GET" {
			playListList(c, w, r)
			return
		} else if r.Method == "POST" {
			playListUpdate(c, w, r)
			return
		} else {
			w.WriteHeader(http.StatusMethodNotAllowed)
			return
		}
	})
}

// Helper that returns a start/end range to query from the current playlist.
// We want the full playlist unless it is huge, in which case we want a small
// chunk of it.
func playlistRange(c Playlister) ([2]int, error) {
	// Don't fetch or display more than this many playlist entries.
	max := 500
	var rng [2]int

	s, err := c.Status()
	if err != nil {
		return rng, err
	}
	if _, ok := s["song"]; !ok {
		// No current song playing, so use the whole (empty) playlist.
		return [2]int{-1, -1}, nil
	}

	pos, err := strconv.Atoi(s["song"])
	if err != nil {
		return rng, err
	}
	length, err := strconv.Atoi(s["playlistlength"])
	if err != nil {
		return rng, err
	}

	if length > max {
		// Fetch this chunk of the current playlist. Adjust the starting position to
		// return n items before the current song, for context.
		rng = [2]int{pos - 1, pos + max}
	} else {
		// Fetch all of the current playlist.
		rng = [2]int{-1, -1}
	}

	return rng, nil
}

func playListList(c Playlister, w http.ResponseWriter, r *http.Request) {
	rng, err := playlistRange(c)
	if err != nil {
		glog.Errorln(err)
		w.WriteHeader(http.StatusInternalServerError)
		return
	}
	// Fetch all, or a slice of the current playlist.
	data, err := c.PlaylistInfo(rng[0], rng[1])
	if err != nil {
		glog.Errorln(err)
		w.WriteHeader(http.StatusInternalServerError)
		return
	}
	out := make([]*PlayListEntry, len(data))
	for i, item := range data {
		var name string
		if artist, ok := item["Artist"]; ok {
			// Artist - Title
			name = fmt.Sprintf("%s - %s", artist, item["Title"])
		} else if n, ok := item["Name"]; ok {
			// Playlist name.
			name = n
		} else {
			// Default to file name.
			name = path.Base(item["file"])
		}
		p, err := strconv.Atoi(item["Pos"])
		if err != nil {
			p = 1
		}
		out[i] = &PlayListEntry{
			Pos:  p + 1,
			Name: name,
		}
	}
	b, err := json.Marshal(out)
	w.Header().Add("Content-Type", "application/json")
	fmt.Fprint(w, string(b))
}

func playListUpdate(c Playlister, w http.ResponseWriter, r *http.Request) {
	// Parse the JSON body.
	decoder := json.NewDecoder(r.Body)
	var params map[string]interface{}
	err := decoder.Decode(&params)
	if err != nil {
		glog.Errorln(err)
		w.WriteHeader(http.StatusBadRequest)
		return
	}

	// Check for required fields.
	for _, f := range []string{"uri", "type", "replace", "play"} {
		if _, ok := params[f]; !ok {
			w.WriteHeader(http.StatusBadRequest)
			return
		}
	}

	uri := params["uri"].(string)
	typ := params["type"].(string)
	replace := params["replace"].(bool)
	play := params["play"].(bool)
	pos := 0
	if uri == "" {
		w.WriteHeader(http.StatusBadRequest)
		return
	}

	// Clear the playlist.
	if replace {
		err := c.Clear()
		if err != nil {
			glog.Errorln(err)
			w.WriteHeader(http.StatusInternalServerError)
			return
		}
	}

	// To play from the start of the new items in the playlist, we need to get the
	// current playlist position.
	if !replace {
		data, err := c.Status()
		if err != nil {
			glog.Errorln(err)
			w.WriteHeader(http.StatusInternalServerError)
			return
		}
		pos, err = strconv.Atoi(data["playlistlength"])
		if err != nil {
			glog.Errorln(err)
			w.WriteHeader(http.StatusInternalServerError)
			return
		}
		glog.Infof("pos: %d", pos)
	}

	// Add to the playlist.
	if typ == "playlist" {
		err = c.PlaylistLoad(uri, -1, -1)
	} else {
		err = c.Add(uri)
	}
	if err != nil {
		glog.Errorln(err)
		w.WriteHeader(http.StatusInternalServerError)
		return
	}

	// Play.
	if play {
		err := c.Play(pos)
		if err != nil {
			glog.Errorln(err)
			w.WriteHeader(http.StatusInternalServerError)
			return
		}
	}

	w.WriteHeader(http.StatusNoContent)
}
<commit_msg>:bug: Fix ocassional playlist fetch with bad start index<commit_after>package handlers

import (
	"encoding/json"
	"fmt"
	"net/http"
	"path"
	"strconv"

	"github.com/zefer/gompd/mpd"
	"gopkg.in/airbrake/glog.v1"
)

type Playlister interface {
	Status() (mpd.Attrs, error)
	PlaylistInfo(int, int) ([]mpd.Attrs, error)
	Clear() error
	PlaylistLoad(string, int, int) error
	Add(string) error
	Play(int) error
}

type PlayListEntry struct {
	Pos  int    `json:"pos"`
	Name string `json:"name"`
}

func PlayListHandler(c Playlister) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		if r.Method == "GET" {
			playListList(c, w, r)
			return
		} else if r.Method == "POST" {
			playListUpdate(c, w, r)
			return
		} else {
			w.WriteHeader(http.StatusMethodNotAllowed)
			return
		}
	})
}

// Helper that returns a start/end range to query from the current playlist.
// We want the full playlist unless it is huge, in which case we want a small
// chunk of it.
func playlistRange(c Playlister) ([2]int, error) {
	// Don't fetch or display more than this many playlist entries.
	max := 500
	// Give MPD a start/end of -1/-1 to fetch the full playlist.
	var rngAll = [2]int{-1, -1}
	// A sensible default for when we can't determine the current playlist size.
	var rngFirst = [2]int{0, max}

	s, err := c.Status()
	if err != nil {
		return rngFirst, err
	}
	if _, ok := s["song"]; !ok {
		// No current song playing, so use the whole (empty) playlist.
		return rngAll, nil
	}

	pos, err := strconv.Atoi(s["song"])
	if err != nil {
		return rngFirst, err
	}
	length, err := strconv.Atoi(s["playlistlength"])
	if err != nil {
		return rngFirst, err
	}

	if length > max {
		// Fetch this chunk of the current playlist. Adjust the starting position to
		// return n items before the current song, for context.
		return [2]int{pos - 1, pos + max}, nil
	} else {
		// Fetch all of the current playlist.
		return rngAll, nil
	}
}

func playListList(c Playlister, w http.ResponseWriter, r *http.Request) {
	rng, err := playlistRange(c)
	if err != nil {
		glog.Errorln(err)
		w.WriteHeader(http.StatusInternalServerError)
		return
	}
	// Fetch all, or a slice of the current playlist.
	data, err := c.PlaylistInfo(rng[0], rng[1])
	if err != nil {
		glog.Errorln(err)
		w.WriteHeader(http.StatusInternalServerError)
		return
	}
	out := make([]*PlayListEntry, len(data))
	for i, item := range data {
		var name string
		if artist, ok := item["Artist"]; ok {
			// Artist - Title
			name = fmt.Sprintf("%s - %s", artist, item["Title"])
		} else if n, ok := item["Name"]; ok {
			// Playlist name.
			name = n
		} else {
			// Default to file name.
			name = path.Base(item["file"])
		}
		p, err := strconv.Atoi(item["Pos"])
		if err != nil {
			p = 1
		}
		out[i] = &PlayListEntry{
			Pos:  p + 1,
			Name: name,
		}
	}
	b, err := json.Marshal(out)
	w.Header().Add("Content-Type", "application/json")
	fmt.Fprint(w, string(b))
}

func playListUpdate(c Playlister, w http.ResponseWriter, r *http.Request) {
	// Parse the JSON body.
	decoder := json.NewDecoder(r.Body)
	var params map[string]interface{}
	err := decoder.Decode(&params)
	if err != nil {
		glog.Errorln(err)
		w.WriteHeader(http.StatusBadRequest)
		return
	}

	// Check for required fields.
	for _, f := range []string{"uri", "type", "replace", "play"} {
		if _, ok := params[f]; !ok {
			w.WriteHeader(http.StatusBadRequest)
			return
		}
	}

	uri := params["uri"].(string)
	typ := params["type"].(string)
	replace := params["replace"].(bool)
	play := params["play"].(bool)
	pos := 0
	if uri == "" {
		w.WriteHeader(http.StatusBadRequest)
		return
	}

	// Clear the playlist.
	if replace {
		err := c.Clear()
		if err != nil {
			glog.Errorln(err)
			w.WriteHeader(http.StatusInternalServerError)
			return
		}
	}

	// To play from the start of the new items in the playlist, we need to get the
	// current playlist position.
	if !replace {
		data, err := c.Status()
		if err != nil {
			glog.Errorln(err)
			w.WriteHeader(http.StatusInternalServerError)
			return
		}
		pos, err = strconv.Atoi(data["playlistlength"])
		if err != nil {
			glog.Errorln(err)
			w.WriteHeader(http.StatusInternalServerError)
			return
		}
		glog.Infof("pos: %d", pos)
	}

	// Add to the playlist.
	if typ == "playlist" {
		err = c.PlaylistLoad(uri, -1, -1)
	} else {
		err = c.Add(uri)
	}
	if err != nil {
		glog.Errorln(err)
		w.WriteHeader(http.StatusInternalServerError)
		return
	}

	// Play.
	if play {
		err := c.Play(pos)
		if err != nil {
			glog.Errorln(err)
			w.WriteHeader(http.StatusInternalServerError)
			return
		}
	}

	w.WriteHeader(http.StatusNoContent)
}
<|endoftext|>


<commit_before>//   Copyright 2018 MSolution.IO
//
//   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 onDemandToRiEc2

import (
	"context"
	"time"

	"github.com/trackit/jsonlog"

	"github.com/trackit/trackit-server/es"
)

const TypeOdToRiEC2Report = "od-to-ri-ec2-report"
const IndexPrefixOdToRiEC2Report = "od-to-ri-ec2-reports"
const TemplateNameOdToRiEC2Report = "od-to-ri-ec2-reports"

// put the ElasticSearch index for *-od-to-ri-ec2-reports indices at startup.
func init() {
	ctx, ctxCancel := context.WithTimeout(context.Background(), 10*time.Second)
	res, err := es.Client.IndexPutTemplate(TemplateNameOdToRiEC2Report).BodyString(TemplateOdToRiEc2Report).Do(ctx)
	if err != nil {
		jsonlog.DefaultLogger.Error("Failed to put ES index OdToRiEC2Report.", err)
	} else {
		jsonlog.DefaultLogger.Info("Put ES index OdToRiEC2Report.", res)
		ctxCancel()
	}
}

const TemplateOdToRiEc2Report = `
{
	"template": "*-od-to-ri-ec2-reports",
	"version": 1,
	"mappings": {
		"od-to-ri-ec2-report": {
			"properties": {
				"account": {
					"type": "keyword"
				},
				"reportDate": {
					"type": "date"
				},
        "onDemandMonthlyCostTotal": {
          "type": "double"
        },
        "onDemand1yrCostTotal": {
          "type": "double"
        },
        "onDemand3yrCostTotal": {
          "type": "double"
        },
        "ri1yrMonthlyCostTotal": {
          "type": "double"
        },
        "ri1yrCostTotal": {
          "type": "double"
        },
        "ri1yrSavingTotal": {
          "type": "double"
        },
        "ri3yrMonthlyCostTotal": {
          "type": "double"
        },
        "ri3yrCostTotal": {
          "type": "double"
        },
        "ri3yrSavingTotal": {
          "type": "double"
        },
        "instances": {
          "type": "nested",
          "properties": {
            "region": {
              "type": "keyword"
            },
            "instanceType": {
              "type": "keyword"
            },
            "platform": {
              "type": "keyword"
            },
            "instanceCount": {
              "type": "integer"
            },
            "onDemandMonthlyCostPerUnit": {
              "type": "double"
            },
            "onDemandMonthlyCostTotal": {
              "type": "double"
            },
            "onDemand1yrCostPerUnit": {
              "type": "double"
            },
            "onDemand1yrCostTotal": {
              "type": "double"
            },
            "onDemand3yrCostPerUnit": {
              "type": "double"
            },
            "onDemand3yrCostTotal": {
              "type": "double"
            },
            "reservationType": {
              "type": "keyword"
            },
            "ri1yrMonthlyCostPerUnit": {
              "type": "double"
            },
            "ri1yrMonthlyCostTotal": {
              "type": "double"
            },
            "ri1yrCostPerUnit": {
              "type": "double"
            },
            "ri1yrCostTotal": {
              "type": "double"
            },
            "ri1yrSavingPerUnit": {
              "type": "double"
            },
            "ri1yrSavingTotal": {
              "type": "double"
            },
            "ri3yrMonthlyCostPerUnit": {
              "type": "double"
            },
            "ri3yrMonthlyCostTotal": {
              "type": "double"
            },
            "ri3yrCostPerUnit": {
              "type": "double"
            },
            "ri3yrCostTotal": {
              "type": "double"
            },
            "ri3yrSavingPerUnit": {
              "type": "double"
            },
            "ri3yrSavingTotal": {
              "type": "double"
            }
          }
        }
			},
			"_all": {
				"enabled": false
			},
			"numeric_detection": false,
			"date_detection": false
		}
	}
}
`
<commit_msg>update mapping<commit_after>//   Copyright 2018 MSolution.IO
//
//   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 onDemandToRiEc2

import (
	"context"
	"time"

	"github.com/trackit/jsonlog"

	"github.com/trackit/trackit-server/es"
)

const TypeOdToRiEC2Report = "od-to-ri-ec2-report"
const IndexPrefixOdToRiEC2Report = "od-to-ri-ec2-reports"
const TemplateNameOdToRiEC2Report = "od-to-ri-ec2-reports"

// put the ElasticSearch index for *-od-to-ri-ec2-reports indices at startup.
func init() {
	ctx, ctxCancel := context.WithTimeout(context.Background(), 10*time.Second)
	res, err := es.Client.IndexPutTemplate(TemplateNameOdToRiEC2Report).BodyString(TemplateOdToRiEc2Report).Do(ctx)
	if err != nil {
		jsonlog.DefaultLogger.Error("Failed to put ES index OdToRiEC2Report.", err)
	} else {
		jsonlog.DefaultLogger.Info("Put ES index OdToRiEC2Report.", res)
		ctxCancel()
	}
}

const TemplateOdToRiEc2Report = `
{
	"template": "*-od-to-ri-ec2-reports",
	"version": 1,
	"mappings": {
		"od-to-ri-ec2-report": {
			"properties": {
				"account": {
					"type": "keyword"
				},
				"reportDate": {
					"type": "date"
				},
				"onDemand": {
					"properties": {
						"monthly": {
							"type": "double"
						},
						"oneYear": {
							"type": "double"
						},
						"threeYears": {
							"type": "double"
						}
					}
				},
				"reservation": {
					"properties": {
						"oneYear": {
							"properties": {
								"monthly": {
									"type": "double"
								},
								"global": {
									"type": "double"
								},
								"saving": {
									"type": "double"
								}
							}
						},
						"threeYears": {
							"properties": {
								"monthly": {
									"type": "double"
								},
								"global": {
									"type": "double"
								},
								"saving": {
									"type": "double"
								}
							}
						}
					}
				},
				"instances": {
					"type": "nested",
					"properties": {
						"region": {
							"type": "keyword"
						},
						"instanceType": {
							"type": "keyword"
						},
						"platform": {
							"type": "keyword"
						},
						"instanceCount": {
							"type": "integer"
						},
						"onDemand": {
							"properties": {
								"monthly": {
									"properties": {
										"perUnit": {
											"type": "double"
										},
										"total": {
											"type": "double"
										}
									}
								},
								"oneYear": {
									"properties": {
										"perUnit": {
											"type": "double"
										},
										"total": {
											"type": "double"
										}
									}
								},
								"threeYears": {
									"properties": {
										"perUnit": {
											"type": "double"
										},
										"total": {
											"type": "double"
										}
									}
								}
							}
						},
						"reservation": {
							"properties": {
								"type": {
									"type": "keyword"
								},
								"oneYear": {
									"properties": {
										"monthly": {
											"properties": {
												"perUnit": {
													"type": "double"
												},
												"total": {
													"type": "double"
												}
											}
										},
										"global": {
											"properties": {
												"perUnit": {
													"type": "double"
												},
												"total": {
													"type": "double"
												}
											}
										},
										"saving": {
											"properties": {
												"perUnit": {
													"type": "double"
												},
												"total": {
													"type": "double"
												}
											}
										}
									}
								},
								"threeYears": {
									"properties": {
										"monthly": {
											"properties": {
												"perUnit": {
													"type": "double"
												},
												"total": {
													"type": "double"
												}
											}
										},
										"global": {
											"properties": {
												"perUnit": {
													"type": "double"
												},
												"total": {
													"type": "double"
												}
											}
										},
										"saving": {
											"properties": {
												"perUnit": {
													"type": "double"
												},
												"total": {
													"type": "double"
												}
											}
										}
									}
								}
							}
						}
					}
				}
			},
			"_all": {
				"enabled": false
			},
			"numeric_detection": false,
			"date_detection": false
		}
	}
}
`
<|endoftext|>