forked from falling-fruit/falling-fruit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rakefile
145 lines (135 loc) · 4.75 KB
/
Rakefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/usr/bin/env rake
# Add your own tasks in files placed in lib/tasks ending in .rake,
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
require File.expand_path('../config/application', __FILE__)
SendEmails = true
FallingfruitWebapp::Application.load_tasks
task(:clear_cache => :environment) do
LocationsController.new.expire_things
end
task(:geocode => :environment) do
Geocoder.configure({:always_raise => :all})
n = Location.where("lat is null and lng is null").count
Location.where("lat is null and lng is null").each{ |l|
begin
puts n
l.geocode
unless [l.lng,l.lat].any? { |e| e.nil? }
l.location = "POINT(#{l.lng} #{l.lat})"
l.save
n -= 1
end
sleep 1
rescue Geocoder::OverQueryLimitError => e
puts e
break
end
}
end
task(:range_changes => :environment) do
sent_okay = 0
User.where('range_updates_email AND range IS NOT NULL').each{ |u|
m = Spammer.range_changes(u,7)
next if m.nil?
if SendEmails
begin
m.deliver
rescue
$stderr.puts "Problem sending message!!! #{m}"
next
end
sent_okay += 1
else
puts m
end
}
$stderr.puts "Sent #{sent_okay} messages successfully"
end
task(:export_data => :environment) do
r = ActiveRecord::Base.connection.execute("SELECT ARRAY_AGG(COALESCE(types.name,locations_types.type_other)) as name, locations.id as id,
description, lat, lng, address, season_start, season_stop, no_season, access, unverified, author, import_id,
locations.created_at, locations.updated_at FROM locations
INNER JOIN locations_types ON locations_types.location_id=locations.id LEFT OUTER
JOIN types ON locations_types.type_id=types.id GROUP BY locations.id")
CSV.open("public/data.csv","wb") do |csv|
cols = ["id","lat","lng","unverified","description","season_start","season_stop",
"no_season","author","address","created_at","updated_at",
"quality_rating","yield_rating","access","import_link","muni","name"]
csv << cols
r.each{ |row|
quality_rating = Location.find(row["id"]).mean_quality_rating
yield_rating = Location.find(row["id"]).mean_yield_rating
csv << [row["id"],row["lat"],row["lng"],row["unverified"],row["description"],
row["season_start"].nil? ? nil : Location::Months[row["season_start"].to_i],
row["season_stop"].nil? ? nil : Location::Months[row["season_stop"].to_i],
row["no_season"],row["author"],
row["address"],row["created_at"],row["updated_at"],
quality_rating.nil? ? nil : Location::Ratings[quality_rating],
yield_rating.nil? ? nil : Location::Ratings[yield_rating],
row["access"].nil? ? nil : Location::AccessShort[row["access"].to_i],
row["import_id"].nil? ? nil : "http://fallingfruit.org/imports/#{row["import_id"]}",
row["import_id"].nil? ? 'f' : (Import.find(row["import_id"]).muni ? 't' : 'f'),
row["name"]]
}
end
end
task(:import => :environment) do
if File.exists? "public/import/lockfile"
puts "Lockfile exists, not running"
exit
end
FileUtils.touch "public/import/lockfile"
typehash = {}
Type.all.each{ |t|
typehash[t.name] = t
}
dh = Dir.open("public/import")
dh.each{ |l|
next unless l =~ /^(\d+).csv$/
import_id = $1.to_i
begin
import = Import.find(import_id)
rescue ActiveRecord::RecordNotFound => e
next
end
next if import.nil?
print "#{import_id}: "
n = 0
errs = []
text_errs = []
ok_count = 0
CSV.foreach("public/import/#{l}") do |row|
print "."
n += 1
next if n == 1 or row.join.blank?
location = Location.build_from_csv(row,typehash)
location.import = import
location.client = 'import'
if location.lat.nil? or location.lng.nil? and (!location.address.nil? and (!location.address.length == 0))
location.geocode
end
if location.valid?
ok_count += 1
location.save
else
text_errs << location.errors
errs << row
end
end
c = Change.new
c.description = "#{ok_count} new locations imported from #{import.name} (#{import.url})"
c.save
if errs.any?
errFile ="public/import/#{import_id}_error.csv"
errs.insert(0,Location.csv_header)
errCSV = CSV.open(errFile,"wb") do |csv|
errs.each {|row| csv << row}
end
end
ApplicationController.cluster_batch_increment(import)
FileUtils.mv "public/import/#{l}", "public/import/#{import_id}_done.csv"
puts
}
dh.close
FileUtils.rm_f "public/import/lockfile"
end