-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
187 lines (161 loc) · 3.69 KB
/
Copy pathmain.go
File metadata and controls
187 lines (161 loc) · 3.69 KB
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package main
import (
"encoding/csv"
"fmt"
"math"
"os"
"strconv"
radix "github.com/armon/go-radix"
)
// LegBTS is representing each row in bts file
type LegBTS struct {
Prefix string
Calls int
Minutes float64
Rate float64
Income float64
}
// Leg is representing each row in mb file
type Leg struct {
Source string
Destination string
Minutes float64
Rate float64
Income float64
Currency string
Prefix string
}
func main() {
btsFile, err := os.Open("./mexico.csv")
if err != nil {
fmt.Println(err)
}
defer btsFile.Close()
btsReader := csv.NewReader(btsFile)
btsReader.FieldsPerRecord = -1
btsReader.LazyQuotes = true
btsData, err := btsReader.ReadAll()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
r := radix.New()
var legsBTS []LegBTS
for i, each := range btsData {
if i != 0 {
r.Insert(each[1], r)
var legBTS LegBTS
legBTS.Prefix = each[1]
legBTS.Calls, _ = strconv.Atoi(each[2])
legBTS.Minutes, _ = strconv.ParseFloat(each[3], 64)
legBTS.Rate, _ = strconv.ParseFloat(each[4], 64)
legBTS.Income, _ = strconv.ParseFloat(each[5], 64)
legsBTS = append(legsBTS, legBTS)
}
}
csvFile, err := os.Open("./logs.csv")
if err != nil {
fmt.Println(err)
}
defer csvFile.Close()
reader := csv.NewReader(csvFile)
reader.FieldsPerRecord = -1
reader.LazyQuotes = true
mbData, err := reader.ReadAll()
if err != nil {
fmt.Println(err)
os.Exit(2)
}
var legs []Leg
for i, each := range mbData {
if i != 0 {
rate, _ := strconv.ParseFloat(each[3], 64)
seconds, _ := strconv.ParseFloat(each[2], 64)
var leg Leg
leg.Source = each[0]
leg.Destination = each[1]
leg.Minutes = math.Ceil(seconds / 60)
leg.Rate = rate / 1000000
leg.Income = leg.Minutes * leg.Rate
leg.Currency = each[4]
// Find the longest prefix match
leg.Prefix, _, _ = r.LongestPrefix(each[1])
if each[4] != "EUR" {
fmt.Printf("call to %s is billed in %s", each[1], each[4])
os.Exit(3)
}
legs = append(legs, leg)
}
}
aggLegs := map[string]map[float64]LegBTS{}
for _, sl := range legs {
newRateMap := map[float64]LegBTS{}
if rateMap, ok := aggLegs[sl.Prefix]; ok {
if elem, ok := rateMap[sl.Rate]; ok {
rateMap[sl.Rate] = LegBTS{
Prefix: elem.Prefix,
Calls: elem.Calls + 1,
Minutes: elem.Minutes + sl.Minutes,
Rate: elem.Rate,
Income: elem.Income + sl.Income,
}
} else {
rateMap[sl.Rate] = LegBTS{
Prefix: sl.Prefix,
Calls: 1,
Minutes: sl.Minutes,
Rate: sl.Rate,
Income: sl.Income,
}
}
} else {
newRateMap[sl.Rate] = LegBTS{
Prefix: sl.Prefix,
Calls: 1,
Minutes: sl.Minutes,
Rate: sl.Rate,
Income: sl.Income,
}
aggLegs[sl.Prefix] = newRateMap
}
}
file, err := os.OpenFile("agg_data.csv", os.O_CREATE|os.O_WRONLY, 0666)
if err != nil {
fmt.Print(err)
os.Exit(5)
}
defer file.Close()
writer := csv.NewWriter(file)
defer writer.Flush()
headers := []string{
"Prefix",
"Calls",
"Minutes",
"Rate",
"Outcome",
}
if err := writer.Write(headers); err != nil {
fmt.Printf("couldn't write headers: %v", err)
os.Exit(6)
}
others := [][]string{}
count := 0
for _, aggLegPrefix := range aggLegs {
for _, item := range aggLegPrefix {
line := make([]string, len(headers))
line[0] = item.Prefix
line[1] = fmt.Sprintf("%d", item.Calls)
line[2] = fmt.Sprintf("%.0f", item.Minutes)
line[3] = fmt.Sprintf("%.4f", item.Rate)
line[4] = fmt.Sprintf("%.4f", item.Income)
others = append(others, line)
}
count++
}
// SAVE
if err := writer.WriteAll(others); err != nil {
fmt.Printf("couldn't save %v: %s", others, err)
}
writer.Flush()
fmt.Print("enjoy!\n")
}