Skip to content
This repository has been archived by the owner on Jul 17, 2021. It is now read-only.

Commit

Permalink
Merge branch 'staging'
Browse files Browse the repository at this point in the history
  • Loading branch information
hackedunit committed Nov 16, 2016
2 parents ebc8528 + 5de5761 commit fadc423
Show file tree
Hide file tree
Showing 37 changed files with 527 additions and 118 deletions.
5 changes: 5 additions & 0 deletions app/admin/certificate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

permit_params :name, :avatar, :description

preserve_default_filters!
filter :locations, collection: proc {
selected_location = params[:q].blank? ? nil : params[:q][:locations_id_eq]
grouped_options_for_select([["Active", false], ["Hidden", true]].collect { |l| [l[0], Location.with_deleted.order('provider ASC').where(hidden: l[1]).load.collect { |l| [l.provider_label, l.id] } ] }, selected_location)
}

index do
column :id
Expand Down
2 changes: 2 additions & 0 deletions app/admin/coupon.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

permit_params :coupon_code, :duration_months, :percentage, :active, :expiry_date

remove_filter :accounts, :invoices, :credit_notes

index do
column :id
column :coupon_code
Expand Down
4 changes: 2 additions & 2 deletions app/admin/dashboard.rb
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,9 @@ def resources
column do
panel 'Servers By Location/Provider' do
table do
Location.all.map do |location|
Location.all.order('provider ASC').map do |location|
tr do
td link_to location.to_s, admin_location_path(location)
td link_to location.provider_label, admin_location_path(location)
td location.servers.count
end
end
Expand Down
6 changes: 3 additions & 3 deletions app/admin/location.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
end
end

filter :servers
filter :servers_name, as: :string, label: 'Server Name'
filter :region
filter :certificates
filter :provider
Expand Down Expand Up @@ -170,7 +170,7 @@ def pingdom_servers_cached
end

def pingdom_servers_raw
UptimeTasks.new.perform(:pingdom_servers)
UptimeTasks.new.perform(:pingdom_servers).sort
end

def update_selected(options)
Expand Down Expand Up @@ -214,4 +214,4 @@ def pingdom_cache_key

class AppHelper
include ApplicationHelper
end
end
20 changes: 20 additions & 0 deletions app/admin/packages.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,25 @@
ActiveAdmin.register Package do
permit_params :location, :location_id, :memory, :cpus, :disk_size, :ip_addresses

preserve_default_filters!
filter :location, collection: proc {
selected_location = params[:q].blank? ? nil : params[:q][:location_id_eq]
grouped_options_for_select([["Active", false], ["Hidden", true]].collect { |l| [l[0], Location.with_deleted.order('provider ASC').where(hidden: l[1]).load.collect { |l| [l.provider_label, l.id] } ] }, selected_location)
}

form do |f|
f.semantic_errors *f.object.errors.keys

f.inputs do
f.input :location_id, as: :select, collection: grouped_options_for_select(([["Active", false], ["Hidden", true]].collect { |l| [l[0], Location.with_deleted.order('provider ASC').where(hidden: l[1]).load.collect { |l| [l.provider_label, l.id] }] }), *f.object.location_id), include_blank: false
f.input :memory
f.input :cpus
f.input :disk_size
f.input :ip_addresses
end

f.actions
end

sidebar :control_panel_links do
ul do
Expand Down
6 changes: 6 additions & 0 deletions app/admin/region.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

permit_params :name, :description, :location_id

preserve_default_filters!
filter :locations, collection: proc {
selected_location = params[:q].blank? ? nil : params[:q][:locations_id_eq]
grouped_options_for_select([["Active", false], ["Hidden", true]].collect { |l| [l[0], Location.with_deleted.order('provider ASC').where(hidden: l[1]).load.collect { |l| [l.provider_label, l.id] } ] }, selected_location)
}

sidebar :control_panel_links do
ul do
li link_to('Dashboard', root_path)
Expand Down
7 changes: 5 additions & 2 deletions app/admin/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,11 @@
filter :identifier, label: 'Onapp identifier'
filter :name
filter :hostname
filter :unscoped_location, label: "Location"
filter :unscoped_user, label: "User"
filter :location, collection: proc {
selected_location = params[:q].blank? ? nil : params[:q][:location_id_eq]
grouped_options_for_select([["Active", false], ["Hidden", true]].collect { |l| [l[0], Location.with_deleted.order('provider ASC').where(hidden: l[1]).load.collect { |l| [l.provider_label, l.id] } ] }, selected_location)
}
filter :user_full_name, as: :string, label: 'User Name'

