添加统计图标接口

This commit is contained in:
qyzh 2021-06-09 11:06:33 +08:00
parent 7776ab29ed
commit c9b15a02d6
6 changed files with 145 additions and 61 deletions

View File

@ -16,12 +16,12 @@ class SponsorshipsController < ApplicationController
def sponsored
if User.current.id == Integer(params[:id])
@sponsorships = Sponsorship.where("developer_id=?", params[:id])
@sponsorships = Sponsorship.where('developer_id=?', params[:id])
else
@sponsorships = Sponsorship.where("developer_id=? AND visible=1", params[:id])
@sponsorships = Sponsorship.where('developer_id=? AND visible=1', params[:id])
end
sort = params[:sort_by] || "created_at"
sort_direction = params[:sort_direction] || "desc"
sort = params[:sort_by] || 'created_at'
sort_direction = params[:sort_direction] || 'desc'
@sponsorships = @sponsorships.reorder("#{sort} #{sort_direction}")
@total = @sponsorships.length
@sponsorships = kaminari_paginate(@sponsorships)
@ -29,12 +29,12 @@ class SponsorshipsController < ApplicationController
def sponsoring
if User.current.id == Integer(params[:id])
@sponsorships = Sponsorship.where("sponsor_id=?", params[:id])
@sponsorships = Sponsorship.where('sponsor_id=?', params[:id])
else
@sponsorships = Sponsorship.where("sponsor_id=? AND visible=1", params[:id])
@sponsorships = Sponsorship.where('sponsor_id=? AND visible=1', params[:id])
end
sort = params[:sort_by] || "created_at"
sort_direction = params[:sort_direction] || "desc"
sort = params[:sort_by] || 'created_at'
sort_direction = params[:sort_direction] || 'desc'
@sponsorships = @sponsorships.reorder("#{sort} #{sort_direction}")
@total = @sponsorships.length
@sponsorships = kaminari_paginate(@sponsorships)
@ -42,12 +42,12 @@ class SponsorshipsController < ApplicationController
def stopped_sponsored
if User.current.id == Integer(params[:id])
@stopped_sponsorships = StoppedSponsorship.where("developer_id=?", params[:id])
@stopped_sponsorships = StoppedSponsorship.where('developer_id=?', params[:id])
else
@stopped_sponsorships = StoppedSponsorship.where("developer_id=? AND visible=1", params[:id])
@stopped_sponsorships = StoppedSponsorship.where('developer_id=? AND visible=1', params[:id])
end
sort = params[:sort_by] || "created_at"
sort_direction = params[:sort_direction] || "desc"
sort = params[:sort_by] || 'created_at'
sort_direction = params[:sort_direction] || 'desc'
@stopped_sponsorships = @stopped_sponsorships.reorder("#{sort} #{sort_direction}")
@total = @stopped_sponsorships.length
@stopped_sponsorships = kaminari_paginate(@stopped_sponsorships)
@ -55,12 +55,12 @@ class SponsorshipsController < ApplicationController
def stopped_sponsoring
if User.current.id == Integer(params[:id])
@stopped_sponsorships = StoppedSponsorship.where("sponsor_id=?", params[:id])
@stopped_sponsorships = StoppedSponsorship.where('sponsor_id=?', params[:id])
else
@stopped_sponsorships = StoppedSponsorship.where("sponsor_id=? AND visible=1", params[:id])
@stopped_sponsorships = StoppedSponsorship.where('sponsor_id=? AND visible=1', params[:id])
end
sort = params[:sort_by] || "created_at"
sort_direction = params[:sort_direction] || "desc"
sort = params[:sort_by] || 'created_at'
sort_direction = params[:sort_direction] || 'desc'
@stopped_sponsorships = @stopped_sponsorships.reorder("#{sort} #{sort_direction}")
@total = @stopped_sponsorships.length
@stopped_sponsorships = kaminari_paginate(@stopped_sponsorships)
@ -68,14 +68,13 @@ class SponsorshipsController < ApplicationController
# GET /sponsorships/1
# GET /sponsorships/1.json
def show
end
def show; end
# POST /sponsorships
# POST /sponsorships.json
def create
sponsor_id = User.current.id
check_sponsorship = Sponsorship.where("sponsor_id=? AND developer_id=?", sponsor_id, params[:developer_id])
check_sponsorship = Sponsorship.where('sponsor_id=? AND developer_id=?', sponsor_id, params[:developer_id])
@sponsorship = Sponsorship.new(sponsorship_params.merge({sponsor_id: sponsor_id}))
@ -144,20 +143,69 @@ class SponsorshipsController < ApplicationController
developer = @sponsorship.developer
sponsor = @sponsorship.sponsor
if (User.current.id == developer.id || User.current.id == sponsor.id) && developer.update(sponsored_num: developer.sponsored_num-1) && sponsor.update(sponsor_num: sponsor.sponsor_num-1) && @sponsorship.stop
render json: {status: 1, message: "终止成功"}
render json: {status: 1, message: '终止成功'}
else
render json: {status: -1, message: "失败"}
render json: {status: -1, message: '失败'}
end
end
def community_data
@sponsorships = Sponsorship.all
t1 = Time.now
t2 = Time.new(t1.year, t1.month, t1.day - 6)
@stopped_sponsorships = StoppedSponsorship.where('created_at >= ?', t2)
@community_data_array = to_array(@sponsorships, @stopped_sponsorships)
end
private
# Use callbacks to share common setup or constraints between actions.
def set_sponsorship
@sponsorship = Sponsorship.find(params[:id])
# Use callbacks to share common setup or constraints between actions.
def set_sponsorship
@sponsorship = Sponsorship.find(params[:id])
end
# Only allow a list of trusted parameters through.
def sponsorship_params
params.require(:sponsorship).permit(:amount, :visible, :sponsor_id, :developer_id, :single, :page, :limit, :sort_by, :search)
end
def to_array(sponsorships, stopped_sponsorships)
t1 = Time.now
start_time = Time.new(t1.year, t1.month, t1.day - 6)
sponsor = Array.new(7)
sponsored = Array.new(7)
date = Array.new(7)
# date[0] = Time.new(start_time.year, start_time.month, start_time.day)
index = 0
(0..6).each do |i|
# 更新日期date[i]表示第i天0点
date[i] = Time.new(start_time.year, start_time.month, start_time.day + i)
end_time = Time.new(start_time.year, start_time.month, start_time.day + i+1)
sponsor_set = Set.new
sponsored_set = Set.new
# 所有创建时间早于今天23:59的sponsorship
today_sponsor = sponsorships.where('created_at < ?', end_time)
# 所有结束时间晚于今天0:00的stopped_sponsorship
today_stopped_sponsor = stopped_sponsorships.where('created_at <= ?', date[i])
today_sponsor.each do |s|
sponsor_set.add s.sponsor_id
sponsored_set.add s.developer_id
end
today_stopped_sponsor.each do |s|
sponsor_set.add s.sponsor_id
sponsored_set.add s.developer_id
end
sponsor[i] = sponsor_set.size
sponsored[i] = sponsored_set.size
end
# Only allow a list of trusted parameters through.
def sponsorship_params
params.require(:sponsorship).permit(:amount, :visible, :sponsor_id, :developer_id, :single, :page, :limit, :sort_by, :search)
end
Array[sponsor, sponsored, date]
end
end

