Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Data to csv #1900

Merged
merged 25 commits into from
Oct 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
73fd907
Data Export To CSV for user : backend apis
Oct 10, 2020
7b9d389
Merge branch 'main' of github.com:ifmeorg/ifme into main
Oct 10, 2020
e7b8fe5
rubocop
Oct 11, 2020
28d1c90
Merge branch 'main' of github.com:ifmeorg/ifme into data_to_csv
Oct 11, 2020
3c0195b
Delete reports.scss
akp2603 Oct 15, 2020
f88ab4a
changed variable name
Oct 15, 2020
68034c4
Merge branch 'data_to_csv' of github.com:ifmeorg/ifme into data_to_csv
Oct 15, 2020
aebe22b
request spec added
Oct 16, 2020
38cd98b
Delete delete_stale_data_worker_spec.rb
akp2603 Oct 17, 2020
e3546db
Delete process_data_request_worker_spec.rb
akp2603 Oct 17, 2020
12c1a64
Delete reports_helper_spec.rb
akp2603 Oct 17, 2020
1967ff8
data request model specs added
Oct 17, 2020
ac9fdea
Merge branch 'data_to_csv' of github.com:ifmeorg/ifme into data_to_csv
Oct 17, 2020
aa369ed
rubocop fixes
Oct 17, 2020
9b2fa1f
refactoring
Oct 17, 2020
47b0af0
refactoring
Oct 17, 2020
79085ff
Delete delete_stale_data_worker_spec.rb
akp2603 Oct 17, 2020
27a6b7d
Delete process_data_request_worker_spec.rb
akp2603 Oct 17, 2020
cbf22e1
added test.example.env
Oct 17, 2020
342e0d3
Merge branch 'data_to_csv' of github.com:ifmeorg/ifme into data_to_csv
Oct 17, 2020
9e04274
Setup Redis in Circle CI config
julianguyen Oct 17, 2020
b1502a6
Update config.yml
akp2603 Oct 17, 2020
ba3c682
review changes
Oct 18, 2020
31f18b2
Merge branch 'data_to_csv' of github.com:ifmeorg/ifme into data_to_csv
Oct 18, 2020
f92ba10
Merge branch 'main' of github.com:ifmeorg/ifme into data_to_csv
Oct 23, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ defaults: &defaults
POSTGRES_USER: circleci
POSTGRES_DB: ifme_test
POSTGRES_PASSWORD: ""
- image: redis
environment:
REDIS_HOST: redis
# The default Redis port
REDIS_PORT: 6379
steps:
- checkout
jobs:
Expand Down Expand Up @@ -202,4 +207,4 @@ workflows:
branches:
ignore: /.*/
tags:
only: /^v[0-9]+\.[0-9]+\.[0-9]+/
only: /^v[0-9]+\.[0-9]+\.[0-9]+/
5 changes: 5 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ gem 'selenium-webdriver', '~> 3.142.3'

gem 'rubyzip', '~> 1.3.0'

gem 'sidekiq', '5.0.5'
gem 'sidekiq-middleware'
gem 'sidekiq-failures'
gem "sidekiq-cron", "~> 1.1"

group :development, :test do
gem 'bundler-audit'
gem 'dotenv-rails', '~> 2.7.2'
Expand Down
16 changes: 16 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,18 @@ GEM
faraday (>= 0.7.6, < 1.0)
shoulda-matchers (4.4.1)
activesupport (>= 4.2.0)
sidekiq (5.0.5)
concurrent-ruby (~> 1.0)
connection_pool (~> 2.2, >= 2.2.0)
rack-protection (>= 1.5.0)
redis (>= 3.3.4, < 5)
sidekiq-cron (1.2.0)
fugit (~> 1.1)
sidekiq (>= 4.2.1)
sidekiq-failures (1.0.0)
sidekiq (>= 4.0.0)
sidekiq-middleware (0.3.0)
sidekiq (>= 2.12.4)
signet (0.14.0)
addressable (~> 2.3)
faraday (>= 0.17.3, < 2.0)
Expand Down Expand Up @@ -583,6 +595,10 @@ DEPENDENCIES
selenium-webdriver (~> 3.142.3)
sentry-raven
shoulda-matchers
sidekiq (= 5.0.5)
sidekiq-cron (~> 1.1)
sidekiq-failures
sidekiq-middleware
simplecov (= 0.16.1)
spring
turbolinks (~> 5.2.0)
Expand Down
28 changes: 28 additions & 0 deletions app/controllers/users/reports_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# frozen_string_literal: true
module Users
class ReportsController < ApplicationController
before_action :authenticate_user!
include Users::ReportsHelper

def submit_request
status, response = submit_request_helper(current_user)
render json: response, status: status
end

def fetch_request_status
status, response = fetch_request_status_helper(current_user,
params[:request_id])
render json: response, status: status
end

def download_data
status, response = download_data_helper(current_user,
params[:request_id])
if status != 200
render json: response, status: status
else
send_file(response, status: 200)
end
end
end
end
35 changes: 35 additions & 0 deletions app/helpers/users/reports_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# frozen_string_literal: true
module Users
module ReportsHelper
def submit_request_helper(user)
[200, { request_id: user.generate_data_request }]
rescue StandardError => e
[422, { error: e.message }]
end

def fetch_request_status_helper(user, request_id)
return 400, { error: "Request id can't be blank." } if request_id.blank?

data_request = user.data_requests.find_by(request_id: request_id)
if data_request.blank?
return 404, { error: 'No such request exists for current user.' }
end

[200, { current_status: data_request.status_id }]
end

