From b81d92af464f4177b079f0dbfc13c026d0f0a969 Mon Sep 17 00:00:00 2001 From: viletyy Date: Thu, 2 Dec 2021 14:39:53 +0800 Subject: [PATCH] add: platform statistic --- app/controllers/accounts_controller.rb | 1 + .../platform_statistics_controller.rb | 10 +++++++++ app/controllers/projects_controller.rb | 1 + app/models/platform_statistic.rb | 22 +++++++++++++++++++ app/models/project_statistic.rb | 13 +++++++++++ .../platform_statistics/index.json.jbuilder | 5 +++++ config/routes.rb | 1 + ...211202021833_create_platform_statistics.rb | 10 +++++++++ spec/models/platform_statistic_spec.rb | 5 +++++ 9 files changed, 68 insertions(+) create mode 100644 app/controllers/platform_statistics_controller.rb create mode 100644 app/models/platform_statistic.rb create mode 100644 app/views/platform_statistics/index.json.jbuilder create mode 100644 db/migrate/20211202021833_create_platform_statistics.rb create mode 100644 spec/models/platform_statistic_spec.rb diff --git a/app/controllers/accounts_controller.rb b/app/controllers/accounts_controller.rb index bbd824d5..18edb635 100644 --- a/app/controllers/accounts_controller.rb +++ b/app/controllers/accounts_controller.rb @@ -16,6 +16,7 @@ class AccountsController < ApplicationController ActiveRecord::Base.transaction do result = autologin_register(username, email, password, platform) if result[:message].blank? + PlatformStatistic.data.increment!(:users_count) render_ok({user: result[:user]}) else render_error(result[:message]) diff --git a/app/controllers/platform_statistics_controller.rb b/app/controllers/platform_statistics_controller.rb new file mode 100644 index 00000000..128085cc --- /dev/null +++ b/app/controllers/platform_statistics_controller.rb @@ -0,0 +1,10 @@ +class PlatformStatisticsController < ApplicationController + + def index + @platform_statistic = PlatformStatistic.data + @project_statistic = ProjectStatistic.data + @platform_statistic.increment!(:visits) + @tasks_count = ActiveRecord::Base.connection.exec_query("SELECT COUNT(*) FROM tasks").rows[0][0] + @memos_count = ActiveRecord::Base.connection.exec_query("SELECT COUNT(*) FROM memos").rows[0][0] + end +end \ No newline at end of file diff --git a/app/controllers/projects_controller.rb b/app/controllers/projects_controller.rb index 15ec3edd..12aec7f5 100644 --- a/app/controllers/projects_controller.rb +++ b/app/controllers/projects_controller.rb @@ -8,6 +8,7 @@ class ProjectsController < ApplicationController before_action :project_public?, only: %i[fork_users praise_users watch_users] def index + PlatformStatistic.data.increment!(:visits) scope = Projects::ListQuery.call(params) # @projects = kaminari_paginate(scope) diff --git a/app/models/platform_statistic.rb b/app/models/platform_statistic.rb new file mode 100644 index 00000000..58613fd0 --- /dev/null +++ b/app/models/platform_statistic.rb @@ -0,0 +1,22 @@ +# == Schema Information +# +# Table name: platform_statistics +# +# id :integer not null, primary key +# visits :integer default("0") +# users_count :integer default("0") +# created_at :datetime not null +# updated_at :datetime not null +# + +class PlatformStatistic < ApplicationRecord + + def self.data + data = self.last + if data.present? + return data + else + return PlatformStatistic.create(users_count: User.count) + end + end +end diff --git a/app/models/project_statistic.rb b/app/models/project_statistic.rb index 10eb14a4..37f6e45d 100644 --- a/app/models/project_statistic.rb +++ b/app/models/project_statistic.rb @@ -12,4 +12,17 @@ # class ProjectStatistic < ApplicationRecord + + def self.data + data = self.last + if data.present? + return data + else + common_projects_count = Project.common.count + mirror_projects_count = Project.mirror.count + sync_mirror_projects_count = Project.sync_mirror.count + return ProjectStatistic.create(common_projects_count: common_projects_count, mirror_projects_count: mirror_projects_count, sync_mirror_projects_count: sync_mirror_projects_count) + + end + end end diff --git a/app/views/platform_statistics/index.json.jbuilder b/app/views/platform_statistics/index.json.jbuilder new file mode 100644 index 00000000..226663d5 --- /dev/null +++ b/app/views/platform_statistics/index.json.jbuilder @@ -0,0 +1,5 @@ +json.visits @platform_statistic.visits +json.projects_count @project_statistic.common_projects_count + @project_statistic.mirror_projects_count + @project_statistic.sync_mirror_projects_count +json.users_count @platform_statistic.users_count +json.tasks_count @tasks_count +json.memos_count @memos_count \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 4219e110..9c1e2587 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -102,6 +102,7 @@ Rails.application.routes.draw do get :template_file end end + resources :platform_statistics, only: [:index] get 'home/index' get 'home/search' get 'main/first_stamp' diff --git a/db/migrate/20211202021833_create_platform_statistics.rb b/db/migrate/20211202021833_create_platform_statistics.rb new file mode 100644 index 00000000..4ce71800 --- /dev/null +++ b/db/migrate/20211202021833_create_platform_statistics.rb @@ -0,0 +1,10 @@ +class CreatePlatformStatistics < ActiveRecord::Migration[5.2] + def change + create_table :platform_statistics do |t| + t.integer :visits, default: 0 + t.integer :users_count, default: 0 + + t.timestamps + end + end +end diff --git a/spec/models/platform_statistic_spec.rb b/spec/models/platform_statistic_spec.rb new file mode 100644 index 00000000..93a5521a --- /dev/null +++ b/spec/models/platform_statistic_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe PlatformStatistic, type: :model do + pending "add some examples to (or delete) #{__FILE__}" +end