Просмотр исходного кода

Merge pull request #1590 from visuddhinanda/laravel

add UserApi in MqDiscussion
visuddhinanda 2 лет назад
Родитель
Сommit
b3fee9b9d9

+ 8 - 2
README.md

@@ -283,10 +283,16 @@ cd ./v1/scripts
 ```
 运行时间较长。本地开发环境大约4小时。
 
+如果不想等待,可以导入其他人已经部署好的postgresql数据库
 
-运行时间较长。本地开发环境大约4小时。
+### 启动消息队列works
 
-如果不想等待,可以导入其他人已经部署好的postgresql数据库
+```dash
+php artisan mq:discussion
+php artisan mq:pr
+php artisan mq:progress
+php artisan mq:wbw.analyses
+```
 
 ### 运行dev server
 

+ 106 - 0
app/Console/Commands/CacheWbwPreference.php

@@ -0,0 +1,106 @@
+<?php
+
+namespace App\Console\Commands;
+
+use Illuminate\Console\Command;
+use Illuminate\Support\Facades\Log;
+use Illuminate\Support\Facades\Cache;
+use App\Models\WbwAnalysis;
+use Illuminate\Support\Facades\DB;
+
+class CacheWbwPreference extends Command
+{
+    /**
+     * The name and signature of the console command.
+     *
+     * @var string
+     */
+    protected $signature = 'cache:wbw.preference {--editor=} {--view=all}';
+
+    /**
+     * The console command description.
+     *
+     * @var string
+     */
+    protected $description = '逐词解析的首选项预热';
+
+    /**
+     * Create a new command instance.
+     *
+     * @return void
+     */
+    public function __construct()
+    {
+        parent::__construct();
+    }
+
+    /**
+     * Execute the console command.
+     *
+     * @return int
+     */
+    public function handle()
+    {
+        $prefix = 'wbw-preference';
+        if($this->option('view')==='all' ||
+           $this->option('view')==='my'){
+            $this->info('个人数据');
+            /**
+             * 个人数据算法
+             * 最新优先
+             */
+            $wbw = WbwAnalysis::select(['wbw_word','type','editor_id']);
+            $count = DB::select('SELECT count(*) from (
+                SELECT wbw_word,type,editor_id from wbw_analyses group by wbw_word,type,editor_id) T');
+            if($this->option('editor')){
+                $wbw = $wbw->where('editor_id',$this->option('editor'));
+                $count = DB::select('SELECT count(*) from (
+                    SELECT wbw_word,type,editor_id from wbw_analyses where editor_id=? group by wbw_word,type,editor_id) T',
+                    [$this->option('editor')]);
+            }
+            $wbw = $wbw->groupBy(['wbw_word','type','editor_id'])->cursor();
+
+            $bar = $this->output->createProgressBar($count[0]->count);
+            foreach ($wbw as $key => $value) {
+                $data = WbwAnalysis::where('wbw_word',$value->wbw_word)
+                                    ->where('type',$value->type)
+                                    ->where('editor_id',$value->editor_id)
+                                    ->orderBy('updated_at','desc')
+                                    ->value('data');
+                Cache::put("{$prefix}/{$value->wbw_word}/{$value->type}/{$value->editor_id}",$data);
+                $bar->advance();
+            }
+            $bar->finish();
+        }
+
+        if($this->option('view')==='all' ||
+           $this->option('view')==='community'
+           ){
+            $this->info('社区通用');
+            /**
+             * 社区数据算法
+             * 多的优先
+             */
+            $wbw = WbwAnalysis::select(['wbw_word','type']);
+            $count = DB::select('SELECT count(*) from (
+                SELECT wbw_word,type from wbw_analyses group by wbw_word,type) T');
+            $wbw = $wbw->groupBy(['wbw_word','type'])->cursor();
+
+            $bar = $this->output->createProgressBar($count[0]->count);
+            foreach ($wbw as $key => $value) {
+                $data = WbwAnalysis::where('wbw_word',$value->wbw_word)
+                                    ->where('type',$value->type)
+                                    ->selectRaw('data,count(*)')
+                                    ->groupBy("data")
+                                    ->orderBy("count", "desc")
+                                    ->first();
+
+                Cache::put("{$prefix}/{$value->wbw_word}/{$value->type}/0",$data->data);
+                $bar->advance();
+            }
+            $bar->finish();
+        }
+
+        return 0;
+    }
+}

+ 1 - 0
app/Console/Commands/MqDiscussion.php

@@ -10,6 +10,7 @@ use App\Models\Article;
 use App\Http\Api\Mq;
 use App\Tools\WebHook as WebHookSend;
 use App\Http\Api\MdRender;
+use App\Http\Api\UserApi;
 
 class MqDiscussion extends Command
 {

+ 14 - 4
app/Console/Commands/UpgradeWbwAnalyses.php

@@ -11,7 +11,7 @@ class UpgradeWbwAnalyses extends Command
 {
     /**
      * The name and signature of the console command.
-     *
+     * php artisan upgrade:wbw.analyses 13607580802879488
      * @var string
      */
     protected $signature = 'upgrade:wbw.analyses {id?}';
@@ -71,8 +71,14 @@ class UpgradeWbwAnalyses extends Command
                 $pali = $word->real->__toString();
                 $factors = [];
                 foreach ($word as $key => $value) {
-                    $strValue = $value->__toString();
-                    if ($strValue !== "?" && $strValue !== "" && $strValue !== ".ctl." && $strValue !== ".a." && $strValue !== " " && mb_substr($strValue, 0, 3, "UTF-8") !== "[a]" && $strValue !== "_un_auto_factormean_" && $strValue !== "_un_auto_mean_") {
+                    $strValue = trim($value->__toString());
+                    if ($strValue !== "?" &&
+                        $strValue !== "" &&
+                        $strValue !== ".ctl." &&
+                        $strValue !== ".a." &&
+                        mb_substr($strValue, 0, 3, "UTF-8") !== "[a]" &&
+                        $strValue !== "_un_auto_factormean_" &&
+                        $strValue !== "_un_auto_mean_") {
                         $iType = 0;
                         $lang = 'pali';
                         $newData = [
@@ -128,11 +134,15 @@ class UpgradeWbwAnalyses extends Command
                                 $newData['type']=6;
                                 WbwAnalysis::insert($newData);
                                 break;
+                            case 'case':
+                                $newData['type']=8;
+                                WbwAnalysis::insert($newData);
+                                break;
                             case 'rela':
                             /*
                             <rela>[{"sour_id":"p199-764-6","sour_spell":"dhammacakkappavattanatthaṃ","dest_id":"p199-764-8","dest_spell":"āmantanā","relation":"ADV","note":""}]</rela>
                             */
-                                $newData['type']=7;
+                                $newData['type']=9;
                                 $rlt = json_decode($strValue);
                                 foreach ($rlt as $rltValue) {
                                     # code...

+ 1 - 1
app/Http/Controllers/AnalysisController.php

@@ -16,7 +16,7 @@ class AnalysisController extends Controller
     {
         //
         $result = WbwAnalysis::selectRaw('d1, data ,count(*) as ct')
-                             ->where('type',7)
+                             ->where('type',9)
                              ->groupby('d1')
                              ->groupby('data')
                              ->orderbyRaw('d1,ct desc')

+ 2 - 2
app/Http/Controllers/WbwAnalysisController.php

@@ -9,14 +9,14 @@ class WbwAnalysisController extends Controller
 {
     /**
      * Display a listing of the resource.
-     *select d1, data ,count(*) as ct from wbw_analyses where type = 7  group by d1,data order by d1,ct desc 
+     *select d1, data ,count(*) as ct from wbw_analyses where type = 7  group by d1,data order by d1,ct desc
      * @return \Illuminate\Http\Response
      */
     public function index()
     {
         //
         $result = WbwAnalysis::selectRaw('d1, data ,count(*) as ct')
-                             ->where('type',7)
+                             ->where('type',9)
                              ->groupby('d1')
                              ->groupby('data')
                              ->orderbyRaw('d1,ct desc')

+ 149 - 78
app/Http/Controllers/WbwLookupController.php

@@ -6,12 +6,12 @@ use App\Models\UserDict;
 use App\Models\DictInfo;
 use App\Models\WbwTemplate;
 use App\Models\Channel;
-use App\Models\WbwAnalysis;
 use Illuminate\Http\Request;
 use App\Tools\CaseMan;
 use Illuminate\Support\Facades\Log;
 use Illuminate\Support\Facades\Cache;
 use App\Http\Api\DictApi;
+use App\Http\Api\AuthApi;
 
 
 
@@ -131,12 +131,52 @@ class WbwLookupController extends Controller
 
         return $output;
     }
+
+    /**
+     *
+     */
     private function langCheck($query,$lang){
         if($query===[]){
             return true;
         }else{
-            return in_array($lang,$query);
+            if(in_array(strtolower($lang),$query)){
+                return true;
+            }else{
+                $langFamily = explode('-',$lang)[0];
+                foreach ($query as $value) {
+                    if(strpos($value,$langFamily) !== false){
+                        return true;
+                    }
+                }
+            }
         }
+        return false;
+    }
+    private function wbwPreference($word,$field,$userId){
+        $prefix = 'wbw-preference';
+        $fieldMap = [
+                    'type'=>1,
+                    'grammar'=>2,
+                    'meaning'=>3,
+                    'factors'=>4,
+                    'factorMeaning'=>5,
+                    'parent'=>6,
+                    'part'=>7,
+                    'case'=>8,
+                ];
+        $fieldId = $fieldMap[$field];
+        $myPreference = Cache::get("{$prefix}/{$word}/{$fieldId}/{$userId}");
+        if(!empty($myPreference)){
+            Log::info($word.'命中我的wbw-'.$field);
+            return ['value'=>$myPreference,'status'=>5];
+        }else{
+            $myPreference = Cache::get("{$prefix}/{$word}/3/0");
+            if(!empty($myPreference)){
+                Log::info($word.'命中社区wbw-'.$field);
+                return ['value'=>$myPreference,'status'=>5];
+            }
+        }
+        return false;
     }
     /**
      * 自动查词
@@ -147,6 +187,12 @@ class WbwLookupController extends Controller
     public function store(Request $request)
     {
         //
+        $user = AuthApi::current($request);
+        if(!$user ){
+            //未登录用户
+            return $this->error(__('auth.failed'),[],401);
+        }
+
         $startAt = microtime(true)*1000;
 
         // system regular
@@ -154,7 +200,10 @@ class WbwLookupController extends Controller
 
         $channel = Channel::find($request->get('channel_id'));
         $orgData = $request->get('data');
-        $lang = $request->get('lang',[]);
+        $lang = [];
+        foreach ($request->get('lang',[]) as $value) {
+            $lang[] = strtolower($value);
+        }
         //句子中的单词
         $words = [];
         foreach ($orgData as  $word) {
@@ -177,105 +226,127 @@ class WbwLookupController extends Controller
             if(empty($word['real']['value'])){
                 continue;
             }
-            {
-                $data = $word;
-                if(isset($indexed[$word['real']['value']])){
-                    //parent
-                    $case = [];
-                    $parent = [];
-                    $factors = [];
-                    $factorMeaning = [];
-                    $meaning = [];
-                    $parent2 = [];
-                    $case2 = [];
-                    foreach ($indexed[$word['real']['value']] as $value) {
-                        //非base优先
-                        if(strstr($value->type,'base') === FALSE){
-                            $increment = 10;
-                        }else{
-                            $increment = 1;
-                        }
-                        //将全部结果加上得分放入数组
+            $data = $word;
+
+            $preference = $this->wbwPreference($word['real']['value'],'meaning',$user['user_id']);
+            if($preference!==false){
+                $data['meaning'] = $preference;
+            }
+            $preference = $this->wbwPreference($word['real']['value'],'factors',$user['user_id']);
+            if($preference!==false){
+                $data['factors'] = $preference;
+            }
+            $preference = $this->wbwPreference($word['real']['value'],'factorMeaning',$user['user_id']);
+            if($preference!==false){
+                $data['factorMeaning'] = $preference;
+            }
+            $preference = $this->wbwPreference($word['real']['value'],'case',$user['user_id']);
+            if($preference!==false){
+                $data['case'] = $preference;
+            }
+            if(isset($indexed[$word['real']['value']])){
+                //parent
+                $case = [];
+                $parent = [];
+                $factors = [];
+                $factorMeaning = [];
+                $meaning = [];
+                $parent2 = [];
+                $case2 = [];
+                foreach ($indexed[$word['real']['value']] as $value) {
+                    //非base优先
+                    if(strstr($value->type,'base') === FALSE){
+                        $increment = 10;
+                    }else{
+                        $increment = 1;
+                    }
+                    //将全部结果加上得分放入数组
+                    if($value->type !== '.cp.'){
                         $parent = $this->insertValue([$value->parent],$parent,$increment);
+                    }
+                    if($data['case']['status']<5){
                         if(!empty($value->type) && $value->type !== ".cp."){
                             $case = $this->insertValue([$value->type."#".$value->grammar],$case,$increment);
                         }
+                    }
+                    if($data['factors']['status']<5){
                         $factors = $this->insertValue([$value->factors],$factors,$increment);
+                    }
+                    if($data['factorMeaning']['status']<5){
                         $factorMeaning = $this->insertValue([$value->factormean],$factorMeaning,$increment);
+                    }
+
+                    if($data['meaning']['status']<5){
                         if($this->langCheck($lang,$value->language)){
                             $meaning = $this->insertValue(explode('$',$value->mean),$meaning,$increment,false);
                         }
                     }
-                    if(count($case)>0){
-                        arsort($case);
-                        $first = array_keys($case)[0];
-                        $data['case'] = ['value'=>$first==="_null"?"":$first,'status'=>3];
-                    }
-                    if(count($parent)>0){
-                        arsort($parent);
-                        $first = array_keys($parent)[0];
-                        $data['parent'] = ['value'=>$first==="_null"?"":$first,'status'=>3];
-                    }
-                    if(count($factors)>0){
-                        arsort($factors);
-                        $first = array_keys($factors)[0];
-                        $data['factors'] = ['value'=>$first==="_null"?"":$first,'status'=>3];
-                    }
-                    //拆分意思
-                    if(count($factorMeaning)>0){
-                        arsort($factorMeaning);
-                        $first = array_keys($factorMeaning)[0];
-                        $data['factorMeaning'] = ['value'=>$first==="_null"?"":$first,'status'=>3];
-                    }
+                }
+                if(count($case)>0){
+                    arsort($case);
+                    $first = array_keys($case)[0];
+                    $data['case'] = ['value'=>$first==="_null"?"":$first,'status'=>3];
+                }
+                if(count($parent)>0){
+                    arsort($parent);
+                    $first = array_keys($parent)[0];
+                    $data['parent'] = ['value'=>$first==="_null"?"":$first,'status'=>3];
+                }
+                if(count($factors)>0){
+                    arsort($factors);
+                    $first = array_keys($factors)[0];
+                    $data['factors'] = ['value'=>$first==="_null"?"":$first,'status'=>3];
+                }
+                //拆分意思
+                if(count($factorMeaning)>0){
+                    arsort($factorMeaning);
+                    $first = array_keys($factorMeaning)[0];
+                    $data['factorMeaning'] = ['value'=>$first==="_null"?"":$first,'status'=>3];
+                }
+                if($data['factorMeaning']['status']<5){
                     $wbwFactorMeaning = [];
                     if(!empty($data['factors']['value'])){
                         foreach (explode("+",$data['factors']['value']) as  $factor) {
-                            # code...
-                            $wbwAnalyses = WbwAnalysis::where('wbw_word',$factor)
-                                                      ->where('type',7)
-                                                      ->selectRaw('data,count(*)')
-                                                      ->groupBy("data")
-                                                      ->orderBy("count", "desc")
-                                                      ->first();
-                            if($wbwAnalyses){
-                                $wbwFactorMeaning[]=$wbwAnalyses->data;
+                            $preference = $this->wbwPreference($$factor,'meaning',$user['user_id']);
+                            if($preference!==false){
+                                $wbwFactorMeaning[] = $preference['value'];
                             }else{
-                                $wbwFactorMeaning[]="";
+                                $wbwFactorMeaning[] = $factor;
                             }
                         }
                     }
                     $data['factorMeaning'] = ['value'=>implode('+',$wbwFactorMeaning),'status'=>3];
+                }
 
-                    if(!empty($data['parent'])){
-                        if(isset($indexed[$data['parent']['value']])){
-                            foreach ($indexed[$data['parent']['value']] as $value) {
-                                //根据base 查找词意
-                                //非base优先
-                                $increment = 10;
-                                if($this->langCheck($lang,$value->language)){
-                                    $meaning = $this->insertValue(explode('$',$value->mean),$meaning,$increment,false);
-                                }
-                                //查找词源
-                                if(!empty($value->parent) && $value->parent !== $value->word && strstr($value->type,"base") !== FALSE ){
-                                    $parent2 = $this->insertValue([$value->grammar."$".$value->parent],$parent2,1,false);
-                                }
+                if(empty($data['meaning']['value']) && !empty($data['parent']['value'])){
+                    if(isset($indexed[$data['parent']['value']])){
+                        foreach ($indexed[$data['parent']['value']] as $value) {
+                            //根据base 查找词意
+                            //非base优先
+                            $increment = 10;
+                            if($this->langCheck($lang,$value->language)){
+                                $meaning = $this->insertValue(explode('$',$value->mean),$meaning,$increment,false);
+                            }
+                            //查找词源
+                            if(!empty($value->parent) && $value->parent !== $value->word && strstr($value->type,"base") !== FALSE ){
+                                $parent2 = $this->insertValue([$value->grammar."$".$value->parent],$parent2,1,false);
                             }
                         }
                     }
-                    if(count($meaning)>0){
-                        arsort($meaning);
-                        $first = array_keys($meaning)[0];
-                        $data['meaning'] = ['value'=>$first==="_null"?"":$first,'status'=>3];
-                    }
-                    if(count($parent2)>0){
-                        arsort($parent2);
-                        $first = explode("$",array_keys($parent2)[0]);
-                        $data['parent2'] = ['value'=>$first[1],'status'=>3];
-                        $data['grammar2'] = ['value'=>$first[0],'status'=>3];
-                    }
                 }
-                $orgData[$key] = $data;
+                if(count($meaning)>0){
+                    arsort($meaning);
+                    $first = array_keys($meaning)[0];
+                    $data['meaning'] = ['value'=>$first==="_null"?"":$first,'status'=>3];
+                }
+                if(count($parent2)>0){
+                    arsort($parent2);
+                    $first = explode("$",array_keys($parent2)[0]);
+                    $data['parent2'] = ['value'=>$first[1],'status'=>3];
+                    $data['grammar2'] = ['value'=>$first[0],'status'=>3];
+                }
             }
+            $orgData[$key] = $data;
         }
         return $this->ok($orgData);
     }

+ 36 - 0
database/migrations/2023_10_15_054756_add_index_count_in_wbw_analyses.php

@@ -0,0 +1,36 @@
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\Schema;
+
+class AddIndexCountInWbwAnalyses extends Migration
+{
+    /**
+     * Run the migrations.
+     *
+     * @return void
+     */
+    public function up()
+    {
+        Schema::table('wbw_analyses', function (Blueprint $table) {
+            //
+            $table->index('wbw_word');
+            $table->index('type');
+        });
+    }
+
+    /**
+     * Reverse the migrations.
+     *
+     * @return void
+     */
+    public function down()
+    {
+        Schema::table('wbw_analyses', function (Blueprint $table) {
+            $table->dropIndex("wbw_analyses_wbw_word_index");
+            $table->dropIndex("wbw_analyses_type_index");
+
+        });
+    }
+}