Skip to content

Commit

Permalink
Fix so NSR import works again
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielJackson-Oslo committed Mar 1, 2024
1 parent 8726acb commit 155592b
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 42 deletions.
17 changes: 13 additions & 4 deletions app/models/space.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,26 @@ class Space < ApplicationRecord # rubocop:disable Metrics/ClassLength
has_paper_trail skip: [:star_rating]

has_many :images, dependent: :destroy
accepts_nested_attributes_for :images

has_many :facility_reviews, dependent: :destroy
accepts_nested_attributes_for :facility_reviews

has_many :space_facilities, dependent: :destroy
accepts_nested_attributes_for :space_facilities

has_many :reviews, dependent: :destroy
accepts_nested_attributes_for :reviews

has_many :space_contacts, dependent: :destroy
accepts_nested_attributes_for :space_contacts

belongs_to :space_group, optional: true
accepts_nested_attributes_for :space_group
accepts_nested_attributes_for :facility_reviews
accepts_nested_attributes_for :space_facilities

has_many :space_types_relations, dependent: :destroy
has_many :space_types, through: :space_types_relations, dependent: :destroy
accepts_nested_attributes_for :space_types

scope :filter_on_title, ->(title) { where("title ILIKE ?", "%#{title}%") }
scope :filter_on_space_types, ->(space_type_ids) { joins(:space_types).where(space_types: space_type_ids).distinct }
Expand All @@ -33,8 +44,6 @@ class Space < ApplicationRecord # rubocop:disable Metrics/ClassLength
)
}

has_many :space_types_relations, dependent: :destroy
has_many :space_types, through: :space_types_relations, dependent: :destroy

has_rich_text :how_to_book
has_rich_text :who_can_use
Expand Down
20 changes: 16 additions & 4 deletions db/seeds/imports/nsr/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ def validate_address(school)

def space_from(school)
space_group = SpaceGroup.find_by space_group_from(school)
space_type = SpaceType.find_by(space_types_from(school))

address = validate_address(school)
return unless address
Expand All @@ -88,12 +87,11 @@ def space_from(school)
lng: position[:lng],
municipality_code: position[:municipality_code],
organization_number: get_meta(school, "organizationalNumber"),
space_group: space_group,
space_type: space_type
space_group: space_group
}
end

def space_types_from(school)
def space_type_from(school)
# TODO: Make it possible to have several space types.
{ type_name: get_meta(school, "types")[0] }
end
Expand Down Expand Up @@ -158,4 +156,18 @@ def new_all_unless_exists(model, items)
new_items.map { |item| model.new item }
end

def count_stats(info:)
p({
info:,
spaces: Space.count,
space_types: SpaceType.count,
space_groups: SpaceGroup.count,
space_contacts: SpaceContact.count,
space_type_relations: SpaceTypesRelation.count,
facilities: Facility.count,
space_type_facility_relations: SpaceTypesFacility.count,
space_facilities: SpaceFacility.count
})
end

# rubocop:enable all
139 changes: 105 additions & 34 deletions db/seeds/imports/nsr/import_function.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,7 @@

def import_spaces_from_nsr_schools
# Counts
p({
info: "In db before import from NSR:",
spaces: Space.count,
space_types: SpaceType.count,
space_groups: SpaceGroup.count,
space_contacts: SpaceContact.count
})
count_stats(info: "Before import from NSR JSON:")

# Load json file
file = File.read(Rails.root.join("db", "seeds", "imports", "nsr", "nsrParsedSchools.json"))
Expand All @@ -23,15 +17,92 @@ def import_spaces_from_nsr_schools
p raw_schools_to_parse: data.length

