Skip to content

Commit

Permalink
Central project endpoint in project analysis
Browse files Browse the repository at this point in the history
  • Loading branch information
rodrigovirgilio committed Jan 11, 2017
1 parent 72a1401 commit 1291f57
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 7 deletions.
6 changes: 6 additions & 0 deletions app/api/entities/project.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
class Entities::Project < Entities::BaseEntity
expose :id
expose :name
expose :slug
expose :point_scale
Expand All @@ -7,6 +8,7 @@ class Entities::Project < Entities::BaseEntity
expose :default_velocity
expose :velocity, if: { type: :full }
expose :volatility, if: { type: :full }
expose :teams

with_options(format_with: :iso_timestamp) do
expose :created_at, if: lambda { |i, o| i.created_at.present? }
Expand All @@ -28,4 +30,8 @@ def iteration_service
object.iteration_service(since: options[:since].to_i.months.ago)
end
end

def teams
object.teams
end
end
16 changes: 16 additions & 0 deletions app/api/entities/project_analysis.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,22 @@ class Entities::ProjectAnalysis < Entities::BaseEntity
expose :velocity
expose :volatility
expose :current_iteration_number
expose :date_for_iteration
expose :backlog
expose :backlog_iterations
expose :current_iteration_details
expose :backlog_date
expose :worst_backlog_date

private

def date_for_iteration
last_iteration_number = object.current_iteration_number + 1

object.date_for_iteration_number(last_iteration_number)
end

def worst_backlog_date
object.backlog_date(true)
end
end
7 changes: 7 additions & 0 deletions app/api/entities/team.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,16 @@ class Entities::Team < Entities::BaseEntity
expose :name
expose :logo
expose :disable_registration
expose :admins

with_options(format_with: :iso_timestamp) do
expose :created_at, if: lambda { |i, o| i.created_at.present? }
expose :archived_at, if: lambda { |i, o| i.archived_at.present? }
end

private

def admins
object.enrollments.includes(:user).where(is_admin: true).map(&:user) rescue []
end
end
1 change: 1 addition & 0 deletions app/api/entities/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ class Entities::User < Entities::BaseEntity
expose :initials
expose :email
expose :sign_in_count
expose :role

with_options(format_with: :iso_timestamp) do
expose :created_at, if: lambda { |i, o| i.created_at.present? }
Expand Down
8 changes: 6 additions & 2 deletions app/api/v1/projects.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,13 @@ def authorize_project!
end

desc 'Return all projects', { tags: ['project'] }
params do
optional :archiveds, type: Boolean, default: false
end
paginate
get '/' do
projects = Project.all
projects = projects.not_archived unless params[:archiveds]
projects = projects.where(slug: @allowed_projects) if @allowed_projects

present paginate(projects), with: Entities::Project
Expand All @@ -38,11 +42,11 @@ def authorize_project!
optional :current_time, type: DateTime
end
get '/:slug/analysis' do
project = Project.find_by_slug(params[:slug])
project = Project.not_archived.find_by_slug(params[:slug])
current_time = params[:current_time] || Time.current
since = params[:since].months.ago

iteration = project.iteration_service(since: since, current_time: current_time)
iteration = project.iteration_service(since: since, current_time: current_time) if project

present iteration, with: Entities::ProjectAnalysis
end
Expand Down
16 changes: 15 additions & 1 deletion spec/api/entities/project_analysis_spec.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
require 'rails_helper'

RSpec.describe Entities::ProjectAnalysis do
let(:date) { Time.current }

let(:iteration) do
double(
:iteration,
velocity: 10,
volatility: 0,
current_iteration_number: 32,
date_for_iteration: date,
date_for_iteration_number: date,
backlog: [1, 2, 3],
backlog_iterations: [3, 2, 1]
backlog_iterations: [3, 2, 1],
current_iteration_details: {
"started": 8,
"finished": 5
},
backlog_date: [59, date],
worst_backlog_date: [59, date]
)
end

Expand All @@ -17,6 +27,10 @@
it { expect(subject[:velocity]).to eq(10) }
it { expect(subject[:volatility]).to eq(0) }
it { expect(subject[:current_iteration_number]).to eq(32) }
it { expect(subject[:date_for_iteration]).to eq(date) }
it { expect(subject[:backlog]).to eq([1, 2, 3]) }
it { expect(subject[:backlog_iterations]).to eq([3, 2, 1]) }
it { expect(subject[:current_iteration_details]).to eq({ "started": 8, "finished": 5 }) }
it { expect(subject[:backlog_date]).to eq([59, date]) }
it { expect(subject[:worst_backlog_date]).to eq([59, date]) }
end
29 changes: 25 additions & 4 deletions spec/api/v1/projects_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,18 +75,41 @@

describe '#GET /api/v1/projects/{slug}/analysis' do
let(:project) { create :project }
let(:date) { Time.current }

let(:iteration) do
double(
:iteration,
velocity: 10,
volatility: 0,
current_iteration_number: 32,
date_for_iteration: date.to_s,
date_for_iteration_number: date,
backlog: [1, 2, 3],
backlog_iterations: [3, 2, 1]
backlog_iterations: [3, 2, 1],
current_iteration_details: {
"started": 8,
"finished": 5
},
backlog_date: [59, date],
worst_backlog_date: [59, date]
)
end

let(:expected) do
{
"velocity" => 10,
"volatility" => 0,
"current_iteration_number" => 32,
"date_for_iteration" => "2016/12/07 08:00:00 -0200",
"backlog" => [1, 2, 3],
"backlog_iterations" => [3, 2, 1],
"current_iteration_details" => {"started" => 8, "finished" => 5},
"backlog_date" => [59, "2016/12/07 08:00:00 -0200"],
"worst_backlog_date" => [59, "2016/12/07 08:00:00 -0200"]
}
end

before(:each) do
allow_any_instance_of(Project).to receive(:iteration_service)
.and_return(iteration)
Expand All @@ -95,9 +118,7 @@
end

it 'returns the project with analysis' do
expected = Entities::ProjectAnalysis.new(iteration).as_json

expect(JSON.parse(response.body).symbolize_keys).to eq(expected)
expect(JSON.parse(response.body)).to eq(expected)
end

context 'when api token is invalid' do
Expand Down

0 comments on commit 1291f57

Please sign in to comment.