Skip to content

Instantly share code, notes, and snippets.

View kbrock's full-sized avatar

Keenan Brock kbrock

View GitHub Profile
@kbrock
kbrock / windowing.sql
Last active December 30, 2015 00:59
Using windowing function to do multiple smaller limits. Give me the first 3 records (for each grouping) If you use limit, this will require 1 query to determine the groupings, and one limit queries per each grouping. N+1
create table windowing_example (id int, counter int);
-- id is the common component of the rows
-- counter is the timestamp / other id where ordering is introduced
insert into windowing_example(id, counter) values
(1, 1),
(1, 2),
(1, 3),
(1, 4),
(1, 5),
@kbrock
kbrock / reasons.rb
Last active January 14, 2016 17:58
Active Record allows you to define relationships in 2 directions. But unfortunately, it is not always smart enough to detect when a relationship on one side is the same as the opposite direction. This aims to help you discover where and why rails is not helping you out.
class Reason
def reason_inverse_name(src, ref)
# def inverse_name; delegate_reflection.send(:inverse_name); end
ref = ref.respond_to?(:delegate_reflection) ? ref.delegate_reflection : ref
if ref.options[:inverse_of]
# will this false positive a :through?
if ref.options[:inverse_of] == ref.send(:automatic_inverse_of)
"UNNEEDED: `options[:inverse_of]`"
else
@kbrock
kbrock / flow.md
Created January 21, 2016 13:47
Our apache proxy creation

environment_manager.rb:

  def prep_apache_proxying
    return unless MiqEnvironment::Command.supports_apache?

    MiqApache::Control.kill_all
    MiqUiWorker.install_apache_proxy_config
    MiqWebServiceWorker.install_apache_proxy_config
 end
@kbrock
kbrock / tasks.rb
Created January 27, 2016 21:18
Purging Schedule and associated methods
miq_schedule_worker_runner:
Jobs.metric_purge_all_timer # role: database_operations
VmdbMetric::Purging.purge_all_timer
# model: VmdbDatabaseMetric, table: vmdb_database_metris
VmdbMetric::Purging.purge_by_date(6.months.ago.utc, "hourly", 10_000)
VmdbMetric::Purging.purge_by_date(6.months.ago.utc, "daily", 10_000)
VmdbMetric::Purging.purge_all_timer
# model: VmdbMetric, table: vmdb_metrics
# PurgingMixin (need interval, so duplicated it)
VmdbMetric::Purging.purge_by_date(6.months.ago.utc, "hourly", 10_000)
@kbrock
kbrock / numbers.md
Last active February 2, 2016 14:32
name time child offset
Executing action: explorer
|22.1|29741.2|+19.0
 VmInfraController#explorer
|7.2|29719.1|+38.0
   VmInfraController#x_build_dynatree
|312.8|25769.1|+39.0
    Rbac.search
|136.9|141.8|+40.0
    TreeBuilderVmsAndTemplates#tree
|64.0|1565.8|+182.0
    TreeBuilderVmsAndTemplates#tree
|50.4|1423.2|+1748.0
      EmsVmware#subtree_arranged
|656.3|656.3|+1748.0

Rbac.search|301.0|666.4|+2418.0

@kbrock
kbrock / readme.md
Last active February 19, 2016 14:30
current render vms
@ ms ms- sql sqlms sqlrows comments
0.0 16,828.7 164 3,660.4 48075 /vm_infra/explorer
0.0 16,828.7 23.0 GET http://localhost:3000/vm_infra/explorer
20.0 16,805.6 25.7 6 1.9 5 .Executing action: explorer
41.0 15,327.8 0.0 ..VmShowMixin#feature_tree
41.0 15,327.8 0.2 ...VmShowMixin#build_vm_feature_tree
41.0 3.3 3.3 3 0.3 3 ....VmShowMixin#build_allowed_features
44.0 15,310.6 2,265.7 ....VmShowMixin#build_vm_tree
45.0 13,044.9 299.4 5 41.8 854 .....ApplicationController::Explorer#x_build_dynatree
WITH RECURSIVE search_tree(id, path) AS (
SELECT id, ARRAY[id]
FROM services
WHERE id = 42
UNION ALL
SELECT services.id, path || services.id
FROM search_tree
JOIN services ON services.service_id = search_tree.id
WHERE NOT services.id = ANY(path)
)
@kbrock
kbrock / just_sql.md
Last active March 15, 2016 13:58
Trying to mix and match active record AND OR
create table tst(val int);
insert into tst values(1),(2),(3),(4),(5),(6),(7),(8),(9),(10);
select val from tst where (val=2) and (mod(val,2)=0) or (val >= 6);

| val |

@kbrock
kbrock / comments.md
Created March 18, 2016 17:13
Comments on a seemingly "simple" PR

My comments on 7388

- s = MiqSearch.find(@sb[:planning][:options][:filter_value])
- s.options ||= {}
- s.options[:userid] = session[:userid]
- s.options[:results_format] = :objects
-vms, attrs = s.search
+ vms = MiqSearch.find(@sb[:planning][:options][:filter_value]).filtered(nil, :userid => current_user)
create index i_hosts_i_h on hosts(name, id);
create index i_hosts_h_i on hosts(id, name);
explain analyze verbose
SELECT "vms".id FROM "vms" left join "hosts" on "hosts"."id" = "vms"."host_id" ORDER BY "hosts"."name"
;
QUERY PLAN
-----------------------------------------------------------------------------------------------------------------------------
Sort (cost=440.76..448.26 rows=3000 width=36) (actual time=30.724..30.884 rows=3000 loops=1)
Output: vms.id, hosts.name