Browse Source

:sparkles: progress 按照channel统计

visuddhinanda@gmail.com 4 năm trước cách đây
mục cha
commit
3415829843

+ 107 - 0
app/Console/Commands/UpgradeProgress.php

@@ -0,0 +1,107 @@
+<?php
+
+namespace App\Console\Commands;
+
+use Illuminate\Console\Command;
+use App\Models\Sentence;
+use App\Models\PaliSentence;
+use App\Models\Progress;
+use App\Models\ProgressChapter;
+use App\Models\PaliText;
+
+class UpgradeProgress extends Command
+{
+    /**
+     * The name and signature of the console command.
+     *
+     * @var string
+     */
+    protected $signature = 'upgrade:progress';
+
+    /**
+     * The console command description.
+     *
+     * @var string
+     */
+    protected $description = 'Command description';
+
+    /**
+     * Create a new command instance.
+     *
+     * @return void
+     */
+    public function __construct()
+    {
+        parent::__construct();
+    }
+
+    /**
+     * Execute the console command.
+     *
+     * @return int
+     */
+    public function handle()
+    {
+
+
+        $channels = Sentence::where('strlen','>',0)
+                          ->where('book_id','<',1000)
+                          ->where('channel_uid','<>','')
+                          ->groupby('book_id','paragraph','channel_uid')
+                          ->select('book_id','paragraph','channel_uid')
+                          ->cursor();
+        $this->info('channels:',count($channels));
+        #第二步 更新段落表
+        $bar = $this->output->createProgressBar(count($channels));
+        foreach ($channels as $channel) {
+            # 第二步 生成para progress 1,2,15,zh-tw
+            # 计算此段落完成时间
+            $finalAt = Sentence::where('strlen','>',0)
+                        ->where('book_id',$channel->book_id)
+                        ->where('paragraph',$channel->paragraph)
+                        ->where('channel_uid',$channel->channel_uid)
+                        ->max('created_at');
+            $updateAt = Sentence::where('strlen','>',0)
+                        ->where('book_id',$channel->book_id)
+                        ->where('paragraph',$channel->paragraph)
+                        ->where('channel_uid',$channel->channel_uid)
+                        ->max('updated_at');
+            # 查询每个段落的等效巴利语字符数
+            $result_sent = Sentence::where('strlen','>',0)
+                                    ->where('book_id',$channel->book_id)
+                                    ->where('paragraph',$channel->paragraph)
+                                    ->where('channel_uid',$channel->channel_uid)
+                                    ->select('word_start')
+                                    ->get();
+            if (count($result_sent) > 0) {
+                #查询这些句子的总共等效巴利语字符数
+                $para_strlen = 0;
+                foreach ($result_sent as $sent) {
+                    # code...
+                    $para_strlen += PaliSentence::where('book',$channel->book_id)
+                                ->where('paragraph',$channel->paragraph)
+                                ->where('word_begin',$sent->word_start)
+                                ->value('length');
+                }
+
+                Progress::updateOrInsert(
+                    [
+                        'book'=>$channel->book_id,
+                        'para'=>$channel->paragraph,
+                        'channel_id'=>$channel->channel_uid
+                    ],
+                    [
+                        'lang'=>'en',
+                        'all_strlen'=>$para_strlen,
+                        'public_strlen'=>$para_strlen,
+                        'created_at'=>$finalAt,
+                        'updated_at'=>$updateAt,
+                    ]);
+            }
+            $bar->advance();
+        }
+        $bar->finish();
+
+        return 0;
+    }
+}

+ 115 - 0
app/Console/Commands/UpgradeProgressChapter.php

