Skip to content

Commit

Permalink
[api][ci] Make statistics mixin a class
Browse files Browse the repository at this point in the history
This is not used as a mixin and can be handled as a simple lib.
  • Loading branch information
bgeuken committed Oct 5, 2017
1 parent ae463e2 commit b1c1acf
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 17 deletions.
5 changes: 2 additions & 3 deletions src/api/app/controllers/statistics_controller.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
require 'rexml/document'
require "rexml/streamlistener"
require 'statistics_calculations'

class StatisticsController < ApplicationController
include StatisticsCalculations

validate_action redirect_stats: {method: :get, response: :redirect_stats}

before_action :get_limit, only: [
Expand Down Expand Up @@ -164,7 +163,7 @@ def latest_updated
@limit = nil if params[:limit].nil?
end

@list = get_latest_updated(@limit, @timelimit, prj_filter, pkg_filter)
@list = StatisticsCalculations.get_latest_updated(@limit, @timelimit, prj_filter, pkg_filter)
end

def updated_timestamp
Expand Down
6 changes: 3 additions & 3 deletions src/api/app/controllers/webui/feeds_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class Webui::FeedsController < Webui::WebuiController
include StatisticsCalculations
require 'statistics_calculations'

class Webui::FeedsController < Webui::WebuiController
layout false
before_action :set_project, only: ['commits']

Expand All @@ -11,7 +11,7 @@ def news

def latest_updates
raise ActionController::RoutingError, 'expected application/rss' unless request.format == Mime[:rss]
@latest_updates = get_latest_updated(10)
@latest_updates = StatisticsCalculations.get_latest_updated(10)
end

def commits
Expand Down
6 changes: 3 additions & 3 deletions src/api/app/controllers/webui/main_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class Webui::MainController < Webui::WebuiController
include StatisticsCalculations
require 'statistics_calculations'

class Webui::MainController < Webui::WebuiController
# permissions.status_message_create
before_action :require_admin, only: [:delete_message, :add_news]
skip_before_action :check_anonymous, only: [:index]
Expand All @@ -27,7 +27,7 @@ def index
@workerstatus = Rails.cache.fetch('workerstatus_hash', expires_in: 10.minutes) do
WorkerStatus.hidden.to_hash
end
@latest_updates = get_latest_updated(6)
@latest_updates = StatisticsCalculations.get_latest_updated(6)
@waiting_packages = 0
@building_workers = @workerstatus.elements('building').length
@overall_workers = @workerstatus['clients']
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module StatisticsCalculations
def get_latest_updated(limit = 10, timelimit = Time.at(0), prj_filter = ".*", pkg_filter = ".*")
class StatisticsCalculations
def self.get_latest_updated(limit = 10, timelimit = Time.at(0), prj_filter = ".*", pkg_filter = ".*")
packages = Package.includes(:project).where(updated_at: timelimit..Time.now).
where('packages.name REGEXP ? AND projects.name REGEXP ?', pkg_filter, prj_filter).
references(:project).order("updated_at DESC").limit(limit).
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
require "webmock/rspec"
require 'rails_helper'
require 'statistics_calculations'

RSpec.describe StatisticsCalculations do
describe 'get_latest_updated' do
let(:object) { Object.new.extend(StatisticsCalculations) }
let(:package_1) { create(:package) }
let(:package_2) { create(:package, name: 'my_package') }
let(:package_3) { create(:package) }
Expand All @@ -26,7 +26,7 @@ def package_result_for(package)
end

context 'when called with no parameters' do
subject { object.get_latest_updated }
subject { StatisticsCalculations.get_latest_updated }

it 'returns all projects available' do
expect(subject[1]).to match_array(project_result_for(package_1.project))
Expand All @@ -43,7 +43,7 @@ def package_result_for(package)
end

context 'when limit n is given' do
subject { object.get_latest_updated(3) }
subject { StatisticsCalculations.get_latest_updated(3) }

it 'returns the newest n packages and projects' do
expect(subject[0]).to match_array(package_result_for(package_1))
Expand All @@ -53,7 +53,7 @@ def package_result_for(package)
end

context 'when timelimit is given' do
subject { object.get_latest_updated(10, 65.seconds.ago) }
subject { StatisticsCalculations.get_latest_updated(10, 65.seconds.ago) }

it { expect(subject.length).to eq(5) }

Expand All @@ -67,7 +67,7 @@ def package_result_for(package)
end

context 'when project filter is given' do
subject { object.get_latest_updated(10, 125.seconds.ago, 'my_proj') }
subject { StatisticsCalculations.get_latest_updated(10, 125.seconds.ago, 'my_proj') }

before do
# We need to be able to identify the project
Expand All @@ -86,7 +86,7 @@ def package_result_for(package)
end

context 'when package filter is given' do
subject { object.get_latest_updated(10, 5.minutes.ago, '.*', 'my_pack') }
subject { StatisticsCalculations.get_latest_updated(10, 5.minutes.ago, '.*', 'my_pack') }

it { expect(subject.length).to eq(5) }

Expand Down

0 comments on commit b1c1acf

Please sign in to comment.