add: platform statistic

This commit is contained in:
viletyy 2021-12-02 14:39:53 +08:00
parent ef6f2c7b42
commit b81d92af46
9 changed files with 68 additions and 0 deletions

View File

@ -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])

View File

@ -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

View File

@ -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)

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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'

View File

@ -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

View File

@ -0,0 +1,5 @@
require 'rails_helper'
RSpec.describe PlatformStatistic, type: :model do
pending "add some examples to (or delete) #{__FILE__}"
end