View File

@ -1,5 +1,5 @@
class WalletsController < ApplicationController
before_action :require_login, except: :balance_chart
before_action :require_login, except: :community_data
def balance
user = User.find_by_id(params[:id])
@ -32,16 +32,23 @@ class WalletsController < ApplicationController
@wallet = user.get_wallet
scope = CoinChange.where('to_wallet_id = ? OR from_wallet_id = ?', @wallet.id, @wallet.id)
t1 = Time.now
t2 = Time.new(t1.year, t1.month, t1.day-6)
t2 = Time.new(t1.year, t1.month, t1.day - 6)
@balance_chart_data = scope.where('created_at > ? AND created_at < ?', t2, t1)
@balance_chart_array = to_array(@balance_chart_data, @wallet.id)
end
def community_data
t1 = Time.now
t2 = Time.new(t1.year, t1.month, t1.day - 6)
coin_changes = CoinChange.where('created_at >= ?', t2)
@community_data_array = community_data_to_array(coin_changes)
end
private
def to_array(data, id)
t1 = Time.now
start_time = Time.new(t1.year, t1.month, t1.day-6)
end_time = Time.new(start_time.year, start_time.month, start_time.day+1)
start_time = Time.new(t1.year, t1.month, t1.day - 6)
end_time = Time.new(start_time.year, start_time.month, start_time.day + 1)
income = Array.new(7, 0) # 收入、支出
outcome = Array.new(7, 0)
@ -50,6 +57,7 @@ class WalletsController < ApplicationController
index = 0
data.each do |i|
# 更新日期
until (i.created_at >= start_time) && (i.created_at < end_time)
index += 1
start_time = end_time
@ -60,6 +68,7 @@ class WalletsController < ApplicationController
if i.from_wallet_id == id
outcome[index] += i.amount
else
next if params[:sponsor] == true && i.from_wallet_id.nil?
income[index] += i.amount
end
@ -74,4 +83,31 @@ class WalletsController < ApplicationController
Array[income, outcome, date]
end
def community_data_to_array(coin_changes)
t1 = Time.now
start_time = Time.new(t1.year, t1.month, t1.day - 6)
nums = Array.new(7, 0)
date = Array.new(7)
end_time = Array.new(7)
# date[0] = Time.new(start_time.year, start_time.month, start_time.day)
index = 0
(0..6).each do |i|
# 更新日期date[i]表示第i天0点
date[i] = Time.new(start_time.year, start_time.month, start_time.day + i)
end_time[i] = Time.new(start_time.year, start_time.month, start_time.day + i+1)
end
coin_changes.each do |cc|
(0..6).each do |i|
if !cc.from_wallet_id.nil? && cc.created_at>=date[i] && cc.created_at<end_time[i]
nums[i] += 1
end
end
end
Array[nums, date]
end
end