@@ -0,0 +1,115 @@
+<?php
+
+namespace App\Console\Commands;
+
+use Illuminate\Console\Command;
+use App\Models\Sentence;
+use App\Models\PaliSentence;
+use App\Models\Progress;
+use App\Models\ProgressChapter;
+use App\Models\PaliText;
+
+class UpgradeProgressChapter extends Command
+{
+    /**
+     * The name and signature of the console command.
+     *
+     * @var string
+     */
+    protected $signature = 'upgrade:progresschapter';
+
+    /**
+     * The console command description.
+     *
+     * @var string
+     */
+    protected $description = '更新章节完成度,以channel为单位';
+
+    /**
+     * Create a new command instance.
+     *
+     * @return void
+     */
+    public function __construct()
+    {
+        parent::__construct();
+    }
+
+    /**
+     * Execute the console command.
+     *
+     * @return int
+     */
+    public function handle()
+    {
+        #第一步 查询有多少书有译文
+		$books = Sentence::where('strlen','>',0)
+                          ->where('book_id','<',1000)
+                          ->where('channel_uid','<>','')
+                          ->groupby('book_id')
+                          ->select('book_id')
+                          ->get();
+
+        $bar = $this->output->createProgressBar(count($books));
+
+        foreach ($books as $book) {
+            # code...
+            $chapters = PaliText::where('book',$book->book_id)
+                                ->where('level','>',0)
+                                ->where('level','<',8)
+                                ->select('paragraph','chapter_strlen','chapter_len')
+                                ->get();
+            foreach ($chapters as $key => $chapter) {
+                # code...
+                $chapter_strlen = PaliSentence::where('book',$book->book_id)
+                                    ->whereBetween('paragraph',[$chapter->paragraph,$chapter->paragraph+$chapter->chapter_len-1])
+                                    ->sum('length');
+                if($chapter_strlen == 0){
+                    $this->error('chapter_strlen is 0 book:'.$book->book_id.' paragraph:'.$chapter->paragraph.'-'.($chapter->paragraph+$chapter->chapter_len-1));
+                    continue;
+                }
+                $strlen = Progress::where('book',$book->book_id)
+                                  ->whereBetween('para',[$chapter->paragraph,$chapter->paragraph+$chapter->chapter_len-1])
+                                  ->groupby('channel_id')
+                                  ->selectRaw('channel_id, sum(all_strlen) as cp_len')
+                                  ->get();
+                foreach ($strlen as $final) {
+                    # code...
+                    # 计算此段落完成时间
+                    $finalAt = Progress::where('book',$book->book_id)
+                                ->whereBetween('para',[$chapter->paragraph,$chapter->paragraph+$chapter->chapter_len-1])
+                                ->where('channel_id',$final->channel_id)
+                                ->max('created_at');
+                    $updateAt = Progress::where('book',$book->book_id)
+                                ->whereBetween('para',[$chapter->paragraph,$chapter->paragraph+$chapter->chapter_len-1])
+                                ->where('channel_id',$final->channel_id)
+                                ->max('updated_at');
+                    #查询标题
+                    $title = Sentence::where('book_id',$book->book_id)
+                          ->where('paragraph',$chapter->paragraph)
+                          ->where('channel_uid',$final->channel_id)
+                          ->value('content');
+                    ProgressChapter::updateOrInsert(
+                        [
+                            'book'=>$book->book_id,
+                            'para'=>$chapter->paragraph,
+                            'channel_id'=>$final->channel_id
+                        ],
+                        [
+                            'lang'=>'en',
+                            'all_trans'=>$final->cp_len/$chapter_strlen,
+                            'public'=>$final->cp_len/$chapter_strlen,
+                            'progress'=>$final->cp_len/$chapter_strlen,
+                            'title'=>mb_substr($title,0,255,"UTF-8"),
+                            'created_at'=>$finalAt,
+                            'updated_at'=>$updateAt,
+                        ]);
+                }
+            }
+            $bar->advance();
+        }
+        $bar->finish();
+        
+        return 0;
+    }
+}

+ 107 - 0
app/Http/Controllers/ProgressChapterController.php