filter :state
filter :built
Expand Down
8 changes: 8 additions & 0 deletions app/admin/template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@
actions :all, except: [:new, :destroy]

permit_params :os_type, :os_distro, :identifier, :name, :min_memory, :min_disk, :hidden, :hourly_cost

preserve_default_filters!
filter :location, collection: proc {
selected_location = params[:q].blank? ? nil : params[:q][:location_id_eq]
grouped_options_for_select([["Active", false], ["Hidden", true]].collect { |l| [l[0], Location.with_deleted.order('provider ASC').where(hidden: l[1]).load.collect { |l| [l.provider_label, l.id] } ] }, selected_location)
}
filter :servers_name, as: :string, label: 'Server Name'
remove_filter :servers

sidebar :control_panel_links do
ul do
Expand Down
5 changes: 5 additions & 0 deletions app/admin/ticket.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
ActiveAdmin.register Ticket do
actions :all, except: [:new, :edit, :destroy]
permit_params :status

preserve_default_filters!
filter :user_full_name, as: :string, label: 'User Name'
filter :server_name, as: :string, label: 'Server Name'
remove_filter :activities, :ticket_replies, :user, :server

sidebar :control_panel_links do
ul do
Expand Down
4 changes: 3 additions & 1 deletion app/admin/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
filter :otp_mandatory
filter :status
filter :unconfirmed_email
filter :servers
filter :servers_name, as: :string, label: 'Server Name'
filter :keys

filter :current_sign_in_ip
Expand All @@ -46,6 +46,8 @@
filter :confirmed_at
filter :current_sign_in_at
filter :last_sign_in_at

remove_filter :keys

index do
column :id
Expand Down
37 changes: 28 additions & 9 deletions app/assets/javascripts/dashboard/stats.js.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ $ ->
</div>
</div>
"""
""" if template
)

statsGraph = () ->
Expand Down Expand Up @@ -77,14 +77,24 @@ $ ->
colours = {}
$('.nv-legend-text').each () ->
colours[$(this).text()] = $(this).prev('circle').css('fill')


randomColor = (i) ->
colors = d3.scale.category20().range()
colors[i % colors.length-1]

colourSelector = (server) ->
if @servers.length > Object.keys(colours).length
return randomColor(server.id)
else
return colours[server.name]

# Create multiple charts, one for each server, to be included using the compose() method
lines = []
_.each @servers, (server) ->
lines.push({
name: server.name,
yData: (datapoint) -> datapoint[server.name],
colour: colours[server.name]
colour: colourSelector(server)
})

# Sum all the servers usages for a given date
Expand All @@ -108,7 +118,7 @@ $ ->
graph.render()

# Add total to center of pies
addTotal = (statistic, key, context) ->
addTotal = (statistic, key, context, showTip = true) ->
width = $jgChart.width()
total = context.usage
unit = context.unit
Expand All @@ -121,7 +131,7 @@ $ ->
.on("click", (d) ->
url = "#{window.location.protocol}//#{window.location.host}/servers/#{d.data.id}"
$(window.location).attr('href', url)
)
) if showTip
pie.append('text')
.classed({'total-unit': true})
.attr("dy", ".35em")
Expand All @@ -148,13 +158,22 @@ $ ->
_.each statistic, (split) ->
split.unit = context.unit
return

svg.datum(statistic)

if statistic.length == 0
showTip = false
totalUsage = {}
totalUsage[key] = totalUsage['usage'] = stats.stats[key].usage
totalUsage['name'] = "Total #{key.replace(/_/g, ' ')}"
svgData = [totalUsage]
else
svgData = statistic

svg.datum(svgData)
.transition().duration(1200)
.call(chart)

nv.utils.windowResize(chart.update)
addTotal(statistic, key, context)
addTotal(statistic, key, context, showTip)
return chart

addTickets = (ticket) ->
Expand Down Expand Up @@ -198,7 +217,7 @@ $ ->

