Skip to content

Commit 160f20f

Browse files
author
Simon Funke
committed
city maps apis
1 parent f8e477e commit 160f20f

10 files changed

+86
-45224
lines changed

app/controllers/city_maps_controller.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ def index
66

77
respond_to do |format|
88
format.html # index.html.erb
9-
format.json { render json: @city_maps }
9+
format.json { render json: @city_maps, :only => [:name, :latitude, :longitude, :id] }
1010
end
1111
end
1212

app/controllers/city_measurements_controller.rb

+27-1
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,37 @@ class CityMeasurementsController < ApplicationController
22
# GET /city_measurements
33
# GET /city_measurements.json
44
def index
5+
if params["id"].nil?
6+
@city_measurements = CityMeasurement.all
7+
else
8+
begin
9+
@city_measurements = CityMeasurement.where(:city_map_id => params["id"])
10+
rescue
11+
@city_measurements = []
12+
end
13+
end
14+
15+
# Find suspicious data by computing the derivatives over time and checking for rapid drops
16+
droplimit = -1.0/2
17+
if @city_measurements.size > 0
18+
@city_measurements[0].is_suspicious = false
19+
20+
(1..@city_measurements.size-1).each do |i|
21+
@city_measurements[i].is_suspicious = false
22+
begin
23+
if ((@city_measurements[i].NumberOfClientsSplitByClientAndByServer - @city_measurements[i-1].NumberOfClientsSplitByClientAndByServer)/1) < droplimit
24+
@city_measurements[i].is_suspicious = true
25+
end
26+
rescue
27+
end
28+
end
29+
end
30+
531
@city_measurements = CityMeasurement.all
632

733
respond_to do |format|
834
format.html # index.html.erb
9-
format.json { render json: @city_measurements }
35+
format.json { render json: @city_measurements.to_json(:only => [:month, :NumberOfClientsSplitByClientAndByServer], :methods => :suspicious) }
1036
end
1137
end
1238

app/models/city_map.rb

+2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
class CityMap < ActiveRecord::Base
2+
has_many :city_measurements
3+
24
end

app/models/city_measurement.rb

+8
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,10 @@
11
class CityMeasurement < ActiveRecord::Base
2+
attr_accessor :is_suspicious
3+
belongs_to :city_map
4+
5+
default_scope :order => 'month DESC'
6+
7+
def suspicious
8+
self.is_suspicious
9+
end
210
end

app/views/city_maps/index.html.erb

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<th>Name</th>
77
<th>Region</th>
88
<th>Latitude</th>
9-
<th>Longtitude</th>
9+
<th>Longitude</th>
1010
<th></th>
1111
<th></th>
1212
<th></th>
@@ -18,7 +18,7 @@
1818
<td><%= city_map.name %></td>
1919
<td><%= city_map.region %></td>
2020
<td><%= city_map.latitude %></td>
21-
<td><%= city_map.longtitude %></td>
21+
<td><%= city_map.longitude %></td>
2222
<td><%= link_to 'Show', city_map %></td>
2323
<td><%= link_to 'Edit', edit_city_map_path(city_map) %></td>
2424
<td><%= link_to 'Destroy', city_map, confirm: 'Are you sure?', method: :delete %></td>

db/migrate/20111108222653_create_city_measurements.rb

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
class CreateCityMeasurements < ActiveRecord::Migration
22
def change
33
create_table :city_measurements do |t|
4+
t.integer :city_map_id
5+
t.string :city
46
t.date :month
57
t.float :DownloadThroughputSplitByClientAndByServer
68
t.float :UploadThroughputSplitByClientAndByServer

db/schema.rb

+2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
end
2525

2626
create_table "city_measurements", :force => true do |t|
27+
t.integer "city_map_id"
28+
t.string "city"
2729
t.date "month"
2830
t.float "DownloadThroughputSplitByClientAndByServer"
2931
t.float "UploadThroughputSplitByClientAndByServer"

lib/tasks/load_mlab_data.rake

+41-2
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,50 @@ namespace :load_mlab_data do
2929
csv = CSV.parse(csv_text, :headers => true)
3030
csv.each do |row|
3131
row = row.to_hash.with_indifferent_access
32-
CountryMeasurement.create!(row.to_hash.symbolize_keys)
32+
countrymeasurement = CountryMeasurement.new(row.to_hash.symbolize_keys)
33+
begin
34+
day = row['month'].split('-')[1]
35+
month = row['month'].split('-')[0]
36+
year = row['month'].split('-')[2]
37+
countrymeasurement.month = "#{year}-#{month}-#{day}"
38+
rescue
39+
end
40+
countrymeasurement.save
41+
end
42+
end
43+
44+
desc "Loading city_map data"
45+
task :load_city_map => :environment do
46+
csv_text = File.read('mlab_data/city_map.txt')
47+
csv = CSV.parse(csv_text, :headers => true)
48+
csv.each do |row|
49+
row = row.to_hash.with_indifferent_access
50+
citymap = CityMap.new(row.to_hash.symbolize_keys)
51+
citymap.id = citymap.city.hash
52+
citymap.save!
53+
end
54+
end
55+
56+
desc "Loading city_measurements data"
57+
task :load_city_measurements => :environment do
58+
csv_text = File.read('mlab_data/city_measurements.txt')
59+
csv = CSV.parse(csv_text, :headers => true)
60+
csv.each do |row|
61+
row = row.to_hash.with_indifferent_access
62+
citymeasurement = CityMeasurement.new(row.to_hash.symbolize_keys)
63+
begin
64+
day = row['month'].split('-')[1]
65+
month = row['month'].split('-')[0]
66+
year = row['month'].split('-')[2]
67+
citymeasurement.month = "#{year}-#{month}-#{day}"
68+
rescue
69+
end
70+
citymeasurement.city_map_id = citymeasurement.city.hash
71+
citymeasurement.save!
3372
end
3473
end
3574

3675
desc "Run all mlab data tasks"
37-
task :all => [:load_world_measurements_data, :load_country_map, :load_country_measurements]
76+
task :all => [:load_world_measurements_data, :load_country_map, :load_country_measurements, :load_city_measurements, :load_city_map]
3877

3978
end

0 commit comments

Comments
 (0)