rate.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. <?php
  2. /** @var yii\web\View $this */
  3. use yii\helpers\Html;
  4. use yii\widgets\DetailView;
  5. $cache = Yii::$app->cache;
  6. $month = array(
  7. 1 => 'Январь',
  8. 2 => 'Февраль',
  9. 3 => 'Март',
  10. 4 => 'Апрель',
  11. 5 => 'Май',
  12. 6 => 'Июнь',
  13. 7 => 'Июль',
  14. 8 => 'Август',
  15. 9 => 'Сентябрь',
  16. 10 => 'Октябрь',
  17. 11 => 'Ноябрь',
  18. 12 => 'Декабрь'
  19. );
  20. $week = 1;
  21. if (Yii::$app->request->isGet){
  22. $week = Yii::$app->request->get('week');
  23. $week = $week?$week:'1';
  24. }
  25. $start = strtotime( date("Y-m-d", strtotime( date("Y-m-d")." $week weeks ago")) );
  26. $mstart = date("Y-m-d", strtotime(" monday this week", $start ));
  27. $mend = date("Y-m-d", strtotime( " sunday this week", $start ));
  28. $this->title = "Контентная аналитика ".$mstart;
  29. $this->params['breadcrumbs'][] = ['label' => $this->title, 'url' => ['index']];
  30. $cache = Yii::$app->cache;
  31. $ckey = "w page:".md5("$mstart $mend");
  32. if( $res = $cache->get($ckey) ){
  33. $r = unserialize($res);
  34. }else{
  35. $r = $model->YGetByTime(['date1'=>$mstart, 'date2'=>$mend,'dimensions'=>'ym:s:publisherArticle','metrics'=>'ym:s:publisherviews','filters'=>'(ym:s:publisherArticle!n)','sort'=>'-ym:s:publisherviews','limit'=>20, 'top_keys'=>20]);
  36. if( $r ){
  37. $cache->set($ckey, serialize( $r ));
  38. }
  39. }
  40. $ckey = "w rubric:".md5("$mstart $mend");
  41. if( $res = $cache->get($ckey) ){
  42. $r1 = unserialize($res);
  43. }else{
  44. $r1 = $model->YGetByTime(['date1'=>$mstart, 'date2'=>$mend,'dimensions'=>'ym:s:publisherArticleRubric, ym:s:publisherArticleRubric2','metrics'=>'ym:s:publisherviews','filters'=>'(ym:s:publisherArticle!n)','sort'=>'-ym:s:publisherviews','limit'=>20, 'top_keys'=>20]);
  45. if( $r ){
  46. $cache->set($ckey, serialize( $r1 ));
  47. }
  48. }
  49. $ckey = "w topic:".md5("$mstart $mend");
  50. if( $res = $cache->get($ckey) ){
  51. $r2 = unserialize($res);
  52. }else{
  53. $r2 = $model->YGetByTime(['date1'=>$mstart, 'date2'=>$mend,'dimensions'=>'ym:s:publisherArticleTopic','metrics'=>'ym:s:publisherviews','filters'=>'(ym:s:publisherArticle!n)','sort'=>'-ym:s:publisherviews','limit'=>20, 'top_keys'=>20]);
  54. if( $r ){
  55. $cache->set($ckey, serialize( $r2 ));
  56. }
  57. }
  58. $ckey = "w tn:".md5("$mstart $mend");
  59. if( $res = $cache->get($ckey) ){
  60. $tncount = unserialize($res);
  61. }else{
  62. $rubricInNews = Yii::$app->db->createCommand("select t.topic_id tid, tn.title, count(news_id) c FROM `news` n, `news_topic_relation` t, `news_topic` tn WHERE `dt_pub` BETWEEN '".$mstart."' AND '".$mend."' and n.active = 'Y' and tn.active = 1 and n.id=news_id and tn.id=t.topic_id group by t.topic_id order by c desc")->queryAll();
  63. $tncount = [];
  64. foreach($rubricInNews as $item){
  65. $tncount[$item['title']] = $item['c'];
  66. }
  67. $cache->set($ckey, serialize( $tncount ));
  68. }
  69. $ckey = "w tagsn:".md5("$mstart $mend");
  70. if( $res = $cache->get($ckey) ){
  71. $tagncount = unserialize($res);
  72. }else{
  73. // echo "select t.topic_id tid, tn.title, count(news_id) c FROM `news` n, `tags_relation` t, `tags` tn WHERE `dt_pub` BETWEEN '".$mstart."' AND '".$mend."' and n.active = 'Y' and tn.active = 1 and n.id=news_id and tn.id=t.topic_id group by t.topic_id order by c desc";
  74. $rubricInNews = Yii::$app->db->createCommand("select t.topic_id tid, tn.title, count(news_id) c FROM `news` n, `tags_relation` t, `tags` tn WHERE `dt_pub` BETWEEN '".$mstart."' AND '".$mend."' and n.active = 'Y' and tn.active = 1 and n.id=news_id and tn.id=t.topic_id group by t.topic_id order by c desc")->queryAll();
  75. $tagncount = [];
  76. foreach($rubricInNews as $item){
  77. $tagncount[$item['title']] = $item['c'];
  78. }
  79. $cache->set($ckey, serialize( $tagncount ));
  80. }
  81. //print_a($tagncount);
  82. //print_a($r2);
  83. ?>
  84. <section class="content">
  85. <div class="container-fluid">
  86. <div class="row">
  87. <div class="col-12">
  88. <div class="alert bg-lightblue disabled color-palette alert-dismissible">
  89. <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
  90. <h5><i class="icon fas fa-info"></i> Alert!</h5>
  91. <h2>Прошлая неделя: <span class="text-white"><b><?=$mstart?> - <?=$mend?></b></span></h2>
  92. </div>
  93. </div>
  94. </div>
  95. <div class="row">
  96. <div class="col-12">
  97. <div class="callout callout-info">
  98. <h5><i class="fas fa-info"></i> amic.ru:</h5>
  99. Данные <b>amic.ru</b> по данным яндекс метрика
  100. </div>
  101. <nav aria-label="Page navigation example">
  102. <ul class="pagination">
  103. <li class="page-item <?=($week == 3)?'active':''?>"><a class="page-link" href="?week=3">3 недели назад</a></li>
  104. <li class="page-item <?=($week == 2)?'active':''?>"><a class="page-link" href="?week=2">2 недели назад</a></li>
  105. <li class="page-item <?=($week == 1)?'active':''?>"><a class="page-link" href="?week=1">1 неделя назад</a></li>
  106. </ul>
  107. </nav>
  108. <div class="card card-primary card-outline">
  109. <div class="card-header border-0">
  110. <h3 class="card-title">ТОП Новостей за неделю</h3>
  111. </div>
  112. <div class="card-body table-responsive p-0">
  113. <table class="table table-striped table-valign-middle" id="reftab">
  114. <thead>
  115. <tr>
  116. <th>Статья</th>
  117. <th class="sorting">Посетителей</th>
  118. <th class="sorting">Рубрика&nbsp;1</th>
  119. <th>Рубрика&nbsp;2</th>
  120. <th>Тэги</th>
  121. </tr>
  122. </thead>
  123. <tbody id="referertab">
  124. <?
  125. foreach($r->data as $item){
  126. if( $item->dimensions[0]->url == 'http://amic.ru/' ) continue;
  127. ?>
  128. <tr>
  129. <?
  130. echo "<td><a href='{$item->dimensions[0]->url}' target='_blank'>".$item->dimensions[0]->title."</a></td>";
  131. echo "<td>".$item->metrics[0][0]."</td>";
  132. echo "<td>".$item->dimensions[0]->rubric."</td>";
  133. echo "<td>".$item->dimensions[0]->rubric2."</td>";
  134. echo "<td>".$item->dimensions[0]->topics."</td>";
  135. ?>
  136. </tr>
  137. <?
  138. }
  139. ?>
  140. </tbody>
  141. </table>
  142. </div>
  143. </div>
  144. </div></div>
  145. <div class="row">
  146. <div class="col-6">
  147. <div class="card card-primary card-outline">
  148. <div class="card-header border-0">
  149. <h3 class="card-title">ТОП Рубрик за неделю</h3>
  150. </div>
  151. <div class="card-body table-responsive p-0">
  152. <table class="table table-striped table-valign-middle" id="reftab">
  153. <thead>
  154. <tr>
  155. <th>Рубрика</th>
  156. <th class="sorting">Посетителей</th>
  157. <th class="sorting">публикаций в рубрике</th>
  158. </tr>
  159. </thead>
  160. <tbody id="referertab">
  161. <?
  162. foreach($r1->data as $item){
  163. ?>
  164. <tr>
  165. <?
  166. $s = isset($tncount[$item->dimensions[0]->name])?'':'<span class="float-right badge bg-success">раздел/сюжет</span>';
  167. echo "<td>".$item->dimensions[0]->name." $s</td>";
  168. echo "<td>".$item->metrics[0][0]."</td>";
  169. if( isset($tncount[$item->dimensions[0]->name]) ){
  170. echo "<td>".$tncount[$item->dimensions[0]->name]."</td>";
  171. }else{
  172. echo "<td></td>";
  173. }
  174. ?>
  175. </tr>
  176. <?
  177. }
  178. ?>
  179. </tbody>
  180. </table>
  181. </div>
  182. </div>
  183. <div class="card card-success card-outline">
  184. <div class="card-header border-0">
  185. <h3 class="card-title">ТОП Тэгов за неделю</h3>
  186. </div>
  187. <div class="card-body table-responsive p-0">
  188. <table class="table table-striped table-valign-middle" id="reftab">
  189. <thead>
  190. <tr>
  191. <th>Тэги</th>
  192. <th class="sorting">Посетителей</th>
  193. <th class="sorting">Новостей с тэгом</th>
  194. <th class="sorting">относительно публикаций</th>
  195. </tr>
  196. </thead>
  197. <tbody id="referertab">
  198. <?
  199. // $sum = array_sum($tagncount);
  200. $tnp1 = [];
  201. foreach($r2->data as $item){
  202. ?>
  203. <tr>
  204. <?
  205. echo "<td>".$item->dimensions[0]->name."</td>";
  206. echo "<td>".$item->metrics[0][0]."</td>";
  207. if( isset($tagncount[$item->dimensions[0]->name]) ){
  208. echo "<td>".$tagncount[$item->dimensions[0]->name]."</td>";
  209. $perc = round($item->metrics[0][0]/$tagncount[$item->dimensions[0]->name]);
  210. echo "<td>".round($perc,1)."</td>";
  211. $tnp1[$item->dimensions[0]->name] = $perc;
  212. }else{
  213. echo "<td></td>";
  214. echo "<td></td>";
  215. }
  216. ?>
  217. </tr>
  218. <?
  219. }
  220. ?>
  221. </tbody>
  222. </table>
  223. </div>
  224. </div>
  225. </div>
  226. <div class="col-6">
  227. <div class="card card-primary card-outline">
  228. <div class="card-header border-0">
  229. <h3 class="card-title">ТОП Рубрик за неделю относительно количества публикаций</h3>
  230. </div>
  231. <div class="card-body table-responsive p-0">
  232. <table class="table table-striped table-valign-middle" id="reftab">
  233. <thead>
  234. <tr>
  235. <th>Рубрика</th>
  236. <th class="sorting">Посетителей на публикацию</th>
  237. </tr>
  238. </thead>
  239. <tbody id="referertab">
  240. <?
  241. $tnp = [];
  242. foreach($r1->data as $item){
  243. if( isset($tncount[$item->dimensions[0]->name]) ){
  244. $perc = round($item->metrics[0][0]/$tncount[$item->dimensions[0]->name],1)."";
  245. $tnp[$item->dimensions[0]->name] = $perc;
  246. }
  247. }
  248. $sum = array_sum($tnp);
  249. arsort($tnp);
  250. foreach($tnp as $key=>$item){
  251. ?>
  252. <tr>
  253. <?
  254. echo "<td>".$key."</td>";
  255. echo "<td>".$item."</td>";
  256. ?>
  257. </tr>
  258. <?
  259. }
  260. ?>
  261. </tbody>
  262. </table>
  263. </div>
  264. </div>
  265. <div class="card card-primary card-outline">
  266. <div class="card-header">
  267. <h3 class="card-title">Эффективность статей по рубрикам</h3>
  268. </div>
  269. <div class="card-body">
  270. <canvas id="pieChart"></canvas>
  271. </div>
  272. </div>
  273. <div class="card card-success card-outline">
  274. <div class="card-header">
  275. <h3 class="card-title">Эффективность статей по тэгам</h3>
  276. </div>
  277. <div class="card-body">
  278. <canvas id="pieChart1"></canvas>
  279. </div>
  280. </div>
  281. </div></div>
  282. <div class="row">
  283. <div class="col-6">
  284. </div>
  285. <div class="col-6">
  286. </div>
  287. </div>
  288. </section>
  289. <?
  290. $srub = "";
  291. $srubc = '';
  292. $bgc = "";
  293. $bga = ['#5225f0','#ae55d9','#4c37b6','#7bf793','#6cbbef','#cba4f9','#15b395','#3dc055','#9cce37','#d2fb46','#ce2319','#1eb696','#cacd9a','#231e6a','#a23c80'];
  294. foreach($tnp as $key=>$item){
  295. $srubc .= round($item*100/$sum,1).',';
  296. $srub .= '"'.$key.' '. round($item*100/$sum,1).'%",';
  297. $r = array_pop($bga);
  298. if( $r ){
  299. $bgc .= "'".$r."',";
  300. }else{
  301. $bgc .= "'#".dechex(rand(10,255)).dechex(rand(10,255)).dechex(rand(1,255))."',";
  302. }
  303. }
  304. ?>
  305. <?
  306. $sum1 = array_sum($tnp1);
  307. arsort($tnp1);
  308. $srub1 = "";
  309. $srubc1 = '';
  310. $bgc1 = "";
  311. $bga1 = ['#5225f0','#ae55d9','#4c37b6','#7bf793','#6cbbef','#cba4f9','#15b395','#3dc055','#9cce37','#d2fb46','#ce2319','#1eb696','#cacd9a','#231e6a','#a23c80'];
  312. foreach($tnp1 as $key=>$item){
  313. $srubc1 .= round($item*100/$sum1,1).',';
  314. $srub1 .= '"'.$key.' '. round($item*100/$sum1,1).'%",';
  315. $r = array_pop($bga1);
  316. if( $r ){
  317. $bgc1 .= "'".$r."',";
  318. }else{
  319. $bgc1 .= "'#".dechex(rand(10,255)).dechex(rand(10,255)).dechex(rand(1,255))."',";
  320. }
  321. }
  322. ?>
  323. <script>
  324. <?=$this->render('/stats/chartjs');?>
  325. var donutData = {
  326. labels: [<?=$srub?>],
  327. datasets: [
  328. {
  329. data: [<?=$srubc?>],
  330. backgroundColor : [<?=$bgc?>],
  331. }
  332. ]
  333. }
  334. var pieChartCanvas = $('#pieChart').get(0).getContext('2d')
  335. var pieData = donutData;
  336. var pieOptions = {
  337. maintainAspectRatio : true,
  338. responsive : true,
  339. legend: {
  340. display: true,
  341. },
  342. cutoutPercentage: 20,
  343. }
  344. new Chart(pieChartCanvas, {
  345. type: 'doughnut',
  346. data: pieData,
  347. options: pieOptions
  348. })
  349. var donutData1 = {
  350. labels: [<?=$srub1?>],
  351. datasets: [
  352. {
  353. data: [<?=$srubc1?>],
  354. backgroundColor : [<?=$bgc1?>],
  355. }
  356. ]
  357. }
  358. var pieChartCanvas = $('#pieChart1').get(0).getContext('2d')
  359. var pieData = donutData1;
  360. var pieOptions = {
  361. maintainAspectRatio : true,
  362. responsive : true,
  363. legend: {
  364. display: true,
  365. },
  366. cutoutPercentage: 20,
  367. }
  368. new Chart(pieChartCanvas, {
  369. type: 'doughnut',
  370. data: pieData,
  371. options: pieOptions
  372. })
  373. $('table').DataTable({paging: false, info: false,searching: false,
  374. columnDefs: [{
  375. orderable: false,
  376. targets: 0
  377. }],
  378. "responsive": true, "lengthChange": false, "autoWidth": false,
  379. order: [[1, 'desc']]
  380. });
  381. // $('.dataTables_length').addClass('bs-select');
  382. </script>