From 5abc2445a7f92f891699d56696c409ac1fe98413 Mon Sep 17 00:00:00 2001 From: Daniel Supernault Date: Wed, 17 May 2023 04:34:30 -0600 Subject: [PATCH 1/2] Update AutospamUpdateCachedDataPipeline --- .../AutospamUpdateCachedDataPipeline.php | 118 +++++++++++------- 1 file changed, 71 insertions(+), 47 deletions(-) diff --git a/app/Jobs/AutospamPipeline/AutospamUpdateCachedDataPipeline.php b/app/Jobs/AutospamPipeline/AutospamUpdateCachedDataPipeline.php index c223e74f7..c836c79a2 100644 --- a/app/Jobs/AutospamPipeline/AutospamUpdateCachedDataPipeline.php +++ b/app/Jobs/AutospamPipeline/AutospamUpdateCachedDataPipeline.php @@ -24,56 +24,80 @@ class AutospamUpdateCachedDataPipeline implements ShouldQueue { } - /** - * Execute the job. - */ - public function handle(): void - { - $spam = json_decode(Storage::get(AutospamService::MODEL_SPAM_PATH), true); - $newSpam = AutospamCustomTokens::whereCategory('spam')->get(); - foreach($newSpam as $ns) { - $key = strtolower($ns->token); - if(isset($spam['words']['spam'][$key])) { - $spam['words']['spam'][$key] = $spam['words']['spam'][$key] + $ns->weight; - } else { - $spam['words']['spam'][$key] = $ns->weight; - } - } - $newSpamCount = count($spam['words']['spam']); - $spam['documents']['spam'] = $newSpamCount; - arsort($spam['words']['spam']); - Storage::put(AutospamService::MODEL_SPAM_PATH, json_encode($spam, JSON_UNESCAPED_SLASHES|JSON_PRETTY_PRINT)); + /** + * Execute the job. + */ + public function handle(): void + { + $spamExists = Storage::exists(AutospamService::MODEL_SPAM_PATH); + if($spamExists) { + $spam = json_decode(Storage::get(AutospamService::MODEL_SPAM_PATH), true); + } else { + $spam = [ + 'documents' => [ + 'spam' => 0 + ], + 'words' => [ + 'spam' => [] + ] + ]; + } + $newSpam = AutospamCustomTokens::whereCategory('spam')->get(); + foreach($newSpam as $ns) { + $key = strtolower($ns->token); + if(isset($spam['words']['spam'][$key])) { + $spam['words']['spam'][$key] = $spam['words']['spam'][$key] + $ns->weight; + } else { + $spam['words']['spam'][$key] = $ns->weight; + } + } + $newSpamCount = count($spam['words']['spam']); + $spam['documents']['spam'] = $newSpamCount; + arsort($spam['words']['spam']); + Storage::put(AutospamService::MODEL_SPAM_PATH, json_encode($spam, JSON_UNESCAPED_SLASHES|JSON_PRETTY_PRINT)); - $ham = json_decode(Storage::get(AutospamService::MODEL_HAM_PATH), true); - $newHam = AutospamCustomTokens::whereCategory('ham')->get(); - foreach($newHam as $ns) { - $key = strtolower($ns->token); - if(isset($spam['words']['ham'][$key])) { - $ham['words']['ham'][$key] = $ham['words']['ham'][$key] + $ns->weight; - } else { - $ham['words']['ham'][$key] = $ns->weight; - } - } + $hamExists = Storage::exists(AutospamService::MODEL_HAM_PATH); + if($hamExists) { + $ham = json_decode(Storage::get(AutospamService::MODEL_HAM_PATH), true); + } else { + $ham = [ + 'documents' => [ + 'ham' => 0 + ], + 'words' => [ + 'ham' => [] + ] + ]; + } + $newHam = AutospamCustomTokens::whereCategory('ham')->get(); + foreach($newHam as $ns) { + $key = strtolower($ns->token); + if(isset($spam['words']['ham'][$key])) { + $ham['words']['ham'][$key] = $ham['words']['ham'][$key] + $ns->weight; + } else { + $ham['words']['ham'][$key] = $ns->weight; + } + } - $newHamCount = count($ham['words']['ham']); - $ham['documents']['ham'] = $newHamCount; - arsort($ham['words']['ham']); + $newHamCount = count($ham['words']['ham']); + $ham['documents']['ham'] = $newHamCount; + arsort($ham['words']['ham']); - Storage::put(AutospamService::MODEL_HAM_PATH, json_encode($ham, JSON_UNESCAPED_SLASHES|JSON_PRETTY_PRINT)); + Storage::put(AutospamService::MODEL_HAM_PATH, json_encode($ham, JSON_UNESCAPED_SLASHES|JSON_PRETTY_PRINT)); - $combined = [ - 'documents' => [ - 'spam' => $newSpamCount, - 'ham' => $newHamCount, - ], - 'words' => [ - 'spam' => $spam['words']['spam'], - 'ham' => $ham['words']['ham'] - ] - ]; + $combined = [ + 'documents' => [ + 'spam' => $newSpamCount, + 'ham' => $newHamCount, + ], + 'words' => [ + 'spam' => $spam['words']['spam'], + 'ham' => $ham['words']['ham'] + ] + ]; - Storage::put(AutospamService::MODEL_FILE_PATH, json_encode($combined, JSON_PRETTY_PRINT,JSON_UNESCAPED_SLASHES)); - Cache::forget(AutospamService::MODEL_CACHE_KEY); - Cache::forget(AutospamService::CHCKD_CACHE_KEY); - } + Storage::put(AutospamService::MODEL_FILE_PATH, json_encode($combined, JSON_PRETTY_PRINT,JSON_UNESCAPED_SLASHES)); + Cache::forget(AutospamService::MODEL_CACHE_KEY); + Cache::forget(AutospamService::CHCKD_CACHE_KEY); + } } From a11e1ee3f816bd27fc199570cd0abcacadf51c5d Mon Sep 17 00:00:00 2001 From: Daniel Supernault Date: Wed, 17 May 2023 04:44:35 -0600 Subject: [PATCH 2/2] Update AutospamUpdateCachedDataPipeline --- .../AutospamUpdateCachedDataPipeline.php | 40 +++++++++++-------- 1 file changed, 23 insertions(+), 17 deletions(-) diff --git a/app/Jobs/AutospamPipeline/AutospamUpdateCachedDataPipeline.php b/app/Jobs/AutospamPipeline/AutospamUpdateCachedDataPipeline.php index c836c79a2..4ac025bb8 100644 --- a/app/Jobs/AutospamPipeline/AutospamUpdateCachedDataPipeline.php +++ b/app/Jobs/AutospamPipeline/AutospamUpdateCachedDataPipeline.php @@ -52,9 +52,11 @@ class AutospamUpdateCachedDataPipeline implements ShouldQueue } } $newSpamCount = count($spam['words']['spam']); - $spam['documents']['spam'] = $newSpamCount; - arsort($spam['words']['spam']); - Storage::put(AutospamService::MODEL_SPAM_PATH, json_encode($spam, JSON_UNESCAPED_SLASHES|JSON_PRETTY_PRINT)); + if($newSpamCount) { + $spam['documents']['spam'] = $newSpamCount; + arsort($spam['words']['spam']); + Storage::put(AutospamService::MODEL_SPAM_PATH, json_encode($spam, JSON_UNESCAPED_SLASHES|JSON_PRETTY_PRINT)); + } $hamExists = Storage::exists(AutospamService::MODEL_HAM_PATH); if($hamExists) { @@ -80,23 +82,27 @@ class AutospamUpdateCachedDataPipeline implements ShouldQueue } $newHamCount = count($ham['words']['ham']); - $ham['documents']['ham'] = $newHamCount; - arsort($ham['words']['ham']); + if($newHamCount) { + $ham['documents']['ham'] = $newHamCount; + arsort($ham['words']['ham']); + Storage::put(AutospamService::MODEL_HAM_PATH, json_encode($ham, JSON_UNESCAPED_SLASHES|JSON_PRETTY_PRINT)); + } - Storage::put(AutospamService::MODEL_HAM_PATH, json_encode($ham, JSON_UNESCAPED_SLASHES|JSON_PRETTY_PRINT)); + if($newSpamCount && $newHamCount) { + $combined = [ + 'documents' => [ + 'spam' => $newSpamCount, + 'ham' => $newHamCount, + ], + 'words' => [ + 'spam' => $spam['words']['spam'], + 'ham' => $ham['words']['ham'] + ] + ]; - $combined = [ - 'documents' => [ - 'spam' => $newSpamCount, - 'ham' => $newHamCount, - ], - 'words' => [ - 'spam' => $spam['words']['spam'], - 'ham' => $ham['words']['ham'] - ] - ]; + Storage::put(AutospamService::MODEL_FILE_PATH, json_encode($combined, JSON_PRETTY_PRINT,JSON_UNESCAPED_SLASHES)); + } - Storage::put(AutospamService::MODEL_FILE_PATH, json_encode($combined, JSON_PRETTY_PRINT,JSON_UNESCAPED_SLASHES)); Cache::forget(AutospamService::MODEL_CACHE_KEY); Cache::forget(AutospamService::CHCKD_CACHE_KEY); }