def download_data_helper(user, request_id)
return 400, { error: "Request id can't be blank." } if request_id.blank?

data_request = user.data_requests.find_by(
request_id: request_id,
status_id: Users::DataRequest::STATUS[:success]
)
if data_request.blank? || !File.exist?(data_request.file_path.to_s)
return 404, { error: 'Requested csv not found.' }
end

[200, data_request.file_path]
end
end
end
8 changes: 8 additions & 0 deletions app/models/allyship.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@
#

class Allyship < ApplicationRecord
USER_DATA_ATTRIBUTES = %w[
id
created_at
updated_at
ally_id
status
].map!(&:freeze).freeze

enum status: { accepted: 0, pending_from_user: 1, pending_from_ally: 2 }

validate :different_users
Expand Down
17 changes: 17 additions & 0 deletions app/models/application_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,21 @@

class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true

def self.build_csv_rows(objects)
return [] if objects.blank?

klass = objects.klass
data = [["#{klass.name.underscore}_info"]]
attributes = if klass.const_defined?('USER_DATA_ATTRIBUTES')
klass.const_get('USER_DATA_ATTRIBUTES')
else
klass.column_names
end
data << attributes
objects.each do |object|
data << attributes.map { |attribute| object.send(attribute.to_sym) }
end
data
end
end
8 changes: 8 additions & 0 deletions app/models/care_plan_contact.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@
#

class CarePlanContact < ApplicationRecord
USER_DATA_ATTRIBUTES = %w[
id
name
phone
created_at
updated_at
].map!(&:freeze).freeze

validates :user_id, :name, presence: true
belongs_to :user
end
10 changes: 10 additions & 0 deletions app/models/category.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,14 @@ class Category < ApplicationRecord
has_many :moments_categories, dependent: :destroy
has_many :strategies_categories, dependent: :destroy
validates :visible, inclusion: [true, false]

USER_DATA_ATTRIBUTES = %w[
id
name
description
created_at
updated_at
slug
visible
].map!(&:freeze).freeze
end
9 changes: 9 additions & 0 deletions app/models/group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ class Group < ApplicationRecord
through: :group_members, source: :user
after_destroy :destroy_notifications

USER_DATA_ATTRIBUTES = %w[
id
name
created_at
updated_at
description
slug
].map!(&:freeze).freeze

def led_by?(user)
leaders.include? user
end
Expand Down
8 changes: 8 additions & 0 deletions app/models/group_member.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@
#

class GroupMember < ApplicationRecord
USER_DATA_ATTRIBUTES = %w[
id
group_id
leader
created_at
updated_at
].map!(&:freeze).freeze

after_destroy :destroy_meeting_memberships

validates :group_id, :user_id, presence: true
Expand Down
25 changes: 23 additions & 2 deletions app/models/medication.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,35 @@
# comments :text
# slug :string
# add_to_google_cal :boolean default(FALSE)
# weekly_dosage :integer
# default(["0", "1", "2", "3", "4", "5", "6"]), is an Array
# rubocop:disable Layout/LineLength
# weekly_dosage :integer default(["0", "1", "2", "3", "4", "5", "6"]), is an Array
# rubocop:enable Layout/LineLength
#

class Medication < ApplicationRecord
# dosage: amount of medication taken at one time
# total: total quantity of medication
# strength: strength of medication

USER_DATA_ATTRIBUTES = %w[
id
name
dosage
refill
created_at
updated_at
user_id
total
strength
strength_unit
dosage_unit
total_unit
comments
slug
add_to_google_cal
weekly_dosage
].map!(&:freeze).freeze

extend FriendlyId
friendly_id :name
belongs_to :user, foreign_key: :user_id
Expand Down
9 changes: 9 additions & 0 deletions app/models/meeting_member.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@
#

class MeetingMember < ApplicationRecord
USER_DATA_ATTRIBUTES = %w[
id
meeting_id
leader
created_at
updated_at
google_cal_event_id
].map!(&:freeze).freeze

validates :meeting_id, :user_id, presence: true
validates :leader, inclusion: [true, false]

Expand Down
17 changes: 17 additions & 0 deletions app/models/moment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,23 @@ class Moment < ApplicationRecord
include CommonMethods
extend FriendlyId

USER_DATA_ATTRIBUTES = %w[
id
name
why
fix
created_at
updated_at
viewers
comment
slug
secret_share_identifier
secret_share_expires_at
published_at
bookmarked
resource_recommendations
].map!(&:freeze).freeze

friendly_id :name
serialize :viewers, Array

Expand Down
10 changes: 10 additions & 0 deletions app/models/mood.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@
#

class Mood < ApplicationRecord
USER_DATA_ATTRIBUTES = %w[
id
name
description
created_at
updated_at
slug
visible
].map!(&:freeze).freeze

extend FriendlyId
friendly_id :name
validates :user_id, :name, presence: true
Expand Down
8 changes: 8 additions & 0 deletions app/models/notification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@
#

class Notification < ApplicationRecord
USER_DATA_ATTRIBUTES = %w[
id
uniqueid
data
created_at
updated_at
].map!(&:freeze).freeze

validates :user_id, :uniqueid, :data, presence: true
belongs_to :user, foreign_key: :user_id

Expand Down
14 changes: 14 additions & 0 deletions app/models/strategy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,20 @@ class Strategy < ApplicationRecord
include CommonMethods
extend FriendlyId

USER_DATA_ATTRIBUTES = %w[
id
description
viewers
comment
created_at
updated_at
name
slug
published_at
visible
bookmarked
].map!(&:freeze).freeze

friendly_id :name
serialize :viewers, Array

Expand Down
Loading