Usage_stats.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. // This file is part of GNU social - https://www.gnu.org/software/social
  3. //
  4. // GNU social is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // GNU social is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * Table for storing Nodeinfo statistics
  18. *
  19. * @package NodeInfo
  20. * @author Diogo Cordeiro <diogo@fc.up.pt>
  21. * @copyright 2018-2019 Free Software Foundation, Inc http://www.fsf.org
  22. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  23. */
  24. defined('GNUSOCIAL') || die();
  25. /**
  26. * Table Definition for usage_stats and some getters
  27. *
  28. * @copyright 2018-2019 Free Software Foundation, Inc http://www.fsf.org
  29. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  30. */
  31. class Usage_stats extends Managed_DataObject
  32. {
  33. public $__table = 'usage_stats'; // table name
  34. public $type; // varchar(191) unique_key not 255 because utf8mb4 takes more space
  35. public $count; // int(4)
  36. public $modified; // datetime() not_null default_CURRENT_TIMESTAMP
  37. /**
  38. * Table Definition for usage_stats
  39. *
  40. * @return array
  41. */
  42. public static function schemaDef(): array
  43. {
  44. return [
  45. 'description' => 'node stats',
  46. 'fields' => [
  47. 'type' => ['type' => 'varchar', 'not null' => true, 'length' => 191, 'description' => 'Type of countable entity'],
  48. 'count' => ['type' => 'int', 'size' => 'int', 'default' => 0, 'description' => 'Number of entities of this type'],
  49. 'modified' => ['type' => 'datetime', 'not null' => true, 'default' => 'CURRENT_TIMESTAMP', 'description' => 'date this record was modified'],
  50. ],
  51. 'primary key' => ['type'],
  52. 'indexes' => [
  53. 'user_stats_idx' => ['type'],
  54. ],
  55. ];
  56. }
  57. /**
  58. * Total number of users
  59. *
  60. * @return int
  61. */
  62. public function getUserCount(): int
  63. {
  64. return Usage_stats::getKV('type', 'users')->count;
  65. }
  66. /**
  67. * Total number of dents
  68. *
  69. * @return int
  70. */
  71. public function getPostCount(): int
  72. {
  73. return Usage_stats::getKV('type', 'posts')->count;
  74. }
  75. /**
  76. * Total number of replies
  77. *
  78. * @return int
  79. */
  80. public function getCommentCount(): int
  81. {
  82. return Usage_stats::getKV('type', 'comments')->count;
  83. }
  84. }