Skip to content

Commit

Permalink
add dynamic datasets
Browse files Browse the repository at this point in the history
  • Loading branch information
kaiomagalhaes committed Apr 10, 2024
1 parent 5c072cb commit 03224ab
Show file tree
Hide file tree
Showing 18 changed files with 133,088 additions and 131,889 deletions.
20 changes: 19 additions & 1 deletion .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2024-04-10 16:30:19 UTC using RuboCop version 1.56.2.
# on 2024-04-10 18:27:42 UTC using RuboCop version 1.56.2.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
Expand All @@ -17,6 +17,12 @@ Lint/MissingSuper:
Metrics/AbcSize:
Max: 34

# Offense count: 1
# Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns.
# AllowedMethods: refine
Metrics/BlockLength:
Max: 26

# Offense count: 1
# Configuration parameters: AllowedMethods, AllowedPatterns.
Metrics/CyclomaticComplexity:
Expand All @@ -27,13 +33,25 @@ Metrics/CyclomaticComplexity:
Metrics/MethodLength:
Max: 23

# Offense count: 1
# Configuration parameters: Include.
# Include: app/controllers/**/*.rb, app/mailers/**/*.rb
Rails/LexicallyScopedActionFilter:
Exclude:
- 'app/controllers/dynamic_datasets_controller.rb'

# Offense count: 2
# Configuration parameters: Include.
# Include: db/**/*.rb
Rails/ReversibleMigration:
Exclude:
- 'db/migrate/20240410162146_remove_index_issue.rb'

# Offense count: 1
Security/Eval:
Exclude:
- 'app/models/dynamic_dataset.rb'

# Offense count: 4
# Configuration parameters: AllowedMethods.
# AllowedMethods: respond_to_missing?
Expand Down
16 changes: 16 additions & 0 deletions app/controllers/dynamic_datasets_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# frozen_string_literal: true

class DynamicDatasetsController < ApplicationController
before_action :set_dynamic_dataset, only: %i[show]
before_action :set_project, only: %i[index]

def index
@dynamic_datasets = @project.dynamic_datasets
end

private

def set_project
@project = Project.find(params[:project_id])
end
end
31 changes: 31 additions & 0 deletions app/models/dynamic_dataset.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# frozen_string_literal: true

# == Schema Information
#
# Table name: dynamic_datasets
#
# id :bigint not null, primary key
# code :string
# name :string
# created_at :datetime not null
# updated_at :datetime not null
# project_id :bigint not null
#
# Indexes
#
# index_dynamic_datasets_on_project_id (project_id)
#
# Foreign Keys
#
# fk_rails_... (project_id => projects.id)
#
class DynamicDataset < ApplicationRecord
belongs_to :project

def execute
# Use `eval` to execute the code. Be very cautious with this approach.
eval(code)
rescue StandardError => e
"Error executing code: #{e.message}"
end
end
1 change: 1 addition & 0 deletions app/models/project.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class Project < ApplicationRecord
belongs_to :customer
has_many :issues, dependent: :destroy
has_many :statement_of_works, dependent: :destroy
has_many :dynamic_datasets, dependent: :destroy

validates :name, presence: true, uniqueness: { case_sensitive: false }
validates :billable, inclusion: { in: [true, false] }
Expand Down
14 changes: 14 additions & 0 deletions app/utils/clients/tts/asana/parsers/taylor_summit_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ def initialize(json, project, asana_users)
@asana_users = asana_users
end

def valid?
true
end

def effort
json.dig('data', 'custom_fields').find { |field| field['name'] == 'Story Points' }['number_value']
end
Expand Down Expand Up @@ -41,6 +45,16 @@ def title; end
def issue_type; end

def issue_id; end

def reported_at; end

def tts_id; end

def parent_tts_id; end

def bug?
false
end
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion app/utils/clients/tts/azure/issue.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class Issue < Client
def list
issues_list = ::Request.post(url, customer_authorization, body)
urls = issues_list['workItems'].pluck('url')
work_items = urls.each_with_index.map do |url, i|
work_items = urls.map do |url|
url_expansion = "#{url}?api-version=6.0&$expand=relations"
::Request.get(url_expansion, customer_authorization)
end
Expand Down
5 changes: 5 additions & 0 deletions app/views/dynamic_datasets/_dynamic_dataset.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# frozen_string_literal: true

json.extract! dynamic_dataset, :id, :name, :project_id

