forked from sourcegraph/srclib
/
stats.go
84 lines (72 loc) · 3.13 KB
/
stats.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package graph
// StatType is the name of a def statistic (see below for a listing).
type StatType string
// Stats holds statistics for a def.
type Stats map[StatType]int
const (
// StatXRefs is the number of external references to a def (i.e.,
// references from other repositories). It is only computed for abstract
// defs (see the docs for DefKey) because it is not easy to determine
// which specific commit a ref references (for external refs).
StatXRefs = "xrefs"
// StatRRefs is the number of references to a def from the same
// repository in which the def is defined. It is inclusive of the
// StatURefs count. It is only computed for concrete defs (see the docs
// for DefKey) because otherwise it would count 1 rref for each unique
// revision of the repository that we have processed. (It is easy to
// determine which specific commit an internal ref references; we just
// assume it references a def in the same commit.)
StatRRefs = "rrefs"
// StatURefs is the number of references to a def from the same source
// unit in which the def is defined. It is included in the StatRRefs
// count. It is only computed for concrete defs (see the docs for
// DefKey) because otherwise it would count 1 uref for each revision of
// the repository that we have processed.
StatURefs = "urefs"
// StatAuthors is the number of distinct resolved people who contributed
// code to a def's definition (according to a VCS "blame" of the
// version). It is only computed for concrete defs (see the docs for
// DefKey).
StatAuthors = "authors"
// StatClients is the number of distinct resolved people who have committed
// refs that reference a def. It is only computed for abstract defs
// (see the docs for DefKey) because it is not easy to determine which
// specific commit a ref references.
StatClients = "clients"
// StatDependents is the number of distinct repositories that contain refs
// that reference a def. It is only computed for abstract defs (see
// the docs for DefKey) because it is not easy to determine which
// specific commit a ref references.
StatDependents = "dependents"
// StatExportedElements is the number of exported defs whose path is a
// descendant of this def's path (and that is in the same repository and
// source unit). It is only computed for concrete defs (see the docs for
// DefKey) because otherwise it would count 1 exported element for each
// revision of the repository that we have processed.
StatExportedElements = "exported_elements"
)
var AllStatTypes = []StatType{StatXRefs, StatRRefs, StatURefs, StatAuthors, StatClients, StatDependents, StatExportedElements}
func (x StatType) IsAbstract() bool {
switch x {
case StatXRefs:
fallthrough
case StatClients:
fallthrough
case StatDependents:
return true
default:
return false
}
}
// UniqueRefDefs groups refs by the RefDefKey field and returns a map of
// how often each RefDefKey appears. If m is non-nil, counts are incremented
// and a new map is not created.
func UniqueRefDefs(refs []*Ref, m map[RefDefKey]int) map[RefDefKey]int {
if m == nil {
m = make(map[RefDefKey]int)
}
for _, ref := range refs {
m[ref.RefDefKey()]++
}
return m
}