Browse Source

add stats for user, video and tag

master
filesite 7 months ago
parent
commit
c3b07a06f2
  1. 104
      plugins/TajianStats.php
  2. 32
      themes/tajian/controller/FrontapiController.php
  3. 6
      themes/tajian/controller/SiteController.php

104
plugins/TajianStats.php

@ -0,0 +1,104 @@ @@ -0,0 +1,104 @@
<?php
/**
* Ta荐的核心指标
*/
Class TajianStats {
//注册用户总数
static $total_user = 0;
//收藏的视频总数
static $total_video = 0;
//添加的分类总数
static $total_tag = 0;
//上一次保存到缓存文件的时间
static $cache_save_time = 0;
static $cache_filename = 'tajian_stats.json';
//初始化数据,优先从本地缓存文件恢复数据
public static function init() {
$filepath = __DIR__ . '/../runtime/' . self::$cache_filename;
try {
if (file_exists($filepath)) {
$json = file_get_contents($filepath);
$stats = json_decode($json, true);
self::$total_user = $stats['user'];
self::$total_video = $stats['video'];
self::$total_tag = $stats['tag'];
self::$cache_save_time = $stats['cache_time'];
}
}catch(Exception $e) {
//文件读取异常
}
return self::get();
}
//保存到本地文件,规则:距离上一次保存时间至少间隔10分钟
public static function save() {
$saved = false;
$filepath = __DIR__ . '/../runtime/' . self::$cache_filename;
try {
self::$cache_save_time = time(); //记录更新时间
$stats = self::get();
$saved = file_put_contents($filepath, json_encode($stats));
}catch(Exception $e) {
//文件写入异常
}
return $saved !== false;
}
//返回统计数据
public static function get() {
return array(
'user' => self::$total_user,
'video' => self::$total_video,
'tag' => self::$total_tag,
'cache_time' => self::$cache_save_time,
);
}
public static function increase($data_type) {
$total = 0;
switch($data_type) {
case 'user':
self::$total_user ++;
$total = self::$total_user;
break;
case 'video':
self::$total_video ++;
$total = self::$total_video;
break;
case 'tag':
self::$total_tag ++;
$total = self::$total_tag;
break;
}
return $total;
}
public static function decrease($data_type) {
$total = 0;
switch($data_type) {
case 'video':
self::$total_video --;
$total = self::$total_video;
break;
case 'tag':
self::$total_tag --;
$total = self::$total_tag;
break;
}
return $total;
}
}

32
themes/tajian/controller/FrontapiController.php

@ -6,6 +6,7 @@ require_once __DIR__ . '/../../../lib/DirScanner.php'; @@ -6,6 +6,7 @@ require_once __DIR__ . '/../../../lib/DirScanner.php';
require_once __DIR__ . '/../../../plugins/Parsedown.php';
require_once __DIR__ . '/../../../plugins/Html.php';
require_once __DIR__ . '/../../../plugins/Common.php';
require_once __DIR__ . '/../../../plugins/TajianStats.php';
require_once __DIR__ . '/SiteController.php';
Class FrontApiController extends SiteController {
@ -128,7 +129,15 @@ Class FrontApiController extends SiteController { @@ -128,7 +129,15 @@ Class FrontApiController extends SiteController {
}
if ($code == 1) { //保存视频
$msg = $this->saveShareVideo($content, $title, $tagName) ? '视频保存完成,系统开始自动处理,1 - 3 分钟后刷新就能看到新添加的视频了。' : '视频保存失败,请稍后重试!';
$done = $this->saveShareVideo($content, $title, $tagName);
$msg = $done ? '视频保存完成,系统开始自动处理,1 - 3 分钟后刷新就能看到新添加的视频了。' : '视频保存失败,请稍后重试!';
//更新统计数据
if ($done) {
$stats = TajianStats::init();
TajianStats::increase('video');
$saved = TajianStats::save();
}
}
return $this->renderJson(compact('code', 'msg', 'err'));
@ -661,6 +670,11 @@ eof; @@ -661,6 +670,11 @@ eof;
Common::saveFriendsCode($cellphone);
Common::initUserData($cellphone, $friends_code);
//更新统计数据
$stats = TajianStats::init();
TajianStats::increase('user');
$saved = TajianStats::save();
$shareUrl = "/{$newUser['username']}/";
$msg = "注册完成,开始收藏你喜欢的视频吧,正在为你跳转到专属网址...";
$code = 1;
@ -924,6 +938,11 @@ eof; @@ -924,6 +938,11 @@ eof;
//保存
$saved = $this->deleteTag($tag_to_delete);
if (!empty($saved)) {
//更新统计数据
$stats = TajianStats::init();
TajianStats::decrease('tag');
$saved = TajianStats::save();
$msg = "分类已删除";
$code = 1;
}else {
@ -998,6 +1017,11 @@ eof; @@ -998,6 +1017,11 @@ eof;
//保存
$saved = $this->addTag(ucfirst($tag_to_add));
if (!empty($saved)) {
//更新统计数据
$stats = TajianStats::init();
TajianStats::increase('tag');
$saved = TajianStats::save();
$msg = "分类已添加";
$code = 1;
}else {
@ -1070,6 +1094,12 @@ eof; @@ -1070,6 +1094,12 @@ eof;
//删除此视频的所有文件
$saved = $this->deleteVideoFiles($video_filename);
if (!empty($saved)) {
//更新统计数据
$stats = TajianStats::init();
TajianStats::decrease('video');
$saved = TajianStats::save();
$msg = "视频已删除";
$code = 1;
}else {

6
themes/tajian/controller/SiteController.php

@ -5,6 +5,7 @@ @@ -5,6 +5,7 @@
require_once __DIR__ . '/../../../lib/DirScanner.php';
require_once __DIR__ . '/../../../plugins/Parsedown.php';
require_once __DIR__ . '/../../../plugins/Common.php';
require_once __DIR__ . '/../../../plugins/TajianStats.php';
Class SiteController extends Controller {
@ -207,6 +208,11 @@ Class SiteController extends Controller { @@ -207,6 +208,11 @@ Class SiteController extends Controller {
rename($tagFile, $newTagFile);
}else {
unlink($tagFile);
//更新统计数据
$stats = TajianStats::init();
TajianStats::decrease('tag');
$saved = TajianStats::save();
}
}
}

Loading…
Cancel
Save