userWatchStats_simple.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. <?php
  2. trait HomepageUserWatchStatsSimple
  3. {
  4. public function userWatchStatsSimpleSettingsArray($infoOnly = false)
  5. {
  6. $homepageInformation = [
  7. 'name' => 'User Watch Statistics Simple',
  8. 'enabled' => true,
  9. 'image' => 'plugins/images/homepage/userWatchStats.png',
  10. 'category' => 'Media Server',
  11. 'settingsArray' => __FUNCTION__
  12. ];
  13. if ($infoOnly) {
  14. return $homepageInformation;
  15. }
  16. $homepageSettings = [
  17. 'debug' => true,
  18. 'settings' => [
  19. 'Enable' => [
  20. $this->settingsOption('enable', 'homepageUserWatchStatsSimpleEnabled'),
  21. $this->settingsOption('auth', 'homepageUserWatchStatsSimpleAuth'),
  22. ],
  23. 'Connection' => [
  24. $this->settingsOption('url', 'plexURL', ['label' => 'Tautulli URL']),
  25. $this->settingsOption('token', 'plexToken', ['label' => 'Tautulli API Key']),
  26. $this->settingsOption('disable-cert-check', 'plexDisableCertCheck'),
  27. $this->settingsOption('use-custom-certificate', 'plexUseCustomCertificate'),
  28. ],
  29. 'Test Connection' => [
  30. $this->settingsOption('blank', null, ['label' => 'Please Save before Testing']),
  31. $this->settingsOption('test', 'userWatchStatsSimple'),
  32. ]
  33. ]
  34. ];
  35. return array_merge($homepageInformation, $homepageSettings);
  36. }
  37. public function testConnectionUserWatchStatsSimple()
  38. {
  39. if (!$this->homepageItemPermissions($this->userWatchStatsSimpleHomepagePermissions('test'), true)) {
  40. return false;
  41. }
  42. $url = $this->qualifyURL($this->config['plexURL']);
  43. $url = $url . "/api/v2?apikey=" . $this->config['plexToken'] . '&cmd=get_server_info';
  44. $options = $this->requestOptions($url, null, $this->config['plexDisableCertCheck'], $this->config['plexUseCustomCertificate']);
  45. try {
  46. $response = Requests::get($url, [], $options);
  47. if ($response->success) {
  48. $data = json_decode($response->body, true);
  49. if (isset($data['response']['result']) && $data['response']['result'] === 'success') {
  50. $this->setAPIResponse('success', 'Successfully connected to Tautulli', 200);
  51. return true;
  52. } else {
  53. $this->setAPIResponse('error', 'Invalid response from Tautulli server', 500);
  54. }
  55. } else {
  56. $this->setAPIResponse('error', 'Tautulli Connection Error', 500);
  57. return false;
  58. }
  59. } catch (Requests_Exception $e) {
  60. $this->setResponse(500, $e->getMessage());
  61. return false;
  62. }
  63. }
  64. public function userWatchStatsSimpleHomepagePermissions($key = null)
  65. {
  66. $permissions = [
  67. 'test' => [
  68. 'enabled' => [
  69. 'homepageUserWatchStatsSimpleEnabled',
  70. ],
  71. 'auth' => [
  72. 'homepageUserWatchStatsSimpleAuth',
  73. ],
  74. 'not_empty' => [
  75. 'plexURL',
  76. 'plexToken'
  77. ]
  78. ],
  79. 'main' => [
  80. 'enabled' => [
  81. 'homepageUserWatchStatsSimpleEnabled'
  82. ],
  83. 'auth' => [
  84. 'homepageUserWatchStatsSimpleAuth'
  85. ],
  86. 'not_empty' => [
  87. 'plexURL',
  88. 'plexToken'
  89. ]
  90. ]
  91. ];
  92. return $this->homepageCheckKeyPermissions($key, $permissions);
  93. }
  94. public function homepageOrderUserWatchStatsSimple()
  95. {
  96. if ($this->homepageItemPermissions($this->userWatchStatsSimpleHomepagePermissions('main'))) {
  97. $refreshInterval = ($this->config['homepageUserWatchStatsSimpleRefresh'] ?? 5) * 60000; // Convert minutes to milliseconds
  98. return '
  99. <div id="' . __FUNCTION__ . '">
  100. <div class="white-box">
  101. <div class="white-box-header">
  102. <i class="fa fa-bar-chart"></i> User Watch Statistics (Simple)
  103. <span class="pull-right">
  104. <small id="watchstats-simple-last-update" class="text-muted"></small>
  105. <button class="btn btn-xs btn-primary" onclick="refreshUserWatchStatsSimple()" title="Refresh Data">
  106. <i class="fa fa-refresh" id="watchstats-simple-refresh-icon"></i>
  107. </button>
  108. </span>
  109. </div>
  110. <div class="white-box-content">
  111. <div class="row" id="watchstats-simple-content">
  112. <div class="col-lg-12 text-center">
  113. <i class="fa fa-spinner fa-spin"></i> Loading statistics...
  114. </div>
  115. </div>
  116. </div>
  117. </div>
  118. </div>
  119. <script>
  120. var watchStatsSimpleRefreshTimer;
  121. var watchStatsSimpleLastRefresh = 0;
  122. function refreshUserWatchStatsSimple() {
  123. var refreshIcon = $("#watchstats-simple-refresh-icon");
  124. refreshIcon.addClass("fa-spin");
  125. // Show loading state
  126. $("#watchstats-simple-content").html(\'<div class="col-lg-12 text-center"><i class="fa fa-spinner fa-spin"></i> Loading statistics...</div>\');
  127. // Load watch statistics
  128. getUserWatchStatsSimpleData()
  129. .always(function() {
  130. refreshIcon.removeClass("fa-spin");
  131. watchStatsSimpleLastRefresh = Date.now();
  132. updateWatchStatsSimpleLastRefreshTime();
  133. });
  134. }
  135. function updateWatchStatsSimpleLastRefreshTime() {
  136. if (watchStatsSimpleLastRefresh > 0) {
  137. var ago = Math.floor((Date.now() - watchStatsSimpleLastRefresh) / 1000);
  138. var timeText = ago < 60 ? ago + "s ago" : Math.floor(ago / 60) + "m ago";
  139. $("#watchstats-simple-last-update").text("Updated " + timeText);
  140. }
  141. }
  142. function getUserWatchStatsSimpleData() {
  143. return organizrAPI2("GET", "api/v2/homepage/userWatchStatsSimple")
  144. .done(function(data) {
  145. if (data && data.response && data.response.result === "success" && data.response.data) {
  146. var stats = data.response.data;
  147. var html = "";
  148. // Display basic statistics
  149. html += \'<div class="col-lg-12"><h4>Basic Statistics (Simplified View)</h4></div>\';
  150. // Show some basic info
  151. html += \'<div class="col-lg-12"><div class="alert alert-info">This is a simplified version of User Watch Statistics for testing purposes.</div></div>\';
  152. $("#watchstats-simple-content").html(html);
  153. } else {
  154. $("#watchstats-simple-content").html(\'<div class="col-lg-12 text-center text-danger">Failed to load statistics</div>\');
  155. }
  156. })
  157. .fail(function(xhr, status, error) {
  158. $("#watchstats-simple-content").html(\'<div class="col-lg-12 text-center text-danger">Error loading statistics</div>\');
  159. });
  160. }
  161. // Auto-refresh setup
  162. var refreshInterval = ' . $refreshInterval . ';
  163. if (refreshInterval > 0) {
  164. watchStatsSimpleRefreshTimer = setInterval(function() {
  165. refreshUserWatchStatsSimple();
  166. }, refreshInterval);
  167. }
  168. // Update time display every 30 seconds
  169. setInterval(updateWatchStatsSimpleLastRefreshTime, 30000);
  170. // Initial load
  171. $(document).ready(function() {
  172. refreshUserWatchStatsSimple();
  173. });
  174. // Cleanup timer when page unloads
  175. $(window).on("beforeunload", function() {
  176. if (watchStatsSimpleRefreshTimer) {
  177. clearInterval(watchStatsSimpleRefreshTimer);
  178. }
  179. });
  180. </script>
  181. ';
  182. }
  183. }
  184. /**
  185. * Main function to get simple watch statistics
  186. */
  187. public function getUserWatchStatsSimple($options = null)
  188. {
  189. if (!$this->homepageItemPermissions($this->userWatchStatsSimpleHomepagePermissions('main'), true)) {
  190. return false;
  191. }
  192. try {
  193. // For the simple version, just return basic test data
  194. $stats = [
  195. 'period' => '30 days',
  196. 'message' => 'Simple version for testing',
  197. 'status' => 'active'
  198. ];
  199. $this->setAPIResponse('success', 'Simple watch statistics retrieved successfully', 200, $stats);
  200. return true;
  201. } catch (Exception $e) {
  202. $this->writeLog('error', 'User Watch Stats Simple Error: ' . $e->getMessage(), 'SYSTEM');
  203. $this->setAPIResponse('error', 'Failed to retrieve simple watch statistics: ' . $e->getMessage(), 500);
  204. return false;
  205. }
  206. }
  207. }