diff options
23 files changed, 204 insertions, 10 deletions
@@ -1,3 +1,3 @@ web: rails s -event_intake: env WORKERS=EventsWorker rake sneakers:run +event_intake: env WORKERS=EventIntake rake sneakers:run poke: env WORKERS=Poke rake sneakers:run diff --git a/app/assets/javascripts/agents.coffee b/app/assets/javascripts/agents.coffee new file mode 100644 index 0000000..24f83d1 --- /dev/null +++ b/app/assets/javascripts/agents.coffee @@ -0,0 +1,3 @@ +# Place all the behaviors and hooks related to the matching controller here. +# All this logic will automatically be available in application.js. +# You can use CoffeeScript in this file: http://coffeescript.org/ diff --git a/app/assets/stylesheets/agents.scss b/app/assets/stylesheets/agents.scss new file mode 100644 index 0000000..e5f96f6 --- /dev/null +++ b/app/assets/stylesheets/agents.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the Agents controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: http://sass-lang.com/ diff --git a/app/controllers/agents_controller.rb b/app/controllers/agents_controller.rb new file mode 100644 index 0000000..a76a7ae --- /dev/null +++ b/app/controllers/agents_controller.rb @@ -0,0 +1,74 @@ +class AgentsController < ApplicationController + before_action :set_agent, only: [:show, :edit, :update, :destroy] + + # GET /agents + # GET /agents.json + def index + @agents = Agent.all + end + + # GET /agents/1 + # GET /agents/1.json + def show + end + + # GET /agents/new + def new + @agent = Agent.new + end + + # GET /agents/1/edit + def edit + end + + # POST /agents + # POST /agents.json + def create + @agent = Agent.new(agent_params) + + respond_to do |format| + if @agent.save + format.html { redirect_to @agent, notice: 'Agent was successfully created.' } + format.json { render :show, status: :created, location: @agent } + else + format.html { render :new } + format.json { render json: @agent.errors, status: :unprocessable_entity } + end + end + end + + # PATCH/PUT /agents/1 + # PATCH/PUT /agents/1.json + def update + respond_to do |format| + if @agent.update(agent_params) + format.html { redirect_to @agent, notice: 'Agent was successfully updated.' } + format.json { render :show, status: :ok, location: @agent } + else + format.html { render :edit } + format.json { render json: @agent.errors, status: :unprocessable_entity } + end + end + end + + # DELETE /agents/1 + # DELETE /agents/1.json + def destroy + @agent.destroy + respond_to do |format| + format.html { redirect_to agents_url, notice: 'Agent was successfully destroyed.' } + format.json { head :no_content } + end + end + + private + # Use callbacks to share common setup or constraints between actions. + def set_agent + @agent = Agent.find(params[:id]) + end + + # Never trust parameters from the scary internet, only allow the white list through. + def agent_params + params.require(:agent).permit(:hostname) + end +end diff --git a/app/controllers/events_controller.rb b/app/controllers/events_controller.rb index 96a51f0..6a8fe42 100644 --- a/app/controllers/events_controller.rb +++ b/app/controllers/events_controller.rb @@ -1,4 +1,6 @@ class EventsController < ApplicationController + before_action :load_agent + def index @events = Event.all end @@ -8,8 +10,8 @@ class EventsController < ApplicationController end def create - Publisher.publish("events", event_params) - redirect_to events_path, notice: 'Event was successfully created.' + Publisher.publish("events", event_params.merge({agent_id: @agent.id})) + redirect_to agent_events_path, notice: 'Event was successfully created.' end def destroy @@ -18,7 +20,13 @@ class EventsController < ApplicationController redirect_to events_url, notice: 'Event was successfully destroyed.' end + private + def event_params params.require(:event).permit(:name, :data) end + + def load_agent + @agent = Agent.find(params[:agent_id]) + end end diff --git a/app/helpers/agents_helper.rb b/app/helpers/agents_helper.rb new file mode 100644 index 0000000..7e54438 --- /dev/null +++ b/app/helpers/agents_helper.rb @@ -0,0 +1,2 @@ +module AgentsHelper +end diff --git a/app/models/agent.rb b/app/models/agent.rb new file mode 100644 index 0000000..e5832da --- /dev/null +++ b/app/models/agent.rb @@ -0,0 +1,3 @@ +class Agent < ActiveRecord::Base + has_many :events +end diff --git a/app/models/event.rb b/app/models/event.rb index 3a829fd..3e55383 100644 --- a/app/models/event.rb +++ b/app/models/event.rb @@ -1,2 +1,3 @@ class Event < ActiveRecord::Base + belongs_to :agent end diff --git a/app/views/agents/_form.html.erb b/app/views/agents/_form.html.erb new file mode 100644 index 0000000..fe9d10e --- /dev/null +++ b/app/views/agents/_form.html.erb @@ -0,0 +1,21 @@ +<%= form_for(@agent) do |f| %> + <% if @agent.errors.any? %> + <div id="error_explanation"> + <h2><%= pluralize(@agent.errors.count, "error") %> prohibited this agent from being saved:</h2> + + <ul> + <% @agent.errors.full_messages.each do |message| %> + <li><%= message %></li> + <% end %> + </ul> + </div> + <% end %> + + <div class="field"> + <%= f.label :hostname %><br> + <%= f.text_field :hostname %> + </div> + <div class="actions"> + <%= f.submit %> + </div> +<% end %> diff --git a/app/views/agents/edit.html.erb b/app/views/agents/edit.html.erb new file mode 100644 index 0000000..bed9b23 --- /dev/null +++ b/app/views/agents/edit.html.erb @@ -0,0 +1,6 @@ +<h1>Editing Agent</h1> + +<%= render 'form' %> + +<%= link_to 'Show', @agent %> | +<%= link_to 'Back', agents_path %> diff --git a/app/views/agents/index.html.erb b/app/views/agents/index.html.erb new file mode 100644 index 0000000..48a36e2 --- /dev/null +++ b/app/views/agents/index.html.erb @@ -0,0 +1,28 @@ +<p id="notice"><%= notice %></p> + +<h1>Listing Agents</h1> + +<table> + <thead> + <tr> + <th>Hostname</th> + <th colspan="4"></th> + </tr> + </thead> + + <tbody> + <% @agents.each do |agent| %> + <tr> + <td><%= agent.hostname %></td> + <td><%= link_to 'Events', agent_events_path(agent) %></td> + <td><%= link_to 'Show', agent %></td> + <td><%= link_to 'Edit', edit_agent_path(agent) %></td> + <td><%= link_to 'Destroy', agent, method: :delete, data: { confirm: 'Are you sure?' } %></td> + </tr> + <% end %> + </tbody> +</table> + +<br> + +<%= link_to 'New Agent', new_agent_path %> diff --git a/app/views/agents/index.json.jbuilder b/app/views/agents/index.json.jbuilder new file mode 100644 index 0000000..65f6f60 --- /dev/null +++ b/app/views/agents/index.json.jbuilder @@ -0,0 +1,4 @@ +json.array!(@agents) do |agent| + json.extract! agent, :id, :hostname + json.url agent_url(agent, format: :json) +end diff --git a/app/views/agents/new.html.erb b/app/views/agents/new.html.erb new file mode 100644 index 0000000..782236f --- /dev/null +++ b/app/views/agents/new.html.erb @@ -0,0 +1,5 @@ +<h1>New Agent</h1> + +<%= render 'form' %> + +<%= link_to 'Back', agents_path %> diff --git a/app/views/agents/show.html.erb b/app/views/agents/show.html.erb new file mode 100644 index 0000000..0e7a061 --- /dev/null +++ b/app/views/agents/show.html.erb @@ -0,0 +1,9 @@ +<p id="notice"><%= notice %></p> + +<p> + <strong>Hostname:</strong> + <%= @agent.hostname %> +</p> + +<%= link_to 'Edit', edit_agent_path(@agent) %> | +<%= link_to 'Back', agents_path %> diff --git a/app/views/agents/show.json.jbuilder b/app/views/agents/show.json.jbuilder new file mode 100644 index 0000000..f156cb0 --- /dev/null +++ b/app/views/agents/show.json.jbuilder @@ -0,0 +1 @@ +json.extract! @agent, :id, :hostname, :created_at, :updated_at diff --git a/app/views/events/_form.html.erb b/app/views/events/_form.html.erb index a803510..2215f6c 100644 --- a/app/views/events/_form.html.erb +++ b/app/views/events/_form.html.erb @@ -1,4 +1,4 @@ -<%= form_for(@event) do |f| %> +<%= form_for([@agent, @event]) do |f| %> <% if @event.errors.any? %> <div id="error_explanation"> <h2><%= pluralize(@event.errors.count, "error") %> prohibited this event from being saved:</h2> diff --git a/app/views/events/index.html.erb b/app/views/events/index.html.erb index 02fb2cb..bf0480c 100644 --- a/app/views/events/index.html.erb +++ b/app/views/events/index.html.erb @@ -18,7 +18,7 @@ <td><%= event.name %></td> <td><%= event.data %></td> <td><%= event.created_at %></td> - <td><%= link_to 'Destroy', event, method: :delete, data: { confirm: 'Are you sure?' } %></td> + <td><%= link_to 'Destroy', [@agent, event], method: :delete, data: { confirm: 'Are you sure?' } %></td> </tr> <% end %> </tbody> @@ -26,4 +26,4 @@ <br> -<%= link_to 'New Event', new_event_path %> +<%= link_to 'New Event', new_agent_event_path(@agent) %> diff --git a/app/views/events/new.html.erb b/app/views/events/new.html.erb index 86707c1..1c0b206 100644 --- a/app/views/events/new.html.erb +++ b/app/views/events/new.html.erb @@ -2,4 +2,4 @@ <%= render 'form' %> -<%= link_to 'Back', events_path %> +<%= link_to 'Back', agent_events_path(@agent) %> diff --git a/app/workers/events_worker.rb b/app/workers/event_intake.rb index dc85009..7e70497 100644 --- a/app/workers/events_worker.rb +++ b/app/workers/event_intake.rb @@ -1,6 +1,6 @@ require 'json' -class EventsWorker +class EventIntake include Sneakers::Worker from_queue "worker.events" diff --git a/config/routes.rb b/config/routes.rb index c6f3589..e83648b 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,5 +1,8 @@ Rails.application.routes.draw do + resources :agents do + resources :events, only: [:index, :new, :create, :destroy] + end + resources :dispositions - resources :events, only: [:index, :new, :create, :destroy] root 'events#index' end diff --git a/db/migrate/20150204041713_create_agents.rb b/db/migrate/20150204041713_create_agents.rb new file mode 100644 index 0000000..151d745 --- /dev/null +++ b/db/migrate/20150204041713_create_agents.rb @@ -0,0 +1,9 @@ +class CreateAgents < ActiveRecord::Migration + def change + create_table :agents, id: :uuid, default: 'uuid_generate_v4()' do |t| + t.string :hostname + + t.timestamps null: false + end + end +end diff --git a/db/migrate/20150204042612_add_agent_id_to_events.rb b/db/migrate/20150204042612_add_agent_id_to_events.rb new file mode 100644 index 0000000..40b4125 --- /dev/null +++ b/db/migrate/20150204042612_add_agent_id_to_events.rb @@ -0,0 +1,5 @@ +class AddAgentIdToEvents < ActiveRecord::Migration + def change + add_reference :events, :agent, index: true + end +end diff --git a/db/schema.rb b/db/schema.rb index 1c756d5..1011d84 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,12 +11,18 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20150204033540) do +ActiveRecord::Schema.define(version: 20150204042612) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" enable_extension "uuid-ossp" + create_table "agents", id: :uuid, default: "uuid_generate_v4()", force: :cascade do |t| + t.string "hostname" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + create_table "dispositions", id: :uuid, default: "uuid_generate_v4()", force: :cascade do |t| t.string "fingerprint" t.integer "state" @@ -29,6 +35,9 @@ ActiveRecord::Schema.define(version: 20150204033540) do t.json "data" t.datetime "created_at", null: false t.datetime "updated_at", null: false + t.integer "agent_id" end + add_index "events", ["agent_id"], name: "index_events_on_agent_id", using: :btree + end |
