func TestGetSystemInfo(t *testing.T) {
	mgrCfg := server.EntityManagerConfig{
		TimeRanges:    []time.Duration{6 * time.Hour, 1 * time.Hour},
		ContentSource: mock.NewMockContentSource(),
	}
	mgr := server.NewEntityManager(mgrCfg)
	appCfg := AppConfig{
		TimeRanges: []time.Duration{1 * time.Hour, 8 * time.Hour, 12 * time.Hour},
	}

	systemInfoHandler := GetSystemInfo(mgr, appCfg)

	w := httptest.NewRecorder()
	r, _ := http.NewRequest("GET", "/api/system_info", nil)
	systemInfoHandler(w, r)
	assert.Equal(t, http.StatusOK, w.Code)

	// Spot check: just make sure the parent JSON keys are present.
	response := json.ParseBytes(w.Body.Bytes())
	assert.Equal(t, "[1 8 12]", response.Get("Runtime").Get("TimeRangesInHours").AsString())
	assert.True(t, response.Get("Deployment").Exists())
	assert.True(t, response.Get("Runtime").Exists())
	assert.True(t, response.Get("Runtime").Get("ItemCounts").Exists())
	assert.True(t, response.Get("Runtime").Get("OldestContent").Exists())
	assert.True(t, response.Get("Runtime").Get("NewestContent").Exists())
}
示例#2
0
// Fires up the EntityManager service.  Once started, the service will pull
// the latest content (documents and entities) from the configured ContentSource
// calculate some aggreate stats on them, and then make the stats available to
// this web app.
func startEntityManager(cfg AppConfig, contentSource server.ContentSource) *server.EntityManager {
	// Create and configure a new EntityManager object.
	entityManager := server.NewEntityManager(server.EntityManagerConfig{
		TimeRanges:    cfg.TimeRanges,
		ContentSource: contentSource,
	})

	now := unixtime.Now()

	// If it exists, load global content saved prior to previous app shutdown.
	dataDir := cfg.DataDir
	if server.FileExists(dataDir) {
		logger.Printf("Loading saved content from %v", dataDir)
		entityManager.LoadState(dataDir, now)
		entityManager.ContentDAO.Load(dataDir)
	} else {
		logger.Printf("Loading recent content directly from content source into content buffer.")
		entityManager.PreFill(now.Subtract(cfg.DataPreFetchWindow), now)
	}

	logger.Printf("Calculating entity stats for the first time...")
	entityManager.RefreshStats(now)

	refreshStatsLock = &sync.Mutex{}

	logger.Printf("Starting MemDb content polling loop.")
	go func() {
		// The show must go on.  Trap any panics, log them as an error, and then
		// continue to attempt to get more data from MemDb service.
		for {
			func() {
				defer func() {
					if r := recover(); r != nil {
						logger.Printf("ERROR: unexpected application error: \n%v\n\n", r)
						debug.PrintStack()
					}
				}()

				ticker := time.NewTicker(cfg.RefreshInterval)
				for _ = range ticker.C {
					refreshStatsLock.Lock()
					startTime := time.Now()
					entityManager.FetchMoreContent(unixtime.Now())
					logger.Printf("entityManager.FetchMoreContent() took %v", time.Since(startTime))
					refreshStatsLock.Unlock()
				}
			}()
		}
	}()

	return entityManager
}
func TestGetAllEntityInfo_noFiltersApplied(t *testing.T) {
	config := server.EntityManagerConfig{
		ContentSource: mock.NewMockContentSource(),
		TimeRanges:    []time.Duration{1 * time.Hour},
	}

	entityMgr := server.NewEntityManager(config)

	// Request the "all entity info" stats with no co-occurrence filter applied
	// (indicated by an empty post body).  In this case, we expect the call to
	// return the globally-computed entity statistics
	// (i.e. entityMgr.ContentBuffer.GetLatestEntityStats()).
	w := httptest.NewRecorder()
	postBody := strings.NewReader("")
	r, _ := http.NewRequest("GET", "/api/some/path", postBody)
	userId := 123
	handler := GetAllEntityInfo(entityMgr)
	handler(w, r, userId)

	assert.Equal(t, http.StatusOK, w.Code)
}