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

Optimizations #517

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 0 additions & 18 deletions app/helpers/users_helper.rb

This file was deleted.

2 changes: 1 addition & 1 deletion app/models/event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class Event

belongs_to :classification

property :timestamp, ZonedTime, :index => :index_timestamp_cid_sid
property :timestamp, ZonedTime, :index => [ :index_timestamp_cid_sid, :timestamp ]

has n, :favorites, :parent_key => [ :sid, :cid ],
:child_key => [ :sid, :cid ], :constraint => :destroy!
Expand Down
14 changes: 1 addition & 13 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,6 @@ class User
# Define if the user has been enabled/disabled
property :enabled, Boolean, :default => true

# Define if get avatar from gravatar.com or not
property :gravatar, Boolean, :default => true

# Define created_at and updated_at timestamps
timestamps :at
property :created_at, ZonedTime
Expand Down Expand Up @@ -117,21 +114,12 @@ def to_s

def avatar
default_url = File.join(::User.snorby_url, "#{Snorby::CONFIG[:baseuri]}/images/default_avatar.png")
return default_url unless self.gravatar

email_address = self.email.downcase

# create the md5 hash
hash = Digest::MD5.hexdigest(email_address)
"https://gravatar.com/avatar/#{hash}.png?s=256&d=#{CGI.escape(default_url)}"
default_url
end

def in_json
# create the md5 hash
hash = Digest::MD5.hexdigest(self.email)
#"https://gravatar.com/avatar/#{hash}.png?s=256&d=#{CGI.escape(default_url)}"
data = self.attributes
data[:gravatar_hash] = hash
data[:classify_count] = classify_count
data
end
Expand Down
48 changes: 20 additions & 28 deletions app/views/events/_signature.html.erb
Original file line number Diff line number Diff line change
@@ -1,48 +1,40 @@
<div class='section-title plus'>Signature Information</div>

<div class='signature-table plus'>

<table class='default' border='0' cellspacing='0' cellpadding='0'>
<tr>
<th class='first'>Generator ID</th>
<th>Sig. ID</th>
<table class='default' border='0' cellspacing='0' cellpadding='0'>
<tr>
<th class='first'>Generator ID</th>
<th>Sig. ID</th>
<th>Sig. Revision</th>

<% total_event_count ||= Event.count %>
<th>Activity (<%= @event.signature.event_percentage(true, total_event_count) %>)</th>

<th>Category</th>

<th>Sig Info</th>
<th class='last'></th>
</tr>
<tbody>
<tr>
<td class='first'><%= @event.signature.sig_gid || 'N/A' %></td>
<td><%= @event.signature.sig_sid || 'N/A' %></td>
<td><%= @event.signature.sig_rev || 'N/A' %></td>

<th class='last'></th>
</tr>
<tbody>
<tr>
<td class='first'><%= @event.signature.sig_gid || 'N/A' %></td>

<td class='search-by-signature add_tipsy'
title="View all '<%= @event.signature.name%>' events."
data-url="/results?title=<%= @event.signature.name %>&search%5B0%5D%5Bcolumn%5D=signature&search%5B0%5D%5Benabled%5D=true&search%5B0%5D%5Boperator%5D=is&search%5B0%5D%5Bvalue%5D=<%= @event.signature.sig_id %>">
<%= css_chart(@event.signature.event_percentage(false, total_event_count)) %>
title="View all '<%= @event.signature.name%>' events."
data-url="/results?title=<%= @event.signature.name %>&search%5B0%5D%5Bcolumn%5D=signature&search%5B0%5D%5Benabled%5D=true&search%5B0%5D%5Boperator%5D=is&search%5B0%5D%5Bvalue%5D=<%= @event.signature.sig_id %>">
<%= @event.signature.sig_sid || 'N/A' %>
</td>

<td><%= @event.signature.sig_rev || 'N/A' %></td>

<td>
<%= @event.signature.category.present? ? @event.signature.category.sig_class_name : 'N/A' %>
</td>

<td style='width:190px;' class=''>
<%= link_to "Query Signature Database",
<%= link_to "Query Signature Database",
@event.signature_url, :target => '_blank' %>
</td>

<td style='width:90px;' class='last'>
<%= link_to "View Rule", rule_events_path(:sid => @event.sid, :cid => @event.cid), :class => 'snorbybox', :target => '_blank' %>
<%= link_to "View Rule", rule_events_path(:sid => @event.sid, :cid => @event.cid), :class => 'snorbybox', :target => '_blank' %>
</td>

</tr>
</tbody>
</table>

</tr>
</tbody>
</table>
</div>
5 changes: 0 additions & 5 deletions app/views/notes/_add_note.html.erb
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
<div id='new_note_box' class='event-note round'>

<div class='note-avatar-holder'>
<img src="<%= get_gravatar_uri(@user.email) %>" height="32px" width="32px"
onerror="this.src=baseuri+'/images/default_avatar.png'">
</div>

