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

Last fix #81

Open
wants to merge 180 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
180 commits
Select commit Hold shift + click to select a range
511e0ea
sources route
mitcha16 Jul 14, 2015
d5dec4e
Merge pull request #17 from mitchashby16/bad_request_identifier
dsshim Jul 14, 2015
1b1ef2a
Application Registration - Bad Request - Identifier
mitcha16 Jul 14, 2015
ee090a2
Merge pull request #19 from mitchashby16/bad_request_identifier
dsshim Jul 14, 2015
b095850
added successful registration path and test
mitcha16 Jul 14, 2015
1a7b329
Merge pull request #20 from mitchashby16/bad_request_identifier
dsshim Jul 14, 2015
6d1e95b
work in progess for multiple identifiers
mitcha16 Jul 14, 2015
a026a18
Merge pull request #21 from mitchashby16/bad_request_identifier
dsshim Jul 14, 2015
bdf1a79
finished app registration tests
dsshim Jul 14, 2015
28b4198
Merge pull request #22 from mitchashby16/app_reg_403
noobjey Jul 14, 2015
7ba2c95
added test_script
mitcha16 Jul 15, 2015
28547cf
Merge pull request #23 from mitchashby16/test_script
noobjey Jul 15, 2015
5097b98
remove hidden fodler
noobjey Jul 15, 2015
a8bca93
Merge pull request #24 from mitchashby16/kill_folder
dsshim Jul 15, 2015
950d9f9
test data to play with in dev
noobjey Jul 15, 2015
bde9aa4
Merge pull request #26 from mitchashby16/test_script
dsshim Jul 15, 2015
8f2219b
registration data saves to database
noobjey Jul 15, 2015
c5b4e43
Merge pull request #27 from mitchashby16/registration_input#25
dsshim Jul 15, 2015
93e64e8
stores urls
noobjey Jul 15, 2015
4e27591
Merge pull request #28 from mitchashby16/5-store_payload
dsshim Jul 15, 2015
f316284
does not save duplicate payloads client
noobjey Jul 15, 2015
f9281cf
Merge pull request #29 from mitchashby16/7-payload_already_recieved
dsshim Jul 15, 2015
1aaa393
working on parser
noobjey Jul 16, 2015
ae78d25
fixed url/parser tests
dsshim Jul 16, 2015
709ce90
WIP
dsshim Jul 16, 2015
cd60d7c
add data handler
dsshim Jul 16, 2015
98967a0
using payload parser
noobjey Jul 16, 2015
ede835a
Merge pull request #31 from mitchashby16/parse_input
dsshim Jul 16, 2015
9b8da88
added guard gems
noobjey Jul 16, 2015
50ed7d9
parser handles screen resolutions
noobjey Jul 16, 2015
ff30ce1
parse returns event input
noobjey Jul 16, 2015
132293d
urls method fix
mitcha16 Jul 16, 2015
eadd25e
Merge pull request #32 from mitchashby16/create_user_agents
dsshim Jul 16, 2015
5e3b612
removed rake from prod deploys
noobjey Jul 16, 2015
05dea98
operating system per request not working, storing to overcome big merge
noobjey Jul 16, 2015
ebc0a6f
return operating system table input
noobjey Jul 16, 2015
6e1baed
refactored methods into initialize
noobjey Jul 16, 2015
9c6a3c8
added screen resolution table
dsshim Jul 16, 2015
51dd385
fix a space
noobjey Jul 16, 2015
cd393ac
Merge branch 'master' into merge_try
noobjey Jul 16, 2015
d77f42c
Merge pull request #33 from mitchashby16/merge_try
dsshim Jul 16, 2015
66abf66
screen_res model test
dsshim Jul 16, 2015
fd499be
removed bad curl from script
noobjey Jul 17, 2015
604326a
added pry to other environment
noobjey Jul 17, 2015
d65eb15
i accidently commited to master, this is try getting it right
noobjey Jul 17, 2015
eddba0d
Merge pull request #34 from mitchashby16/fixing_master
dsshim Jul 17, 2015
48d729d
fixed issue with guard
noobjey Jul 17, 2015
8ff077c
fixed shim's mistakes
mitcha16 Jul 17, 2015
d4011af
Merge pull request #35 from mitchashby16/create_user_agents
dsshim Jul 17, 2015
68c47f0
os tables added
dsshim Jul 17, 2015
a11835c
os test
dsshim Jul 17, 2015
3d7327b
Merge pull request #36 from mitchashby16/os_tables
noobjey Jul 17, 2015
ec947b6
events table
mitcha16 Jul 17, 2015
73a7395
using registration handler
noobjey Jul 17, 2015
f7cebc4
fixed merge conflict
mitcha16 Jul 17, 2015
a649c31
Merge pull request #37 from mitchashby16/events_table
dsshim Jul 17, 2015
b115f9f
Merge branch 'master' into registration_handler
noobjey Jul 17, 2015
1bef3a2
Merge pull request #38 from mitchashby16/registration_handler
noobjey Jul 17, 2015
0bf5e32
data processing handler test base
noobjey Jul 17, 2015
132eb15
registration handler model tests complete
noobjey Jul 17, 2015
a179f4c
added most visited urls
dsshim Jul 18, 2015
98cb015
Merge pull request #39 from mitchashby16/app_details
mitcha16 Jul 18, 2015
147e7b7
fixed url error message
dsshim Jul 18, 2015
e831cee
urls route
mitcha16 Jul 18, 2015
75658f4
Merge pull request #40 from mitchashby16/url_error
mitcha16 Jul 18, 2015
cbc3946
urls path
mitcha16 Jul 18, 2015
27dd012
urls path
mitcha16 Jul 18, 2015
3e1d85b
work in progress
mitcha16 Jul 18, 2015
87fc4ba
working on link data
dsshim Jul 18, 2015
0634a5e
Merge pull request #41 from mitchashby16/link_stats
mitcha16 Jul 18, 2015
3f4ee5c
urls
mitcha16 Jul 18, 2015
8cb56cb
urls
mitcha16 Jul 18, 2015
41a25d9
merged urls statistics
mitcha16 Jul 18, 2015
246f7cd
Merge pull request #42 from mitchashby16/urls_statistics
dsshim Jul 18, 2015
d9d8d32
payload parser returns nil when there is no payload
noobjey Jul 18, 2015
b49b07c
fixed bug and changed tests to catch it
noobjey Jul 18, 2015
38a03ad
refacorted for URL parser
mitcha16 Jul 18, 2015
644bbc0
Merge pull request #43 from mitchashby16/urls_statistics
dsshim Jul 18, 2015
a8f1c73
links parser working
dsshim Jul 18, 2015
1e2e425
Merge pull request #44 from mitchashby16/link_parsing
mitcha16 Jul 18, 2015
0691225
fixed links
dsshim Jul 18, 2015
29aa574
merging
mitcha16 Jul 18, 2015
5368de2
url parser added
mitcha16 Jul 18, 2015
4dda11c
data processing handler tests
noobjey Jul 18, 2015
783738b
Merge branch 'master' into handler_model_tests
noobjey Jul 18, 2015
7f24769
uncomment tests
noobjey Jul 18, 2015
d9367ee
all data processing tests use same input payload
noobjey Jul 18, 2015
f837d8c
Merge pull request #45 from mitchashby16/handler_model_tests
mitcha16 Jul 18, 2015
21a42db
merged jason's changes
mitcha16 Jul 18, 2015
8470cd5
added feature tests
dsshim Jul 18, 2015
c250dac
finished handler tests
mitcha16 Jul 18, 2015
52849ef
added readme
noobjey Jul 18, 2015
bc13698
Merge branch 'master' of github.com:mitchashby16/traffic_spy
noobjey Jul 18, 2015
1437ccb
Merge pull request #46 from mitchashby16/feature_tests
mitcha16 Jul 18, 2015
1e59c8c
Merge branch 'master' of github.com:mitchashby16/traffic_spy
noobjey Jul 18, 2015
96374c4
Merge pull request #47 from mitchashby16/tests
dsshim Jul 18, 2015
8b2c97e
changed method name
noobjey Jul 18, 2015
bde198c
change data processor to return success response body to whats requir…
noobjey Jul 18, 2015
8e026d3
feature tests for app data and sources
dsshim Jul 18, 2015
5f6979a
Merge pull request #48 from mitchashby16/fix_200_message_text
dsshim Jul 18, 2015
2be2aec
small refactorings
noobjey Jul 18, 2015
23bba07
removed unneeded spaces
noobjey Jul 18, 2015
72a8a59
Merge pull request #49 from mitchashby16/tests_and_test
mitcha16 Jul 18, 2015
8cb919d
work in progess url statistics
mitcha16 Jul 19, 2015
495d60b
fixed merge conflicts
mitcha16 Jul 19, 2015
cb64596
Merge pull request #50 from mitchashby16/tests
dsshim Jul 19, 2015
cf0804e
removed unused code
noobjey Jul 19, 2015
509d6af
made feature tests clean database between each test
noobjey Jul 19, 2015
dcff927
added events page, no stats yet
noobjey Jul 19, 2015
95247d5
fixed merge conflicts
noobjey Jul 19, 2015
b9db88e
Merge pull request #51 from mitchashby16/events
mitcha16 Jul 19, 2015
7c06c1b
fixing merge
mitcha16 Jul 19, 2015
ac150f5
fixing merge
mitcha16 Jul 19, 2015
8054f3f
Merge pull request #52 from mitchashby16/finishing_url_statistics
dsshim Jul 19, 2015
4238977
css, views and refactored app data
dsshim Jul 19, 2015
5fcd8ba
merge conflicts resolved
dsshim Jul 19, 2015
8bcdf69
refactored app stats and css
dsshim Jul 19, 2015
8143907
Merge pull request #53 from mitchashby16/css
noobjey Jul 19, 2015
cce6b5d
users can click on an event to goto its details
noobjey Jul 19, 2015
6ae6129
fixed urls and events table
mitcha16 Jul 19, 2015
dbb95f0
Merge pull request #54 from mitchashby16/finishing_url_statistics
dsshim Jul 19, 2015
84b5511
fixed registration
mitcha16 Jul 19, 2015
9777def
Merge pull request #55 from mitchashby16/finishing_url_statistics
dsshim Jul 19, 2015
1b2ca30
controller test for events
noobjey Jul 19, 2015
7bb6e05
fixed tests after db changes
dsshim Jul 19, 2015
9935189
Merge pull request #56 from mitchashby16/fixed_tests
mitcha16 Jul 19, 2015
474523e
finished calculator test
mitcha16 Jul 19, 2015
9ffefd1
feature tests for urls
mitcha16 Jul 19, 2015
6bc746d
Merge pull request #57 from mitchashby16/final_url_stats
dsshim Jul 19, 2015
ee48835
displays message when no events
noobjey Jul 19, 2015
86999aa
fixed bug with not storing everything to same table row
noobjey Jul 19, 2015
789e2a5
Merge pull request #58 from mitchashby16/payload_storage_fixed
dsshim Jul 19, 2015
3d77815
fixed repeat links
mitcha16 Jul 19, 2015
a3ff55f
Merge pull request #59 from mitchashby16/final_url_stats
dsshim Jul 19, 2015
c70eba4
Merge branch 'master' into events
noobjey Jul 19, 2015
a2f34b2
all tests working
mitcha16 Jul 19, 2015
e37eac9
fixed bug with events lookup and creation of events for tests
noobjey Jul 19, 2015
53e9ced
Merge pull request #60 from mitchashby16/most_user_events
dsshim Jul 19, 2015
0ae580f
Merge pull request #61 from mitchashby16/events
dsshim Jul 19, 2015
df375ad
active record fix
dsshim Jul 20, 2015
44e7cae
Merge pull request #62 from mitchashby16/active_record_methods_fix
noobjey Jul 20, 2015
6dee1c3
event showing with count and no event message working
noobjey Jul 20, 2015
ec5606e
Merge branch 'master' into event
noobjey Jul 20, 2015
ed2cfe0
Merge pull request #63 from mitchashby16/event
dsshim Jul 20, 2015
696867e
view event by hour
noobjey Jul 20, 2015
88ddd93
Merge pull request #64 from mitchashby16/event_by_time
dsshim Jul 20, 2015
889b4f2
finished
mitcha16 Jul 20, 2015
1d6a44f
Merge pull request #65 from mitchashby16/finishing
dsshim Jul 20, 2015
9393048
fixed tests
mitcha16 Jul 20, 2015
6ea5a63
Merge pull request #66 from mitchashby16/finishing
dsshim Jul 20, 2015
94e7bdb
remove uneeded test
noobjey Jul 20, 2015
8221016
added partial
noobjey Jul 20, 2015
00c25f8
event using partial
noobjey Jul 20, 2015
706db9c
changed to match other styling
noobjey Jul 20, 2015
e344b8f
Merge pull request #67 from mitchashby16/events_partial
dsshim Jul 20, 2015
2a7788c
final style
dsshim Jul 20, 2015
6fcb0f5
final style
dsshim Jul 20, 2015
518de1c
final styling
dsshim Jul 20, 2015
300610b
Merge pull request #68 from mitchashby16/final_style
mitcha16 Jul 20, 2015
329f014
fixed line lenths above 80
noobjey Jul 20, 2015
730905a
partail for event
noobjey Jul 20, 2015
61e2ee7
final tests and css
dsshim Jul 20, 2015
8a33503
Merge pull request #69 from mitchashby16/final_style
mitcha16 Jul 20, 2015
ddd7696
partials
dsshim Jul 20, 2015
c5f81d2
Merge pull request #70 from mitchashby16/partials
mitcha16 Jul 20, 2015
d6224c3
Merge pull request #71 from mitchashby16/fix_line_length
mitcha16 Jul 20, 2015
8fd5e13
event partial working
noobjey Jul 20, 2015
c43e889
Merge pull request #72 from mitchashby16/event_partial
mitcha16 Jul 20, 2015
3744e73
fixed tests final
dsshim Jul 20, 2015
97bfd43
Merge pull request #73 from mitchashby16/fixed_tests
mitcha16 Jul 20, 2015
6bcf1dc
pulled out shared test code
noobjey Jul 20, 2015
c692a1a
Merge branch 'master' into refactor_test
noobjey Jul 20, 2015
d318152
typo
noobjey Jul 20, 2015
9577d9f
Merge pull request #74 from mitchashby16/refactor_test
mitcha16 Jul 20, 2015
f334a46
fixed bug with event not found message
noobjey Jul 20, 2015
b7b6fae
Merge branch 'master' into event_view
noobjey Jul 20, 2015
9edb96c
Merge pull request #75 from mitchashby16/event_fix
mitcha16 Jul 20, 2015
75fe907
took pry out of prod deploy
noobjey Jul 20, 2015
0433238
Merge pull request #76 from mitchashby16/gemfix
mitcha16 Jul 20, 2015
accf496
line length
noobjey Jul 20, 2015
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ gem 'sinatra', require: 'sinatra/base'
gem 'pg'
gem 'activerecord'
gem 'sinatra-activerecord'
gem 'json'
gem 'useragent'

