Skip to content
This repository has been archived by the owner on Feb 23, 2023. It is now read-only.

Commit

Permalink
elasticsearch wroking for social media tab
Browse files Browse the repository at this point in the history
  • Loading branch information
dvito committed Aug 31, 2016
1 parent 9433c13 commit 06f1922
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 40 deletions.
4 changes: 2 additions & 2 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ RUBY_VERSION=2.1.9
REGISTRY_USER_GROUP=AGY-GSA-OCSIT-APPS.USERS
REGISTRY_ADMIN_GROUP=AGY-GSA-OCSIT-APPS.APPADMINS

REGISTRY_HOSTNAME=localhost
REGISTRY_API_HOST=http://localhost:3000
REGISTRY_HOSTNAME=socialmobileregistry.digital.gov
REGISTRY_API_HOST=http://socialmobileregistry.digital.gov

REGISTRY_EMAIL_SERVER=smtp.gmail.com
REGISTRY_EMAIL_PORT=587
Expand Down
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
source 'http://rubygems.org'
ruby ENV['RUBY_VERSION']
#ruby ENV['RUBY_VERSION']

# Rails
gem 'rails', '4.2.5'
Expand Down
54 changes: 34 additions & 20 deletions app/controllers/admin/social_media_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,37 @@ def index
{ "#{sort_column}" => "#{sort_direction}"}
]
)

@result_count = @outlets.count
@outlets = @outlets.page(current_page).per(params["iDisplayLength"].to_i).records
@result_count = @outlets.total_count
@outlets = @outlets.page(current_page).per(params["iDisplayLength"].to_i).results
else
@outlets = Outlet.includes(:official_tags,:agencies,:users).references(:official_tags,:agencies,:users).where("draft_id IS NULL")
@result_count = @outlets.count
@outlets = @outlets.order("outlets.#{sort_column} #{sort_direction}").page(current_page).per(params["iDisplayLength"].to_i)
@outlets = Outlet.search(
query: {
bool: {
must: [
{
constant_score: {
filter: {
missing: {
field: "draft_id",
existence: true,
null_value: true
}
}
}
}
]
}
},
sort: [
{ "#{sort_column}" => "#{sort_direction}"}
]
)

@result_count = @outlets.total_count
@outlets = @outlets.page(current_page).per(params["iDisplayLength"].to_i).results
# @outlets = Outlet.includes(:official_tags,:agencies,:users).references(:official_tags,:agencies,:users).where("draft_id IS NULL")
# @result_count = @outlets.count
# @outlets = @outlets.order("outlets.#{sort_column} #{sort_direction}").page(current_page).per(params["iDisplayLength"].to_i)
end
}
format.csv { send_data @outlets.to_csv }
Expand Down Expand Up @@ -222,33 +246,23 @@ def current_page
end
def sort_column
columns = {
"0" => "organization",
"0" => "agencies",
"1" => "contacts",
"2" => "service",
"3" => "organization",
"3" => "account_name",
"4" => "account",
"5" => "updated_at",
"6" => "status"
}
params["iSortCol_0"] ? columns[params["iSortCol_0"]] : "account"
params["iSortCol_0"] ? columns[params["iSortCol_0"]] : "updated_at"
end

def sort_direction
%w[asc desc].include?(params["sSortDir_0"]) ? params["sSortDir_0"] : "asc"
%w[asc desc].include?(params["sSortDir_0"]) ? params["sSortDir_0"] : "desc"
end

def items_per_page_handler
per_page_count = 10 || params["iDisplayLength"].to_i
if params[:hidden_service_value]
params[:service] = params[:hidden_service_value]
end
if cookies[:per_page_count_social_media_accounts]
per_page_count = cookies[:per_page_count_social_media_accounts]
end
if params[:per_page]
per_page_count = params[:per_page]
cookies[:per_page_count_social_media_accounts] = per_page_count
end
return per_page_count.to_i
end

