-
Notifications
You must be signed in to change notification settings - Fork 0
/
set.go
167 lines (154 loc) · 4.13 KB
/
set.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
167
package twilio
import (
"encoding/json"
"fmt"
"io/ioutil"
"path/filepath"
"regexp"
"strconv"
"strings"
"github.com/grokify/gophonenumbers/common"
"github.com/grokify/simplego/io/ioutilmore"
"github.com/grokify/simplego/time/timeutil"
"github.com/grokify/simplego/type/stringsutil"
"github.com/rs/zerolog/log"
)
var MultiLimit = 0 // test limit to gracefully exit process early.
// MultiResults is designed to handle large volumes
// of requests.
type MultiResults struct {
CountsByStatusCode map[string]int
Responses map[string]*NumberInfo
}
func NewMultiResults() MultiResults {
return MultiResults{
CountsByStatusCode: map[string]int{},
Responses: map[string]*NumberInfo{}}
}
func (mr *MultiResults) Inflate() {
counts := map[string]int{}
for _, resp := range mr.Responses {
scStr := strconv.Itoa(resp.ApiResponseInfo.StatusCode)
if _, ok := counts[scStr]; !ok {
counts[scStr] = 0
}
counts[scStr]++
}
counts["all"] = len(mr.Responses)
mr.CountsByStatusCode = counts
}
func (mr *MultiResults) AddResponses(resps map[string]*NumberInfo) {
for k, v := range resps {
existing, ok := mr.Responses[k]
if !ok ||
(existing.ApiResponseInfo.StatusCode >= 300 && v.ApiResponseInfo.StatusCode < 300) {
mr.Responses[k] = v
}
}
}
func (mr *MultiResults) GetNumberInfo(e164Number string) (*NumberInfo, error) {
e164Number = strings.TrimSpace(e164Number)
if ni, ok := mr.Responses[e164Number]; ok {
return ni, nil
}
return nil, fmt.Errorf("number [%s] not found", e164Number)
}
func (mr *MultiResults) NumbersSuccess() []string {
numbers := []string{}
for _, resp := range mr.Responses {
pn := strings.TrimSpace(resp.PhoneNumber)
if len(pn) > 0 {
numbers = append(numbers, pn)
}
}
return numbers
}
func (mr *MultiResults) Write(filename string) error {
mr.Inflate()
bytes, err := json.MarshalIndent(mr, "", " ")
if err != nil {
return err
}
return ioutil.WriteFile(filename, bytes, 0644)
}
func GetWriteValidationMulti(client *Client, requestNumbers, skipNumbers []string, filenameBase string, logAt, fileAt uint) MultiResults {
uniquesRequests := stringsutil.SliceCondenseSpace(requestNumbers, true, true)
uniqueSkips := stringsutil.SliceCondenseSpace(skipNumbers, true, true)
uniqueSkipsMap := map[string]int{}
for _, pnSkip := range uniqueSkips {
uniqueSkipsMap[pnSkip] = 1
}
resps := NewMultiResults()
count := len(uniquesRequests)
i := 0
for _, e164Number := range uniquesRequests {
i++
if _, ok := uniqueSkipsMap[e164Number]; ok {
continue
}
validate, _ := client.Validate(
e164Number, &Params{Type: "carrier"})
resps.Responses[e164Number] = &validate
if logAt > 0 && i%int(logAt) == 0 {
/*apiStatus := "S"
if validate.ApiResponseInfo.StatusCode >= 300 {
apiStatus = "F"
}*/
log.Info().
Int("num", i).
Int("count", count).
Str("e164number", e164Number).
Int("httpStatus", validate.ApiResponseInfo.StatusCode).
Msg("logAt")
}
if fileAt > 0 && i%int(fileAt) == 0 && len(resps.Responses) > 0 {
err := resps.Write(common.BuildFilename(filenameBase, i, count))
if err != nil {
log.Error().Err(err)
}
resps = NewMultiResults()
}
if MultiLimit > 0 && i > MultiLimit {
break
}
}
if len(resps.Responses) > 0 {
err := resps.Write(common.BuildFilename(filenameBase, i, count))
if err != nil {
log.Error().Err(err)
}
}
return resps
}
func NewMultiResultsFiles(dir string, rxPattern string) (MultiResults, error) {
dir = strings.TrimSpace(dir)
if len(dir) == 0 {
dir = "."
}
all := NewMultiResults()
rx, err := regexp.Compile(rxPattern)
if err != nil {
return all, err
}
files, _, err := ioutilmore.ReadDirMore(dir, rx, true, true)
if err != nil {
return all, err
}
for _, fi := range files {
mResults := NewMultiResults()
err := ioutilmore.ReadFileJSON(
filepath.Join(dir, fi.Name()), &mResults)
if err != nil {
return all, err
}
fileModTime := fi.ModTime()
for key, ni := range mResults.Responses {
if timeutil.TimeIsZeroAny(ni.ApiResponseInfo.Time) {
ni.ApiResponseInfo.Time = fileModTime
mResults.Responses[key] = ni
}
}
all.AddResponses(mResults.Responses)
}
return all, nil
}