-
Notifications
You must be signed in to change notification settings - Fork 3
/
gf_crawl_stats__by_day.go
317 lines (262 loc) · 9.75 KB
/
gf_crawl_stats__by_day.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/*
GloFlow application and media management/publishing platform
Copyright (C) 2019 Ivan Trajkovic
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package gf_crawl_stats
import (
"fmt"
"time"
"strconv"
"sort"
"context"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/bson"
"github.com/gloflow/gloflow/go/gf_core"
)
//-------------------------------------------------
type Gf_stats__objs_by_days struct {
Obj_type_str string `json:"obj_type_str" bson:"obj_type_str"`
Counts_by_day__sorted_lst []int `json:"counts_by_day__sorted_lst" bson:"counts_by_day__sorted_lst"` //global count of fetches per day
Domain_counts_by_day__sorted_lst []*Gf_domain_counts_for_all_days `json:"domain_counts_by_day__sorted_lst" bson:"domain_counts_by_day__sorted_lst"` //counts of fetches per domain per day
}
type Gf_domain_counts_for_all_days struct {
Domain_str string `json:"domain_str" bson:"domain_str"`
Total_count_int int `json:"total_count_int" bson:"total_count_int"`
Days_counts_lst []int `json:"days_counts_lst" bson:"days_counts_lst"`
}
//-------------------------------------------------
func stats__objs_by_days(p_match_query_map map[string]interface{},
p_obj_type_str string,
pRuntimeSys *gf_core.RuntimeSys) (*Gf_stats__objs_by_days, *gf_core.GFerror) {
pRuntimeSys.LogFun("FUN_ENTER","gf_crawl_stats__by_day.stats__objs_by_days()")
type Domain_objs__stat struct {
Domain_str string `bson:"_id"`
Count_int int `bson:"count_int"`
Creation_times_lst []float64 `bson:"creation_times_lst"`
}
match_query := bson.M{
"t": p_obj_type_str,
}
for k,v := range p_match_query_map {
match_query[k] = v
}
ctx := context.Background()
pipeline := mongo.Pipeline{
{
{"$match", match_query},
},
{
{"$project", bson.D{
{"creation_unix_time_f", true},
{"domain_str", true},
}},
},
{
{"$sort", bson.D{{"creation_unit_time_f", -1},}},
},
{
{"$group", bson.D{
{"_id", "$domain_str"},
{"count_int", bson.M{"$sum": 1}},
{"creation_times_lst", bson.M{"$push": "$creation_unix_time_f"}},
}},
},
{
{"$sort", bson.D{{"fetches_count_int", -1},}},
},
}
/*pipe := pRuntimeSys.Mongo_db.Collection("gf_crawl").Pipe([]bson.M{
// bson.M{"$match":bson.M{
// "t":p_obj_type_str, //"crawler_url_fetch",
// },
// },
bson.M{"$match": match_query},
bson.M{"$project": bson.M{
"creation_unix_time_f": true,
"domain_str": true,
},
},
bson.M{"$sort":bson.M{
"creation_unix_time_f":-1,
},
},
bson.M{"$group":bson.M{
"_id": "$domain_str",
"count_int": bson.M{"$sum" :1},
"creation_times_lst": bson.M{"$push":"$creation_unix_time_f"},
},
},
bson.M{"$sort":bson.M{
"fetches_count_int": -1,
},
},
})*/
cursor, err := pRuntimeSys.Mongo_db.Collection("gf_crawl").Aggregate(ctx, pipeline)
if err != nil {
gf_err := gf_core.MongoHandleError("failed to run an aggregation pipeline to count objects by days",
"mongodb_aggregation_error",
map[string]interface{}{},
err, "gf_crawl_stats", pRuntimeSys)
return nil, gf_err
}
defer cursor.Close(ctx)
/*results_lst := []Domain_objs__stat{}
err := pipe.AllowDiskUse().All(&results_lst)
if err != nil {
gf_err := gf_core.MongoHandleError("failed to run an aggregation pipeline to count objects by days",
"mongodb_aggregation_error",
map[string]interface{}{"obj_type_str": p_obj_type_str,},
err, "gf_crawl_stats", pRuntimeSys)
return nil, gf_err
}*/
results_lst := []Domain_objs__stat{}
for cursor.Next(ctx) {
var r Domain_objs__stat
err := cursor.Decode(&r)
if err != nil {
gf_err := gf_core.MongoHandleError("failed to run an aggregation pipeline to count objects by days",
"mongodb_cursor_decode",
map[string]interface{}{},
err, "gf_crawl_stats", pRuntimeSys)
return nil, gf_err
}
results_lst = append(results_lst, r)
}
//--------------------
// AGGREGATE DAY COUNTS
type stat__objs_in_day struct {
Year_int int `bson:"year_int" json:"year_int"`
Day_int int `bson:"day_int" json:"day_int"`
Total_count_int int `bson:"total_count_int" json:"total_count_int"`
Total_count__per_domain_map map[string]int `bson:"total_count__per_domain_map" json:"total_count__per_domain_map"`
}
days_stats_map := map[int]*stat__objs_in_day{}
days_keys_lst := []int{}
all_domains_total_counts_map := map[string]int{} // total count of all days
for _, domain := range results_lst {
all_domains_total_counts_map[domain.Domain_str] = domain.Count_int
// IMPORTANT!! - process each creation timestamp for a fetch
for _, t := range domain.Creation_times_lst {
tm := time.Unix(int64(t), 0)
year_day_id_int, _ := strconv.Atoi(fmt.Sprintf("%d%d", tm.Year(), tm.YearDay()))
//--------------
var day_stat *stat__objs_in_day
if cached_stat, ok := days_stats_map[year_day_id_int]; ok {
day_stat = cached_stat
} else {
//-----------------
// CREATE_NEW
new_stat := &stat__objs_in_day{
Year_int: tm.Year(),
Day_int: tm.YearDay(),
Total_count__per_domain_map: map[string]int{},
}
days_stats_map[year_day_id_int] = new_stat
day_stat = new_stat
days_keys_lst = append(days_keys_lst, year_day_id_int)
//-----------------
}
//--------------
day_stat.Total_count_int++
day_stat.Total_count__per_domain_map[domain.Domain_str]++
}
}
//--------------------
// SORT DAY STATS BY DAY
sort.Ints(days_keys_lst)
stats__sorted_by_day_lst := []*stat__objs_in_day{}
for _,k := range days_keys_lst {
day_stat := days_stats_map[k]
stats__sorted_by_day_lst = append(stats__sorted_by_day_lst, day_stat)
}
//------------------
// ZERO_OUT_BLANK_VALUES
// since in a particular day not all possible domain fetches are done, some of the domains counts
// for some day are 0, and this needs to be set manually, by checking if counts exist for that day.
// if no then set it to 0.
for _, day_stat := range stats__sorted_by_day_lst {
for domain_str, _ := range all_domains_total_counts_map {
if _, ok := day_stat.Total_count__per_domain_map[domain_str]; !ok {
day_stat.Total_count__per_domain_map[domain_str] = 0
}
}
}
//----------------------
// ACCUMULATE DOMAINS COUNTS IN COLUMNS
// gets for each domain a list of fetches number for each day
// this is formated this way for easy feeding of columns of data (per domain)
// to visualization routines, without need to do a bunch of app joins client side
domains__counts_for_all_days_lst := []*Gf_domain_counts_for_all_days{} // map[string][]int{}
for domain_str, _ := range all_domains_total_counts_map {
domain_days_count_lst := []int{}
for _, day_stat := range stats__sorted_by_day_lst {
domain_day_count_int := day_stat.Total_count__per_domain_map[domain_str]
domain_days_count_lst = append(domain_days_count_lst, domain_day_count_int)
}
//domains__counts_for_all_days_map[domain_str] = domain_days_count_lst
d := &Gf_domain_counts_for_all_days{
Domain_str: domain_str,
Total_count_int: all_domains_total_counts_map[domain_str],
Days_counts_lst: domain_days_count_lst,
}
domains__counts_for_all_days_lst = append(domains__counts_for_all_days_lst, d)
}
//----------------------
// SORT
sort__domains_counts(domains__counts_for_all_days_lst, pRuntimeSys)
//------------------
// ACCUMULATE LIST OF GLOBAL FETCH COUNTS PER DAY
total_counts_by_day__sorted_lst := []int{}
for _, day_stat := range stats__sorted_by_day_lst {
total_counts_by_day__sorted_lst = append(total_counts_by_day__sorted_lst, day_stat.Total_count_int)
}
//------------------
stats := &Gf_stats__objs_by_days{
Obj_type_str: p_obj_type_str,
Counts_by_day__sorted_lst: total_counts_by_day__sorted_lst,
Domain_counts_by_day__sorted_lst: domains__counts_for_all_days_lst,
}
//------------------
return stats, nil
}
//-------------------------------------------------
// SORT
type domains_counts []*Gf_domain_counts_for_all_days
func (d_lst domains_counts) Len() int {
return len(d_lst)
}
func (d_lst domains_counts) Swap(i, j int) {
d_lst[i],d_lst[j] = d_lst[j],d_lst[i]
}
func (d_lst domains_counts) Less(i, j int) bool {
return d_lst[i].Total_count_int > d_lst[j].Total_count_int
}
func sort__domains_counts(p_domains__counts_for_all_days_lst []*Gf_domain_counts_for_all_days, pRuntimeSys *gf_core.RuntimeSys) {
/*//------------------
//SORT
func (s domains_counts) Len() int {
return len(s)
}
func (s domains_counts) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
func (s domains_counts) Less(i, j int) bool {
//return len(s[i]) < len(s[j])
a:=s[i].(*domain_counts_for_all_days)
b:=s[i].(*domain_counts_for_all_days)
return a.Domain_total_fetches_count_int < b.Domain_total_fetches_count_int
}*/
sort.Sort(domains_counts(p_domains__counts_for_all_days_lst))
return
}