Skip to content

Commit

Permalink
Merge pull request #198 from unepwcmc/release-1.3.1
Browse files Browse the repository at this point in the history
Release 1.3.1
  • Loading branch information
defaye authored Aug 14, 2021
2 parents 34d5bc8 + 92494e9 commit efdf5e0
Show file tree
Hide file tree
Showing 8 changed files with 120 additions and 50 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,8 @@ opened by Dependabot.
* refactor: raise more informative exception in Esri class if the response is not what was expected
* feat: add utilities/files.rb with `latest_file_by_glob` method to help select the latest filename-timestamped CSV that is used in the _habitat coverage protection_ imports
* refactor: add more informative error when a CSV is missing an expected header
* fix: wrap _import:refresh_ within a DB transaction so that it doesn't commit anything on failure
* fix: wrap _import:refresh_ within a DB transaction so that it doesn't commit anything on failure

## 1.3.1

* correctly merge develop into release and re-release
62 changes: 51 additions & 11 deletions app/models/habitat.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,26 @@ class Habitat < ApplicationRecord
has_many :change_stats
has_many :species

validates_presence_of :name,
:title,
:theme,
:total_area, # see NOTE(1)
:protected_area # see NOTE(1)

validates_numericality_of :total_area, { # see NOTE(1)
greater_than_or_equal_to: ->(habitat) { habitat.protected_area }
}

validates_numericality_of :protected_area, { # see NOTE(1)
greater_than_or_equal_to: 0,
less_than_or_equal_to: ->(habitat) { habitat.total_area }
}

# NOTE(1): if stats are ever generated automatically again - this could be removed
def percent_protected
protected_area / total_area * 100
end

def global_coverage_title(habitat_type)
habitat_type == 'points' ? "Total number of #{title.downcase} records globally" : "Total global recorded coverage of #{title.downcase}"
end
Expand Down Expand Up @@ -121,28 +141,48 @@ def type
end

def global_stats
stats = geo_entity_stats.country_stats.to_a # reduce hits to database
{
total_habitat_cover: stats.pluck(:total_value).compact.reduce(&:+),
protected_habitat_cover: stats.pluck(:protected_value).compact.reduce(&:+)
total_habitat_cover: total_area,
protected_habitat_cover: protected_area
}

# TODO: maybe re-implement this code if stats gets generated by the app again
# otherwise, allow this to remain commented for the indefinite future

# stats = geo_entity_stats.country_stats.to_a # reduce hits to database
# {
# total_habitat_cover: stats.pluck(:total_value).compact.reduce(&:+),
# protected_habitat_cover: stats.pluck(:protected_value).compact.reduce(&:+)
# }
end

