コード例 #1
0
func (me *MockContentSource) FetchNewsArticles(startTime unixtime.Time, endTime unixtime.Time) []server.NewsArticle {
	articlesPerMin := 200 + rand.Intn(400)

	logger.Printf("Exporting docs from %v to %v", startTime, endTime)

	articlesPerSec := articlesPerMin / 60
	timeWindow := endTime.Time().Sub(startTime.Time())
	numArticlesToFetch := articlesPerSec * int(timeWindow/time.Second)
	logger.Printf("articlesPerMin=%v, timeWindow=%v => generating %v fake news articles", articlesPerMin, timeWindow, numArticlesToFetch)

	newsArticles := make([]server.NewsArticle, 0, numArticlesToFetch)
	for i := 0; i < numArticlesToFetch; i++ {
		doc := makeFakeDocument(me.nextDocumentId(), startTime, endTime)

		// Create some fake people
		numPersonsPerDoc := 0 + rnd.Intn(20)
		persons := make([]server.Entity, 0, numPersonsPerDoc)
		for j := 0; j < numPersonsPerDoc; j++ {
			persons = append(persons, fakePersons[rnd.Intn(len(fakePersons))])
		}

		// Create some fake orgs
		numOrgsPerDoc := 0 + rnd.Intn(20)
		orgs := make([]server.Entity, 0, numOrgsPerDoc)
		for j := 0; j < numOrgsPerDoc; j++ {
			orgs = append(orgs, fakeOrgs[rnd.Intn(len(fakeOrgs))])
		}

		// Create some fake places
		numPlacesPerDoc := 0 + rnd.Intn(20)
		places := make([]server.Entity, 0, numPlacesPerDoc)
		for j := 0; j < numPlacesPerDoc; j++ {
			places = append(places, fakePlaces[rnd.Intn(len(fakePlaces))])
		}

		newsArticle := server.NewsArticle{Document: doc, Persons: persons, Orgs: orgs, Places: places}
		newsArticles = append(newsArticles, newsArticle)
	}

	return newsArticles
}
コード例 #2
0
func makeFakeDocument(docId int, startTime unixtime.Time, endTime unixtime.Time) server.Document {
	fakeNewsSources := []string{"The Onion", "NYTimes", "Wired.com", "SciCentral", "Reuters", "BBC News"}

	// Returns a UnixTime object that falls within the specified time endpoints (inclusive).
	randomTimeBetween := func(startTime unixtime.Time, endTime unixtime.Time) unixtime.Time {
		timeDiff := endTime.Time().Unix() - startTime.Time().Unix() + 1
		randomUnixTime := int32(startTime.Time().Unix() + rnd.Int63n(timeDiff))
		return unixtime.Unix(randomUnixTime)
	}

	return server.Document{
		Id:         docId,
		Headline:   fmt.Sprintf("Document %v", docId),
		InsertDate: randomTimeBetween(startTime, endTime),
		Source:     fakeNewsSources[rnd.Intn(len(fakeNewsSources))],
		Url:        fmt.Sprintf("http://www.example.com/fake-document/%v", docId),
	}
}