Skip to content

Commit

Permalink
Merge pull request #1 from geoblacklight/move-to-constants
Browse files Browse the repository at this point in the history
Move to constants and add support for new fields
  • Loading branch information
mejackreed authored Jan 16, 2018
2 parents b0fdf3b + 5cd004c commit e4cfce3
Show file tree
Hide file tree
Showing 12 changed files with 108 additions and 29 deletions.
15 changes: 11 additions & 4 deletions app/models/geo_monitor/layer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,17 @@ def self.from_geoblacklight(schema_json)
schema = JSON.parse(schema_json)
references = JSON.parse(schema['dct_references_s'])
find_or_create_by(slug: schema['layer_slug_s']) do |layer|
layer.checktype = 'WMS'
layer.checktype =
if references['http://www.opengis.net/def/serviceType/ogc/wms']
'WMS'
elsif references['http://iiif.io/api/image']
'IIIF'
end
layer.institution = schema['dct_provenance_s']
layer.rights = schema['dc_rights_s']
layer.layername = schema['layer_id_s']
layer.bbox = schema['solr_geom']
layer.url = references['http://www.opengis.net/def/serviceType/ogc/wms']
layer.url = references['http://www.opengis.net/def/serviceType/ogc/wms'] || references['http://iiif.io/api/image']
layer.active = true
end
end
Expand All @@ -28,11 +35,11 @@ def check
bbox: bounding_box, url: url, layers: layername
).tile
end
GeoMonitor::Status.from_response(response, self, time.real.to_f)
::GeoMonitor::Status.from_response(response, self, time.real.to_f)
end

def availability_score
statuses.where(res_code: '200').count.to_f / statuses.count
statuses.where(res_code: '200', content_type: 'image/png').count.to_f / statuses.count
end
end
end
7 changes: 4 additions & 3 deletions app/models/geo_monitor/status.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ class Status < ApplicationRecord
# Limits the number of statuses per layer to prevent a ballooing database
def limit_by_layer
statuses_by_layer = self.class.where(layer: layer).count
max = GeoMonitor::Engine.config.max_status_per_layer
max = ::GeoMonitor::Engine.config.max_status_per_layer
self.class
.where(layer: layer)
.last(statuses_by_layer - max + 1)
.map(&:destroy) if statuses_by_layer >= max
end

##
# @param [Faraday::Resposne] response
# @param [Faraday::Response] response
# @param [GeoMonitor::Layer] layer
# @param [Float] time
def self.from_response(response, layer, time)
Expand All @@ -24,7 +24,8 @@ def self.from_response(response, layer, time)
res_code: response.status,
submitted_query: response.env[:url].to_s,
layer: layer,
res_headers: response.headers
res_headers: response.headers,
content_type: response.headers.try(:[], :content_type)
)
end
end
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20180112220603_add_content_type_to_status.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddContentTypeToStatus < ActiveRecord::Migration[5.1]
def change
add_column :geo_monitor_statuses, :content_type, :string
end
end
6 changes: 6 additions & 0 deletions db/migrate/20180116150504_add_more_layer_fields.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class AddMoreLayerFields < ActiveRecord::Migration[5.1]
def change
add_column :geo_monitor_layers, :rights, :string
add_column :geo_monitor_layers, :institution, :string
end
end
3 changes: 3 additions & 0 deletions lib/geo_monitor.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
require "geo_monitor/engine"

module GeoMonitor
extend ActiveSupport::Autoload


# A simple structure to conform to Faraday::Response
FailedResponse = Struct.new(:env, :status, :headers)
end
6 changes: 3 additions & 3 deletions lib/geo_monitor/bounding_box.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ def zoom_level
lng_diff = east - west
max_diff = [lat_diff, lng_diff].max

if max_diff < ::GeoMonitor::DEGREES_IN_CIRCLE / 2**20
if max_diff < ::GeoMonitor::Constants::DEGREES_IN_CIRCLE / 2**20
zoom = 21
else
zoom = -1 * ((Math.log(max_diff) / Math.log(2)) - (Math.log(::GeoMonitor::DEGREES_IN_CIRCLE) / Math.log(2)))
zoom = -1 * ((Math.log(max_diff) / Math.log(2)) - (Math.log(::GeoMonitor::Constants::DEGREES_IN_CIRCLE) / Math.log(2)))
zoom = 1 if zoom < 1
end
zoom.ceil
Expand All @@ -52,7 +52,7 @@ def zoom_level
def tile_number
lat_rad = south / 180 * Math::PI
n = 2.0**zoom_level
x = ((west + 180.0) / ::GeoMonitor::DEGREES_IN_CIRCLE * n).to_i
x = ((west + 180.0) / ::GeoMonitor::Constants::DEGREES_IN_CIRCLE * n).to_i
y = ((1.0 - Math.log(Math.tan(lat_rad) + (1 / Math.cos(lat_rad))) / Math::PI) / 2.0 * n)
y = if y.infinite?.nil?
y.to_i
Expand Down
6 changes: 6 additions & 0 deletions lib/geo_monitor/constants.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module GeoMonitor
module Constants
R = 6_378_137 # Radius of Earth in meters
DEGREES_IN_CIRCLE = 360.0
end
end
11 changes: 5 additions & 6 deletions lib/geo_monitor/engine.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
require 'rails/all'
require 'faraday'
require 'geo_monitor/bounding_box'
require 'geo_monitor/lat_lng_point'
require 'geo_monitor/requests/wms'

module GeoMonitor
R = 6_378_137 # Radius of Earth in meters
DEGREES_IN_CIRCLE = 360.0

##
# Top level Rails Engine class
class Engine < ::Rails::Engine
require 'geo_monitor/constants'
require 'geo_monitor/bounding_box'
require 'geo_monitor/lat_lng_point'
require 'geo_monitor/requests/wms'