def global_protection
stats = {
global_stats_data = global_stats

{
'name' => name,
'total_value' => 0,
'protected_value' => 0
'protected_percentage' => percent_protected,
'total_value' => global_stats_data[:total_habitat_cover],
'protected_value' => global_stats_data[:protected_habitat_cover]
}

global_stats_data = global_stats # reduce hits to database
# TODO: see global_stats comments - we can now use a computed property if the stats
# are pre-calculated - this logic below could be restored if stats are generated
# automatically again

# stats = {
# 'name' => name,
# 'total_value' => 0,
# 'protected_value' => 0
# }

stats['total_value'] = global_stats_data[:total_habitat_cover]
stats['protected_value'] = global_stats_data[:protected_habitat_cover]
# global_stats_data = global_stats # reduce hits to database

protected_value = stats['protected_value'].positive? ? stats['protected_value'] : 1
# stats['total_value'] = global_stats_data[:total_habitat_cover]
# stats['protected_value'] = global_stats_data[:protected_habitat_cover]

stats.merge({ 'protected_percentage' => protected_value / stats['total_value'] * 100 })
# protected_value = stats['protected_value'].positive? ? stats['protected_value'] : 1
# stats.merge({ 'protected_percentage' => protected_value / stats['total_value'] * 100 })
end

def self.global_protection
Expand Down
37 changes: 23 additions & 14 deletions config/deploy.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

# config valid only for current version of Capistrano
# lock "3.11.0"

Expand All @@ -7,55 +9,62 @@

set :nvm_type, :user # or :system, depends on your nvm setup
set :nvm_node, 'v10.15.1'
set :nvm_map_bins, %w{node npm yarn}
set :nvm_map_bins, %w[node npm yarn]

set :deploy_user, 'wcmc'


set :backup_path, "/home/#{fetch(:deploy_user)}/Backup"



# Default deploy_to directory is /var/www/my_app_name
set :deploy_to, "/home/#{fetch(:deploy_user)}/#{fetch(:application)}"

# Default value for :scm is :git
set :scm, :git
set :scm_username, "unepwcmc-read"

set :scm_username, 'unepwcmc-read'

set :rvm_type, :user
set :rvm_ruby_version, '2.5.0'



set :ssh_options, {
forward_agent: true,
forward_agent: true
}


# Default value for :format is :pretty
# set :format, :pretty

# Default value for :log_level is :debug
#set :log_level, :debug
# set :log_level, :debug

# Default value for :pty is false
set :pty, true

# Default value for :linked_files is []
#set :linked_files, fetch(:linked_files, []).push('config/database.yml', 'config/secrets.yml')
# set :linked_files, fetch(:linked_files, []).push('config/database.yml', 'config/secrets.yml')

set :linked_files, %w{config/database.yml .env}
set :linked_files, %w[config/database.yml .env]

# Default value for linked_dirs is []
set :linked_dirs, fetch(:linked_dirs, []).push('log', 'tmp/pids', 'tmp/cache', 'tmp/sockets', 'vendor/bundle', 'public/system', 'public/downloads', 'node_modules', 'client/node_modules')


# Default value for default_env is {}
# set :default_env, { path: "/opt/ruby/bin:$PATH" }

# Default value for keep_releases is 5
set :keep_releases, 5

set :passenger_restart_with_touch, false

namespace :deploy do
desc 'Run any rake task if specified (e.g. cap staging deploy TASK=import:refresh)'
task :run_task do
on roles(:app) do
within release_path do
with rails_env: fetch(:rails_env) do
rake ENV['TASK']
end
end
end
end
end

before 'deploy:publishing', 'deploy:run_task' if ENV['TASK']
24 changes: 6 additions & 18 deletions config/deploy/staging.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
set :stage, :staging
set :branch, :develop



server "web-supported-staging.linode.unep-wcmc.org", user: 'wcmc', roles: %w{app web db}

set :application, "ocean-plus-habitats"
set :server_name, "ocean-plus-habitats.web-supported-staging.linode.unep-wcmc.org"
set :sudo_user, "wcmc"
set :app_port, "80"


set :branch, ENV['BRANCH'] || :develop

server 'web-supported-staging.linode.unep-wcmc.org', user: 'wcmc', roles: %w[app web db]

set :application, 'ocean-plus-habitats'
set :server_name, 'ocean-plus-habitats.web-supported-staging.linode.unep-wcmc.org'
set :sudo_user, 'wcmc'
set :app_port, '80'

# server-based syntax
# ======================
Expand All @@ -23,8 +17,6 @@
# server "example.com", user: "deploy", roles: %w{app web}, other_property: :other_value
# server "db.example.com", user: "deploy", roles: %w{db}



# role-based syntax
# ==================

Expand All @@ -37,8 +29,6 @@
# role :web, %w{[email protected] [email protected]}, other_property: :other_value
# role :db, %w{[email protected]}



# Configuration
# =============
# You can set any configuration variable like in config/deploy.rb
Expand All @@ -47,8 +37,6 @@
# http://capistranorb.com/documentation/getting-started/configuration/
# Feel free to add new variables to customise your setup.



# Custom SSH Options
# ==================
# You may pass any option but keep in mind that net/ssh understands a
Expand Down
10 changes: 10 additions & 0 deletions config/habitats.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,42 @@ habitats:
poly_table: 'wcmc008_coralreef2010_py_v4'
point_table: 'wcmc008_coralreef2010_pt_v4'
wms_url: 'https://gis.unep-wcmc.org/arcgis/rest/services/marine/WCMC_008_CoralReefs_WMS/MapServer/export?transparent=true&format=png32&bbox={bbox-epsg-3857}&bboxSR=EPSG:3857&imageSR=EPSG:3857&size=256,256&f=image'
total_area: 149886.974126303
protected_area: 67431.4170347656
saltmarshes:
name: 'saltmarshes'
title: 'Saltmarshes'
theme: 'green'
poly_table: 'wcmc027_saltmarshes_py_v6'
point_table: 'wcmc027_saltmarshes_pt_v6'
wms_url: 'https://gis.unep-wcmc.org/arcgis/rest/services/marine/WCMC_027_Saltmarsh_WMS/MapServer/export?transparent=true&format=png32&bbox={bbox-epsg-3857}&bboxSR=EPSG:3857&imageSR=EPSG:3857&size=256,256&f=image'
total_area: 224435.075089843
protected_area: 111636.441534896
mangroves:
name: 'mangroves'
title: 'Mangroves'
theme: 'yellow'
poly_table: 'wcmc011_atlasmangrove2010_py_v3'
point_table:
wms_url: 'https://gis.unep-wcmc.org/arcgis/rest/services/marine/WCMC_011_WorldAtlasMangroves_WMS/MapServer/export?transparent=true&format=png32&bbox={bbox-epsg-3857}&bboxSR=EPSG:3857&imageSR=EPSG:3857&size=256,256&f=image'
total_area: 135869.552269994
protected_area: 58109.098834161
seagrasses:
name: 'seagrasses'
title: 'Seagrasses'
theme: 'blue'
poly_table: 'wcmc_013_014_seagrassespy_v6'
point_table: 'wcmc_013_014_seagrassespt_v6'
wms_url: 'https://gis.unep-wcmc.org/arcgis/rest/services/marine/WCMC_013_014_Seagrass_WMS/MapServer/export?transparent=true&format=png32&bbox={bbox-epsg-3857}&bboxSR=EPSG:3857&imageSR=EPSG:3857&size=256,256&f=image'
total_area: 314001.940552758
protected_area: 83606.1196777014
coldcorals:
name: 'coldcorals'
title: 'Cold-water corals'
theme: 'pink'
poly_table: 'wcmc001_coldcorals2017_py_v5'
point_table: 'wcmc001_coldcorals2017_pt_v5'
wms_url: 'https://gis.unep-wcmc.org/arcgis/rest/services/marine/WCMC_001_ColdCorals2017_WMS/MapServer/export?transparent=true&format=png32&bbox={bbox-epsg-3857}&bboxSR=EPSG:3857&imageSR=EPSG:3857&size=256,256&f=image'
total_area: 15336.9752787556
protected_area: 4505.64215821009

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class AddGlobalHabitatCoverageProtectionStatsToHabitats < ActiveRecord::Migration[5.1]
def change
add_column :habitats, :total_area, :float, null: true
add_column :habitats, :protected_area, :float, null: true
end
end
4 changes: 3 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: 20210525133211) do
ActiveRecord::Schema.define(version: 20210812135252) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand Down Expand Up @@ -125,6 +125,8 @@
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.text "wms_url"
t.float "total_area"
t.float "protected_area"
end

create_table "sources", force: :cascade do |t|
Expand Down
21 changes: 16 additions & 5 deletions lib/tasks/import_habitats.rake
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
# NOTE(1): if the global stats are ever generated by the app again, we might
# no-longer need to have the total_area and protected_area attributes
namespace :import do
desc "import CSV data into database"
desc 'import CSV data into database'
task :habitats, [:csv_file] => [:environment] do
habitats_config = YAML.load(File.open("#{Rails.root}/config/habitats.yml", 'r'))

habitats_config['habitats'].each do |name, data|
Habitat.where(name: data['name']).first_or_create do |habitat|
habitat.update_attributes(data)
end
attributes = {
title: data['title'],
theme: data['theme'],
poly_table: data['poly_table'],
point_table: data['point_table'],
wms_url: data['wms_url'],
total_area: data['total_area'].to_f, # see NOTE(1)
protected_area: data['protected_area'].to_f # see NOTE(1)
}

Habitat.find_or_create_by(name: data['name']).update_attributes(attributes)

Rails.logger.info "#{name.capitalize} habitat created!"
end
end
end
end

0 comments on commit efdf5e0

Please sign in to comment.