diff --git a/app/controllers/statistic_controller.rb b/app/controllers/statistic_controller.rb new file mode 100644 index 000000000..6eaa5bc99 --- /dev/null +++ b/app/controllers/statistic_controller.rb @@ -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 \ No newline at end of file diff --git a/app/libs/trustie/database.rb b/app/libs/trustie/database.rb new file mode 100644 index 000000000..10db7ee5f --- /dev/null +++ b/app/libs/trustie/database.rb @@ -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 \ No newline at end of file diff --git a/app/models/trustie/course.rb b/app/models/trustie/course.rb new file mode 100644 index 000000000..6cf6411e1 --- /dev/null +++ b/app/models/trustie/course.rb @@ -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 \ No newline at end of file diff --git a/app/models/trustie/course_group.rb b/app/models/trustie/course_group.rb new file mode 100644 index 000000000..d9fee73b3 --- /dev/null +++ b/app/models/trustie/course_group.rb @@ -0,0 +1,3 @@ +class Trustie::CourseGroup < Trustie::Database + belongs_to :course, class_name: "Trustie::Course" +end \ No newline at end of file diff --git a/app/models/trustie/homework_common.rb b/app/models/trustie/homework_common.rb new file mode 100644 index 000000000..70dd31e6a --- /dev/null +++ b/app/models/trustie/homework_common.rb @@ -0,0 +1,3 @@ +class Trustie::HomeworkCommon < Trustie::Database + belongs_to :course, class_name: "Trustie::Course" +end \ No newline at end of file diff --git a/app/queries/statistic/active_developer_rank_query.rb b/app/queries/statistic/active_developer_rank_query.rb new file mode 100644 index 000000000..9c0af3d8b --- /dev/null +++ b/app/queries/statistic/active_developer_rank_query.rb @@ -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 \ No newline at end of file diff --git a/app/queries/statistic/active_project_rank_query.rb b/app/queries/statistic/active_project_rank_query.rb new file mode 100644 index 000000000..2ce0d8195 --- /dev/null +++ b/app/queries/statistic/active_project_rank_query.rb @@ -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 \ No newline at end of file diff --git a/app/queries/statistic/platform_commit_query.rb b/app/queries/statistic/platform_commit_query.rb new file mode 100644 index 000000000..db4ee8337 --- /dev/null +++ b/app/queries/statistic/platform_commit_query.rb @@ -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 \ No newline at end of file diff --git a/app/queries/statistic/platform_course_query.rb b/app/queries/statistic/platform_course_query.rb new file mode 100644 index 000000000..6b6ec8603 --- /dev/null +++ b/app/queries/statistic/platform_course_query.rb @@ -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 \ No newline at end of file diff --git a/app/queries/statistic/platform_project_query.rb b/app/queries/statistic/platform_project_query.rb new file mode 100644 index 000000000..711f508a4 --- /dev/null +++ b/app/queries/statistic/platform_project_query.rb @@ -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 \ No newline at end of file diff --git a/app/queries/statistic/platform_pull_request_query.rb b/app/queries/statistic/platform_pull_request_query.rb new file mode 100644 index 000000000..d4a5f72b5 --- /dev/null +++ b/app/queries/statistic/platform_pull_request_query.rb @@ -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 \ No newline at end of file diff --git a/app/queries/statistic/platform_user_query.rb b/app/queries/statistic/platform_user_query.rb new file mode 100644 index 000000000..67dacee8a --- /dev/null +++ b/app/queries/statistic/platform_user_query.rb @@ -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 \ No newline at end of file diff --git a/app/services/gitea/activity/develop_service.rb b/app/services/gitea/activity/develop_service.rb new file mode 100644 index 000000000..9ec419fa6 --- /dev/null +++ b/app/services/gitea/activity/develop_service.rb @@ -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 \ No newline at end of file diff --git a/app/services/gitea/activity/get_service.rb b/app/services/gitea/activity/get_service.rb new file mode 100644 index 000000000..48a7e56e4 --- /dev/null +++ b/app/services/gitea/activity/get_service.rb @@ -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 \ No newline at end of file diff --git a/app/services/gitea/activity/project_service.rb b/app/services/gitea/activity/project_service.rb new file mode 100644 index 000000000..43783b68f --- /dev/null +++ b/app/services/gitea/activity/project_service.rb @@ -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 \ No newline at end of file diff --git a/app/views/statistic/active_developer_rank.json.jbuilder b/app/views/statistic/active_developer_rank.json.jbuilder new file mode 100644 index 000000000..5ff1b00db --- /dev/null +++ b/app/views/statistic/active_developer_rank.json.jbuilder @@ -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 diff --git a/app/views/statistic/active_project_rank.json.jbuilder b/app/views/statistic/active_project_rank.json.jbuilder new file mode 100644 index 000000000..9c5d0cdf4 --- /dev/null +++ b/app/views/statistic/active_project_rank.json.jbuilder @@ -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 \ No newline at end of file diff --git a/app/views/statistic/platform_code.json.jbuilder b/app/views/statistic/platform_code.json.jbuilder new file mode 100644 index 000000000..10ca0a018 --- /dev/null +++ b/app/views/statistic/platform_code.json.jbuilder @@ -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 \ No newline at end of file diff --git a/app/views/statistic/platform_profile.json.jbuilder b/app/views/statistic/platform_profile.json.jbuilder new file mode 100644 index 000000000..5f8972bd9 --- /dev/null +++ b/app/views/statistic/platform_profile.json.jbuilder @@ -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 \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 067adcce0..041b84454 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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