isolate_namespace GeoMonitor

GeoMonitor::Engine.config.max_status_per_layer = 5
Expand Down
4 changes: 2 additions & 2 deletions lib/geo_monitor/lat_lng_point.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ def to_3857
max = 1 - 1E-15
sin = [[Math.sin(lng * d), max].min, -max].max
self.class.new(
lat: GeoMonitor::R * lat * d,
lng: GeoMonitor::R * Math.log((1 + sin) / (1 - sin)) / 2
lat: ::GeoMonitor::Constants::R * lat * d,
lng: ::GeoMonitor::Constants::R * Math.log((1 + sin) / (1 - sin)) / 2
)
end

Expand Down
4 changes: 2 additions & 2 deletions lib/geo_monitor/requests/wms.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def request_params
# Request the tile.
def tile
unless url.present?
return GeoMonitor::FailedResponse.new(
return ::GeoMonitor::FailedResponse.new(
{ url: url }, 'No URL provided', {}
)
end
Expand All @@ -46,7 +46,7 @@ def tile
request.options.open_timeout = 10
end
rescue Faraday::ConnectionFailed, Faraday::TimeoutError => e
GeoMonitor::FailedResponse.new(
::GeoMonitor::FailedResponse.new(
{ url: conn.url_prefix.to_s },
e.class,
nil
Expand Down
34 changes: 34 additions & 0 deletions spec/fixtures/image.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"solr_year_i": "1777",
"dc_description_s": "The provinces of New York, and New Jersey; with part of Pensilvania and the province of Quebec. Drawn by Major Holland, Surveyor General of the Northern District in America",
"layer_modified_dt": "2015-01-25T02:06:19Z",
"dc_creator_sm": [
"Holland, Samuel,",
"Coentgen, Heinrich Hugo.",
"Brönner, H. L.",
"Fox, N. B.,"
],
"layer_id_s": "jd472z992",
"dc_type_s": "Image",
"dc_identifier_s": "jd472z992",
"dct_provenance_s": "Princeton",
"dc_title_s": "The provinces of New York, and New Jersey; with part of Pensilvania and the province of Quebec. Drawn by Major Holland, Surveyor General of the Northern District in America",
"layer_slug_s": "princeton-jd472z992",
"dc_format_s": "Scanned Map",
"dct_references_s": "{\"http://www.loc.gov/standards/marcxml\": \"https://geowebservices.princeton.edu/download/items/jd472z992/marc.xml\", \"http://iiif.io/api/image\": \"https://libimages.princeton.edu/loris/pulmap/jd/47/2z/99/2/00000001.jp2/info.json\"}",
"layer_geom_type_s": "Image",
"dct_temporal_sm": [
"1777"
],
"solr_geom": "ENVELOPE(-73.727775, -66.8850751, 47.459686, 40.950943)",
"dc_rights_s": "Public",
"dct_spatial_sm": [
"New England",
"New York (State)",
"New Jersey",
"Québec (Province)",
"New York (State)",
"New Jersey"
],
"geoblacklight_version": "1.0"
}
36 changes: 27 additions & 9 deletions spec/models/geo_monitor/layer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,32 @@
let(:schema_json) { File.read('spec/fixtures/cz128vq0535.json') }
subject { described_class.from_geoblacklight(schema_json) }
describe '.from_geoblacklight' do
it 'creates an instance with expected fields' do
expect(subject.checktype).to eq 'WMS'
expect(subject.slug).to eq 'stanford-cz128vq0535'
expect(subject.layername).to eq 'druid:cz128vq0535'
expect(subject.bbox)
.to eq 'ENVELOPE(29.572742, 35.000308, 4.234077, -1.478794)'
expect(subject.active).to eq true
expect(subject.url).to eq 'https://geowebservices.stanford.edu/geoserver/wms'
context 'WMS layer' do
it 'creates an instance with expected fields' do
expect(subject.checktype).to eq 'WMS'
expect(subject.slug).to eq 'stanford-cz128vq0535'
expect(subject.layername).to eq 'druid:cz128vq0535'
expect(subject.bbox)
.to eq 'ENVELOPE(29.572742, 35.000308, 4.234077, -1.478794)'
expect(subject.active).to eq true
expect(subject.institution).to eq 'Stanford'
expect(subject.rights).to eq 'Public'
expect(subject.url).to eq 'https://geowebservices.stanford.edu/geoserver/wms'
end
end
context 'IIIF layer' do
let(:schema_json) { File.read('spec/fixtures/image.json') }
it 'creates an instance with expected fields' do
expect(subject.checktype).to eq 'IIIF'
expect(subject.slug).to eq 'princeton-jd472z992'
expect(subject.layername).to eq 'jd472z992'
expect(subject.bbox)
.to eq 'ENVELOPE(-73.727775, -66.8850751, 47.459686, 40.950943)'
expect(subject.active).to eq true
expect(subject.institution).to eq 'Princeton'
expect(subject.rights).to eq 'Public'
expect(subject.url).to eq 'https://libimages.princeton.edu/loris/pulmap/jd/47/2z/99/2/00000001.jp2/info.json'
end
end
end
describe '#bounding_box' do
Expand Down Expand Up @@ -46,7 +64,7 @@
end
describe '#availability_score' do
before do
4.times { GeoMonitor::Status.create(layer: subject, res_code: '200') }
4.times { GeoMonitor::Status.create(layer: subject, res_code: '200', content_type: 'image/png') }
1.times { GeoMonitor::Status.create(layer: subject, res_code: '404') }
end
it 'calculates the availability score' do
Expand Down

0 comments on commit e4cfce3

Please sign in to comment.