json.data dynamic_dataset.execute
3 changes: 3 additions & 0 deletions app/views/dynamic_datasets/index.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# frozen_string_literal: true

json.array! @dynamic_datasets, partial: 'dynamic_datasets/dynamic_dataset', as: :dynamic_dataset
3 changes: 3 additions & 0 deletions app/views/dynamic_datasets/show.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# frozen_string_literal: true

json.partial! 'dynamic_datasets/dynamic_dataset', dynamic_dataset: @dynamic_dataset
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
require 'sidekiq/cron/web'

Rails.application.routes.draw do
resources :dynamic_datasets
resources :time_offs, only: [:create]
post 'time_off/destroy', to: 'time_offs#destroy'
resources :time_entries
Expand Down
13 changes: 13 additions & 0 deletions db/migrate/20240410173008_create_dynamic_datasets.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

class CreateDynamicDatasets < ActiveRecord::Migration[7.0]
def change
create_table :dynamic_datasets do |t|
t.string :name
t.string :code
t.references :project, null: false, foreign_key: true

t.timestamps
end
end
end
13 changes: 12 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 33 additions & 0 deletions spec/controllers/dynamic_datasets_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe DynamicDatasetsController, type: :controller do
include_context 'authentication'
render_views

let(:project) { create(:project) }
let(:dynamic_dataset) do
code = 'Customer.all.pluck(:id, :name)'
create(:dynamic_dataset, code:, project:)
end

before do
create_list(:customer, 5)
end

describe 'GET #index' do
it 'returns a success response' do
expected_result = [{
'id' => dynamic_dataset.id,
'name' => dynamic_dataset.name,
'project_id' => dynamic_dataset.project_id,
'data' => Customer.all.pluck(:id, :name)
}]

get :index, params: { project_id: project.id }

expect(response.parsed_body).to eq(expected_result)
end
end
end
28 changes: 28 additions & 0 deletions spec/factories/dynamic_datasets.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# frozen_string_literal: true

# == Schema Information
#
# Table name: dynamic_datasets
#
# id :bigint not null, primary key
# code :string
# name :string
# created_at :datetime not null
# updated_at :datetime not null
# project_id :bigint not null
#
# Indexes
#
# index_dynamic_datasets_on_project_id (project_id)
#
# Foreign Keys
#
# fk_rails_... (project_id => projects.id)
#
FactoryBot.define do
factory :dynamic_dataset do
name { 'MyString' }
code { 'MyString' }
project { FactoryBot.create(:project) }
end
end
264,754 changes: 132,870 additions & 131,884 deletions spec/fixtures/vcr_cassettes/clients_tts_azure_issue_list.yml

Large diffs are not rendered by default.

35 changes: 35 additions & 0 deletions spec/models/dynamic_dataset_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# frozen_string_literal: true

# == Schema Information
#
# Table name: dynamic_datasets
#
# id :bigint not null, primary key
# code :string
# name :string
# created_at :datetime not null
# updated_at :datetime not null
# project_id :bigint not null
#
# Indexes
#
# index_dynamic_datasets_on_project_id (project_id)
#
# Foreign Keys
#
# fk_rails_... (project_id => projects.id)
#
require 'rails_helper'

RSpec.describe DynamicDataset, type: :model do
context '#execute' do
it 'executes the code and returns the result' do
users = create_list(:user, 5)
expected_result = users.map { |user| [user.id, user.first_name] }
code = 'User.all.pluck(:id, :first_name)'
dynamic_dataset = create(:dynamic_dataset, code:)
result = dynamic_dataset.execute
expect(result).to eq(expected_result)
end
end
end
1 change: 1 addition & 0 deletions spec/models/project_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,6 @@
context 'associations' do
it { should have_many(:issues).dependent(:destroy) }
it { should have_many(:statement_of_works).dependent(:destroy) }
it { should have_many(:dynamic_datasets).dependent(:destroy) }
end
end
4 changes: 2 additions & 2 deletions spec/services/issues_creator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
user
expect do
IssuesCreator.call(project)
end.to change(Issue, :count).by(1203)
end.to change(Issue, :count).by(1206)
end
end

Expand Down Expand Up @@ -85,7 +85,7 @@
user
expect do
IssuesCreator.call(project)
end.to change(Issue, :count).by(43)
end.to change(Issue, :count).by(174)
end
end
end
Expand Down

0 comments on commit 03224ab

Please sign in to comment.