/
models_frontpage.go
513 lines (457 loc) · 15.6 KB
/
models_frontpage.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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
package marketplace
import (
"fmt"
"github.com/lib/pq"
"math/rand"
)
/*
Database Queries
*/
func FindItemsNewArrivals() SerpItems {
items := []SerpItem{}
database.
Table("vm_frontpage_items_new_arrivals").
Find(&items)
PriceInCurrencies(items)
return items
}
func FindItemsTopDrugs() SerpItems {
items := []SerpItem{}
database.
Table("vm_frontpage_items_top_drugs").
Find(&items)
PriceInCurrencies(items)
return items
}
func FindItemsTopOther() SerpItems {
items := []SerpItem{}
database.
Table("vm_frontpage_items_top_other").
Find(&items)
PriceInCurrencies(items)
return items
}
func removeDuplicates(elements pq.StringArray) pq.StringArray {
// Use map to record duplicates as we find them.
encountered := map[string]bool{}
result := pq.StringArray{}
for v := range elements {
if encountered[elements[v]] == true {
// Do not add duplicate.
} else {
// Record this element as an encountered element.
encountered[elements[v]] = true
// Append to result slice.
result = append(result, elements[v])
}
}
// Return the new slice.
return result
}
/* Randomly select categories to show, if vendor
items in more than 6 categories
*/
func setRandomCategories(slice pq.StringArray) pq.StringArray {
lengthArray := len(slice)
var categories pq.StringArray
if lengthArray <= 6 {
categories = slice
} else {
var generated map[int]bool
generated = make(map[int]bool)
for i := 0; i < 6; i++ {
foundUniqueInt := false
for !foundUniqueInt {
randomInt := rand.Intn(lengthArray)
if generated[randomInt] == true {
// do nothing
} else {
foundUniqueInt = true
generated[randomInt] = true
categories = append(categories, slice[i])
}
}
}
}
return categories
}
func (ais SerpStores) SetAdditionalData() {
for i, _ := range ais {
categoriesNameEnUnique := removeDuplicates(ais[i].CategoriesNameEn)
categoriesLimited := setRandomCategories(categoriesNameEnUnique)
ais[i].CategoriesNameEn = categoriesLimited
ais[i].ScoreString = fmt.Sprintf("%.2f", ais[i].Score)
}
}
func FindVendorsTop() SerpStores {
vendors := []SerpItem{}
database.Table("vm_frontpage_top_vendors").
Find(&vendors)
serpItems := SerpItems(vendors)
serpStoresUniqued := serpItems.SerpStores()
serpStoresUniqued.SetAdditionalData()
return serpStoresUniqued
}
func FindVendorsNew() SerpStores {
vendors := []SerpItem{}
database.Table("vm_frontpage_new_vendors").
Find(&vendors)
serpItems := SerpItems(vendors)
serpStoresUniqued := serpItems.SerpStores()
serpStoresUniqued.SetAdditionalData()
return serpStoresUniqued
}
func CountAvailableItems() int{
//
var count int
database.
Table("vm_available_items").
Count(&count)
return count
}
func GetCountItems() int {
key := "stats-" + "total-available-items"
cCountItems, _ := cache1h.Get(key)
countItems, ok := cCountItems.(int)
if ok == false {
countItems = CountAvailableItems()
cache1h.Set(key, countItems)
return countItems
}
return countItems
}
func CountAvailableVendors() int {
var count int
database.
Table("stores").Where("is_gold_account = ?", "True").
Or("is_silver_account = ?", "True").
Or("is_bronze_account = ?", "True").
Or("is_free_account = ?", "True").
Count(&count)
return count
}
func GetCountVendors() int {
key := "stats-" + "total-available-vendors"
cCountVendors, _ := cache1h.Get(key)
countVendors, ok := cCountVendors.(int)
if ok == false {
countVendors := CountAvailableVendors()
cache1h.Set(key, countVendors)
return countVendors
}
return countVendors
}
func CacheShippingToList() []string {
key := "shipping-to-list"
cShippingToList, _ := cache1h.Get(key)
shippingToList, ok := cShippingToList.([]string)
if ok == false {
ais := FindSerpItems()
shippingToList = ais.ShippingToList()
cache1h.Set(key, shippingToList)
return shippingToList
}
return shippingToList
}
/*
Database Views
*/
func setupFrontPageItemsViews() {
database.Exec("DROP MATERIALIZED VIEW IF EXISTS vm_frontpage_top_vendors CASCADE;")
database.Exec(`
CREATE MATERIALIZED VIEW vm_frontpage_top_vendors AS (
with popular_vendors AS (SELECT max(item_uuid) as one_item_uuid,
store_uuid,
store_score,
array_agg(item_category_id) as category_ids
FROM vm_available_items
GROUP BY store_uuid,
store_score
ORDER BY store_score DESC
LIMIT 200),
cat1 AS (SELECT
item_categories.name_en as categories_name_en,
t2.store_uuid,
t2.one_item_uuid as one_item_uuid
FROM (SELECT
store_uuid,
unnest(category_ids) category_id,
one_item_uuid FROM
popular_vendors) t2
JOIN item_categories ON
item_categories.id=t2.category_id),
cat2 AS (SELECT
store_uuid,
max(one_item_uuid) as one_item_uuid,
array_agg(categories_name_en) as categories_name_en
FROM cat1 GROUP BY store_uuid)
SELECT
t1.*, t2.categories_name_en as categories_name_en
FROM
vm_available_items t1 RIGHT OUTER JOIN cat2 t2 ON
t1.item_uuid=t2.one_item_uuid
ORDER BY random()
LIMIT 3
)`)
database.Exec("DROP MATERIALIZED VIEW IF EXISTS vm_frontpage_new_vendors CASCADE;")
database.Exec(`
CREATE MATERIALIZED VIEW vm_frontpage_new_vendors AS (
with new_vendors AS (SELECT max(item_uuid) as one_item_uuid,
store_uuid,
store_registration_date,
array_agg(item_category_id) as category_ids
FROM vm_available_items
GROUP BY store_uuid,
store_registration_date
ORDER BY store_registration_date DESC
LIMIT 10),
cat1 AS (SELECT
item_categories.name_en as categories_name_en,
t2.store_uuid,
t2.one_item_uuid as one_item_uuid
FROM (SELECT
store_uuid,
unnest(category_ids) category_id,
one_item_uuid FROM
new_vendors) t2
JOIN item_categories ON
item_categories.id=t2.category_id),
cat2 AS (SELECT
store_uuid,
max(one_item_uuid) as one_item_uuid,
array_agg(categories_name_en) as categories_name_en
FROM cat1 GROUP BY store_uuid)
SELECT
t1.*, t2.categories_name_en as categories_name_en
FROM
vm_available_items t1 RIGHT OUTER JOIN cat2 t2 ON
t1.item_uuid=t2.one_item_uuid
ORDER BY random()
LIMIT 3
)`)
database.Exec("DROP MATERIALIZED VIEW IF EXISTS vm_frontpage_items_top_drugs CASCADE;")
query := fmt.Sprintf(`
CREATE MATERIALIZED VIEW vm_frontpage_items_top_drugs AS (
SELECT
item_uuid,
drop_city_id,
country_name_en_shipping_from,
country_name_en_shipping_to,
currency,
type,
min_price,
max_price,
store_uuid,
store_description,
store_is_free_account,
store_is_gold_account,
store_is_silver_account,
store_is_bronze_account,
store_is_suspended,
store_is_trusted,
store_is_allowed_to_sell,
store_last_login_date,
store_registration_date,
item_created_at,
item_name,
item_description,
item_category_id,
reviewed_by_user_uuid,
parent_item_category_id,
parent_parent_item_category_id,
store_score,
store_score_count,
item_score,
item_score_count,
store_bitcoin_tx_number,
store_bitcoin_tx_volume,
store_bitcoin_cash_tx_number,
store_bitcoin_cash_tx_volume,
store_ethereum_tx_number,
store_ethereum_tx_volume,
item_bitcoin_tx_number,
item_bitcoin_tx_volume,
item_bitcoin_cash_tx_number,
item_bitcoin_cash_tx_volume,
item_ethereum_tx_number,
item_ethereum_tx_volume,
(select count(*) from store_warnings uw where uw.store_uuid=stores.uuid and severety='GREEN' and deleted_at IS NULL and is_approved=true and uw.created_at >= now() - interval '%d hours') as number_of_green_warnings,
(select count(*) from store_warnings uw where uw.store_uuid=stores.uuid and severety='YELLOW' and deleted_at IS NULL and is_approved=true and uw.created_at >= now() - interval '%d hours') as number_of_yellow_warnings,
(select count(*) from store_warnings uw where uw.store_uuid=stores.uuid and severety='RED' and deleted_at IS NULL and is_approved=true and uw.created_at >= (now() - interval '%d hours')) as number_of_red_warnings
FROM
vm_available_items
JOIN stores on stores.uuid = store_uuid
WHERE (item_category_id = 1 OR
parent_item_category_id=1 OR
parent_parent_item_category_id=1) AND
item_score > 4.5 AND
number_of_red_warnings=0 AND
number_of_yellow_warnings=0 AND
number_of_green_warnings=0 AND
store_is_suspended=false AND
store_is_allowed_to_sell=true
ORDER BY random()
LIMIT 5
);`, int(GREEN_WARNING_DURATION.Hours()), int(YELLOW_WARNING_DURATION.Hours()), int(RED_WARNING_DURATION.Hours()))
database.Exec(query)
database.Exec("DROP MATERIALIZED VIEW IF EXISTS vm_frontpage_items_top_other CASCADE;")
query2 := fmt.Sprintf(`
CREATE MATERIALIZED VIEW vm_frontpage_items_top_other AS (
SELECT
item_uuid,
drop_city_id,
country_name_en_shipping_from,
country_name_en_shipping_to,
currency,
type,
min_price,
max_price,
store_uuid,
store_description,
store_is_free_account,
store_is_gold_account,
store_is_silver_account,
store_is_bronze_account,
store_is_suspended,
store_is_trusted,
store_is_allowed_to_sell,
store_last_login_date,
store_registration_date,
item_created_at,
item_name,
item_description,
item_category_id,
reviewed_by_user_uuid,
parent_item_category_id,
parent_parent_item_category_id,
store_score,
store_score_count,
item_score,
item_score_count,
store_bitcoin_tx_number,
store_bitcoin_tx_volume,
store_bitcoin_cash_tx_number,
store_bitcoin_cash_tx_volume,
store_ethereum_tx_number,
store_ethereum_tx_volume,
item_bitcoin_tx_number,
item_bitcoin_tx_volume,
item_bitcoin_cash_tx_number,
item_bitcoin_cash_tx_volume,
item_ethereum_tx_number,
item_ethereum_tx_volume,
t1.name_en AS item_category_name_en,
t2.name_en AS parent_item_category_name_en,
t3.name_en AS parent_parent_item_category_name_en,
t1.name_ru AS item_category_name_ru,
t2.name_ru AS parent_item_category_name_ru,
t3.name_ru AS parent_parent_item_category_name_ru,
(select count(*) from store_warnings uw where uw.store_uuid=stores.uuid and severety='GREEN' and deleted_at IS NULL and is_approved=true and uw.created_at >= now() - interval '%d hours') as number_of_green_warnings,
(select count(*) from store_warnings uw where uw.store_uuid=stores.uuid and severety='YELLOW' and deleted_at IS NULL and is_approved=true and uw.created_at >= now() - interval '%d hours') as number_of_yellow_warnings,
(select count(*) from store_warnings uw where uw.store_uuid=stores.uuid and severety='RED' and deleted_at IS NULL and is_approved=true and uw.created_at >= (now() - interval '%d hours')) as number_of_red_warnings
FROM
vm_available_items
LEFT JOIN item_categories t1 ON
vm_available_items.item_category_id = t1.id
LEFT JOIN item_categories t2 ON
vm_available_items.parent_item_category_id = t2.id
LEFT JOIN item_categories t3 ON
vm_available_items.parent_parent_item_category_id = t3.id
JOIN stores on stores.uuid = store_uuid
WHERE (item_category_id != 1 AND
parent_item_category_id !=1 AND
parent_parent_item_category_id != 1 AND
item_category_id != 8 AND
parent_item_category_id !=8 AND
parent_parent_item_category_id != 8) AND
item_score > 4.5 AND
number_of_red_warnings=0 AND
number_of_yellow_warnings=0 AND
number_of_green_warnings=0 AND
store_is_suspended=false AND
store_is_allowed_to_sell=true
ORDER BY random()
LIMIT 4
)`, int(GREEN_WARNING_DURATION.Hours()), int(YELLOW_WARNING_DURATION.Hours()), int(RED_WARNING_DURATION.Hours()))
database.Exec(query2)
database.Exec("DROP MATERIALIZED VIEW IF EXISTS vm_frontpage_items_new_arrivals CASCADE;")
query3 := fmt.Sprintf(`
CREATE MATERIALIZED VIEW vm_frontpage_items_new_arrivals AS (
SELECT
item_uuid,
drop_city_id,
country_name_en_shipping_from,
country_name_en_shipping_to,
currency,
type,
min_price,
max_price,
store_uuid,
store_description,
store_is_free_account,
store_is_gold_account,
store_is_silver_account,
store_is_bronze_account,
store_is_suspended,
store_is_trusted,
store_is_allowed_to_sell,
store_last_login_date,
store_registration_date,
item_created_at,
item_name,
item_description,
item_category_id,
reviewed_by_user_uuid,
parent_item_category_id,
parent_parent_item_category_id,
store_score,
store_score_count,
item_score,
item_score_count,
store_bitcoin_tx_number,
store_bitcoin_tx_volume,
store_bitcoin_cash_tx_number,
store_bitcoin_cash_tx_volume,
store_ethereum_tx_number,
store_ethereum_tx_volume,
item_bitcoin_tx_number,
item_bitcoin_tx_volume,
item_bitcoin_cash_tx_number,
item_bitcoin_cash_tx_volume,
item_ethereum_tx_number,
item_ethereum_tx_volume,
t1.name_en AS item_category_name_en,
t2.name_en AS parent_item_category_name_en,
t3.name_en AS parent_parent_item_category_name_en,
t1.name_ru AS item_category_name_ru,
t2.name_ru AS parent_item_category_name_ru,
t3.name_ru AS parent_parent_item_category_name_ru,
(select count(*) from store_warnings uw where uw.store_uuid=stores.uuid and severety='GREEN' and deleted_at IS NULL and is_approved=true and uw.created_at >= now() - interval '%d hours') as number_of_green_warnings,
(select count(*) from store_warnings uw where uw.store_uuid=stores.uuid and severety='YELLOW' and deleted_at IS NULL and is_approved=true and uw.created_at >= now() - interval '%d hours') as number_of_yellow_warnings,
(select count(*) from store_warnings uw where uw.store_uuid=stores.uuid and severety='RED' and deleted_at IS NULL and is_approved=true and uw.created_at >= (now() - interval '%d hours')) as number_of_red_warnings
FROM
vm_available_items
LEFT JOIN item_categories t1 ON
vm_available_items.item_category_id = t1.id
LEFT JOIN item_categories t2 ON
vm_available_items.parent_item_category_id = t2.id
LEFT JOIN item_categories t3 ON
vm_available_items.parent_parent_item_category_id = t3.id
JOIN stores on stores.uuid = store_uuid
WHERE number_of_red_warnings=0 AND
number_of_yellow_warnings=0 AND
number_of_green_warnings=0 AND
store_is_suspended=false AND
store_is_allowed_to_sell=true
ORDER BY random()
LIMIT 4
);`, int(GREEN_WARNING_DURATION.Hours()), int(YELLOW_WARNING_DURATION.Hours()), int(RED_WARNING_DURATION.Hours()))
database.Exec(query3)
}
func RefreshFrontPageItemsMaterializedViews() {
database.Exec("REFRESH MATERIALIZED VIEW vm_frontpage_items_new_arrivals;")
database.Exec("REFRESH MATERIALIZED VIEW vm_frontpage_items_top_drugs;")
database.Exec("REFRESH MATERIALIZED VIEW vm_frontpage_items_top_other;")
database.Exec("REFRESH MATERIALIZED VIEW vm_frontpage_top_vendors;")
database.Exec("REFRESH MATERIALIZED VIEW vm_frontpage_new_vendors;")
}