-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProduct.php
More file actions
233 lines (192 loc) · 6.84 KB
/
Product.php
File metadata and controls
233 lines (192 loc) · 6.84 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
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
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Custom\ApifyClient;
use OwenIt\Auditing\Contracts\Auditable;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Carbon;
class Product extends Model implements Auditable
{
use \OwenIt\Auditing\Auditable;
use SoftDeletes;
protected $primaryKey = 'sku';
public $incrementing = false;
protected static function boot()
{
parent::boot();
static::saving(function ($model) {
if($model->calculateAggreates){
$model->cacheProductAggregates();
}
});
}
protected $guarded = [];
public $sendAlerts = true;
public $calculateAggreates = true;
protected $dispatchesEvents = [
'saving' => \App\Events\ProductSaving::class,
];
protected $dates = [
'scraped_at',
'sale_end',
];
protected $cast = [
'scraped_end' => 'datetime:Y-m-d',
];
public function sendAlerts()
{
$this->sendAlerts = true;
}
public function dontSendAlerts()
{
$this->sendAlerts = false;
}
public function calculateAggregates()
{
$this->calculateAggreates = true;
}
public function dontCalculateAggregates()
{
$this->calculateAggreates = false;
}
public function import(string $act_id, string $run_id)
{
$apify = new ApifyClient();
$run = $apify->get("acts/$act_id/runs/$run_id", [
'format' => 'json'
]);
if(!$run){
abort(404, 'Apify run not found');
}
$crawler_results = $apify->get("datasets/{$run->data->defaultDatasetId}/items", [
'format' => 'json'
]);
if(!$crawler_results){
abort(404, 'Apify dataset not found');
}
if($crawler_results){
$this->transformAndSaveResults($crawler_results[0]);
}
}
public function transformAndSaveResults($crawler_data, $delete=true)
{
if(empty($crawler_data->products)){
throw new \ErrorException('Import payload is empty. Imported aborted.');
}
foreach($crawler_data->products as $product){
Product::updateOrCreate(['sku' => $product->sku],
[
'type' => $product->type,
'category' => $product->category,
'name' => $product->name,
'msrp' => floatval($product->msrp),
'sale_price' => floatval($product->salePrice),
'note' => $product->note,
'sale_end' => $product->saleEnd
? Carbon::parse($product->saleEnd)->format('Y-m-d H:i:s')
: null,
'badge' => $product->badge,
'thumbnail_url' => $product->thumbnailUrl,
'scraped_at' => Carbon::parse($crawler_data->finishedAt)->format('Y-m-d H:i:s'),
'url' => $product->url,
]
);
}
if($delete){
$all_skus = array_column((array)$crawler_data->products, 'sku');
Product::whereNotIn('sku', $all_skus)
->delete();
}
}
public static function historicalAggregates()
{
$allEvents = DB::table('audits')
->where('auditable_type', 'App\Product')
->whereIn('event', ['created', 'updated'])
->selectRaw('*, ROW_NUMBER() OVER (PARTITION BY auditable_id ORDER BY created_at desc) as createdRank');
$historicalOnlyEvents = DB::table( DB::raw("({$allEvents->toSql()}) as allEvents") )
->mergeBindings($allEvents)
->where('createdRank', '>', 1);
$historicalAggregate = DB::table( DB::raw("({$historicalOnlyEvents->toSql()}) as historicalOnlyEvents") )
->mergeBindings($historicalOnlyEvents)
->groupBy('auditable_id')
->select(DB::raw("
auditable_id as sku,
avg(JSON_EXTRACT(new_values, '$.sale_price')) as average_sale_price
"));
return DB::table('products')
->whereNull('deleted_at')
->leftJoinSub($historicalAggregate, 'historicalAggregate', function($join) {
$join->on('products.sku', '=', 'historicalAggregate.sku');
})
->select(DB::raw("
products.*,
historicalAggregate.average_sale_price,
(historicalAggregate.average_sale_price - products.sale_price) / historicalAggregate.average_sale_price as discount
"));
}
public static function bestDeals()
{
$withRank = DB::table('products')
->whereNull('deleted_at')
->selectRaw('products.*, percent_rank() OVER (ORDER BY cached_discount) as discount_rank');
return DB::table(DB::raw("({$withRank->toSql()}) as withRank"))
->mergeBindings($withRank)
->where('discount_rank', '>=', .9)
->orderBy('discount_rank', 'desc');
}
public function discount($fresh=false)
{
return $fresh || !$this->cached_discount
? self::historicalAggregates()
->where('products.sku', $this->sku)
->pluck('discount')
->first()
: $this->cached_discount;
}
public function averageHistoricalSalePrice($fresh=false)
{
return $fresh || !$this->cached_average_sale_price
? self::historicalAggregates()
->where('products.sku', $this->sku)
->pluck('average_sale_price')
->first()
: $this->cached_average_sale_price;
}
public function preSaveDiscount()
{
return $this->exists() && $this->sale_price !== $this->fresh()->sale_price
? ($this->preSaveAverageHistoricalSalePrice() - $this->sale_price) / $this->preSaveAverageHistoricalSalePrice()
: false;
}
public function preSaveAverageHistoricalSalePrice()
{
return $this->exists() && $this->sale_price !== $this->fresh()->sale_price
? $this->audits()->avg('new_values->sale_price')
: false;
}
public function cacheProductAggregates()
{
$averageHistoricalSalePrice = $this->preSaveAverageHistoricalSalePrice();
if($averageHistoricalSalePrice !== false){
$this->cached_average_sale_price = $averageHistoricalSalePrice;
}
$discount = $this->preSaveDiscount();
if($averageHistoricalSalePrice !== false){
$this->cached_discount = $discount;
}
}
public function forceCacheProductAggregates()
{
$this->cached_average_sale_price = $this->averageHistoricalSalePrice(true);
$this->cached_discount = $this->discount(true);
$this->save();
}
public function exists()
{
return self::find($this->sku)
? true
: false;
}
}