@@ -0,0 +1,107 @@
+<?php
+
+namespace App\Http\Controllers;
+
+use App\Models\ProgressChapter;
+use App\Models\Channel;
+use Illuminate\Http\Request;
+
+class ProgressChapterController extends Controller
+{
+    /**
+     * Display a listing of the resource.
+     *
+     * @return \Illuminate\Http\Response
+     */
+    public function index(Request $request)
+    {
+        //
+        switch ($request->get('view')) {
+			case 'studio':
+            #查询该studio的channel
+            $channels = Channel::where('owner_uid',$request->get('id'))->select('uid')->get();
+            $aChannel = [];
+            foreach ($channels as $channel) {
+                # code...
+                $aChannel[] = $channel->uid;
+            }
+            $chapters = ProgressChapter::select('book','para','channel_id','title','progress','created_at','updated_at')
+                                       ->whereIn('channel_id', $aChannel)
+                                       ->where('progress','>',0.85)
+                                       ->orderby('created_at','desc')
+                                       ->get();
+            if($chapters){
+                return $this->ok(["rows"=>$chapters,"count"=>count($chapters)]);
+            }else{
+                return $this->error("没有查询到数据");
+            }
+            break;
+        }
+    }
+
+    /**
+     * Show the form for creating a new resource.
+     *
+     * @return \Illuminate\Http\Response
+     */
+    public function create()
+    {
+        //
+    }
+
+    /**
+     * Store a newly created resource in storage.
+     *
+     * @param  \Illuminate\Http\Request  $request
+     * @return \Illuminate\Http\Response
+     */
+    public function store(Request $request)
+    {
+        //
+    }
+
+    /**
+     * Display the specified resource.
+     *
+     * @param  \App\Models\ProgressChapter  $progressChapter
+     * @return \Illuminate\Http\Response
+     */
+    public function show(ProgressChapter $progressChapter)
+    {
+        //
+    }
+
+    /**
+     * Show the form for editing the specified resource.
+     *
+     * @param  \App\Models\ProgressChapter  $progressChapter
+     * @return \Illuminate\Http\Response
+     */
+    public function edit(ProgressChapter $progressChapter)
+    {
+        //
+    }
+
+    /**
+     * Update the specified resource in storage.
+     *
+     * @param  \Illuminate\Http\Request  $request
+     * @param  \App\Models\ProgressChapter  $progressChapter
+     * @return \Illuminate\Http\Response
+     */
+    public function update(Request $request, ProgressChapter $progressChapter)
+    {
+        //
+    }
+
+    /**
+     * Remove the specified resource from storage.
+     *
+     * @param  \App\Models\ProgressChapter  $progressChapter
+     * @return \Illuminate\Http\Response
+     */
+    public function destroy(ProgressChapter $progressChapter)
+    {
+        //
+    }
+}

+ 39 - 0
database/migrations/2022_02_27_103552_add_channel_to_progress_chapter.php

@@ -0,0 +1,39 @@
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\Schema;
+
+class AddChannelToProgressChapter extends Migration
+{
+    /**
+     * Run the migrations.
+     *
+     * @return void
+     */
+    public function up()
+    {
+        Schema::table('progress_chapters', function (Blueprint $table) {
+            //
+            $table->uuid('channel_id')->index();
+            $table->float('progress');
+            $table->string('title',256)->nullable()->index();
+
+        });
+    }
+
+    /**
+     * Reverse the migrations.
+     *
+     * @return void
+     */
+    public function down()
+    {
+        Schema::table('progress_chapters', function (Blueprint $table) {
+            //
+            $table->dropColumn('channel_id');
+            $table->dropColumn('progress');
+            $table->dropColumn('title');
+        });
+    }
+}

+ 34 - 0
database/migrations/2022_02_27_151744_add_channel_to_progress.php

@@ -0,0 +1,34 @@
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\Schema;
+
+class AddChannelToProgress extends Migration
+{
+    /**
+     * Run the migrations.
+     *
+     * @return void
+     */
+    public function up()
+    {
+        Schema::table('progress', function (Blueprint $table) {
+            //
+            $table->uuid('channel_id')->index();
+        });
+    }
+
+    /**
+     * Reverse the migrations.
+     *
+     * @return void
+     */
+    public function down()
+    {
+        Schema::table('progress', function (Blueprint $table) {
+            //
+            $table->dropColumn('channel_id');
+        });
+    }
+}

+ 1 - 1
public/app/uhome/palicanon.php

@@ -15,7 +15,7 @@ include "../pcdl/html_head.php";
 ?>
 
 <div class='section_inner'>
-	<div id='bio' class='course_info_block'></div>
+	<div id='content' class='course_info_block'></div>
 	<div id='wikipali_step' class='course_info_block'></div>
 </div>
 

+ 9 - 0
public/app/uhome/style.css

@@ -13,3 +13,12 @@
 .user_home_category {
 	border-bottom: 1px solid var(--border-line-color);
 }
+
+.chapter_list {
+    border-bottom: 1px solid var(--border-line-color);
+    padding: 1em 1em;
+}
+.chapter_list>.title {
+    font-size: 1.8em;
+    padding: 10px 0;
+}

+ 27 - 1
public/app/uhome/uhome.js

@@ -26,4 +26,30 @@ function getUserBio(userid) {
 	);
 }
 