# Set up and save SpaceTypes
space_types = [
{ type_name: "barneskole" },
{ type_name: "ungdomsskole" },
{ type_name: "grunnskole" },
{ type_name: "vgs" }
space_type_data = [
{
type_name: "barneskole",
related_facilities_by_title: %w[
Klasserom
Gymsal
Sove gulvet
Kjøkken med ovn
Rullestolvennlig inngang
Rullestolvennlig inne
HC-toalett
Wifi
Lov å spise medbrakt
Nær kollektiv
Parkering
]
},
{
type_name: "ungdomsskole",
related_facilities_by_title: %w[
Klasserom
Gymsal
Sove gulvet
Kjøkken med ovn
Rullestolvennlig inngang
Rullestolvennlig inne
HC-toalett
Wifi
Lov å spise medbrakt
Nær kollektiv
Parkering
]
},
{
type_name: "grunnskole",
related_facilities_by_title: %w[
Klasserom
Gymsal
Sove gulvet
Kjøkken med ovn
Rullestolvennlig inngang
Rullestolvennlig inne
HC-toalett
Wifi
Lov å spise medbrakt
Nær kollektiv
Parkering
]
},
{
type_name: "vgs",
related_facilities_by_title: %w[
Klasserom
Gymsal
Sove gulvet
Rullestolvennlig inngang
Rullestolvennlig inne
HC-toalett
Wifi
Prosjektor
Nær kollektiv
Parkering
]
}
]
space_types = space_type_data.map do |data|
{
type_name: data[:type_name]
}
end
# TODO: Set up facility relations?
p "importing #{space_types.length} space types"
SpaceType.import new_all_unless_exists(SpaceType, space_types)

space_types_facility_relations = space_type_data.map do |data|
space_type = SpaceType.find_by(type_name: data[:type_name])

data[:related_facilities_by_title].map do |facility_title|
{
space_type:,
facility: Facility.find_by(title: facility_title)
}
end
end.flatten
SpaceTypesFacility.import new_all_unless_exists(SpaceTypesFacility, space_types_facility_relations)

# Trawl through it, and extract information into Rails format

# Set up owners first:
Expand All @@ -46,13 +117,22 @@ def import_spaces_from_nsr_schools
# Then start parsing Spaces, as they depend on the above
spaces = []
space_contacts = []
space_type_relations = []
data.each_with_index do |school, index|
print "\rParsing spaces and space contacts from nsr school data: #{index} / #{data.length}"
print "\rParsing spaces, space contacts and space type relations from nsr school data: #{index} / #{data.length}"

space = new_unless_exists Space, space_from(school)
spaces << space if space

space_contact_space = space || (space_from(school) && Space.find_by(space_from(school)))
guaranteed_space_for_relations = space || (space_from(school) && Space.find_by(space_from(school)))

space_type_relation = new_unless_exists(SpaceTypesRelation, {
space: guaranteed_space_for_relations,
space_type: SpaceType.find_by(space_type_from(school))
})
space_type_relations << space_type_relation if space_type_relation

space_contact_space = guaranteed_space_for_relations
next unless space_contact_space

space_contacts_from(school).each do |contact|
Expand All @@ -65,34 +145,25 @@ def import_spaces_from_nsr_schools
end

# Save them all with import
p "\nimporting #{spaces.length} spaces"
print "\nimporting #{spaces.length} spaces"
Space.import(spaces)

spaces.each_with_index do |space, index|
print "\rAdding unknown aggregated facility reviews for spaces #{index} / #{spaces.length}"
print "\nimporting #{space_type_relations.length} space type relations:"
SpaceTypesRelation.import(space_type_relations)

Facility.all.order(:created_at).map do |facility|
AggregatedFacilityReview.create!(experience: "unknown", space: space, facility: facility)
end

print "\nAggregating facility reviews for all #{Space.count} spaces:\n"

Space.all.each_with_index do |space, index|
print "\raggregating facility reviews for ##{index} / #{Space.count} / space_id: #{space.id}"
space.aggregate_facility_reviews
end

p "\nimporting #{space_contacts.length} space contacts"
print "\nimporting #{space_contacts.length} space contacts"
SpaceContact.import(space_contacts)


p({
info: "To import from NSR JSON:",
spaces: spaces.length,
space_types: space_types.length,
space_groups: space_groups.length
})
p({
info: "In db after import from NSR:",
spaces: Space.count,
space_types: SpaceType.count,
space_groups: SpaceGroup.count,
space_contacts: SpaceContact.count
})
count_stats(info: "After import from NSR JSON:")
end

# rubocop:enable all

0 comments on commit 155592b

Please sign in to comment.