_.each keys, (key) ->
split = stats.stats[key].split
if split.length == 0
if split.length == 0 && stats.stats[key].usage == 0
html =
"""
<div class="no-data">
Expand Down
2 changes: 1 addition & 1 deletion app/assets/stylesheets/dashboard.scss
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
span.jg-icon {
&:before {
position: absolute;
left: 0;
left: -22px;
padding: 10px 0 0 15px;
color: #505050;
font-size: 16px;
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/concerns/session_order_report.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ def prepare_order
#@server_params = Rack::Utils.parse_nested_query(
# URI::parse(session[:user_return_to]).query).symbolize_keys
@server_params = session[:server_wizard_params] || {}
@location = Location.find(@server_params[:location_id])
set_price
@location = Location.find(@server_params[:location_id]) unless @server_params.blank?
set_price if @location
end

def set_price
Expand Down
36 changes: 29 additions & 7 deletions app/controllers/dashboard_controller.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,38 @@
class DashboardController < ApplicationController

helper_method :cache_key_for_dashboard

def index
stats
costs
@servers = current_user.servers.includes(:location).order(id: :asc)
@cache_key_params = cache_key_params
@stats = stats
@costs = costs
end

private

def stats
@stats = DashboardStats.gather_stats(current_user)
Rails.cache.fetch(cache_key_for_dashboard('stats'), expires_in: 12.hours) do
DashboardStats.gather_stats(current_user, @servers)
end
end

def costs
@costs = DashboardStats.gather_costs(current_user)
Rails.cache.fetch(cache_key_for_dashboard('costs'), expires_in: 12.hours) do
DashboardStats.gather_costs(current_user, @servers)
end
end

def cache_key_for_dashboard(key)
"#{key}/" + @cache_key_params
end

def cache_key_params
server_count = @servers.size
server_max_updated_at = current_user.servers.maximum(:updated_at).try(:utc).try(:to_s, :number)
ticket_max_updated_at = current_user.tickets.maximum(:updated_at).try(:utc).try(:to_s, :number)
server_usages_updated_at = ServerUsage.where("usage_type = 'cpu' AND server_id IN (?)", @servers.map(&:id)).maximum(:updated_at).try(:utc).try(:to_s, :number)
"#{current_user.id}/servers/all-#{server_count}-#{server_max_updated_at}/server_usages/#{server_usages_updated_at}/tickets/#{ticket_max_updated_at}"
end

end
7 changes: 2 additions & 5 deletions app/controllers/ip_addresses_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ def create
if @server.can_add_ips? && !@server.primary_network_interface.blank? && charge(@server.ip_addresses + 1)
AssignIpAddress.perform_async(current_user.id, @server.id)
Analytics.track(current_user, event: 'Added a new IP address')
@server.increment! :ip_addresses
Rails.cache.write([Server::IP_ADDRESS_ADDED_CACHE, @server.id], true)
@server.ip_requested = @server.ip_requested + 1
create_sift_event :create_ip_address, @server.sift_server_properties
redirect_to server_ip_addresses_path(@server), notice: 'IP address has been requested and will be added shortly'
else
Expand All @@ -36,11 +35,9 @@ def create
def destroy
raise "Cannot remove Primary IP address" if @ip_address.primary?
IpAddressTasks.new.perform(:remove_ip, current_user.id, @server.id, @ip_address.identifier)
@ip_address.destroy!
charge(@server.ip_addresses - 1)
@server.decrement! :ip_addresses
@ip_address.destroy!
Analytics.track(current_user, event: 'Removed IP address')
Rails.cache.delete([Server::IP_ADDRESS_ADDED_CACHE, @server.id])
create_sift_event :destroy_ip_address, @server.sift_server_properties
redirect_to server_ip_addresses_path(@server), notice: 'IP address has been removed'
rescue StandardError => e
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/servers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ def destroy
end

def calculate_credit
@server.refresh_usage
@server.refresh_usage rescue nil # always prepare data
credit = @server.generate_credit_item(CreditNote.hours_till_next_invoice(current_user.account))
paid_bandwidth = Billing::BillingBandwidth.new(@server).bandwidth_usage
badwidth_price = @server.location.price_bw
Expand Down
11 changes: 10 additions & 1 deletion app/mailers/admin_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def periodic_csv(start_date, end_date, report, admin_id)
attachments[filename] = reporter.send(@report)
mail(to: admin_user.email, subject: "#{ENV['BRAND_NAME']} Billing Reports")
end

def cost_analysis(mailto = FINANCE_RECIPIENTS)
time = Time.now
@date_name = time.strftime("#{ActiveSupport::Inflector.ordinalize(time.day)} %B %Y")
Expand Down Expand Up @@ -109,4 +109,13 @@ def notify_faulty_server(server, no_disk, no_ip)
subject: "#{ENV['BRAND_NAME']}: Faulty server for user #{server.user.full_name}"
)
end

def notify_automatic_invoice(server, old_server_specs)
@server, @old_server_specs = server, old_server_specs
@link_to_onapp_server = "#{ENV['ONAPP_CP']}/virtual_machines/#{server.identifier}"
mail(
to: ADMIN_RECIPIENTS,
subject: "#{ENV['BRAND_NAME']}: VM parameters discrepancy - automatic billing for user #{server.user.full_name}"
)
end
end
Loading

0 comments on commit fadc423

Please sign in to comment.