Commit 2085d8a
Changed files (23)
app
assets
javascripts
stylesheets
controllers
helpers
views
workers
config
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/
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/
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
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
app/helpers/agents_helper.rb
@@ -0,0 +1,2 @@
+module AgentsHelper
+end
app/models/agent.rb
@@ -0,0 +1,3 @@
+class Agent < ActiveRecord::Base
+ has_many :events
+end
app/models/event.rb
@@ -1,2 +1,3 @@
class Event < ActiveRecord::Base
+ belongs_to :agent
end
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 %>
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 %>
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 %>
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
app/views/agents/new.html.erb
@@ -0,0 +1,5 @@
+<h1>New Agent</h1>
+
+<%= render 'form' %>
+
+<%= link_to 'Back', agents_path %>
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 %>
app/views/agents/show.json.jbuilder
@@ -0,0 +1,1 @@
+json.extract! @agent, :id, :hostname, :created_at, :updated_at
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>
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) %>
app/views/events/new.html.erb
@@ -2,4 +2,4 @@
<%= render 'form' %>
-<%= link_to 'Back', events_path %>
+<%= link_to 'Back', agent_events_path(@agent) %>
app/workers/events_worker.rb → app/workers/event_intake.rb
@@ -1,6 +1,6 @@
require 'json'
-class EventsWorker
+class EventIntake
include Sneakers::Worker
from_queue "worker.events"
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
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
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
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
Procfile
@@ -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