示例#1
0
func TestEngine(t *testing.T) {
	engine := suggest.NewEngine()
	assertValueEquals(t, 0, len(engine.Suggest(0, "")))
	engine.Add("A")
	engine.Add("Hi")
	engine.Add("Hello")
	engine.Add("Suggest")
	engine.Add("A")
	engine.Add("Hi")
	engine.Add("Hello")
	engine.Add("Suggest")
	engine.Await()
	assertValueDeepEquals(
		t, []string{"Hello", "Hi"}, engine.Suggest(3, "H"))
	assertValueDeepEquals(
		t, []string{"Hello", "Hi"}, engine.Suggest(2, "H"))
	assertValueDeepEquals(
		t, []string{"Hello"}, engine.Suggest(1, "H"))
	assertValueDeepEquals(
		t, []string{"Hello", "Hi"}, engine.Suggest(0, "H"))
	assertValueDeepEquals(
		t, []string{"A", "Hello", "Hi", "Suggest"}, engine.Suggest(0, ""))
	assertValueEquals(t, 0, len(engine.Suggest(0, "J")))
	assertValueEquals(t, 0, len(engine.Suggest(5, "J")))

}
示例#2
0
文件: scotty.go 项目: keep94/scotty
func main() {
	tricorder.RegisterFlags()
	flag.Parse()
	circularBuffer := logbuf.New()
	logger := log.New(circularBuffer, "", log.LstdFlags)
	handleSignals(logger)
	// Read configs early so that we will fail fast.
	maybeNilMemoryManager := maybeCreateMemoryManager(logger)
	consumerBuilders, err := newPStoreConsumers(maybeNilMemoryManager)
	if err != nil {
		log.Println("Pstore config file error:", err)
		logger.Println("Pstore config file error:", err)
	}
	metricNameEngine := suggest.NewEngine()
	metricNameAdder := newTsdbAdder(metricNameEngine)
	tagkEngine := suggest.NewSuggester("appname", "HostName")
	tagvEngine := suggest.NewEngine()
	tagvAdder := newTsdbAdder(tagvEngine)

	appList := createApplicationList()
	for _, app := range appList.All() {
		tagvAdder.Add(app.Name())
	}
	applicationStats := createApplicationStats(
		appList, logger, tagvAdder, maybeNilMemoryManager)
	rpc.RegisterName(
		"Scotty",
		&rpcType{AS: applicationStats},
	)
	rpc.HandleHTTP()
	connectionErrors := newConnectionErrorsType()
	if consumerBuilders == nil {
		startCollector(
			applicationStats,
			connectionErrors,
			totalCountCollectionType(nil),
			metricNameAdder,
			&maybeNilMemoryManagerWrapperType{maybeNilMemoryManager})
	} else {
		var coord coordinatorBuilderType
		if *fCoord != "" {
			var err error
			coord, err = consul.NewCoordinator(logger)
			if err != nil {
				logger.Println(err)
				coord = &blockingCoordinatorType{}
			}
		}
		totalCounts := startPStoreLoops(
			applicationStats,
			consumerBuilders,
			logger,
			coord)
		startCollector(
			applicationStats,
			connectionErrors,
			totalCountCollectionType(totalCounts),
			metricNameAdder,
			&maybeNilMemoryManagerWrapperType{maybeNilMemoryManager})
	}

	http.Handle(
		"/",
		gzipHandler{&splash.Handler{
			AS:  applicationStats,
			Log: circularBuffer,
		}})
	http.Handle(
		"/showAllApps",
		gzipHandler{&showallapps.Handler{
			AS:             applicationStats,
			CollectionFreq: *fCollectionFrequency,
		}})
	http.Handle(
		"/api/hosts/",
		http.StripPrefix(
			"/api/hosts/",
			gzipHandler{&byEndpointHandler{
				AS: applicationStats,
			}}))
	http.Handle(
		"/api/latest/",
		http.StripPrefix(
			"/api/latest/",
			gzipHandler{&latestHandler{
				AS: applicationStats,
			}}))

	http.Handle(
		"/api/errors/",
		gzipHandler{&errorHandler{
			ConnectionErrors: connectionErrors,
		}},
	)

	tsdbServeMux := http.NewServeMux()

	tsdbServeMux.Handle(
		"/api/query",
		tsdbexec.NewHandler(
			func(r *tsdbjson.QueryRequest) ([]tsdbjson.TimeSeries, error) {
				return tsdbexec.Query(
					r, applicationStats, *fCollectionFrequency)
			}))
	tsdbServeMux.Handle(
		"/api/suggest",
		tsdbexec.NewHandler(
			func(req url.Values) ([]string, error) {
				return tsdbexec.Suggest(
					req,
					map[string]suggest.Suggester{
						"metrics": metricNameEngine,
						"tagk":    tagkEngine,
						"tagv":    tagvEngine,
					})
			},
		))
	tsdbServeMux.Handle(
		"/api/aggregators",
		tsdbexec.NewHandler(
			func(req url.Values) ([]string, error) {
				return aggregators.Names(), nil
			},
		))
	tsdbServeMux.Handle(
		"/api/version",
		tsdbexec.NewHandler(
			func(req url.Values) (map[string]string, error) {
				return map[string]string{
					"version": "1.0",
				}, nil
			},
		))
	tsdbServeMux.Handle(
		"/api/config",
		tsdbexec.NewHandler(
			func(req url.Values) (map[string]string, error) {
				return map[string]string{
					"tsd.ore.auto_create_metrics": "true",
					"tsd.ore.auto_create_tagks":   "true",
					"tsd.ore.auto_create_tagvs":   "true",
				}, nil
			},
		))
	tsdbServeMux.Handle(
		"/api/config/filters",
		tsdbexec.NewHandler(
			func(req url.Values) (interface{}, error) {
				return tsdbjson.AllFilterDescriptions(), nil
			},
		))
	tsdbServeMux.Handle(
		"/api/dropcaches",
		tsdbexec.NewHandler(
			func(req url.Values) (map[string]string, error) {
				return map[string]string{
					"message": "Caches dropped",
					"status":  "200",
				}, nil
			},
		))
	tsdbServeMux.Handle(
		"/api",
		tsdbexec.NotFoundHandler,
	)

	go func() {
		if err := http.ListenAndServe(fmt.Sprintf(":%d", *fTsdbPort), tsdbServeMux); err != nil {
			log.Fatal(err)
		}
	}()

	if err := http.ListenAndServe(fmt.Sprintf(":%d", *fPort), nil); err != nil {
		log.Fatal(err)
	}
}