Skip to content

Commit a837b11

Browse files
authored
Fix the merged CA bug that use 20180601 for the first several years (#265)
* add log * fix unitest * Update directory.go * more log * more * fix merge func * fix * fox unittest * clean up code * Update directory.go * Update directory.go * Update directory_test.go * Update directory.go
1 parent d3f6f21 commit a837b11

File tree

4 files changed

+44
-11
lines changed

4 files changed

+44
-11
lines changed

directory/directory.go

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,9 @@ var (
3838

3939
// CompositeAnnotator wraps several annotators, and calls to Annotate() are forwarded to all of them.
4040
type CompositeAnnotator struct {
41-
// latest date of the component annotators. This is precomputed, and returned by AnnotatorDate()
42-
latestDate time.Time
41+
// date of CompositeAnnotator is the earliest date of anntators inside this CA.
42+
// It is precomputed, and returned by AnnotatorDate()
43+
date time.Time
4344
annotators []api.Annotator
4445
}
4546

@@ -56,11 +57,19 @@ func (ca CompositeAnnotator) Annotate(ip string, ann *api.GeoData) error {
5657
return nil
5758
}
5859

60+
// PrintALl prints all dates inside this CompositeAnnotator
61+
func (ca CompositeAnnotator) PrintAll() {
62+
log.Println("Date of this CA: ", ca.date.Format("20160102"))
63+
log.Println("contains anntators with the following dates:")
64+
for i := range ca.annotators {
65+
log.Println(ca.annotators[i].AnnotatorDate().Format("20160102"))
66+
}
67+
}
5968
// AnnotatorDate returns the date of the most recent wrapped annotator. Most recent is returned
6069
// as we try to apply the most recent annotators that predate the test we are annotating. So the
6170
// most recent of all the annotators is the date that should be compared to the test date.
6271
func (ca CompositeAnnotator) AnnotatorDate() time.Time {
63-
return ca.latestDate
72+
return ca.date
6473
}
6574

6675
// Compute the latest AnnotatorDate() value from a slice of annotators.
@@ -75,6 +84,17 @@ func computeLatestDate(annotators []api.Annotator) time.Time {
7584
return t
7685
}
7786

87+
func computerEarliestDate(annotators []api.Annotator) time.Time {
88+
t := time.Now()
89+
for i := range annotators {
90+
at := annotators[i].AnnotatorDate()
91+
if at.Before(t) {
92+
t = at
93+
}
94+
}
95+
return t
96+
}
97+
7898
// String creates a string representation of the CA.
7999
// Base annotators will appear as [YYYYMMDD], and composite annotators as (A1A2), e.g.,
80100
// ([20100102]([20110304][20120506]))
@@ -95,7 +115,7 @@ func NewCompositeAnnotator(annotators []api.Annotator) api.Annotator {
95115
if annotators == nil {
96116
return nil
97117
}
98-
ca := CompositeAnnotator{latestDate: computeLatestDate(annotators), annotators: annotators}
118+
ca := CompositeAnnotator{date: computerEarliestDate(annotators), annotators: annotators}
99119
return ca
100120
}
101121

@@ -157,7 +177,7 @@ func advance(lists [][]api.Annotator) ([][]api.Annotator, bool) {
157177

158178
// MergeAnnotators merges multiple lists of annotators, and returns a list of CompositeAnnotators.
159179
// Result will include a separate CompositeAnnotator for each unique date in any list, and each
160-
// CA will include the most recent annotator from each list, prior to or equal to the CA date.
180+
// CA will include annotator of different types, that was the earlist available one after the CA date.
161181
func MergeAnnotators(lists ...[]api.Annotator) []api.Annotator {
162182
listCount := len(lists)
163183
if listCount == 0 {
@@ -229,3 +249,11 @@ func (d *Directory) lastEarlierThan(date time.Time) api.Annotator {
229249
}
230250
return d.annotators[index-1]
231251
}
252+
253+
func (d *Directory) PrintAll() {
254+
log.Println("Here are all datasets in dir currently:")
255+
for _, ann := range d.annotators {
256+
ann.(CompositeAnnotator).PrintAll()
257+
}
258+
log.Println("end of dir dataset list")
259+
}

directory/directory_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ func TestBuild(t *testing.T) {
5555
}{
5656
{"20170101", "20140608"},
5757
{"20110101", "20100124"},
58+
{"20180501", "20180408"},
5859
// TODO: Add test cases.
5960
}
6061
for _, tt := range tests {
@@ -115,6 +116,15 @@ func TestMergeAnnotators(t *testing.T) {
115116
t.Run(tt.name, func(t *testing.T) {
116117
got := directory.MergeAnnotators(tt.lists[0], tt.lists[1])
117118
// This is just a hack to allow us to create a useful signature.
119+
expectedDate := make([]string, 3)
120+
expectedDate[0] = "[20100101]"
121+
expectedDate[1] = "[20100203]"
122+
expectedDate[2] = "[20110101]"
123+
for i, ann := range got {
124+
if expectedDate[i] != ann.AnnotatorDate().Format("[20060102]") {
125+
t.Errorf("Expect AnnotateDate %s got %s", expectedDate[i], ann.AnnotatorDate().Format("[20060102]"))
126+
}
127+
}
118128
gotString := directory.NewCompositeAnnotator(got).(directory.CompositeAnnotator).String()
119129
if gotString != tt.want {
120130
t.Errorf("MergeAnnotators() =\n %v want:\n %v", gotString, tt.want)

manager/manager.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -188,18 +188,14 @@ func (bldr *listBuilder) build() []api.Annotator {
188188

189189
// merge the legacy V4 & V6 annotators
190190
legacy := mergeV4V6(bldr.legacyV4.Fetch(), bldr.legacyV6.Fetch(), "legacy")
191-
192191
// Now append the Geolite2 annotators
193192
g2 := directory.SortSlice(bldr.geolite2.Fetch())
194-
195193
geo := make([]api.Annotator, 0, len(g2)+len(legacy))
196194
geo = append(geo, legacy...)
197195
geo = append(geo, g2...)
198-
199196
// here we have all the geo annotators in the ordered list.
200197
// now merge the ASN V4 & V6 annotators
201198
asn := mergeV4V6(bldr.asnV4.Fetch(), bldr.asnV6.Fetch(), "ASN")
202-
203199
// and now we need to create the composite annotators. First list is the
204200
// geo annotators, the second is the ASN
205201
combo := directory.MergeAnnotators(geo, asn)
@@ -217,7 +213,6 @@ func (bldr *listBuilder) build() []api.Annotator {
217213
func mergeV4V6(v4Annotators, v6Annotators []api.Annotator, discriminator string) []api.Annotator {
218214
v4 := directory.SortSlice(v4Annotators)
219215
v6 := directory.SortSlice(v6Annotators)
220-
221216
var merged []api.Annotator
222217
if len(v4)*len(v6) < 1 {
223218
log.Printf("empty v4 or v6 annotator list for %s data, skipping", discriminator)

manager/manager_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func TestInitDataset(t *testing.T) {
5454
`{"Geo":{"continent_code":"AS","country_code":"TH","country_code3":"THA","country_name":"Thailand","region":"40","city":"Bangkok","latitude":13.754,"longitude":100.501},"Network":{"Systems":[{"ASNs":[23969]}]}}`},
5555
// This request needs a geolite2 dataset
5656
{"1.9.128.0", "1512086400",
57-
`{"Geo":{"continent_code":"AS","country_code":"MY","country_code3":"MYS","country_name":"Malaysia","region":"05","city":"Pantai","latitude":2.787,"longitude":101.995},"Network":{"Systems":[{"ASNs":[4788]}]}}`},
57+
`{"Geo":{"continent_code":"AS","country_code":"MY","country_code3":"MYS","country_name":"Malaysia","region":"14","city":"Kuala Lumpur","postal_code":"50586","latitude":3.167,"longitude":101.7},"Network":{"Systems":[{"ASNs":[4788]}]}}`},
5858
// This request needs the latest dataset in the memory.
5959
{"1.22.128.0", "1544400000",
6060
`{"Geo":{"continent_code":"AS","country_code":"IN","country_name":"India","region":"HR","city":"Faridabad","latitude":28.4333,"longitude":77.3167},"Network":{"Systems":[{"ASNs":[45528]}]}}`},

0 commit comments

Comments
 (0)