73 lines
3.5 KiB
PHP
73 lines
3.5 KiB
PHP
<?php
|
|
header('Content-Type: application/json');
|
|
header('Access-Control-Allow-Origin: *');
|
|
|
|
function q($sql) {
|
|
$cmd = 'PGPASSWORD=admin123 psql -h 10.1.0.3 -U admin -d adx_system -t -A -F "|" -c ' . escapeshellarg($sql) . ' 2>&1';
|
|
$raw = @shell_exec($cmd);
|
|
$rows = [];
|
|
foreach (array_filter(array_map('trim', explode("\n", $raw ?? ''))) as $line) {
|
|
if (strpos($line, '|') === false) continue;
|
|
$rows[] = array_map('trim', explode('|', $line));
|
|
}
|
|
return $rows;
|
|
}
|
|
|
|
$out = ['ts' => date('c'), 'generator' => 'contacts-segmentation-live.php v3 (pipeline included)'];
|
|
|
|
$summary = [];
|
|
foreach ([
|
|
['leads','admin.leads'],
|
|
['send_contacts','admin.send_contacts'],
|
|
['weval_leads','admin.weval_leads'],
|
|
['pipeline_contacts','admin.pipeline_contacts']
|
|
] as $pair) {
|
|
$rows = q("SELECT COALESCE(segment_type,'UNCLASSIFIED'), count(*) FROM " . $pair[1] . " GROUP BY 1 ORDER BY 2 DESC");
|
|
foreach ($rows as $r) $summary[$pair[0]][$r[0]] = (int)$r[1];
|
|
}
|
|
$out['summary'] = $summary;
|
|
|
|
$industries = [];
|
|
$rows = q("SELECT industry, SUM(cnt) FROM admin.contacts_breakdown WHERE segment_type='B2B' AND industry IS NOT NULL GROUP BY 1 ORDER BY 2 DESC");
|
|
foreach ($rows as $r) $industries[$r[0]] = (int)$r[1];
|
|
$out['industries_b2b_all_sources'] = $industries;
|
|
|
|
$rows = q("SELECT industry, count(*) FROM admin.pipeline_contacts WHERE segment_type='B2B' AND industry IS NOT NULL GROUP BY 1 ORDER BY 2 DESC");
|
|
$pc_ind = [];
|
|
foreach ($rows as $r) $pc_ind[$r[0]] = (int)$r[1];
|
|
$out['industries_pipeline_contacts_b2b'] = $pc_ind;
|
|
|
|
$pc = q("SELECT industry, count(*) FROM admin.pipeline_companies GROUP BY 1 ORDER BY 2 DESC LIMIT 25");
|
|
$pc_out = [];
|
|
foreach ($pc as $r) $pc_out[$r[0]] = (int)$r[1];
|
|
$out['pipeline_companies_by_industry'] = $pc_out;
|
|
|
|
$tt = q("SELECT job_title, count(*) FROM admin.pipeline_contacts WHERE (job_title ILIKE '%director%' OR job_title ILIKE '%directeur%' OR job_title ILIKE '%ceo%' OR job_title ILIKE '%cfo%' OR job_title ILIKE '%cto%' OR job_title ILIKE '%chief%' OR job_title ILIKE '%achat%' OR job_title ILIKE '%procurement%') AND job_title != '' GROUP BY 1 ORDER BY 2 DESC LIMIT 20");
|
|
$tt_out = [];
|
|
foreach ($tt as $r) if (count($r) >= 2) $tt_out[] = ['title' => $r[0], 'count' => (int)$r[1]];
|
|
$out['top_decision_makers'] = $tt_out;
|
|
|
|
$src = q("SELECT source, count(*) FROM admin.pipeline_contacts GROUP BY 1 ORDER BY 2 DESC LIMIT 10");
|
|
$src_out = [];
|
|
foreach ($src as $r) if (count($r) >= 2) $src_out[$r[0]] = (int)$r[1];
|
|
$out['contacts_by_source'] = $src_out;
|
|
|
|
$tot = q("SELECT (SELECT count(*) FROM admin.leads), (SELECT count(*) FROM admin.leads WHERE segment_type IS NOT NULL), (SELECT count(*) FROM admin.send_contacts), (SELECT count(*) FROM admin.send_contacts WHERE segment_type IS NOT NULL), (SELECT count(*) FROM admin.pipeline_companies), (SELECT count(*) FROM admin.pipeline_contacts), (SELECT count(*) FROM admin.pipeline_contacts WHERE company_id IS NOT NULL)");
|
|
if (!empty($tot[0]) && count($tot[0]) >= 7) {
|
|
$p = $tot[0];
|
|
$out['progress'] = [
|
|
'leads_total' => (int)$p[0],
|
|
'leads_classified' => (int)$p[1],
|
|
'send_contacts_total' => (int)$p[2],
|
|
'send_contacts_classified' => (int)$p[3],
|
|
'pipeline_companies_total' => (int)$p[4],
|
|
'pipeline_contacts_total' => (int)$p[5],
|
|
'pipeline_contacts_linked_company' => (int)$p[6],
|
|
'leads_pct' => $p[0] > 0 ? round(100*$p[1]/$p[0], 1) : 0,
|
|
'send_contacts_pct' => $p[2] > 0 ? round(100*$p[3]/$p[2], 1) : 0,
|
|
'contacts_linked_pct' => $p[5] > 0 ? round(100*$p[6]/$p[5], 1) : 0,
|
|
];
|
|
}
|
|
|
|
echo json_encode($out, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
|