平台数据统计功能

1. 新增平台数据统计相关API
2.新增平台数据统计邮件消息系统
This commit is contained in:
jasder 2021-02-06 11:27:28 +08:00
commit 178991b245
20 changed files with 371 additions and 0 deletions

View File

@ -0,0 +1,25 @@
class StatisticController < ApplicationController
# 平台概况
def platform_profile
@platform_user_query = Statistic::PlatformUserQuery.new(params).call
@platform_project_query = Statistic::PlatformProjectQuery.new(params).call
@platform_course_query = Statistic::PlatformCourseQuery.new(params).call
end
# 平台代码提交数据
def platform_code
@platform_pull_request_query = Statistic::PlatformPullRequestQuery.new(params).call
@platform_commit_query = Statistic::PlatformCommitQuery.new(params,current_user).call
end
# 项目案例活跃度排行榜
def active_project_rank
@active_project_rank_query = Statistic::ActiveProjectRankQuery.new(params, current_user).call
end
# 开发者活跃度排行榜
def active_developer_rank
@active_developer_rank_query = Statistic::ActiveDeveloperRankQuery.new(params, current_user).call
end
end

View File

@ -0,0 +1,12 @@
module Trustie
class Database < ActiveRecord::Base
self.abstract_class = true
def self.set_connection
trustie_server_config = Rails.configuration.database_configuration[Rails.env]["trustie_web_server"]
raise 'trustie database config missing' if trustie_server_config.blank?
establish_connection trustie_server_config
end
end
end

View File

@ -0,0 +1,4 @@
class Trustie::Course < Trustie::Database
has_many :course_groups, class_name: "Trustie::CourseGroup"
has_many :homework_commons, class_name: "Trustie::HomeworkCommon"
end

View File

@ -0,0 +1,3 @@
class Trustie::CourseGroup < Trustie::Database
belongs_to :course, class_name: "Trustie::Course"
end

View File

@ -0,0 +1,3 @@
class Trustie::HomeworkCommon < Trustie::Database
belongs_to :course, class_name: "Trustie::Course"
end

View File

@ -0,0 +1,34 @@
class Statistic::ActiveDeveloperRankQuery < ApplicationQuery
attr_reader :params, :user
def initialize(params, user)
@params = params
@user = user
end
def call
begin
result = Gitea::Activity::DevelopService.call(start_time, end_time, top, user.gitea_token)
return result["develop"]
rescue
return []
end
end
private
def start_time
params.fetch(:start_time, Time.now.beginning_of_day.to_i).to_i
end
def end_time
params.fetch(:end_time, Time.now.to_i).to_i
end
def top
top = params.fetch(:top, 5).to_i
top = top >= 20 ? 20 : top
top = top <= 0 ? 5 : top
top
end
end

View File

@ -0,0 +1,34 @@
class Statistic::ActiveProjectRankQuery < ApplicationQuery
attr_reader :params, :user
def initialize(params, user)
@params = params
@user = user
end
def call
begin
result = Gitea::Activity::ProjectService.call(start_time, end_time, top, user.gitea_token)
return result["project"]
rescue
return []
end
end
private
def start_time
params.fetch(:start_time, Time.now.beginning_of_day.to_i).to_i
end
def end_time
params.fetch(:end_time, Time.now.to_i).to_i
end
def top
top = params.fetch(:top, 5).to_i
top = top >= 20 ? 20 : top
top = top <= 0 ? 5 : top
top
end
end

View File

@ -0,0 +1,28 @@
class Statistic::PlatformCommitQuery < ApplicationQuery
attr_reader :params, :user
def initialize(params, user)
@params = params
@user = user
end
def call
begin
result = Gitea::Activity::GetService.call(start_time, end_time, user.gitea_token)
result = result["commit"]
return [result["total_count"], result["active_count"]]
rescue
return [0, 0]
end
end
private
def start_time
params.fetch(:start_time, Time.now.beginning_of_day.to_i).to_i
end
def end_time
params.fetch(:end_time, Time.now.to_i).to_i
end
end

View File

@ -0,0 +1,29 @@
class Statistic::PlatformCourseQuery < ApplicationQuery
attr_reader :params
def initialize(params)
@params = params
end
def call
Trustie::Database.set_connection
course_total_count = Trustie::Course.count
course_active_count = Trustie::Course.joins(:course_groups)
.where("course_groups.created_at > ? and course_groups.created_at < ?", start_time, end_time).count
+
Trustie::Course.joins(:homework_commons)
.where("homework_commons.created_at > ? and homework_commons.created_at < ?", start_time, end_time).count
course_fresh_count = Trustie::Course.where("created_at > ? and created_at < ?", start_time, end_time).count
[course_total_count, course_active_count, course_fresh_count]
end
private
def start_time
Time.at(params.fetch(:start_time, Time.now.beginning_of_day.to_i).to_i)
end
def end_time
Time.at(params.fetch(:end_time, Time.now.to_i).to_i)
end
end

View File

