Commit eb8a9ae5 authored by Frank Bergmann's avatar Frank Bergmann

- Now pre-aggegating data before doing a CRM query

parent d6fec3f2
<if "" eq @message@>
<div id="map" style="width: @width@px; height: @height@px;"></div>
<script>
// Create a map
var map = L.map('map').setView([@zoom_lat@, @zoom_lng@], @zoom_zoom@);
L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
attribution: '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);
var iconUrls = {
blue: "/intranet-leaflet/resources/marker-0067a3.png",
light_blue: "/intranet-leaflet/resources/marker-38aadd.png",
blue_grey: "/intranet-leaflet/resources/marker-436978.png",
purple: "/intranet-leaflet/resources/marker-5b396b.png",
green: "/intranet-leaflet/resources/marker-728224.png",
light_green: "/intranet-leaflet/resources/marker-72b026.png",
red: "/intranet-leaflet/resources/marker-a23336.png",
light_red: "/intranet-leaflet/resources/marker-d13d29.png",
pink: "/intranet-leaflet/resources/marker-d252b9.png",
orange: "/intranet-leaflet/resources/marker-f69730.png"
};
// Loop through all companies and put a marker for each.
var store = @store;noquote@;
store.each(function(model) {
// Get basic properties of the customer
var count = parseInt(model.get('cnt'));
var lat = parseFloat(model.get('geo_lat'));
var lng = parseFloat(model.get('geo_lng'));
var color = model.get('color');
var countryCode = model.get('country_code');
var countryName = model.get('country_name');
var popup = "<b>"+countryName+"</b><br>";
popup = popup+'Hits: '+count+"<br>";
// Determine the size of the marker icon to be shown:
// revenue < 1000: f=0.3, revenue > 40000: f=1.0, everything else in the middle
var sq = Math.sqrt(count);
if (sq > 1000) sq = 1000.0;
var f = 0.01 + 0.5 * sq/10.0;
var iconUrl = iconUrls[color];
var icon = L.icon({
iconUrl: iconUrl,
iconSize: [25*f, 41*f],
iconAnchor: [12*f, 41*f],
popupAnchor: [1, -34*f],
shadowUrl: '/intranet-leaflet/resources/marker-shadow.png',
shadowSize: [41*f, 41*f],
shadowAnchor: [12*f, 41*f]
});
// Geo-position was calculated before, so just add the popup
if (!!lat && !!lng) {
var marker = L.marker([lat, lng], {icon: icon}).addTo(map).bindPopup(popup);
};
return;
});
</script>
</if>
<else>
@message;noquote@
</else>
# /intranet-leaflet/lib/leaflet-crm-ip-component.tcl
#
# Variables from page:
#
# width
# height
if {![info exists width]} { set width 600 }
if {![info exists height]} { set height 400 }
# template::head::add_css -href "https://unpkg.com/leaflet@1.3.3/dist/leaflet.css" -media "screen" -order 100
# template::head::add_javascript -src "https://unpkg.com/leaflet@1.3.3/dist/leaflet.js" -order 200
template::head::add_css -href "/intranet-leaflet/resources/leaflet.css" -media "screen" -order 100
template::head::add_javascript -src "/intranet-leaflet/resources/leaflet.js" -order 200
set zoom_lat [parameter::get_from_package_key -package_key "intranet-leaflet" -parameter ZoomLatitude -default "41.4"]
set zoom_lng [parameter::get_from_package_key -package_key "intranet-leaflet" -parameter ZoomLongitude -default "2.2"]
set zoom_zoom [parameter::get_from_package_key -package_key "intranet-leaflet" -parameter ZoomZoom -default "3"]
set ip_sql "
select 'blue' as color,
ip.cnt,
c.code as country_code,
c.country as country_name,
c.lat as geo_lat,
c.lon as geo_lng
from (select count(*) as cnt,
n.country
from (select *,
(split_part(ao.creation_ip, '.', 1)::bigint << 24) +
(split_part(ao.creation_ip, '.', 2)::bigint << 16) +
(split_part(ao.creation_ip, '.', 3)::bigint << 8) +
(split_part(ao.creation_ip, '.', 4)::bigint) as ip_address
from acs_objects ao
where ao.creation_ip is not null and
ao.creation_ip != '127.0.0.1' and
ao.creation_ip not like '192.168.%' and
ao.creation_ip not like '172.26.%' and
ao.creation_ip ~ '^\[0-9\]+\.\[0-9\]+\.\[0-9\]+\.\[0-9\]+$' and
length(ao.creation_ip) > 7
) o
join ip2nation n on ip = (select max(ip) from ip2nation where ip_address > ip)
group by
n.country
) ip
LEFT join ip2nationcountries c on c.code = ip.country
"
#ad_return_complaint 1 [im_ad_hoc_query -subtotals_p 0 -format html $ip_sql]
set store [lindex [im_sencha_sql_to_store -sql $ip_sql] 0]
<if "" eq @message@>
<div id="map" style="width: @width@px; height: @height@px;"></div>
<script>
......@@ -39,9 +40,9 @@ store.each(function(model) {
// Determine the size of the marker icon to be shown:
// revenue < 1000: f=0.3, revenue > 40000: f=1.0, everything else in the middle
var sq = Math.sqrt(count);
var sq = Math.cbrt(count);
if (sq > 1000) sq = 1000.0;
var f = 0.2 + 0.5 * sq/300;
var f = 0.01 + 0.5 * sq/30.0;
var iconUrl = iconUrls[color];
var icon = L.icon({
iconUrl: iconUrl,
......@@ -62,4 +63,13 @@ store.each(function(model) {
});
</script>
</if>
<else>
@message;noquote@
<br>
switch_string: @switch_string@<br>
@lock@: @lock_exists_p@<br>
@aggr@: @aggr_exists_p@<br>
</else>
......@@ -4,9 +4,23 @@
#
# width
# height
# year
ad_proc im_tmp_table_exists { table } {
set lock_exists_p [db_string lock_exists_p "
select count(*) FROM pg_catalog.pg_class c LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
where n.nspname like 'pg_temp_%' AND pg_catalog.pg_table_is_visible(c.oid) and
upper(relname) = upper(:table)
"]
return $lock_exists_p
}
set message ""
if {![info exists width]} { set width 600 }
if {![info exists height]} { set height 400 }
if {![info exists year]} { set year [db_string year "select to_char(now(), 'YYYY')"] }
# template::head::add_css -href "https://unpkg.com/leaflet@1.3.3/dist/leaflet.css" -media "screen" -order 100
# template::head::add_javascript -src "https://unpkg.com/leaflet@1.3.3/dist/leaflet.js" -order 200
......@@ -17,34 +31,120 @@ set zoom_lat [parameter::get_from_package_key -package_key "intranet-leaflet" -p
set zoom_lng [parameter::get_from_package_key -package_key "intranet-leaflet" -parameter ZoomLongitude -default "2.2"]
set zoom_zoom [parameter::get_from_package_key -package_key "intranet-leaflet" -parameter ZoomZoom -default "3"]
set aggr "tmp_crm_online_interactions_aggregate"
set lock "tmp_crm_online_interactions_aggregate_lock"
#db_dml tt1 "drop table if exists tmp_crm_online_interactions_aggregate_lock"
#db_dml tt2 "drop table if exists tmp_crm_online_interactions_aggregate"
set lock_exists_p [db_table_exists tmp_crm_online_interactions_aggregate_lock]
set aggr_exists_p [db_table_exists tmp_crm_online_interactions_aggregate]
# Dummy SQL
set ip_sql "
select 'blue' as color,
0 as cnt,
'nn' as country_code,
'None' as country_name,
0.0 as geo_lat,
0.0 as geo_lng
"
set switch_string "$lock_exists_p-$aggr_exists_p"
# ad_return_complaint 1 "switch_string=$switch_string<br>lock=[db_table_exists tmp_crm_online_interactions_aggregate_lock]<br>aggr=[db_table_exists tmp_crm_online_interactions_aggregate]<br>"
switch $switch_string {
"0-0" {
# No lock and no aggr
set message "<b>Aggretating Data - Please wait for ~1 minute</b><br>&nbsp;<br>
Please wait for approx 1 minute and try again.<br>
Our system will need this time in order to perform<br>
some complex operations.
"
im_report_write_http_headers -output_format html
ns_write "
[im_header "Agregating Data - Please wait"]
[im_navbar crm]
<p>$message</p>
[im_footer]
"
# db_dml drop_lock "drop table if exists tmp_crm_online_interactions_aggregate_lock"
# db_dml drop_aggr "drop table if exists tmp_crm_online_interactions_aggregate"
db_dml create_lock "create table tmp_crm_online_interactions_aggregate_lock as select now() as today"
db_dml create_aggr "
create table tmp_crm_online_interactions_aggregate as
select count(*) as cnt,
t.interaction_type_id,
t.year,
t.ip_address
from (select interaction_type_id,
to_char(interaction_date, 'YYYY') as year,
ip_address
from crm_online_interactions
LIMIT 20000
) t
group by
interaction_type_id, year, ip_address
"
ad_script_abort
}
"1-0" {
# Lock exists but no aggregation yet - wait!
set message "<b>Aggretating Data - Please wait for ~1 minute</b><br>&nbsp;<br>
Aggregation has already started.<br>
Please wait for approx 1 minute and try again.<br>
Our system will need this time in order to perform<br>
some complex operations.
"
}
"0-1" - "1-1" {
# Aggregation exists
# ad_return_complaint 1 "$aggr_exists_p<br>$lock_exists_p"
# ad_return_complaint 1 [im_ad_hoc_query -format html -subtotals_p 0 "select * from tmp_crm_online_interactions_aggregate order by cnt DESC"]
# ad_return_complaint 1 [im_ad_hoc_query -format html -subtotals_p 0 "select count(*) from tmp_crm_online_interactions_aggregate"]
set ip_sql "
select 'blue' as color,
ip.cnt,
c.code as country_code,
c.country as country_name,
c.lat as geo_lat,
c.lon as geo_lng
from (select count(*) as cnt,
from (select sum(o.cnt) as cnt,
n.country
from (select *,
(split_part(ao.modifying_ip, '.', 1)::bigint << 24) +
(split_part(ao.modifying_ip, '.', 2)::bigint << 16) +
(split_part(ao.modifying_ip, '.', 3)::bigint << 8) +
(split_part(ao.modifying_ip, '.', 4)::bigint) as ip_address
from acs_objects ao
where ao.modifying_ip is not null and
ao.modifying_ip != '127.0.0.1' and
ao.modifying_ip not like '192.168.%' and
ao.modifying_ip not like '172.26.%' and
length(ao.modifying_ip) > 7
from (select coi.interaction_type_id,
coi.cnt,
(split_part(coi.ip_address, '.', 1)::bigint << 24) +
(split_part(coi.ip_address, '.', 2)::bigint << 16) +
(split_part(coi.ip_address, '.', 3)::bigint << 8) +
(split_part(coi.ip_address, '.', 4)::bigint) as ip_address
from tmp_crm_online_interactions_aggregate coi
where coi.ip_address is not null and
coi.ip_address not in ('0.0.0.0', '127.0.0.1') and
coi.ip_address not like '192.168.%' and
coi.ip_address not like '172.26.%' and
coi.ip_address ~ '^\[0-9\]+\\.\[0-9\]+\\.\[0-9\]+\\.\[0-9\]+\$' and
length(coi.ip_address) > 7
) o
join ip2nation n on ip = (select max(ip) from ip2nation where ip_address > ip)
group by
n.country
) ip
LEFT join ip2nationcountries c on c.code = ip.country
"
"
}
default {
ad_return_complaint 1 "Invalid switch_string=$switch_string"
}
}
#ad_return_complaint 1 [im_ad_hoc_query -subtotals_p 0 -format html $ip_sql]
......
......@@ -59,11 +59,12 @@ if {"" ne $customer_status_id} {
set sql "
select t.*,
CASE WHEN invoice_amount <= 0 THEN 'red'
CASE WHEN invoice_amount <= 0.0 THEN 'red'
WHEN company_status_id in ([join [im_sub_categories [im_company_status_potential]] ","]) THEN 'green'
ELSE 'blue'
END as color
from ($customer_sql) t
where invoice_amount > 0.0
order by
t.company_id
"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment