-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapachesolr_stats.module
1015 lines (937 loc) · 34.3 KB
/
apachesolr_stats.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* @file
* Keeps and reports statistics about Apache Solr usage and performance.
*/
/**
* Implements hook_menu().
*/
function apachesolr_stats_menu() {
return array(
'admin/config/search/apachesolr/stats' => array(
'title' => 'Statistics',
'description' => 'Apache Solr Statistics settings to measure usage and performance.',
'page callback' => 'drupal_get_form',
'page arguments' => array('apachesolr_stats_admin'),
'access arguments' => array('administer search'),
'type' => MENU_LOCAL_TASK,
),
'admin/reports/apachesolr_stats' => array(
'title' => 'Apache Solr Search Page Statistics',
'description' => 'Report of Apache Solr Page usage and performance.',
'page callback' => 'apachesolr_stats_report',
'page arguments' => array(),
'access arguments' => array('access site reports'),
'type' => MENU_NORMAL_ITEM,
),
'admin/reports/apachesolr_stats/%apachesolr_search_page' => array(
'title' => 'Apache Solr Search Page Statistics',
'page callback' => 'apachesolr_stats_report',
'page arguments' => array(3),
'access arguments' => array('access site reports'),
'type' => MENU_NORMAL_ITEM,
),
);
}
/**
* Build the settings form.
*/
function apachesolr_stats_admin($form, &$form_state) {
$form = array();
$form['tip'] = array(
'#markup' => t('You can also visit the <a href="@report-url">reports page</a> and <a href="@blocks-url">block administration page</a> for the enabled search pages.',
array('@report-url' => url('admin/reports/apachesolr_stats'), '@blocks-url' => url('admin/structure/block'))),
);
$search_pages = apachesolr_search_load_all_search_pages();
$options = array();
foreach ($search_pages as $search_page) {
$options[$search_page['page_id']] = $search_page['label'] . ' (' . $search_page['search_path'] . ')';
}
$form['apachesolr_stats_enabled'] = array(
'#type' => 'checkboxes',
'#title' => t('Enable search logging for these search pages'),
'#default_value' => variable_get('apachesolr_stats_enabled', array()),
'#options' => $options,
'#description' => t('Log information about all queries launched via the Apache Solr Search Integration module. Disabling a page here will also disable the associated block.')
);
$periods = drupal_map_assoc(array(3600, 10800, 21600, 32400, 43200, 86400, 172800, 259200, 604800, 1209600, 2419200, 4838400, 9676800), 'format_interval');
$form['apachesolr_stats_flush_log_timer'] = array(
'#type' => 'select',
'#title' => t('Discard query logs older than'),
'#default_value' => variable_get('apachesolr_stats_flush_log_timer', 259200),
'#options' => $periods,
'#description' => t('Older query log entries will be automatically discarded. (Requires a correctly configured <a href="@cron">cron maintenance task</a>.)', array('@cron' => url('admin/reports/status'))),
);
// Blacklist settings.
$form['access'] = array(
'#type' => 'fieldset',
'#title' => t('Log blacklist'),
'#description' => t('Note: Changing this does not alter existing logged queries.'),
);
$form['access']['apachesolr_stats_ignore_ip_list'] = array(
'#type' => 'textarea',
'#title' => t('IP addresses that will not be logged'),
'#default_value' => variable_get('apachesolr_stats_ignore_ip_list', ''),
'#description' => t('Enter IP addresses (e.g.: 192.168.1.2), one per line. You can match entire subnets using a partial IP address ending with a period (e.g.: 192.168.)'),
);
$form['access']['apachesolr_stats_ignore_role_list'] = array(
'#type' => 'checkboxes',
'#title' => t('User roles that will not be logged'),
'#options' => user_roles(),
'#default_value' => variable_get('apachesolr_stats_ignore_role_list', array()),
'#description' => t('Check all roles which should not be logged.'),
);
return system_settings_form($form);
}
/**
* Implements hook_apachesolr_query_alter().
*
* Adds debugQuery parameter to Solr call that returns processing time, etc.
*/
function apachesolr_stats_apachesolr_query_alter($query) {
if (variable_get('apachesolr_stats_enabled', array())) {
// Add the debug query argument.
// See: http://wiki.apache.org/solr/CommonQueryParameters#head-f45a9396425956a4db8d6478ed6029adfb7b0858
$query->replaceParam('debugQuery', 'true');
}
}
/**
* Implements hook_exit().
*
* This is the spot where actual logging takes place.
*/
function apachesolr_stats_exit() {
$enabled_pages = variable_get('apachesolr_stats_enabled', array());
if (! $enabled_pages) {
return;
}
// Apparently there can be cases where some modules aren't loaded.
if (! function_exists('apachesolr_has_searched')) {
return;
}
// Log only for current applicable search page.
$search_page = apachesolr_stats_get_search_page_by_path($_GET['q']);
// If no search page matches this path, return.
if (! $search_page) {
return;
}
// Continue if current search page is enabled.
$page_id = $search_page['page_id'];
if (!isset($enabled_pages[$page_id]) || (isset($enabled_pages[$page_id]) && !$enabled_pages[$page_id])) {
return;
}
// Continue if current page request has issued a search.
$env_id = $search_page['env_id'];
if (! apachesolr_has_searched($env_id)) {
return;
}
// Ignore certain IPs
$ignore_list = variable_get('apachesolr_stats_ignore_ip_list', '');
if ($ignore_list) {
$ips_to_ignore = preg_split('/[\s]+/', $ignore_list);
$request_ip_address = ip_address();
foreach ($ips_to_ignore as $ip) {
if ($ip != "" && strpos($request_ip_address, $ip) === 0) {
return;
}
}
}
// Ignore certain roles
global $user;
$roles_ignore_list = variable_get('apachesolr_stats_ignore_role_list', array());
$test = array_intersect(array_keys($user->roles), array_values($roles_ignore_list));
if (count($test) > 0) {
return;
}
$query = apachesolr_current_query($env_id);
$response = apachesolr_static_response_cache($query->getSearcher());
$filters = apachesolr_stats_get_active_facets($query->getSearcher());
$solrsort = $query->getSolrSort();
$keywords = $query->getParam('q');
$params = $query->getParams();
$num_suggestions = 0;
// If spellchecking is enabled for this search page...
if ($search_page['settings']['apachesolr_search_spellcheck']) {
// ... and Solr returned a suggestion...
if (isset($response->spellcheck) && isset($response->spellcheck->suggestions) && $response->spellcheck->suggestions != NULL) {
// .. log the spellchecker suggestion.
$num_suggestions = 1;
}
}
$id = db_insert('apachesolr_stats')
->fields(array(
'timestamp' => REQUEST_TIME,
'uid' => $user->uid,
'sid' => session_id(),
'numfound' => $response->response->numFound,
'showed_suggestions' => $num_suggestions,
'total_time' => $response->debug->timing->time,
'prepare_time' => $response->debug->timing->prepare->time,
'process_time' => $response->debug->timing->process->time,
'page' => isset($_GET['page']) ? $_GET['page'] : '',
'keywords' => $keywords,
'filters' => serialize(apachesolr_stats_get_active_facets($query->getSearcher())),
'sort' => serialize($query->getSolrsort()),
'env_id' => $env_id,
'page_id' => $search_page['page_id']
))
->execute();
return;
/*
$times = array();
$times['total']['total'] = $response->debug->timing->time;
foreach (array('prepare', 'process') as $phase) {
foreach($response->debug->timing->prepare as $key => $value) {
if (is_object($value)) {
$times[$phase][$key] = (int) $value->time;
} else {
$times[$phase]['total'] = (int) $value;
}
}
}
dsm($times);
return;
*/
}
/**
* Return a search page by the base path, e.g. 'search/site'
*
* @param string $path
* @return bool|array
* Returns FALSE if no page found, or the search page as returned by
* apachesolr_search_page_load($page_id);
*/
function apachesolr_stats_get_search_page_by_path($path) {
$search_pages = apachesolr_search_load_all_search_pages();
foreach ($search_pages as $search_page) {
if (strpos($path, $search_page['search_path']) === 0) {
return $search_page;
}
}
return FALSE;
}
/**
* Gets the searcher's active facets from Facet API.
*
* @param $searcher
* The machine name of the searcher.
*
* @return array
* An array keyed by facet name to value.
*/
function apachesolr_stats_get_active_facets($searcher) {
$filters = array();
if (function_exists('facetapi_adapter_load')) {
if ($adapter = facetapi_adapter_load($searcher)) {
$active = $adapter->getAllActiveItems();
foreach ($active as $filter) {
foreach ($filter['facets'] as $facet_name) {
$filters[$facet_name] = $filter['value'];
}
}
}
}
return $filters;
}
/**
* Callback for admin/reports/apachesolr/stats.
* @param array $search_page
* @param string $picked_granularity
* @internal param string $granularity Granularity to use for report.* Granularity to use for report.
* @return string
* The page output as HTML.
* @see apachesolr_stats_menu()
*/
function apachesolr_stats_report($search_page = NULL, $picked_granularity = "minute") {
$enabled_pages = variable_get('apachesolr_stats_enabled', array());
// If not given a search_page argument, show a listing.
if (empty($search_page)) {
$search_pages = apachesolr_search_load_all_search_pages();
$items = array();
foreach ($search_pages as $search_page) {
$items[] = l($search_page['label'], 'admin/reports/apachesolr_stats/' . $search_page['page_id']);
}
$output = t('Pick a search page to view the report:');
$output .= theme('item_list', array('items' => $items));
return $output;
}
drupal_set_title(t("Apache Solr statistics report: @label", array('@label' => $search_page['label'])));
$disabled = true;
if (isset($enabled_pages[$search_page['page_id']]) && $enabled_pages[$search_page['page_id']]) {
$disabled = false;
}
if ($disabled) {
drupal_set_message(t('Logging is disabled for this page in the !link. Enable it to log Apache Solr queries.', array('!link' => l('configuration page', 'admin/config/search/apachesolr/stats'))));
}
$granularities = apachesolr_stats_get_granularities();
// Decide what granularity to use: minute, hour or day
// Check if given argument exists; if not, reset to "hour"
if (!isset($granularities[$picked_granularity])) {
$picked_granularity = "minute";
}
$granularity = $granularities[$picked_granularity];
// Process latest log entries
$report_elements = apachesolr_stats_generate_report_elements($search_page['page_id'], $granularity);
// Create the output HTML:::::::::::::::::::::::::::::::::::::
// Granularity picker:
// Leave only those less than apachesolr_stats_flush_log_timer
$timer_max = variable_get('apachesolr_stats_flush_log_timer', 259200);
$output = "<div class='granularity'>" . t('Choose the report time span:');
foreach ($granularities as $name => $granularity) {
if ($name != "all" && $granularity['time_before'] > $timer_max) {
continue;
}
$output .= " ";
if ($name != $picked_granularity) {
$output .= l($granularity["last_msg"], "admin/reports/apachesolr_stats/" . $search_page['page_id'] . '/' . $name);
}
else {
$output .= "<strong>" . $granularity["last_msg"] . "</strong>";
}
}
$output .= "</div><hr>";
if ($report_elements) {
// Report description
$output .= t('This is an overview of Apache Solr usage and performance.');
$output .= ' ' . t('You can also visit the <a href="@settings-url">settings page</a>.',
array('@settings-url' => url('admin/config/search/apachesolr/stats'))
);
// Render report elements
foreach ($report_elements as $id => $data) {
// Table data
$rows[] = array(
"data" => array(
array(
'data' => $data['name'],
'header' => true,
'style' => 'width:33%',
),
array('data' => $data['value']),
),
);
}
$output .= theme('table', array('header' => array(), 'rows' => $rows));
}
else {
drupal_set_message(t('There is not enough stored data to build a report for the current time span.'));
}
return $output;
}
/**
* Generate an IMG tag with the URL to generate a chart using Google Charts API.
*
* @param string $granularity
* The granularity to use.
* @param array $data
* The array of data to chart.
* @param integer $start_timeslot
* The index of the first data element to chart.
* @param integer $last_timeslot
* The index of the first data element to chart.
* @param integer $total_queries
* Integer with the total number of queries included in this chart.
* @param bool|float $average
* Boolean flag: show an average value in the chart.
* @return string
*/
function apachesolr_stats_chart($granularity, $data, $start_timeslot, $last_timeslot, $total_queries, $average = FALSE) {
// Sample: http://chart.apis.google.com/chart?cht=lc&chs=350x100&chdlp=b&chma=10,10,10,10&chd=s:[encoded chart data]
$chart_prefix = 'http://chart.apis.google.com/chart?cht=lc&chs=350x100';
$chart_prefix .= '&chdlp=b&chma=30,100,20,20&chd=s:';
$chd = array();
$chd_min = 9999999;
$chd_max = 0;
$total = 0;
for ($t = $start_timeslot; $t <= $last_timeslot; $t++) {
$num = isset($data[$t]) ? intval($data[$t]) : 0;
$chd_min = ($chd_min > $num) ? $num : $chd_min;
$chd_max = ($chd_max < $num) ? $num : $chd_max;
$chd[] = $num;
}
// Add missing data for time between last timeslot and current time.
$current_timeslot = intval(REQUEST_TIME / $granularity['timespan']);
for ($t = $last_timeslot + 1; $t <= $current_timeslot; $t++) {
$chd_min = ($chd_min > 0) ? 0 : $chd_min;
// Insert at beginning.
array_unshift($chd, 0);
}
// Fix min if min and max are the same.
if ($chd_min == $chd_max) {
$chd_min = 0;
}
// Since we read the log from the database newest-first, reverse the data for rendering.
$chd = array_reverse($chd);
// Generate basic image URL
$image_url = $chart_prefix . apachesolr_stats_encodedata($chd, $chd_min, $chd_max);
// Add labels
$chxl = "";
if ($chd_max > 0) {
// Add y-axis labels.
$chxl .= "0:|" . intval($chd_min) . "|" . intval($chd_max);
// Show average value in a label on right-hand side.
if ($average !== FALSE) {
$image_url .= "&chxp=1," . intval($average / $chd_max * 100);
$chxl .= sprintf("|1:|%s=%.2f", t('average'), $average);
$chxt = "y,r";
}
else {
$chxt = "y";
}
// Add time/date labels
$earliest_timestamp = $start_timeslot * $granularity['timespan'];
$last_timestamp = $current_timeslot * $granularity['timespan'];
$mid_timestamp = ($last_timestamp + $earliest_timestamp) / 2;
$time_msg_1 = drupal_encode_path(strftime($granularity['format'], $earliest_timestamp));
$time_msg_2 = drupal_encode_path(strftime($granularity['format'], $mid_timestamp));
$time_msg_3 = drupal_encode_path(strftime($granularity['format'], $last_timestamp));
if ($chxt) {
$chxt = "{$chxt},x";
$chxl .= "|2:|{$time_msg_1}|{$time_msg_2}|{$time_msg_3}";
}
else {
$chxt = "x";
$chxl .= "|1:|{$time_msg_1}|{$time_msg_2}|{$time_msg_3}";
}
$image_url .= "&chxl={$chxl}&chxt={$chxt}";
}
// Return the image tag
return "<img src='$image_url' />";
}
/**
* Encode data using Chart's simple encoding.
* See http://code.google.com/apis/chart/formats.html#simple
*
* @param array $chd
* an array of integer values to encode.
* @param integer $chd_min
* an integer with the smallest value to encode.
* @param integer $chd_max
* an integer with the greatest value to encode.
* @return string
* a string representing the Google Charts API simple encoding of the data.
*/
function apachesolr_stats_encodedata($chd, $chd_min, $chd_max) {
$encoder_string = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
$encoded_values = '';
if (is_array($chd)) {
foreach ($chd as $value) {
$encoded_values .= substr($encoder_string, (($value - $chd_min) / $chd_max) * 61, 1);
}
}
// Google does not like single-valued line charts; fix that.
if (strlen($encoded_values) == 1) {
$encoded_values = "99";
}
return $encoded_values;
}
/**
* Implements hook_cron().
*
* Remove expired log messages.
*/
function apachesolr_stats_cron() {
db_delete('apachesolr_stats')
->condition('timestamp', REQUEST_TIME - variable_get('apachesolr_stats_flush_log_timer', 259200), '<')
->execute();
}
/**
* Returns a themed table for facet usage
* @param array $facets
* An array of calculated data to report.
* @return string
* HTML for a themed table containing the report data.
*/
function apachesolr_stats_facet_usage_table($facets) {
// Report usage in table
$header = array(
array(
'data' => t('Facet ID'),
'Xfield' => 'id',
'sort' => 'asc',
),
array(
'data' => t('Facet info'),
'Xfield' => 'info',
'sort' => '',
),
array(
'data' => t('Queries containing this facet'),
'Xfield' => 'queries',
'sort' => '',
),
array(
'data' => t('% of queries containing this facet'),
'Xfield' => 'queries',
'sort' => '',
),
#array('data' => 'Clickthrus from queries containing this facet', 'Xfield' => 'ct', 'sort' => ''),
#array('data' => 'Clickthru rate for facet', 'Xfield' => 'ctp', 'sort' => ''),
);
foreach ($facets as $fieldname => $facet) {
$rows[$fieldname][] = $fieldname;
$rows[$fieldname][] = $facet['info'];
$rows[$fieldname][] = $facet['usage'];
$rows[$fieldname][] = sprintf("%2.1f%%", $facet['usage'] / $facets['any']['usage'] * 100);
#$rows[$fieldname][] = $facet['clickthru'];
#if ($facet['usage']>0) {
# $rows[$fieldname][] = sprintf("%2.1f%%", ($facet['clickthru'] / $facet['usage'])*100);
#}
}
$output = theme('table', array('header' => $header, 'rows' => $rows, 'attributes' => array('style' => 'font-size:80%')));
return $output;
}
/**
* Returns the <img> tag for a Google chart with the facet usage
*
* @param array $facets
* An array of calculated data to report.
* @return string
* HTML for an IMG tag to a Google chart.
*/
function apachesolr_stats_facet_usage_graph($facets) {
// Chart for field usage
$leyends = array();
$data = array();
$label_cutoff = 40;
$total_queries = $facets['any']['usage'];
foreach ($facets as $fieldname => $facet) {
$leyend = preg_replace("/^.*ilter by /", "", $facet['info']);
if (strlen($leyend) > $label_cutoff) {
$leyend = substr($leyend, 0, $label_cutoff) . "...";
}
$leyends[] = drupal_encode_path($leyend);
$data[] = ($facet['usage'] / $total_queries) * 100;
}
$chd = 's:' . apachesolr_stats_encodedata($data, 0, 100);
// array_reverse() in next line due to apachesolr_stats_encodedata() encoding data backwards
$chl = implode('|', array_reverse($leyends));
$height = 30 + sizeof($leyends) * 28;
// Percentage labels
$chm = "N*f0*%,000000,0,-1,11";
$chart = "<img src='http://chart.apis.google.com/chart?chxt=y&cht=bhs&chma=20,20,20,20&chd={$chd}&chs=350x{$height}&chds=0,100&chxl=0:|{$chl}&chm={$chm}' />";
return $chart;
}
/**
* Returns the facet array to report on.
*/
function apachesolr_stats_get_facets() {
$facets = array();
if (module_exists('facetapi')) {
$environments = apachesolr_load_all_environments();
foreach ($environments as $env_id => $environment) {
$searcher = 'apachesolr@' . $env_id;
foreach (facetapi_get_enabled_facets($searcher) as $facet_name => $facet) {
$facets[$facet_name] = array(
'facet_field' => $facet['field'],
'info' => $facet['label'],
'usage' => 0,
);
}
}
}
// Add some "virtual" facets to report on.
$facets['kw'] = array(
'facet_field' => 'kw',
'info' => 'Keyword search',
'usage' => 0,
);
$facets['any'] = array(
'facet_field' => 'any',
'info' => '[All queries including any filter and/or keywords]',
'usage' => 0,
);
/*
$facets['none'] = array(
'facet_field' => 'none',
'info' => '[Clickthrus with no previous queries]',
);
*/
return $facets;
}
/**
* Returns an array of preset granularities.
* @return array
* an array of preset granularities for reports.
*/
function apachesolr_stats_get_granularities() {
$granularities = array(
'minute' => array(
'name' => t('minute'),
'timespan' => 60,
'time_before' => 60 * 60 * 24, // One day before
'last_msg' => t('last day'),
'format' => '%H:%M',
),
'hour' => array(
'name' => t('hour'),
'timespan' => 60 * 60,
'time_before' => 60 * 60 * 24 * 7, // One week before
'last_msg' => t('last week'),
'format' => '%m/%d %H:%M',
),
'day' => array(
'name' => t('day'),
'timespan' => 60 * 60 * 24,
'time_before' => 60 * 60 * 24 * 2 * 16, // 4 weeks before
'last_msg' => t('last month'),
'format' => '%m/%d',
),
'all' => array(
'name' => t('day'),
'timespan' => 60 * 60 * 24,
'time_before' => 60 * 60 * 24 * 7 * 16, // 16 weeks before
'last_msg' => t('all time (depends on settings)'),
'format' => '%m/%d',
),
);
return $granularities;
}
/**
* Generates report elements for the given granularity.
*
* @param string $granularity
* Timespan to aggregate report by. Possible values: 'minute', 'hour' or 'day'
* @return array
* An indexed array with the report elements; each element is an array with
* the indexes:
* 'name' => human-readable name of the element, e.g. "Total queries"
* 'value' => html with the result. Can be an image, a number, etc.
*/
function apachesolr_stats_generate_report_elements($page_id, $granularity) {
// Initialize
$facets = apachesolr_stats_get_facets();
$suggestions = 0;
$users = array();
$sessions = array();
$total_requests = 0;
$last_timeslot = 0;
$first_timestamp = 0;
$no_keywords = 0;
$total_queries = 0;
$time['max'] = -1;
$time['min'] = 9999.999;
$report_elements = array();
$keywords = array();
$keywords_noresults = array();
$simultaneous_fields = array();
$count_per_granularity = array();
$data_per_granularity = array();
$sort_usage = array();
// Scan the logfile and build statistics arrays
$result = db_query("SELECT * FROM {apachesolr_stats} WHERE timestamp > :timestamp AND page_id = :page_id ORDER BY timestamp DESC", array(':timestamp' => REQUEST_TIME - $granularity['time_before'], ':page_id' => $page_id));
foreach ($result as $record) {
$filters = unserialize($record->filters);
$timeslot = intval($record->timestamp / $granularity['timespan']);
if ($last_timeslot == 0) {
$last_timeslot = $timeslot;
}
@$users[$record->uid]++;
@$sessions[$record->sid]++;
// Tally suggestions
if ($record->showed_suggestions) {
$suggestions++;
}
@$total_requests++;
@$time['total'] += $record->total_time;
// $time['prepare'] += $record->prepare_time;
// $time['process'] += $record->process_time;
// Track max and min response times
$time['max'] = ($time['max'] < $record->total_time ? $record->total_time : $time['max']);
$time['min'] = ($time['min'] > $record->total_time ? $record->total_time : $time['min']);
// Field usage; only when on first results page (meaning it's a fresh search)
if ($record->page == "") {
$facet_processed_flag = array();
foreach (unserialize($record->filters) as $facet_name => $facet_value) {
if (isset($facets[$facet_name]) && !isset($facet_processed_flag[$facet_name])) {
// Add 1 to usage of facet.
$facets[$facet_name]['usage']++;
// Mark so we don't count it again for this query.
$facet_processed_flag[$facet_name] = TRUE;
}
}
if (trim($record->keywords) != "") {
if (!isset($facet_processed_flag['kw']) || $facet_processed_flag['kw'] != true) {
$facets['kw']['usage']++;
$facet_processed_flag['kw'] = true;
// Keep track of individual keywords used
$keys_filtered = drupal_strtolower(trim($record->keywords));
@$keywords[$keys_filtered]++;
// Count keywords with zero results; but only when no filters issued.
if ($record->numfound == 0 && !$filters) {
@$keywords_noresults[$keys_filtered]++;
}
}
}
else {
$no_keywords++;
}
// Count each unique query
$facets["any"]['usage']++;
// Keep track of how many fields were active per query
@$simultaneous_fields[sizeof($facet_processed_flag)]++;
$total_queries++;
}
// Sort usage; count only the first page of results
if ($record->page == "") {
$sort = unserialize($record->sort);
if (sizeof($sort) == 0) {
@$sort_usage["relevance"]++;
}
else {
@$sort_usage[$sort['#name']]++;
}
}
// Group some stats into timeslots (minutes, hours) to show trends
if (empty($user_slot[$record->uid][$timeslot])) {
@$data_per_granularity['users_per_slot'][$timeslot]++;
$user_slot[$record->uid][$timeslot] = TRUE;
}
if (empty($session_slot[$record->sid][$timeslot])) {
@$data_per_granularity['sessions_per_slot'][$timeslot]++;
$session_slot[$record->sid][$timeslot] = TRUE;
}
@$data_per_granularity['queries'][$timeslot]++;
@$count_per_granularity[$timeslot]++;
@$data_per_granularity['total_time'][$timeslot] += $record->total_time;
$first_timestamp = $record->timestamp;
}
if (sizeof($sessions) == 0 || sizeof($users) == 0 || $total_queries == 0) {
return array();
}
$start_timeslot = $timeslot;
$earliest_timestamp = $start_timeslot * $granularity['timespan'];
$report_elements['span'] = array(
'name' => t('Report span'),
'value' => t('Last @interval (@startdate to @enddate)', array(
'@interval' => format_interval(3600 + REQUEST_TIME - $first_timestamp),
'@startdate' => format_date($first_timestamp),
'@enddate' => format_date(REQUEST_TIME),
))
. '<br />'
. t('Data points in charts are one point per @granularity.', array('@granularity' => $granularity['name'])),
);
#$report_elements['queries'] = array('name' => t('Total requests to Solr'), 'value' => $total_queries);
// Chart for queries per timeslot
$chart = apachesolr_stats_chart($granularity, $data_per_granularity['queries'], $start_timeslot, $last_timeslot, $total_queries, $total_queries / ($last_timeslot -$start_timeslot + 1));
$report_elements['total_queries_per'] = array(
'name' => t('Requests'),
'value' => t('Total: @total', array('@total' => $total_queries)) . '<br />' . $chart,
);
// Chart for sessions per timeslot
$chart = apachesolr_stats_chart($granularity, $data_per_granularity['sessions_per_slot'], $start_timeslot, $last_timeslot, sizeof($sessions), sizeof($sessions) / ($last_timeslot -$start_timeslot + 1));
$report_elements['total_sessions_per'] = array(
'name' => t('Unique sessions'),
'value' => t('Total: @total', array('@total' => sizeof($sessions))) . '<br />' . $chart,
);
$report_elements['avg_queries_session'] = array(
'name' => t('Average requests per session'),
'value' => sprintf("%.1f", $total_queries / sizeof($sessions)),
);
// Chart for average time per timeslot
$data = array();
foreach ($data_per_granularity['total_time'] as $timeslot => $value) {
$data[$timeslot] = $value / $count_per_granularity[$timeslot];
}
// Call with average_empty = FALSE
$chart = apachesolr_stats_chart($granularity, $data, $start_timeslot, $last_timeslot, $total_queries, $time['total'] / $total_queries);
$report_elements['query_avg_time'] = array(
'name' => t('Average time per request (miliseconds)'),
'value' => sprintf("%s: %.2f ms / %s: %.2f ms / %s: %.2f ms", t('Minimum'), $time['min'], t('Average'), $time['total'] / $total_queries, t('Maximum'), $time['max'])
. '</br>'
. $chart,
);
// Most-used keywords
$report_elements['keywords'] = array(
'name' => t('Top search phrases'),
'value' => apachesolr_stats_report_frequent_keywords($page_id, $keywords, $keywords_noresults)
);
// Most-used keywords with no results
$report_elements['keywords_noresults'] = array(
'name' => t('Top search phrases with no results'),
'value' => apachesolr_stats_report_frequent_keywords($page_id, $keywords_noresults, $keywords_noresults, "error")
);
// Total spellchecker suggestions
$report_elements['spellchecker'] = array(
'name' => t('Total spellchecker suggestions'),
'value' => $suggestions,
);
// Chart for sort usage
$leyends = array();
foreach ($sort_usage as $key => $value) {
$leyends[] = drupal_encode_path($key);
}
$chl = implode('|', $leyends);
$chd = implode(',', $sort_usage);
$chart = "<img src='http://chart.apis.google.com/chart?cht=p3&chd=t:{$chd}&chs=350x100&chl={$chl}' />";
$report_elements['sort_usage'] = array(
'name' => t('Sort usage'),
'value' => $chart,
);
// Chart for field usage
$report_elements['field_usage'] = array(
'name' => t('Facet usage'),
'value' => apachesolr_stats_facet_usage_graph($facets) . apachesolr_stats_facet_usage_table($facets),
);
return $report_elements;
}
/**
* Recieves an array of keyword => count and reports the top-used terms.
* @param string $page_id
* ID of the search page.
* @param array $keywords
* array of keyword => count pairs.
* @param array $keywords_noresults
* array containing keywords that returned 0 results.
* @param string $class
* CSS class for each link.
* @param int $number
* Number of terms to show.
* @return string
*/
function apachesolr_stats_report_frequent_keywords($page_id, $keywords, $keywords_noresults, $class = '', $number = 25) {
$search_page = apachesolr_search_page_load($page_id);
$search_path = $search_page['search_path'];
if (empty($keywords)) {
return '';
}
arsort($keywords);
// Final elements are the most frequent, get $number elements off the array
$slice = array_slice($keywords, 0, $number);
// Calculate font size for display
$min = 1e9;
$max = -1e9;
$steps = 6;
$weighted_slice = array();
foreach ($slice as $word => $count) {
$min = min($min, $count);
$max = max($max, $count);
$weighted_slice[$word] = array(
'count' => $count,
'log_count' => log($count),
);
}
// Note: we need to ensure the range is slightly too large to make sure even
// the largest element is rounded down.
$range = max(.01, $max - $min) * 1.0001;
// Add "weight"
foreach ($weighted_slice as $word => $data) {
$weighted_slice[$word]['weight'] = floor($steps * ($data['count'] - $min) / $range);
}
$items = array();
foreach ($weighted_slice as $word => $data) {
if (isset($keywords_noresults[$word])) {
$class = "error";
}
$font_size_pct = 80 + ($data['weight']) * 12;
$items[] = l(
$word, $search_path . '/' . $word, array(
'attributes' => array(
'class' => $class,
'style' => 'font-size:' . sprintf("%d%%", $font_size_pct),
),
'absolute' => TRUE,
)
) . " (" . $data["count"] . ")";
}
return implode(", ", $items);
}
/**
* Return a listing of keywords for the Popular Searches block.
*/
function apachesolr_stats_block_frequent_keywords($page_id, $limit = 10) {
$search_page = apachesolr_search_page_load($page_id);
$search_path = $search_page['search_path'];
$cid = "apachesolr_stats_block_frequent_keywords_{$page_id}";
$cached = cache_get($cid, 'cache_block');
if ($cached && $cached->data && $cached->expire > REQUEST_TIME) {
return $cached->data;
}
$keywords = array();
// Return keywords only last week's logged queries.
$timestamp = REQUEST_TIME - 3600 * 24 * 7;
$result = db_query_range("SELECT keywords FROM {apachesolr_stats} WHERE
numfound > :numfound AND timestamp > :timestamp AND page_id = :pageid
ORDER BY timestamp DESC", 0, 5000, array(':numfound' => 0, ':timestamp' => $timestamp, ':pageid' => $page_id));
foreach ($result as $record) {
if (trim($record->keywords) != "") {
// Keep track of individual keywords used
$keys_filtered = drupal_strtolower(trim($record->keywords));
if (isset($keywords[$keys_filtered])) {
$keywords[$keys_filtered]++;
}
else {
$keywords[$keys_filtered] = 1;
}
}
}
// Sort by most frequent first.
arsort($keywords);
// Get first $limit items.
$keywords = array_slice($keywords, 0, $limit);
$links = array();
foreach ($keywords as $key => $frequency) {
$links[] = l($key, $search_path . '/' . $key);
}
// Cache information for 10 minutes.
$expire = REQUEST_TIME + 600;
cache_set($cid, $links, 'cache_block', $expire);
return $links;
}
/**
* Implements hook_block_info().
*/
function apachesolr_stats_block_info() {
$search_pages = apachesolr_search_load_all_search_pages();
$enabled_search_pages = variable_get('apachesolr_stats_enabled', array());
$blocks = array();
// Default block settings.
foreach ($enabled_search_pages as $page_id) {
if ($page_id) {
$blocks[$page_id] = array(
'info' => t('Apache Solr Statistics: popular searches for @page_id page', array('@page_id' => $search_pages[$page_id]['label'])),
// Start out disabled
'status' => 0,
'region' => 'right',
// Should be cached equally across paths and roles.
'cache' => DRUPAL_CACHE_GLOBAL,
);
}
}
return $blocks;
}
/**
* Implements hook_block_view().
*/
function apachesolr_stats_block_view($delta) {
$search_pages = apachesolr_search_load_all_search_pages();
$links = apachesolr_stats_block_frequent_keywords($delta);
if ($links) {
// Return a block array.
$block = array(
'subject' => t('Popular searches for @page_name', array('@page_name' => $search_pages[$delta]['label'])),
'content' => theme('apachesolr_stats_block', array('links' => $links)),
);
return $block;
}
}
/**
* Implements hook_theme().
*/
function apachesolr_stats_theme() {
return array(
'apachesolr_stats_block' => array(
'variables' => array('links'),
),
);