示例#1
0
文件: db.go 项目: MarkBruns/gcse
func statTops(N int) []StatList {
	indexDB := indexDBBox.Get().(*index.TokenSetSearcher)
	if indexDB == nil {
		return nil
	}

	var topStaticScores []gcse.HitInfo
	var tssProjects villa.StrSet

	topImported := NewTopN(func(a, b interface{}) int {
		ia, ib := a.(gcse.HitInfo), b.(gcse.HitInfo)
		return villa.IntValueCompare(len(ia.Imported)+len(ia.TestImported),
			len(ib.Imported)+len(ib.TestImported))
	}, N)

	topTestStatic := NewTopN(func(a, b interface{}) int {
		return villa.FloatValueCompare(a.(gcse.HitInfo).TestStaticScore,
			b.(gcse.HitInfo).TestStaticScore)
	}, N)

	sites := make(map[string]int)

	indexDB.Search(nil, func(docID int32, data interface{}) error {
		hit := data.(gcse.HitInfo)
		orgName := hit.Name
		hit.Name = packageShowName(hit.Name, hit.Package)

		// assuming all packages has been sorted by static-scores.
		if len(topStaticScores) < N {
			if len(hit.Imported) > 0 &&
				orgName != "" && orgName != "main" &&
				!inProjects(tssProjects, hit.ProjectURL) {
				topStaticScores = append(topStaticScores, hit)
				tssProjects.Put(hit.ProjectURL)
			}
		}

		if len(hit.TestImported) > 0 {
			topTestStatic.Append(hit)
		}
		topImported.Append(hit)

		host := strings.ToLower(gcse.HostOfPackage(hit.Package))
		if host != "" {
			sites[host] = sites[host] + 1
		}

		return nil
	})

	tlStaticScore := StatList{
		Name:  "Hot",
		Info:  "refs stars",
		Items: make([]StatItem, 0, len(topStaticScores)),
	}
	for idx, hit := range topStaticScores {
		tlStaticScore.Items = append(tlStaticScore.Items, StatItem{
			Index:   idx + 1,
			Name:    hit.Name,
			Package: hit.Package,
			Info:    fmt.Sprintf("%d %d", len(hit.Imported), hit.StarCount),
		})
	}

	tlTestStatic := StatList{
		Name:  "Hot Test",
		Info:  "refs stars",
		Items: make([]StatItem, 0, topTestStatic.Len()),
	}
	for idx, item := range topTestStatic.PopAll() {
		hit := item.(gcse.HitInfo)
		tlTestStatic.Items = append(tlTestStatic.Items, StatItem{
			Index:   idx + 1,
			Name:    hit.Name,
			Package: hit.Package,
			Info: fmt.Sprintf("%d %d", len(hit.TestImported),
				hit.StarCount),
		})
	}

	tlImported := StatList{
		Name:  "Most Imported",
		Info:  "refs",
		Items: make([]StatItem, 0, topImported.Len()),
	}
	for idx, item := range topImported.PopAll() {
		hit := item.(gcse.HitInfo)
		tlImported.Items = append(tlImported.Items, StatItem{
			Index:   idx + 1,
			Name:    hit.Name,
			Package: hit.Package,
			Info:    fmt.Sprintf("%d", len(hit.Imported)+len(hit.TestImported)),
		})
	}

	topSites := NewTopN(func(a, b interface{}) int {
		return villa.IntValueCompare(sites[a.(string)], sites[b.(string)])
	}, N)
	for site := range sites {
		topSites.Append(site)
	}
	tlSites := StatList{
		Name:  "Sites",
		Info:  "packages",
		Items: make([]StatItem, 0, topSites.Len()),
	}
	for idx, st := range topSites.PopAll() {
		site := st.(string)
		cnt := sites[site]
		tlSites.Items = append(tlSites.Items, StatItem{
			Index: idx + 1,
			Name:  site,
			Link:  "http://" + site,
			Info:  fmt.Sprintf("%d", cnt),
		})
	}

	return []StatList{
		tlStaticScore, tlTestStatic, tlImported, tlSites,
	}
}
示例#2
0
func statTops(N int) []StatList {
	db := getDatabase()
	if db == nil {
		return nil
	}
	var topStaticScores []gcse.HitInfo
	var tssProjects stringsp.Set

	topImported := NewTopN(func(a, b interface{}) bool {
		ia, ib := a.(gcse.HitInfo), b.(gcse.HitInfo)
		return ia.ImportedLen+ia.TestImportedLen < ib.ImportedLen+ib.TestImportedLen
	}, N)

	topTestStatic := NewTopN(func(a, b interface{}) bool {
		return a.(gcse.HitInfo).TestStaticScore < b.(gcse.HitInfo).TestStaticScore
	}, N)

	sites := make(map[string]int)

	db.Search(nil, func(_ int32, data interface{}) error {
		hit := data.(gcse.HitInfo)
		orgName := hit.Name
		hit.Name = packageShowName(hit.Name, hit.Package)

		// assuming all packages has been sorted by static-scores.
		if len(topStaticScores) < N {
			if hit.ImportedLen > 0 && orgName != "" && orgName != "main" && !inProjects(tssProjects, hit.ProjectURL) {
				topStaticScores = append(topStaticScores, hit)
				tssProjects.Add(hit.ProjectURL)
			}
		}
		if hit.TestImportedLen > 0 {
			topTestStatic.Append(hit)
		}
		topImported.Append(hit)

		host := strings.ToLower(gcse.HostOfPackage(hit.Package))
		if host != "" {
			sites[host] = sites[host] + 1
		}
		return nil
	})
	tlStaticScore := StatList{
		Name:  "Hot",
		Info:  "refs stars",
		Items: make([]StatItem, 0, len(topStaticScores)),
	}
	for idx, hit := range topStaticScores {
		tlStaticScore.Items = append(tlStaticScore.Items, StatItem{
			Index:   idx + 1,
			Name:    hit.Name,
			Package: hit.Package,
			Info:    fmt.Sprintf("%d %d", hit.ImportedLen, hit.StarCount),
		})
	}
	tlTestStatic := StatList{
		Name:  "Hot Test",
		Info:  "refs stars",
		Items: make([]StatItem, 0, topTestStatic.Len()),
	}
	for idx, item := range topTestStatic.PopAll() {
		hit := item.(gcse.HitInfo)
		tlTestStatic.Items = append(tlTestStatic.Items, StatItem{
			Index:   idx + 1,
			Name:    hit.Name,
			Package: hit.Package,
			Info:    fmt.Sprintf("%d %d", hit.TestImportedLen, hit.StarCount),
		})
	}
	tlImported := StatList{
		Name:  "Most Imported",
		Info:  "refs",
		Items: make([]StatItem, 0, topImported.Len()),
	}
	for idx, item := range topImported.PopAll() {
		hit := item.(gcse.HitInfo)
		tlImported.Items = append(tlImported.Items, StatItem{
			Index:   idx + 1,
			Name:    hit.Name,
			Package: hit.Package,
			Info:    fmt.Sprintf("%d", hit.ImportedLen+hit.TestImportedLen),
		})
	}
	topSites := NewTopN(func(a, b interface{}) bool {
		return sites[a.(string)] < sites[b.(string)]
	}, N)
	for site := range sites {
		topSites.Append(site)
	}
	tlSites := StatList{
		Name:  "Sites",
		Info:  "packages",
		Items: make([]StatItem, 0, topSites.Len()),
	}
	for idx, st := range topSites.PopAll() {
		site := st.(string)
		cnt := sites[site]
		tlSites.Items = append(tlSites.Items, StatItem{
			Index: idx + 1,
			Name:  site,
			Link:  "http://" + site,
			Info:  fmt.Sprintf("%d", cnt),
		})
	}
	return []StatList{
		tlStaticScore, tlTestStatic, tlImported, tlSites,
	}
}