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

Log weather periodically via OpenWeatherAPI - proof of concept/WIP #1307

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,4 @@ after_deploy:
addons:
code_climate:
repo_token: 462e015bbdaabfb20910fc07f2fea253410ecb131444e00f97dbf32dc6789ca6
postgresql: "9.4"
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ gem "hashie", ">= 3.5.3"

gem 'rake', '>= 10.0.0'

# For https://github.com/Growstuff/growstuff/issues/863
gem 'open-weather'

# # CMS
# gem 'comfortable_mexican_sofa', '~> 1.12.0'

Expand Down
3 changes: 3 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,8 @@ GEM
omniauth-twitter (1.4.0)
omniauth-oauth (~> 1.1)
rack
open-weather (0.11.0)
json
orm_adapter (0.5.0)
paperclip (5.1.0)
activemodel (>= 4.2.0)
Expand Down Expand Up @@ -570,6 +572,7 @@ DEPENDENCIES
omniauth-facebook
omniauth-flickr (>= 0.0.15)
omniauth-twitter (~> 1.2)
open-weather
pg
poltergeist
pry
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/likes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def success(like, liked_by_member: nil, status_code: nil)

def failed(like, message)
respond_to do |format|
format.json { render(json: { 'error': message }, status: :forbidden) }
format.json { render(json: { error: message }, status: :forbidden) }
format.html do
flash[:error] = message
if like && like.likeable
Expand Down
1 change: 1 addition & 0 deletions app/models/planting.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class Planting < ActiveRecord::Base
belongs_to :owner, class_name: 'Member', counter_cache: true
belongs_to :crop, counter_cache: true
has_many :harvests, -> { order(harvested_at: :desc) }, dependent: :destroy
has_many :planting_weather_logs

default_scope { order("plantings.created_at desc") }
scope :finished, -> { where(finished: true) }
Expand Down
50 changes: 50 additions & 0 deletions app/models/planting_weather_log.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Defines a JSON data type as weather_log
# See http://edgeguides.rubyonrails.org/active_record_postgresql.html#json for how to query it
# See http://openweathermap.org/current for documentation

# Example call: http://api.openweathermap.org/data/2.5/weather?q=London,uk&appid=df8abc00e3162fbd98bc48063cc6c4b5
# {
# "coord":{
# "lon":-0.13,
# "lat":51.51
# },
# "weather":[
# {
# "id":800,
# "main":"Clear",
# "description":"clear sky",
# "icon":"01d"
# }
# ],
# "base":"stations",
# "main":{
# "temp":281.83,
# "pressure":1025,
# "humidity":61,
# "temp_min":280.15,
# "temp_max":283.15
# },
# "visibility":10000,
# "wind":{
# "speed":5.7,
# "deg":300
# },
# "clouds":{
# "all":0
# },
# "dt":1491808800,
# "sys":{
# "type":1,
# "id":5091,
# "message":0.0097,
# "country":"GB",
# "sunrise":1491801287,
# "sunset":1491850179
# },
# "id":2643743,
# "name":"London",
# "cod":200
# }
class PlantingWeatherLog < ActiveRecord::Base
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be per garden? Or even per location? The basil and the tomatoes are next to each other so have the same weather afaik

Copy link
Collaborator Author

@CloCkWeRX CloCkWeRX Apr 11, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I went for planting so you can answer things like "Why did my leeks grow better last year than this year? Oh, 27 days of 40 degree temperature in a row on last year's planting" more easily

IE its simpler to do SELECT AVG(temp) FROM planting_weather_log WHERE planting_id = ? and only have data recorded for the duration of the planting; than it is to do

SELECT AVG(temp) FROM garden_weather_log JOIN gardens JOIN plantings WHERE garden_weather_log.created_at BETWEEN planting.start and planting.end GROUP BY planting_id
x lots.

Plantings also have a start and end date usually (planting.active); while a Garden is forever; and at least one per user; even if they never do anything with the site.
I suspect we'd run out of the free tier of API usage if we did that for everyone's gardens.

For dealing with plantings in the same garden/location/etc; we can probably cache the API requests at a city/geocoded location level.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about a weather log for a timestamp+location, then a planting links to that? Then the weather for a member's 100+ plantings isn't duplicated.

belongs_to :planting
end
4 changes: 4 additions & 0 deletions config/application.yml.example
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ GROWSTUFF_PAYPAL_SIGNATURE: "dummy"
GROWSTUFF_FACEBOOK_KEY: ""
GROWSTUFF_FACEBOOK_SECRET: ""

# Used to log weather, see https://github.com/Growstuff/growstuff/issues/863
# Generate by signing up to http://openweathermap.org/
GROWSTUFF_OPENWEATHER_KEY: "dummy"

# Elasticsearch is used for flexible search and it requires another component
# to be installed. To make it easy for people who don't need to test this feature
# it's been turned off for test and development environment as a default.
Expand Down
11 changes: 11 additions & 0 deletions db/migrate/20170410080449_planting_weather_log.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class PlantingWeatherLog < ActiveRecord::Migration
def change
create_table :planting_weather_logs do |t|
t.integer :planting_id
t.timestamps
t.json 'weather_data'
end

add_index :planting_weather_logs, :planting_id
end
end
Loading