This repository has been archived by the owner on Apr 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
identifier.go
99 lines (84 loc) · 2.45 KB
/
identifier.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package main
import (
"encoding/gob"
"os"
"strconv"
"time"
"github.com/google/go-github/github"
"github.com/patrickmn/go-cache"
"github.com/sirupsen/logrus"
"github.com/ti-community-infra/devstats/internal/pkg/identifier"
"github.com/ti-community-infra/devstats/internal/pkg/lib"
)
func main() {
// Init context.
var ctx identifier.Ctx
err := ctx.Init()
lib.FatalOnError(err)
// Init logger.
log := logrus.WithField("program", "identifier")
// Init database client.
db, err := lib.NewConn(ctx.IDDbDialect, ctx.IDDbHost, ctx.IDDbPort, ctx.IDDbUser, ctx.IDDbPass, ctx.IDDbName)
lib.FatalOnError(err)
// Init database client used to import data.
pgPort, err := strconv.Atoi(ctx.PgPort)
lib.FatalOnError(err)
dataSource, err := lib.NewConn(
"postgresql", ctx.PgHost, pgPort, ctx.PgUser,
ctx.PgPass, ctx.PgDB,
)
lib.FatalOnError(err)
// Init the memory cache.
memCache := cache.New(15*24*time.Hour, 30*24*time.Hour)
gob.Register(github.Response{})
gob.Register(github.User{})
gob.Register(github.RateLimits{})
gob.Register(identifier.GitHubGetUserResult{})
gob.Register(identifier.LocationCacheEntry{})
gob.Register(lib.StringSet{})
_, err = os.Stat(ctx.CacheFilePath)
if err != nil && !os.IsExist(err) {
_, err := os.Create(ctx.CacheFilePath)
if err != nil {
panic(err)
}
}
err = memCache.LoadFile(ctx.CacheFilePath)
if err != nil && err.Error() != "EOF" {
log.WithError(err).Errorf("Failed to load cache from file: %s", ctx.CacheFilePath)
return
}
if !ctx.SkipAutoImportProfile {
// Init GitHub Client.
gc := identifier.GitHubClient{}
err = gc.Init(&ctx, log, memCache)
if err != nil {
lib.FatalOnError(err)
return
}
// Init Google Map Client.
locationClient := identifier.LocationClient{}
err = locationClient.Init(&ctx, log, memCache)
if err != nil {
lib.FatalOnError(err)
return
}
// Init employee manager.
employeeManager := identifier.EmployeeManager{}
err = employeeManager.Init(ctx, log, memCache)
if err != nil {
log.WithError(err).Errorf("Failed to init employee manager.")
return
}
identifier.AutoImportProfile(log, &ctx, db, dataSource, &gc, &locationClient, &employeeManager, memCache)
// Save the cache to file.
err = memCache.SaveFile(ctx.CacheFilePath)
if err != nil {
log.WithError(err).Errorf("Failed to save the memory cache to file: %s", ctx.CacheFilePath)
return
}
}
if !ctx.SkipOutputGitHubUserJSON {
identifier.OutputGitHubUserToJSON(log, &ctx, db)
}
}