View File

@ -0,0 +1,12 @@
json.sponsor do
json.array! (0..6).each do |i|
json.y @community_data_array[0][i]
json.x @community_data_array[2][i].to_date
end
end
json.sponsored do
json.array! (0..6).each do |i|
json.y @community_data_array[1][i]
json.x @community_data_array[2][i].to_date
end
end

View File

@ -1,31 +1,12 @@
# json.income do
# json.array! (0..6).each do |i|
# json.amount @balance_chart_array[0][i]
# json.date @balance_chart_array[2][i]
# end
# end
# json.outcome do
# json.array! (0..6).each do |i|
# json.amount @balance_chart_array[1][i]
# json.date @balance_chart_array[2][i]
# end
# end
json.array! (0..1).each do |index|
if index == 0
json.label '收入'
json.data do
json.array! (0..6).each do |i|
json.primary @balance_chart_array[2][i]
json.secondary @balance_chart_array[0][i]
end
end
else
json.label '支出'
json.data do
json.array! (0..6).each do |i|
json.primary @balance_chart_array[2][i]
json.secondary @balance_chart_array[1][i]
end
end
json.income do
json.array! (0..6).each do |i|
json.y @balance_chart_array[0][i]
json.x @balance_chart_array[2][i].to_date
end
end
json.outcome do
json.array! (0..6).each do |i|
json.y @balance_chart_array[1][i]
json.x @balance_chart_array[2][i].to_date
end
end

View File

@ -0,0 +1,4 @@
json.array! (0..6).each do |i|
json.y @community_data_array[0][i]
json.x @community_data_array[1][i].to_date
end

View File

@ -28,6 +28,9 @@ Rails.application.routes.draw do
get 'wallets/coin_changes'
get 'wallets/balance_chart'
get 'sponsorships/community_data'
get 'wallets/community_data'
get 'log/list', to: 'log#list'
# post 'log/download', to: 'log#download'
match 'log/download/:filename' => 'log#download', :constraints => { filename: /[0-z\.]+/ }, via:[:get]