<commit_before>package main

import (
	"net/http"
	_ "net/http/pprof"
	"time"

	"github.com/appscode/log"
	_ "github.com/appscode/searchlight/api/install"
	acs "github.com/appscode/searchlight/client/clientset"
	_ "github.com/appscode/searchlight/client/clientset/fake"
	"github.com/appscode/searchlight/pkg/analytics"
	"github.com/appscode/searchlight/pkg/client/icinga"
	acw "github.com/appscode/searchlight/pkg/watcher"
	"github.com/prometheus/client_golang/prometheus/promhttp"
	"github.com/spf13/cobra"
	clientset "k8s.io/client-go/kubernetes"
	apiv1 "k8s.io/client-go/pkg/api/v1"
	"k8s.io/client-go/tools/clientcmd"
	_ "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset/fake"
)

var (
	masterURL      string
	kubeconfigPath string

	icingaSecretName      string
	icingaSecretNamespace string = apiv1.NamespaceDefault

	address string = ":8080"

	kubeClient clientset.Interface
	extClient  acs.ExtensionInterface

	enableAnalytics bool = true
)

func NewCmdRun() *cobra.Command {
	cmd := &cobra.Command{
		Use:   "run",
		Short: "Run operator",
		Run: func(cmd *cobra.Command, args []string) {
			run()
		},
	}

	cmd.Flags().StringVar(&masterURL, "master", masterURL, "The address of the Kubernetes API server (overrides any value in kubeconfig)")
	cmd.Flags().StringVar(&kubeconfigPath, "kubeconfig", kubeconfigPath, "Path to kubeconfig file with authorization information (the master location is set by the master flag).")
	cmd.Flags().StringVarP(&icingaSecretName, "icinga-secret-name", "s", icingaSecretName, "Icinga secret name")
	cmd.Flags().StringVarP(&icingaSecretNamespace, "icinga-secret-namespace", "n", icingaSecretNamespace, "Icinga secret namespace")

	cmd.Flags().StringVar(&address, "address", address, "Address to listen on for web interface and telemetry.")

	// Analytics flags
	cmd.Flags().BoolVar(&enableAnalytics, "analytics", enableAnalytics, "Send analytical event to Google Analytics")

	return cmd
}

func run() {
	config, err := clientcmd.BuildConfigFromFlags(masterURL, kubeconfigPath)
	if err != nil {
		log.Fatalf("Could not get Kubernetes config: %s", err)
	}

	kubeClient = clientset.NewForConfigOrDie(config)
	extClient = acs.NewForConfigOrDie(config)

	w := &acw.Watcher{
		KubeClient:      kubeClient,
		ExtClient:       extClient,
		EnableAnalytics: enableAnalytics,
		SyncPeriod:      time.Minute * 2,
	}
	if icingaSecretName == "" {
		log.Fatalln("Missing icinga secret")
	}
	icingaClient, err := icinga.NewIcingaClient(w.KubeClient, icingaSecretName, icingaSecretNamespace)
	if err != nil {
		log.Fatalln(err)
	}
	w.IcingaClient = icingaClient

	log.Infoln("Starting Searchlight operator...")
	go w.Run()

	if enableAnalytics {
		analytics.Enable()
	}
	analytics.SendEvent("", "started", Version)

	http.Handle("/metrics", promhttp.Handler())
	log.Infoln("Listening on", address)
	log.Fatal(http.ListenAndServe(address, nil))
}<commit_msg>Collect analytics for searchligth start & stop events<commit_after>package main

import (
	"net/http"
	_ "net/http/pprof"
	"time"

	"github.com/appscode/log"
	_ "github.com/appscode/searchlight/api/install"
	acs "github.com/appscode/searchlight/client/clientset"
	_ "github.com/appscode/searchlight/client/clientset/fake"
	"github.com/appscode/searchlight/pkg/analytics"
	"github.com/appscode/searchlight/pkg/client/icinga"
	acw "github.com/appscode/searchlight/pkg/watcher"
	"github.com/prometheus/client_golang/prometheus/promhttp"
	"github.com/spf13/cobra"
	clientset "k8s.io/client-go/kubernetes"
	apiv1 "k8s.io/client-go/pkg/api/v1"
	"k8s.io/client-go/tools/clientcmd"
	_ "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset/fake"
)

var (
	masterURL      string
	kubeconfigPath string

	icingaSecretName      string
	icingaSecretNamespace string = apiv1.NamespaceDefault

	address string = ":8080"

	kubeClient clientset.Interface
	extClient  acs.ExtensionInterface

	enableAnalytics bool = true
)

func NewCmdRun() *cobra.Command {
	cmd := &cobra.Command{
		Use:   "run",
		Short: "Run operator",
		Run: func(cmd *cobra.Command, args []string) {
			run()
		},
		PostRun: func(cmd *cobra.Command, args []string) {
			analytics.SendEvent("operator", "stopped", Version)
		},
	}

	cmd.Flags().StringVar(&masterURL, "master", masterURL, "The address of the Kubernetes API server (overrides any value in kubeconfig)")
	cmd.Flags().StringVar(&kubeconfigPath, "kubeconfig", kubeconfigPath, "Path to kubeconfig file with authorization information (the master location is set by the master flag).")
	cmd.Flags().StringVarP(&icingaSecretName, "icinga-secret-name", "s", icingaSecretName, "Icinga secret name")
	cmd.Flags().StringVarP(&icingaSecretNamespace, "icinga-secret-namespace", "n", icingaSecretNamespace, "Icinga secret namespace")

	cmd.Flags().StringVar(&address, "address", address, "Address to listen on for web interface and telemetry.")

	// Analytics flags
	cmd.Flags().BoolVar(&enableAnalytics, "analytics", enableAnalytics, "Send analytical event to Google Analytics")

	return cmd
}

func run() {
	config, err := clientcmd.BuildConfigFromFlags(masterURL, kubeconfigPath)
	if err != nil {
		log.Fatalf("Could not get Kubernetes config: %s", err)
	}

	kubeClient = clientset.NewForConfigOrDie(config)
	extClient = acs.NewForConfigOrDie(config)

	w := &acw.Watcher{
		KubeClient:      kubeClient,
		ExtClient:       extClient,
		EnableAnalytics: enableAnalytics,
		SyncPeriod:      time.Minute * 2,
	}
	if icingaSecretName == "" {
		log.Fatalln("Missing icinga secret")
	}
	icingaClient, err := icinga.NewIcingaClient(w.KubeClient, icingaSecretName, icingaSecretNamespace)
	if err != nil {
		log.Fatalln(err)
	}
	w.IcingaClient = icingaClient

	log.Infoln("Starting Searchlight operator...")
	go w.Run()

	if enableAnalytics {
		analytics.Enable()
	}
	analytics.SendEvent("operator", "started", Version)

	http.Handle("/metrics", promhttp.Handler())
	log.Infoln("Listening on", address)
	log.Fatal(http.ListenAndServe(address, nil))
}
{'fraction_non_alphanumeric': 0.12934071464519376, 'fraction_numerical': 0.004697198456634793, 'mean_word_length': 5.004028197381672, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '78410', 'n_tokens_mistral': 2170, 'n_tokens_neox': 2080, 'n_words': 408}