-function getUserPalicanon(userid) {}
+function getUserPalicanon(userid) {
+
+    $.getJSON(
+        "/api/v2/progress?view=studio&id="+userid
+    ).done(function(data){
+
+        $('#content').html(render_palicanon_chapter_list(data.data.rows));
+    });
+
+}
+
+function render_palicanon_chapter_list(data){
+        let html = '';
+        for (const iterator of data) {
+            let link = "<a href='../article/?view=chapter&book="+iterator.book+"&par="+iterator.para+"&channel="+iterator.channel_id+"' target='_blank'>";
+            html += "<div class='chapter_list'>";
+            let title = iterator.title;
+            if(title==''){
+                title = 'unkow';
+            }
+            html += "<div class='title'>"+link+title+"</a>"+"<tag>"+"</tag></div>";
+            html += "<div class='path'>长部>沙门果经"+"</div>";
+            html += "<div class='date'> 创建:"+iterator.created_at+" 更新:"+iterator.updated_at+"</div>";
+            html += "</div>";
+        }
+        return html;
+}

+ 15 - 15
public/app/upgrade/upgrade_pali_toc.php

@@ -86,9 +86,9 @@ foreach ($result_lang as $lang) {
 			if($redis){
 				$redis->hSet("progress_{$para["book"]}-{$para["paragraph"]}", $lang["language"], $para_strlen);
 			}
-			{
-				$sth_toc->execute(array($para["book"], $para["paragraph"], $lang["language"], $para_strlen, 0));
-			}
+
+			$sth_toc->execute(array($para["book"], $para["paragraph"], $lang["language"], $para_strlen, 0));
+
         }
     }
 }
@@ -100,7 +100,7 @@ if (!$sth_toc || ($sth_toc && $sth_toc->errorCode() != 0)) {
     echo "error:" . $error[2] . "\n";
 }
 
-#第三步生成 段落完成度库
+#第三步生成 章节完成度库
 /* 开始一个事务,关闭自动提交 */
 $dbh_toc->beginTransaction();
 $query = "INSERT INTO "._TABLE_PROGRESS_CHAPTER_." (book, para , lang , all_trans,public) VALUES (?, ?, ? , ? ,? )";
@@ -138,19 +138,19 @@ foreach ($valid_book as $key => $book) {
                         $redis->hSet("progress_chapter_{$book["book"]}_{$chapter["paragraph"]}", $lang["language"], $progress);
                     }
                 } 
-				{
-                    $query = "SELECT sum(all_strlen) as all_strlen from "._TABLE_PROGRESS_." where book = ? and (para between ? and ? )and lang = ?";
-                    $stmt = $dbh_toc->prepare($query);
-                    $stmt->execute(array($book["book"], $chapter["paragraph"], (int) $chapter["paragraph"] + (int) $chapter["chapter_len"] - 1, $lang["language"]));
-                    $result_chapter_trans_strlen = $stmt->fetch(PDO::FETCH_ASSOC);
-                    if ($result_chapter_trans_strlen) {
-                        $tran_strlen = (int) $result_chapter_trans_strlen["all_strlen"];
-                        if ($tran_strlen > 0) {
-                            $progress = $tran_strlen / $pali_strlen;
-                            $sth_toc->execute(array($book["book"], $chapter["paragraph"], $lang["language"], $progress, 0));
-                        }
+				
+                $query = "SELECT sum(all_strlen) as all_strlen from "._TABLE_PROGRESS_." where book = ? and (para between ? and ? )and lang = ?";
+                $stmt = $dbh_toc->prepare($query);
+                $stmt->execute(array($book["book"], $chapter["paragraph"], (int) $chapter["paragraph"] + (int) $chapter["chapter_len"] - 1, $lang["language"]));
+                $result_chapter_trans_strlen = $stmt->fetch(PDO::FETCH_ASSOC);
+                if ($result_chapter_trans_strlen) {
+                    $tran_strlen = (int) $result_chapter_trans_strlen["all_strlen"];
+                    if ($tran_strlen > 0) {
+                        $progress = $tran_strlen / $pali_strlen;
+                        $sth_toc->execute(array($book["book"], $chapter["paragraph"], $lang["language"], $progress, 0));
                     }
                 }
+                
                 #插入段落数据
             }
         }

+ 2 - 0
routes/api.php

@@ -5,6 +5,7 @@ use Illuminate\Support\Facades\Route;
 use App\Http\Controllers\WbwTemplateController;
 use App\Http\Controllers\DhammaTermController;
 use App\Http\Controllers\SentenceController;
+use App\Http\Controllers\ProgressChapterController;
 /*
 |--------------------------------------------------------------------------
 | API Routes
@@ -24,4 +25,5 @@ Route::group(['prefix' => 'v2'],function(){
 	Route::apiResource('wbw_templates',WbwTemplateController::class);
 	Route::apiResource('terms',DhammaTermController::class);
 	Route::apiResource('sentence',SentenceController::class);
+	Route::apiResource('progress',ProgressChapterController::class);
 });