forked from liudanking/didi-car-rank
-
Notifications
You must be signed in to change notification settings - Fork 0
/
analysis.go
166 lines (138 loc) · 3.72 KB
/
analysis.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package main
import (
"errors"
"fmt"
"os"
"path/filepath"
"sort"
"github.com/olekukonko/tablewriter"
"github.com/liudanking/goutil/encodingutil"
log "github.com/liudanking/goutil/logutil"
"github.com/urfave/cli"
)
func analysisCity(c *cli.Context) error {
dir := c.String("dir")
city := c.String("city")
analylizer := NewCityAnalyzer(dir, city)
if _, err := os.Lstat(analylizer.cityDataDir); err != nil {
return errors.New("未找到城市数据")
}
modelCount := analylizer.analysisCurrentOrder()
modelScore := analylizer.analysisRepurchase()
topn := c.Int("top")
analylizer.Output(modelCount, modelScore, topn)
return nil
}
type CityAnalyzer struct {
cityName string
cityDataDir string
}
func NewCityAnalyzer(dir, city string) *CityAnalyzer {
return &CityAnalyzer{
cityName: city,
cityDataDir: filepath.Join(dir, city),
}
}
type CarModelCount struct {
Model string
Count int
}
func (ca *CityAnalyzer) analysisCurrentOrder() map[string]int {
currentOrderDir := filepath.Join(ca.cityDataDir, "currentorder")
modelCount := map[string]int{}
filepath.Walk(currentOrderDir, func(path string, info os.FileInfo, err error) error {
if info.IsDir() {
return nil
}
items := map[string]CurrentOrderItem{}
if err := encodingutil.UnmarshalJSONFromFile(path, &items); err != nil {
log.Warning("unmarshal from %s failed:%v", path, err)
return nil
}
for _, item := range items {
if item.CarModel != "" {
modelCount[item.CarModel]++
}
}
return nil
})
return modelCount
}
type CarModelScore struct {
Model string
Score int
}
func (ca *CityAnalyzer) analysisRepurchase() map[string]int {
repurchaseDir := filepath.Join(ca.cityDataDir, "repurchase")
modelScore := map[string]int{}
filepath.Walk(repurchaseDir, func(path string, info os.FileInfo, err error) error {
if info.IsDir() {
return nil
}
items := map[string]RepurchaseItem{}
if err := encodingutil.UnmarshalJSONFromFile(path, &items); err != nil {
log.Warning("unmarshal from %s failed:%v", path, err)
return nil
}
for _, item := range items {
if item.CarModel != "" {
modelScore[item.CarModel] += item.OrderCount1M
}
}
return nil
})
return modelScore
}
func (ca *CityAnalyzer) Output(modelCount, modelScore map[string]int, topn int) {
carModelCountList := []CarModelCount{}
for model, count := range modelCount {
carModelCountList = append(carModelCountList, CarModelCount{
Model: model,
Count: count,
})
}
sort.Slice(carModelCountList, func(i, j int) bool { return carModelCountList[i].Count > carModelCountList[j].Count })
log.Notice("\n车型订单数量排名:")
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"排名", "车型", "实时订单数"})
for i, mc := range carModelCountList {
if i >= topn {
break
}
table.Append([]string{
fmt.Sprint(i + 1),
mc.Model,
fmt.Sprint(mc.Count),
})
}
table.Render()
carModelScoreList := []CarModelScore{}
for model, score := range modelScore {
carModelScoreList = append(carModelScoreList, CarModelScore{
Model: model,
Score: score,
})
}
sort.Slice(carModelScoreList, func(i, j int) bool { return carModelScoreList[i].Score > carModelScoreList[j].Score })
log.Notice("\n车型加油积分排名:")
table = tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"排名", "车型", "加油积分", "平均积分"})
for i, ms := range carModelScoreList {
if i >= topn {
break
}
avgScore := ""
if count, found := modelCount[ms.Model]; found && count != 0 {
avgScore = fmt.Sprintf("%.02f", float64(ms.Score)/float64(count))
} else {
avgScore = "N/A"
}
table.Append([]string{
fmt.Sprint(i + 1),
ms.Model,
fmt.Sprint(ms.Score),
avgScore,
})
}
table.Render()
}