@ -0,0 +1,24 @@
class Statistic::PlatformProjectQuery < ApplicationQuery
attr_reader :params
def initialize(params)
@params = params
end
def call
project_total_count = Project.count
project_active_count = Project.where("updated_on > ? and updated_on < ?", start_time, end_time).count
project_fresh_count = Project.where("created_on > ? and created_on < ?", start_time, end_time).count
[project_total_count, project_active_count, project_fresh_count]
end
private
def start_time
Time.at(params.fetch(:start_time, Time.now.beginning_of_day.to_i).to_i)
end
def end_time
Time.at(params.fetch(:end_time, Time.now.to_i).to_i)
end
end

View File

@ -0,0 +1,23 @@
class Statistic::PlatformPullRequestQuery < ApplicationQuery
attr_reader :params
def initialize(params)
@params = params
end
def call
pull_request_total_count = PullRequest.count
pull_request_fresh_count = PullRequest.where("created_at > ? and created_at < ?", start_time, end_time).count
[pull_request_total_count, pull_request_fresh_count]
end
private
def start_time
Time.at(params.fetch(:start_time, Time.now.beginning_of_day.to_i).to_i)
end
def end_time
Time.at(params.fetch(:end_time, Time.now.to_i).to_i)
end
end

View File

@ -0,0 +1,24 @@
class Statistic::PlatformUserQuery < ApplicationQuery
attr_reader :params
def initialize(params)
@params = params
end
def call
user_total_count = User.count
user_active_count = User.where("last_login_on > ? and last_login_on < ?", start_time, end_time).count
user_fresh_count = User.where("created_on > ? and created_on < ?", start_time, end_time).count
[user_total_count, user_active_count, user_fresh_count]
end
private
def start_time
Time.at(params.fetch(:start_time, Time.now.beginning_of_day.to_i).to_i)
end
def end_time
Time.at(params.fetch(:end_time, Time.now.to_i).to_i)
end
end

View File

@ -0,0 +1,24 @@
class Gitea::Activity::DevelopService < Gitea::ClientService
attr_reader :from, :to, :top, :token
def initialize(from, to, top, token)
@from = from
@to = to
@top = top
@token = token
end
def call
response = get(url, params)
render_200_response(response)
end
private
def params
Hash.new.merge(from: from, to: to, top: top, token: token)
end
def url
"/activity/develop".freeze
end
end

View File

@ -0,0 +1,23 @@
class Gitea::Activity::GetService < Gitea::ClientService
attr_reader :from, :to, :token
def initialize(from, to, token)
@from = from
@to = to
@token = token
end
def call
response = get(url, params)
render_200_response(response)
end
private
def params
Hash.new.merge(from: from, to: to, token: token)
end
def url
"/activity".freeze
end
end

View File

@ -0,0 +1,24 @@
class Gitea::Activity::ProjectService < Gitea::ClientService
attr_reader :from, :to, :top, :token
def initialize(from, to, top, token)
@from = from
@to = to
@top = top
@token = token
end
def call
response = get(url, params)
render_200_response(response)
end
private
def params
Hash.new.merge(from: from, to: to, top: top, token: token)
end
def url
"/activity/project".freeze
end
end

View File

@ -0,0 +1,13 @@
json.total_count @active_developer_rank_query.size
json.developers @active_developer_rank_query.each_with_index.to_a do |item, index|
user = User.find_by(login: item["develop_name"])
projects = user.projects
json.no index + 1
json.login item["develop_name"]
json.name user.full_name
json.develop_projects projects do |project|
json.(project, :name, :identifier, :description)
end
json.total_commit_count item["total_count"]
json.active_commit_count item["active_count"]
end

View File

@ -0,0 +1,10 @@
json.total_count @active_project_rank_query.size
json.projects @active_project_rank_query.each_with_index.to_a do |item, index|
project = Project.find_by(identifier: item["name"])
json.no index + 1
json.identifier item["name"]
json.name project.name
json.total_commit_count item["total_count"]
json.active_commit_count item["active_count"]
json.member_count project.members.size
end

View File

@ -0,0 +1,9 @@
json.commit do
json.total_count @platform_commit_query[0]
json.fresh_count @platform_commit_query[1]
end
json.pull_request do
json.total_count @platform_pull_request_query[0]
json.fresh_count @platform_pull_request_query[1]
end

View File

@ -0,0 +1,17 @@
json.user do
json.total_count @platform_user_query[0]
json.active_count @platform_user_query[1]
json.fresh_count @platform_user_query[2]
end
json.project do
json.total_count @platform_project_query[0]
json.active_count @platform_project_query[1]
json.fresh_count @platform_project_query[2]
end
json.course do
json.total_count @platform_course_query[0]
json.active_count @platform_course_query[1]
json.fresh_count @platform_course_query[2]
end

View File

@ -69,6 +69,14 @@ Rails.application.routes.draw do
# end
end
resources :statistic, only: [:index] do
collection do
get :platform_profile
get :platform_code
get :active_project_rank
get :active_developer_rank
end
end
resources :sync_forge, only: [:create] do
collection do
post :sync_users