-
Notifications
You must be signed in to change notification settings - Fork 246
Expand file tree
/
Copy pathRecommendationEngine.php
More file actions
204 lines (175 loc) · 6.51 KB
/
RecommendationEngine.php
File metadata and controls
204 lines (175 loc) · 6.51 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
<?php
namespace ProcessMaker;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Query\Builder;
use ProcessMaker\Jobs\GenerateUserRecommendations;
use ProcessMaker\Models\Recommendation;
use ProcessMaker\Models\RecommendationUser;
use ProcessMaker\Models\User;
class RecommendationEngine
{
/**
* Target user's recommendations
*
* @var User
*/
protected User $user;
public function __construct(User $user)
{
$this->user = $user;
}
/**
* Returns an instance of the RecommendationEngine for the given user
*
* @param User $user
*
* @return static
*/
public static function for(User $user): static
{
return new static($user);
}
/**
* Generate the recommendations for the given user
*
* @return void
*/
public function generate(): void
{
$this->debug("Generating recommendations for user {$this->user->id}");
if (static::disabled()) {
return;
}
foreach (Recommendation::active()->get() as $recommendation) {
$query = $recommendation->baseQuery($this->user);
// Set up the RecommendationUser query
$recommendationUsersQuery = $recommendation->recommendationUsers()->where('user_id', '=', $this->user->id);
// Check if this RecommendationUser exists
$recommendationUsersExists = $recommendationUsersQuery->exists();
if ($recommendationUsersExists) {
$this->debug("RecommendationUser {$recommendation->id} exists for user {$this->user->id}");
} else {
$this->debug("RecommendationUser {$recommendation->id} does not exist for user {$this->user->id}");
}
// Check if there are enough results to satisfy the
// minimum matches required by the recommendation
$minimumMatchesMet = $this->minimumMatchesMet($recommendation, ($count = $query->count()));
$this->debug("Recommendation {$recommendation->id} matches {$count} results");
if ($recommendationUsersExists) {
// If we find the RecommendationUser records, we need
// to make sure they're up-to-date
$this->debug('Modifying existing RecommendationUser records');
$this->modifyExisting($recommendationUsersQuery, $minimumMatchesMet, $count);
} elseif ($minimumMatchesMet) {
// If the minimum number of matches is satisfied and the RecommendationUser
// records don't exist, we need to create them
$this->debug('Creating new RecommendationUser records');
$this->create($recommendation, $count);
} else {
$this->debug(
"Recommendation {$recommendation->id} does not meet minimum: {$recommendation->min_matches}"
);
}
}
}
/**
* Create a new RecommendationUser
*
* @param Recommendation $recommendation
* @param int $count
*
* @return void
*/
protected function create(Recommendation $recommendation, int $count): void
{
$recommendationUser = (new RecommendationUser())->fill([
'count' => $count,
'user_id' => $this->user->id,
'recommendation_id' => $recommendation->id,
]);
$recommendationUser->save();
}
/**
* Update or delete existing RecommendationUser records
*
* @param HasMany|Builder $query
* @param bool $minimumMatchesMet
* @param int $resultCount
*
* @return void
*/
protected function modifyExisting(HasMany|Builder $query, bool $minimumMatchesMet, int $resultCount): void
{
foreach ($query->get() as $recommendationUser) {
// If the minimum matches are met, then we can
// update the existing RecommendationUser records
if ($minimumMatchesMet) {
// Update the matching count
$recommendationUser->count = $resultCount;
// Check if the dismissed_until has passed
if ($recommendationUser->isExpired()) {
$recommendationUser->setAttribute('dismissed_until', null);
}
// Persist the updates
$recommendationUser->save();
} else {
// Otherwise, if the minimum matches are not met, we
// need to delete the existing RecommendationUser
// rows since they are now invalid
$query->delete();
}
}
}
/**
* Check if the matches/result count satisfies the threshold set for the Recommendation
*
* @param Recommendation $recommendation
* @param int $count
*
* @return bool
*/
protected function minimumMatchesMet(Recommendation $recommendation, int $count): bool
{
return $count >= $recommendation->min_matches;
}
/**
* Indicates if the RecommendationEngine is turned on or off globally
*
* @return bool
*/
public static function disabled(): bool
{
return config('app.recommendations_enabled') === false;
}
public static function shouldGenerateFor(User|null $user): bool
{
if (!$user || self::disabled()) {
return false;
}
if (isset($user->meta->disableRecommendations) && $user->meta->disableRecommendations) {
return false;
}
return true;
}
public function debug(string $message, array $params = []): void
{
if (config('app.debug')) {
\Log::debug($message, $params);
}
}
public static function handleUserSettingChanges(User $user, array $originalAttributes): void
{
$originalMeta = $originalAttributes['meta'] ?? null;
$newMeta = $user->meta ?? null;
$originalDisableRecommendations = $originalMeta?->disableRecommendations ?? false;
$newDisableRecommendations = $newMeta?->disableRecommendations ?? false;
if ($originalDisableRecommendations && !$newDisableRecommendations) {
// The user enabled recommendations. Generate new recommendations.
GenerateUserRecommendations::dispatch($user->id);
}
if (!$originalDisableRecommendations && $newDisableRecommendations) {
// The user disabled recommendations. Delete all existing recommendations.
RecommendationUser::deleteFor($user);
}
}
}