-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfeatscomp.go
135 lines (113 loc) · 2.7 KB
/
featscomp.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// Copyright 2014-2015 The DevMine authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package featscomp computes features.
package main
import (
"flag"
"fmt"
"os"
"sync"
"time"
_ "github.com/lib/pq"
"github.com/DevMine/featscomp/config"
"github.com/DevMine/featscomp/features"
"github.com/DevMine/featscomp/util"
)
const (
averageForksName = "average_forks"
averageStarsName = "average_stars"
commitsCountName = "commits_count"
contributionsCountName = "contributions_count"
followersCountName = "followers_count"
hireableName = "hireable"
)
func main() {
flag.Usage = func() {
fmt.Printf("usage: %s [CONFIGURATION_FILE]\n", os.Args[0])
flag.PrintDefaults()
os.Exit(0)
}
flag.Parse()
configPath := flag.Arg(0)
if len(configPath) == 0 {
flag.Usage()
}
cfg, err := config.ReadConfig(configPath)
if err != nil {
fatal(err)
}
db, err := util.OpenDBSession(cfg.Database)
if err != nil {
fatal(err)
}
defer db.Close()
var feats []features.Feature
cf := cfg.Features
// insert features in the database
// TODO refactor this since all features implement the same interface.
if cf.AverageForks {
averageForks, err := features.NewAverageForks(averageForksName, db)
if err != nil {
fatal(err)
}
feats = append(feats, averageForks)
}
if cf.AverageStars {
averageStars, err := features.NewAverageStars(averageStarsName, db)
if err != nil {
fatal(err)
}
feats = append(feats, averageStars)
}
if cf.CommitsCount {
commitsCount, err := features.NewCommitsCount(commitsCountName, db)
if err != nil {
fatal(err)
}
feats = append(feats, commitsCount)
}
if cf.ContributionsCount {
contributionsCount, err := features.NewContributionsCount(contributionsCountName, db)
if err != nil {
fatal(err)
}
feats = append(feats, contributionsCount)
}
if cf.FollowersCount {
followersCount, err := features.NewFollowersCount(followersCountName, db)
if err != nil {
fatal(err)
}
feats = append(feats, followersCount)
}
if cf.Hireable {
hireable, err := features.NewHireable(hireableName, db)
if err != nil {
fatal(err)
}
feats = append(feats, hireable)
}
run(feats)
}
func run(feats []features.Feature) {
var wg sync.WaitGroup
wg.Add(len(feats))
tic := time.Now()
for _, f := range feats {
fmt.Printf("computing feature %s\n", f.Name())
go func(f features.Feature) {
defer wg.Done()
if err := f.Score(); err != nil {
util.FPrintlnErr(err)
}
}(f)
}
wg.Wait()
toc := time.Now()
fmt.Println("total elapsed time: ", toc.Sub(tic))
}
func fatal(v ...interface{}) {
fmt.Fprintln(os.Stderr, v...)
os.Exit(1)
}