Skip to content

Commit

Permalink
Merge pull request #24 from roma-glushko/17-geo-report-error-on-non-i…
Browse files Browse the repository at this point in the history
…p-entries

#17 [Bug] Geo Report can throw an error on saving
  • Loading branch information
roma-glushko authored Nov 20, 2019
2 parents d8adac6 + 0037f76 commit 8cc5d30
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 16 deletions.
16 changes: 13 additions & 3 deletions internal/infrastructure/geo/maxmind-geo-provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package geo
import (
"log"
"net"
"tango/internal/usecase/report"

"github.com/oschwald/geoip2-golang"
)
Expand All @@ -26,15 +27,24 @@ func NewMaxMindGeoProvider(maxmindGeoLibPath string) *MaxMindGeoProvider {
}

// GetGeoLocationByIP provides geo location data by IP
func (p *MaxMindGeoProvider) GetGeoLocationByIP(ip string) *geoip2.City {
func (p *MaxMindGeoProvider) GetGeoDataByIP(ip string) *report.GeoData {
parsedIP := net.ParseIP(ip)
geoLocation, err := p.maxmindCityDatabase.City(parsedIP)

if err != nil {
log.Fatal(err)
// todo: would be nice to log this errors
return &report.GeoData{
Country: "N/A",
City: "N/A",
Continent: "N/A",
}
}

return geoLocation
return &report.GeoData{
Country: geoLocation.Country.Names["en"],
City: geoLocation.City.Names["en"],
Continent: geoLocation.Continent.Names["en"],
}
}

// Close
Expand Down
6 changes: 3 additions & 3 deletions internal/infrastructure/writer/geo-report-csv-writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ func (w *GeoReportCsvWriter) Save(filePath string, geolocationReport map[string]
for ip, geoLocation := range geolocationReport {
err := writer.Write([]string{
ip,
geoLocation.Country,
geoLocation.City,
geoLocation.Continent,
geoLocation.GeoData.Country,
geoLocation.GeoData.City,
geoLocation.GeoData.Continent,
geoLocation.SampleRequest,
geoLocation.BrowserAgent,
strconv.FormatUint(geoLocation.Requests, 10),
Expand Down
21 changes: 11 additions & 10 deletions internal/usecase/report/geo-report-usecase.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,26 @@ package report

import (
"tango/internal/domain/entity"

"github.com/oschwald/geoip2-golang"
)

// Geo Location Data
type GeoData struct {
Country string
City string
Continent string
}

// Geolocation report information
type Geolocation struct {
Country string
City string
Continent string
GeoData *GeoData
SampleRequest string
BrowserAgent string
Requests uint64
}

// GeoLocationProvider provides ability to find geolocation data by IP
type GeoLocationProvider interface {
GetGeoLocationByIP(ip string) *geoip2.City // keep geoip2 references in the usecase layer, don't plan to support anything else
GetGeoDataByIP(ip string) *GeoData // keep geoip2 references in the usecase layer, don't plan to support anything else
Close() error
}

Expand Down Expand Up @@ -55,12 +58,10 @@ func (u *GeoReportUsecase) GenerateReport(reportPath string, accessRecords []ent
continue
}

geoLocation := u.geoLocationProvider.GetGeoLocationByIP(ip)
geoData := u.geoLocationProvider.GetGeoDataByIP(ip)

geoReport[ip] = &Geolocation{
Country: geoLocation.Country.Names["en"],
City: geoLocation.City.Names["en"],
Continent: geoLocation.Continent.Names["en"],
GeoData: geoData,
SampleRequest: accessRecord.URI,
BrowserAgent: accessRecord.UserAgent,
Requests: 1,
Expand Down

0 comments on commit 8cc5d30

Please sign in to comment.