Expand Down
37 changes: 33 additions & 4 deletions app/models/outlet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,46 @@ class Outlet < ActiveRecord::Base
include PublicActivity::Model
include Notifications
include Elasticsearch::Model


settings index: { number_of_shards: 1 } do
mappings dynamic: 'false' do
indexes :id, type: :integer
indexes :title, analyzer: 'english'
indexes :agencies, analyzer: 'english'
indexes :service, analyzer: 'english'
indexes :contacts, analyzer: 'english'
indexes :account_name, analyzer: 'english'
indexes :account, analyzer: 'english'
indexes :status, analyzer: 'english'
indexes :draft_id, type: :integer
indexes :updated_at, type: :date
end
end

def as_indexed_json(options={})
result = {
id: self.id,
draft_id: self.draft_id,
service: self.service,
agencies: self.agencies.map(&:name).join(", "),
contacts: self.users.map(&:email).join(", "),
account_name: self.organization,
account: self.account,
status: self.status.humanize,
updated_at: self.updated_at
}
result
end

tracked owner: Proc.new{ |controller, model| controller.current_user }

scope :by_agency, lambda {|id| joins(:agencies).where("agencies.id" => id) }
scope :api, -> { where("draft_id IS NOT NULL") }

enum status: { under_review: 0, published: 1, archived: 2,
publish_requested: 3, archive_requested: 4 }
#handles versioning
#attr_accessor :auth_token
#attr_accessible :service_url, :organization, :info_url, :language, :account, :service, :auth_token, :agency_ids, :tag_list, :location_id, :location_name


# Outlets have a relationship to themselvs
# The "published" outlet will have a draft_id pointing to its parent
# The "draft" outlet will not have a draft_id field
Expand Down
1 change: 0 additions & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ class User < ActiveRecord::Base
paginates_per 200

def cas_extra_attributes=(extra_attributes)
puts extra_attributes
extra_attributes.each do |name, value|
case name
when "Email-Address"
Expand Down
16 changes: 8 additions & 8 deletions app/views/admin/social_media/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -60,20 +60,20 @@
$(document).ready(function() {

data_tables = $('#data-tables').dataTable({
"bServerSide" : true,
"serverSide" : true,
"sAjaxSource" : "/admin/social_media.json",
// "fnServerParams": function ( aoData ) {
// "fnServerParams": function(aoData) {
// aoData.push( { "service": $('#service').val() } ) ;
// },
"scrollY": "400px",
"columns": [
{ "data" : "agencies[, ]", "orderable": false},
{ "data" : "contacts[, ]", "orderable": false},
{ "data" : "service"},
{ "data" : "agencies" },
{ "data" : "contacts" },
{ "data" : "service" },
{ "data" : "account_name" },
{ "data" : "account"},
{ "data" : "updated_at"},
{ "data" : "status"}
{ "data" : "account" },
{ "data" : "updated_at" },
{ "data" : "status" }
],
"columnDefs": [
{
Expand Down
8 changes: 4 additions & 4 deletions app/views/admin/social_media/index.json.jbuilder
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ json.set! "iTotalRecords", @total_outlets
json.set! "iTotalDisplayRecords", @result_count
json.aaData @outlets do |outlet|
json.set! "DT_RowId", outlet.id
json.set! :agencies, outlet.agencies.map(&:name)
json.set! :contacts, outlet.users.map(&:email)
json.set! :agencies, outlet.agencies
json.set! :contacts, outlet.contacts
json.set! :service, outlet.service
json.set! :account_name, !outlet.organization.blank? ? outlet.organization : "n/a"
json.set! :account_name, outlet.account_name ? outlet.account_name : "n/a"
json.set! :account, outlet.account
json.set! :status, outlet.status.humanize
json.set! :updated_at, outlet.updated_at.strftime("%m/%d/%Y %H:%M %Z")
json.set! :updated_at, Time.parse(outlet.updated_at).strftime("%m/%d/%Y %H:%M %Z")
end

0 comments on commit 06f1922

Please sign in to comment.