group :development, :test do
gem 'shotgun'
Expand All @@ -12,6 +14,10 @@ group :development, :test do
gem 'capybara'
gem 'launchy'
gem 'rack-test'
gem 'pry'
gem 'database_cleaner'
gem 'guard'
gem 'guard-rake'
end

group :evaluations do
Expand Down
14 changes: 10 additions & 4 deletions Guardfile
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
# A sample Guardfile
# More info at https://github.com/guard/guard#readme

guard 'rspec' do
watch(%r{^spec/.+_spec\.rb$})
watch(%r{^app/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
watch('spec/spec_helper.rb') { "spec" }
# guard 'rspec' do
# watch(%r{^spec/.+_spec\.rb$})
# watch(%r{^app/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
# watch('spec/spec_helper.rb') { "spec" }
# end

guard 'rake', :task => 'test' do
watch(%r{^test/.+_test\.rb$})
watch(%r{^app/(.+)\.rb$})
watch('test/test_helper.rb') { "test" }
end
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Traffic Spy

In this project you'll use Ruby, Sinatra, and ActiveRecord to build a web traffic tracking and analysis tool.

Your application will be live on the Internet. It will recieve data over HTTP from a simulation engine. The simulator will construct and transmit HTTP requests which include tracking data.

Imagine that I run a commercial website and embed JavaScript code which gets activated each time a page is viewed on my site. That JavaScript captures information about the visitor and the page they're viewing then, in the background, submits that data to your TrafficSpy application.

Your application parses and stores that data.

Later, I visit your site and can view data about my traffic through a HTML interface.

https://github.com/JumpstartLab/curriculum/blob/5cd41b3635cc4d32431032d42bcfc9f7da3135d5/source/projects/traffic_spy.markdown
11 changes: 9 additions & 2 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,19 @@ Bundler.require

require "sinatra/activerecord/rake"

require "rake/testtask"

Rake::TestTask.new do |t|
t.test_files = FileList['test/**/*_test.rb']
end


namespace :sanitation do
desc "Check line lengths & whitespace with Cane"
task :lines do
puts ""
puts "== using cane to check line length =="
system("cane --no-abc --style-glob 'lib/**/*.rb' --no-doc")
system("cane --no-abc --style-glob 'app/**/*.rb' --no-doc")
puts "== done checking line length =="
puts ""
end
Expand All @@ -17,7 +24,7 @@ namespace :sanitation do
task :methods do
puts ""
puts "== using reek to check method length =="
system("reek -n lib/**/*.rb 2>&1 | grep -v ' 0 warnings'")
system("reek -n app/**/*.rb 2>&1 | grep -v ' 0 warnings'")
puts "== done checking method length =="
puts ""
end
Expand Down
85 changes: 85 additions & 0 deletions app/controllers/server.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,96 @@
require 'json'
require 'uri'
module TrafficSpy

class Server < Sinatra::Base
get '/' do
@identifier = params[:identifier]
erb :index
end


get '/sources' do
erb :new
end

get '/sources/:identifier' do
app_handler = AppDataHandler.new(params[:identifier])
if app_handler.registration.nil?
@message = app_handler.message
erb app_handler.erb
else
@urls = app_handler.url_stats
@browsers = app_handler.browser_stats
@os = app_handler.os_stats
@resolutions = app_handler.resolution_stats
@avg_response_times = app_handler.response_times
@links = app_handler.link_list
@link_paths = app_handler.link_paths
erb :identifier_index
end
end

not_found do
erb :error
end

post '/sources' do
sources_handler = RegistrationHandler.new(params)
status sources_handler.status
body sources_handler.body
end

post "/sources/:identifier/data" do |identifier|
data_handler = DataProcessingHandler.new(params, identifier)
status data_handler.status
body data_handler.body
end


get '/sources/:identifier/urls/:path' do |identifier, path|
@registration = Registration.find_by(:identifier => params[:identifier])
urls_handler = UrlStatisticsHandler.new(identifier, path)
@url = urls_handler.url
@message = urls_handler.message
erb urls_handler.erb
end


get '/sources/:identifier/events' do |identifier|
registration = Registration.find_by(:identifier => identifier)

if registration.nil?
redirect '/not_found'
else
@events_by_popularity = registration.events.group(:name)
.order('count_name desc')
.count(:name)
erb :events
end

end


get '/sources/:identifier/events/:event_name' do |identifier, event_name|
@identifier = identifier
registration = Registration.find_by(:identifier => @identifier)

if registration.nil?
redirect '/not_found'
else
@event_name = event_name
@event_total = registration.events.where(:name => @event_name)
.group(:name)
.count(:name)
@event_by_times = registration.events.where(:name => @event_name)
.group(:requested_at)
.order('requested_at asc')
.count('requested_at')

erb :event
end

end

end
end
Empty file removed app/models/.keep
Empty file.
72 changes: 72 additions & 0 deletions app/models/app_statistics_handler.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
class AppDataHandler

attr_reader :registration, :identifier, :message, :erb

def initialize(identifier)
@identifier = identifier
@registration = Registration.find_by(:identifier => identifier)
check_registration
end

def check_registration
@message = "The #{identifier} identifier does not exist"
@erb = :identifier_error
end

def url_stats
urls = registration.urls.group(:url).count
urls.map do |key, value|
if !key.nil?
[value, key]
end
end.uniq.compact.sort.reverse
end


def browser_stats
registration.browsers.map do |key, value|
if !key.nil?
[value, key[:name]]
end
end.compact.sort.reverse
end

def os_stats
registration.operating_systems.map do |key, value|
if !key.nil?
[value, key[:name]]
end
end.compact.sort.reverse
end

def resolution_stats
registration.screen_resolutions.map do |key, value|
if !key.nil?
[value, key[:width],key[:height]]
end
end.compact.sort.reverse
end

def response_times
res_times = registration.urls.group(:url).average(:responded_in)
res_times.map do |key, value|
if !key.nil?
[value,key]
end
end.sort.reverse
end

def link_list
@links = registration.urls.map do |key, value|
if !key.nil?
[key[:url]]
end
end.compact
end

def link_paths
@links.map do |link|
URI(link.join).path
end.uniq
end
end
3 changes: 3 additions & 0 deletions app/models/browser.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Browser < ActiveRecord::Base
has_many :payloads
end
63 changes: 63 additions & 0 deletions app/models/data_processing_handler.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
class DataProcessingHandler
attr_reader :payload,
:body,
:status,
:parser

def initialize(raw_payload, identifier)
@parser = PayloadParser.new(raw_payload)
@payload = @parser.payload
@status = 0
@body = ''
registration = Registration.find_by(identifier: identifier)
process(registration, @payload)
end

def process(registration, payload)

if registration.nil?
@status = 403
@body = 'Application Not Registered - 403 Forbidden'
elsif payload.nil?
@status = 400
@body = 'Missing Payload - 400 Bad Request'
else

current_sha = Digest::SHA1.hexdigest(payload.to_s)

if Payload.exists?(payload_sha: current_sha)
@status = 403
@body = 'Already Received Request - 403 Forbidden'
else
store_payload(registration)
store_payload_sha(current_sha, registration)
@status = 200
@body = 'Success - 200 OK'
end

end
end

private

def store_payload_sha(current_sha, registration)
payload = registration.payloads.last
payload.update(payload_sha: current_sha)
end

def store_payload(registration)
registration.payloads.create(
url:
Url.find_or_create_by(parser.url),
screen_resolution:
ScreenResolution.find_or_create_by(parser.screen_resolution),
browser:
Browser.find_or_create_by(parser.browser),
event:
Event.find_or_create_by(parser.event),
operating_system:
OperatingSystem.find_or_create_by(parser.operating_system)
)
end

end
3 changes: 3 additions & 0 deletions app/models/event.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Event < ActiveRecord::Base
has_many :payloads
end
3 changes: 3 additions & 0 deletions app/models/operating_system.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class OperatingSystem < ActiveRecord::Base
has_many :payloads
end
9 changes: 9 additions & 0 deletions app/models/payload.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class Payload < ActiveRecord::Base
belongs_to :url
belongs_to :user_agent
belongs_to :registration
belongs_to :screen_resolution
belongs_to :browser
belongs_to :event
belongs_to :operating_system
end