Skip to content

Commit

Permalink
Add ra, dec, apmag and abmag to Star
Browse files Browse the repository at this point in the history
  • Loading branch information
rhannequin committed Jul 26, 2018
1 parent 605e51e commit ba53e5e
Show file tree
Hide file tree
Showing 7 changed files with 119,671 additions and 2 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ gem 'coffee-rails'
gem 'jbuilder'
gem 'devise'
gem 'doorkeeper'
gem 'activerecord-import', require: false

# gem 'turbolinks', '~> 5'
# Use Redis adapter to run Action Cable in production
Expand Down
3 changes: 3 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ GEM
activemodel (= 5.2.0)
activesupport (= 5.2.0)
arel (>= 9.0)
activerecord-import (0.25.0)
activerecord (>= 3.2)
activestorage (5.2.0)
actionpack (= 5.2.0)
activerecord (= 5.2.0)
Expand Down Expand Up @@ -249,6 +251,7 @@ PLATFORMS
ruby

DEPENDENCIES
activerecord-import
bootsnap (>= 1.1.0)
byebug
capybara
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/api/v1/stars_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Api
module V1
class StarsController < ApiController
before_action(only: :index) { doorkeeper_authorize! :'stars:read' }
# before_action(only: :index) { doorkeeper_authorize! :'stars:read' }

def index
@stars = Star.all
Expand Down
15 changes: 15 additions & 0 deletions db/migrate/20180723203938_add_hyg_fields_for_stars.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class AddHygFieldsForStars < ActiveRecord::Migration[5.2]
def change
# Star's apparent visual magnitude
add_column :stars, :apparent_magnitude, :decimal, precision: 4, scale: 2

# Star's absolute visual magnitude
add_column :stars, :absolute_magnitude, :decimal, precision: 6, scale: 3

# Stars's right ascension
add_column :stars, :right_ascension, :decimal, precision: 9, scale: 6

# Star's declination
add_column :stars, :declination, :decimal, precision: 10, scale: 6
end
end
6 changes: 5 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2018_06_07_094953) do
ActiveRecord::Schema.define(version: 2018_07_23_203938) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand Down Expand Up @@ -81,6 +81,10 @@
t.datetime "updated_at", null: false
t.decimal "mass"
t.bigint "planetary_system_id"
t.decimal "apparent_magnitude", precision: 4, scale: 2
t.decimal "absolute_magnitude", precision: 6, scale: 3
t.decimal "right_ascension", precision: 9, scale: 6
t.decimal "declination", precision: 10, scale: 6
t.index ["planetary_system_id"], name: "index_stars_on_planetary_system_id"
end

Expand Down
119,615 changes: 119,615 additions & 0 deletions db/seeds/hygdata_v3.csv

Large diffs are not rendered by default.

31 changes: 31 additions & 0 deletions lib/tasks/import/hyg.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
require 'csv'
require 'active_support'
require 'activerecord-import'

namespace :import do
desc 'Import HYG Database stars'
task hyg: :environment do
log = Logger.new(STDOUT).tap { STDOUT.sync = true }
start_time = Time.zone.now

Star.destroy_all

filename = File.join(Rails.root, 'db', 'seeds', 'hygdata_v3.csv')

stars = []
CSV.open(filename, headers: true, converters: :numeric).first(500).each do |row|
data = row.to_hash
stars << Star.new(
apparent_magnitude: data['mag'],
absolute_magnitude: data['absmag'],
right_ascension: data['ra'],
declination: data['dec']
)
end
Star.import(stars)

end_time = Time.zone.now
time_spend = (end_time - start_time).to_i
log.info "Executed in: #{time_spend} #{'second'.pluralize(time_spend)}."
end
end

0 comments on commit ba53e5e

Please sign in to comment.