<div class='username'>
<span><%= @user %></span>
<i><%= time_ago_in_words(Time.now) %></i>
Expand Down
5 changes: 0 additions & 5 deletions app/views/notes/_note.html.erb
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
<div id='note_<%= note.id %>' class='event-note round'>

<div class='note-avatar-holder'>
<img src="<%= get_gravatar_uri(note.user.email) %>" height="32px" width="32px"
onerror="this.src=baseuri+'/images/default_avatar.png'">
</div>

<% if current_user.admin %>

<div class='note-options'>
Expand Down
1 change: 0 additions & 1 deletion app/views/page/dashboard.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,6 @@
<ul class='box-list'>
<% @favers.each do |user| %>
<li class='has-image'>
<%= image_tag(get_gravatar_uri(user.email), :size => '16x16', :class => 'avatar-small') %>
<%= link_to truncate(user.name, :length => 30), results_path(:title => "#{truncate("Events Classified By " + user.name.to_s, :length => 60)}",
"match_all"=>"true", "search"=>{"user"=>{"column"=>"user", "operator"=>"is", "value"=> user.id } }), :title => user.name %>
<span><%= number_with_delimiter user.classify_count %></span>
Expand Down
2 changes: 1 addition & 1 deletion app/views/users/_user.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

<% end %>

<td class='name' data-user-id='<%= user.id %>'><%= image_tag(get_gravatar_uri(user.email), :size => '16x16', :class => 'avatar-small') %> <%= user.name %></td>
<td class='name' data-user-id='<%= user.id %>'><%= user.name %></td>
<td class='email'><%= user.email %></td>
<td class='email'><%= number_with_delimiter user.sign_in_count %></td>
<td class='last_sign_in_ip'><%= !user.last_sign_in_ip.blank? ? user.last_sign_in_ip : 'N/A' %></td>
Expand Down
6 changes: 0 additions & 6 deletions app/views/users/new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@

<div class='user-show-avatar'>

<%= image_tag(get_gravatar_uri(@user.email), :class => 'avatar') %>

<p><strong>Login Count:</strong> <%= @user.sign_in_count %></p>

<p><strong>Current Login IP:</strong> <%= @user.current_sign_in_ip || 'N/A' %></p>
Expand All @@ -59,10 +57,6 @@

</div>

<p>
<em>(To change/add a avatar please visit <a href="http://gravatar.com">http://gravatar.com</a>)</em>
</p>

<p>
<%= f.label 'Time Zone:' %>
<%= f.time_zone_select :timezone, ActiveSupport::TimeZone.us_zones %>
Expand Down
6 changes: 0 additions & 6 deletions app/views/users/registrations/edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@

<div class='user-show-avatar'>

<%= image_tag(get_gravatar_uri(@user.email), :class => 'avatar') %>

<p><strong>Login Count:</strong> <%= @user.sign_in_count %></p>

<p><strong>Current Login IP:</strong> <%= @user.current_sign_in_ip || 'N/A' %></p>
Expand All @@ -67,10 +65,6 @@

</div>

<p>
<em>(To change/add a avatar please visit <a href="http://gravatar.com">http://gravatar.com</a>)</em>
</p>

<p>
<%= f.label 'Time Zone:' %>
<%= f.time_zone_select :timezone, ActiveSupport::TimeZone.us_zones %>
Expand Down
16 changes: 8 additions & 8 deletions lib/snorby/jobs/cache_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -197,12 +197,11 @@ def update_classification_count
end

def update_signature_count
sql = %{
update signature set events_count = (select count(*) as count
from event where event.signature = signature.sig_id);
}
sigs = db_select("SELECT COUNT(*) AS c, signature AS s FROM event GROUP BY signature")

db_execute(sql)
sigs.each do |s|
db_execute("UPDATE signature SET events_count=#{s.c} WHERE signature.sig_id=#{s.s}")
end
end

def has_event_id?
Expand Down Expand Up @@ -281,6 +280,9 @@ def validate_cache_indexes
puts "[~] Building events_with_join database view"
db_execute("create or replace view events_with_join as select event.*, iphdr.ip_src, iphdr.ip_dst, signature.sig_priority, signature.sig_name from event inner join iphdr on event.sid = iphdr.sid and event.cid = iphdr.cid inner join signature on event.signature = signature.sig_id;")

puts "[~] Creating timestamp index on event table"
db_execute("CREATE INDEX IF NOT EXISTS index_event_timestamp ON event (timestamp) USING BTREE;")

end
alias :checkdb :validate_cache_indexes

Expand Down Expand Up @@ -411,9 +413,7 @@ def sql_icmp

def latest_five_distinct_signatures
sql = %{
select signature from (
select signature, MAX(timestamp) as timestamp from event group by signature,timestamp order by timestamp desc limit 5
) as signature;
SELECT UNIQUE(signature) AS signature FROM event ORDER BY timestamp DESC LIMIT 5;
}

db_select(sql)
Expand Down