Project

General

Profile

« Previous | Next » 

Revision e2464cf8

Added by Andreas Kohlbecker almost 11 years ago

new debug block implemented, see #3316 (dataportal debug box as table and as block)

View differences:

7.x/modules/cdm_dataportal/cdm_api/cdm_api.module
63 63
  return $items;
64 64
}
65 65

  
66
/**
67
 * Implements hook_block_info().
68
 */
69
function cdm_api_block_info() {
70

  
71
  $block['cdm_ws_debug'] = array(
72
      "info" => t("CDM web service debug"),
73
      "cache" => DRUPAL_NO_CACHE
74
  );
75
  return $block;
76
}
77

  
78
/**
79
 * Implements hook_block_view().
80
 */
81
function cdm_api_block_view($delta) {
82
  switch ($delta) {
83
    case 'cdm_ws_debug':
84

  
85
    $field_map = array(
86
        'ws_uri' => t('URI'),
87
        'time' => t('Time'),
88
        'fetch_seconds' => t('Fetching [s]'),
89
        'parse_seconds' => t('Parsing [s]'),
90
        'size_kb' => t('Size [kb]'),
91
        'status' => t('Status'),
92
        'data_links' =>  t('Links'),
93
    );
94

  
95

  
96
    if (!isset($_SESSION['cdm']['ws_debug'])) {
97
      $_SESSION['cdm']['ws_debug'] = array();
98
    }
99

  
100
    $header = '<thead><tr><th>' . join('</th><th>' , array_values($field_map)) . '</th></thead>';
101
    $footer = '<tfoot><tr><th>' . join('</th><th>' , array_values($field_map)) . '</th></tfoot>';
102
    $rows = array();
103

  
104
    foreach ($_SESSION['cdm']['ws_debug'] as $data){
105
      $cells = array();
106
      $data = unserialize($data);
107
      foreach ($field_map as $field => $label){
108
        $cells[] = '<td class="' . $field . '">' .  $data[$field] . '</td>';
109
      }
110
      $rows[] = '<tr class="' . $data['status']  . '">' . join('' , $cells). '</tr>';
111
    }
112
    // clear session again
113
    $_SESSION['cdm']['ws_debug'] = array();
114

  
115
    _add_js_ws_debug();
116

  
117
    $block['subject'] = t('CDM Debug');
118
    // cannot use theme_tabel() since table footer is not jet supported in D7
119
    $block['content'] =
120
        '<table id="cdm-ws-debug-table">'
121
        . $header
122
        . '<tbody>' . join('', $rows) . '</tbody>'
123
        . $footer
124
        . '</table>';
125

  
126
    return $block;
127
  }
128
}
129

  
66 130
/**
67 131
 * Implements hook_cron().
68 132
 *
......
1432 1496
    //
1433 1497
    $obj = $cacheL1_obj;
1434 1498
    if (variable_get('cdm_webservice_debug', 1) && user_access('administer')) {
1435
      _add_status_message_toggler();
1436
      _add_debugMessageStr('Using cacheL1 for: ' . $uri);
1499
      cdm_ws_debug_add($uri, 0, 0, NULL, 'cacheL1');
1437 1500
    }
1438 1501
  }
1439 1502
  elseif ($cacheL2_entry) {
1440 1503
    //
1441 1504
    // The object has been found in the L2 cache.
1442 1505
    //
1506
    $duration_parse_start = microtime(TRUE);
1443 1507
    $obj = unserialize($cacheL2_entry->data);
1508
    $duration_parse = microtime(TRUE) - $duration_parse_start;
1444 1509

  
1445 1510
    if (variable_get('cdm_webservice_debug', 1) && user_access('administer')) {
1446
      _add_status_message_toggler();
1447
      _add_debugMessageStr('Using cacheL2 for: ' . $uri);
1511
      cdm_ws_debug_add($uri, 0, $duration_parse, NULL, 'cacheL2');
1448 1512
    }
1449 1513
  }
1450 1514
  else {
1451 1515
    //
1452 1516
    // Get the object from the webservice and cache it.
1453 1517
    //
1454
    $time_get_start = microtime(TRUE);
1518
    $duration_fetch_start = microtime(TRUE);
1455 1519
    // Request data from webservice JSON or XML.
1456 1520
    $response = cdm_http_request($uri, $method);
1457 1521
    $datastr = NULL;
1458 1522
    if (isset($response->data)) {
1459 1523
      $datastr = $response->data;
1460 1524
    }
1461
    $time_get = microtime(TRUE) - $time_get_start;
1462
    $time_parse_start = microtime(TRUE);
1525
    $duration_fetch = microtime(TRUE) - $duration_fetch_start;
1526
    $duration_parse_start = microtime(TRUE);
1463 1527

  
1464 1528
    // Parse data and create object.
1465 1529
    $obj = cdm_load_obj($datastr);
1466 1530

  
1467
    $time_parse = microtime(TRUE) - $time_parse_start;
1531
    $duration_parse = microtime(TRUE) - $duration_parse_start;
1468 1532
    if (variable_get('cdm_webservice_debug', 1) && user_access('administer')) {
1469 1533
      if ($obj || $datastr == "[]") {
1470
        $success_msg = 'valid';
1534
        $status = 'valid';
1471 1535
      }
1472 1536
      else {
1473
        $success_msg = 'invalid';
1537
        $status = 'invalid';
1474 1538
      }
1475
      _add_debugMessage($uri, $time_get, $time_parse, strlen($datastr), $success_msg);
1539
      cdm_ws_debug_add($uri, $duration_fetch, $duration_parse, strlen($datastr), $status);
1476 1540
    }
1477 1541
    if ($set_cacheL2) {
1478 1542
      // Store the object in cache L2.
......
1491 1555
}
1492 1556

  
1493 1557
/**
1494
 * @todo Please document this function.
1495
 * @see http://drupal.org/node/1354
1496
 */
1497
function _add_debugMessageStr($message) {
1498
  _add_status_message_toggler();
1499
  drupal_set_message(check_plain($message), 'debug');
1500
}
1501

  
1502
/**
1503
 * @todo Please document this function.
1504
 * @see http://drupal.org/node/1354
1558
 * Processes and stores the given information in $_SESSION['cdm']['ws_debug'] as table row.
1559
 *
1560
 * The cdm_ws_debug block will display the debug information.
1561
 *
1562
 * @param $uri
1563
 *    The CDM REST URI to which the request has been send
1564
 * @param $duration_fetch
1565
 *    The time in seconds it took to fetch the data from the web service
1566
 * @param $duration_parse
1567
 *    Time in seconds which was needed to parse the json response
1568
 * @param $datasize
1569
 *    Size of the data received from the server
1570
 * @param $status
1571
 *    A status string, possible values are: 'valid', 'invalid', 'cacheL1', 'cacheL2'
1572
 * @return bool
1573
 *    TRUE if adding the debug information was successful
1505 1574
 */
1506
function _add_debugMessage($uri, $time_get, $time_parse, $datasize, $success_msg) {
1507
  static $cumulated_time_parse = 0;
1508
  static $cumulated_time_get = 0;
1509
  _add_status_message_toggler();
1575
function cdm_ws_debug_add($uri, $duration_fetch, $duration_parse, $datasize, $status) {
1510 1576

  
1511
  $cumulated_time_get += $time_get;
1512
  $cumulated_time_parse += $time_parse;
1577
  static $initial_time = NULL;
1578
  if(!$initial_time) {
1579
    $initial_time = microtime(TRUE);
1580
  }
1581
  $time = microtime(TRUE) - $initial_time;
1513 1582

  
1514 1583
  // Decompose uri into path and query element.
1515 1584
  // URI query elements are stored in an array
......
1532 1601
    $path = $uri;
1533 1602
  }
1534 1603

  
1535
  $message = '<span class="uri">' . $uri . '</span><br />';
1536
  $message .= '[fetched in: ' . sprintf('%3.3f', $time_get) . 's(' . sprintf('%3.3f', $cumulated_time_get) . 's); ';
1537
  $message .= 'parsed in ' . sprintf('%3.3f', $time_parse) . ' s(' . sprintf('%3.3f', $cumulated_time_parse) . 's); ';
1538
  $message .= 'size:' . sprintf('%3.1f', ($datasize / 1024)) . ' kb of ' . $success_msg . ' data: ';
1604
  // data links to make data accecsible as json and xml
1605
  $data_links = '';
1539 1606
  if (_is_cdm_ws_uri($path)) {
1540
    $message .= '<a href="' . url($path . '.xml', array(
1541
      'query' => $query,
1542
    )) . '" target="data" class="' . $success_msg . '">xml</a>-';
1543
    $message .= '<a href="' . url('cdm_api/proxy/' . urlencode(url($path . '.xml', array(
1544
      'query' => $query,
1545
    )))) . '" target="data" class="' . $success_msg . '">proxied</a>,';
1546
    $message .= '<a href="' . url($path . '.json', array(
1547
      'query' => $query,
1548
    )) . '" target="data" class="' . $success_msg . '">json</a>-';
1549
    $message .= '<a href="' . url('cdm_api/proxy/' . urlencode(url($path . '.json', array(
1550
      'query' => $query,
1551
    )))) . '" target="data" class="' . $success_msg . '">proxied</a>';
1607
    $data_links .= '<a href="'
1608
        . url($path . '.xml', array('query' => $query))
1609
        . '" target="data">xml</a>-';
1610
    $data_links .= '<a href="'
1611
        . url('cdm_api/proxy/' . urlencode(url($path . '.xml', array('query' => $query))))
1612
        . '" target="data">proxied</a>';
1613
    $data_links .= '<br/>';
1614
    $data_links .= '<a href="'
1615
        . url($path . '.json', array('query' => $query))
1616
        . '" target="data">json</a>-';
1617
    $data_links .= '<a href="'
1618
        . url('cdm_api/proxy/' . urlencode(url($path . '.json', array('query' => $query))))
1619
        . '" target="data">proxied</a>';
1552 1620
  }
1553 1621
  else {
1554
    $message .= '<a href="' . url($path, array(
1555
      'query' => $query,
1556
    )) . '" target="data" class="' . $success_msg . '">open</a>';
1622
    $data_links .= '<a href="' . url($path, array(
1623
        'query' => $query)) . '" target="data">open</a>';
1624
  }
1625

  
1626
  //
1627
  $data = array(
1628
      'ws_uri' => $uri,
1629
      'time' => sprintf('%3.3f', $time),
1630
      'fetch_seconds' => sprintf('%3.3f', $duration_fetch),
1631
      'parse_seconds' => sprintf('%3.3f', $duration_parse),
1632
      'size_kb' => sprintf('%3.1f', ($datasize / 1024)) ,
1633
      'status' => $status,
1634
      'data_links' => $data_links
1635
  );
1636
  if (!isset($_SESSION['cdm']['ws_debug'])) {
1637
    $_SESSION['cdm']['ws_debug'] = array();
1557 1638
  }
1558
  $message .= '] ';
1559
  drupal_set_message(($message), 'debug');
1639
  $_SESSION['cdm']['ws_debug'][] = serialize($data);
1640

  
1641
  // Mark this page as being uncacheable.
1642
  // taken over from drupal_get_messages() but it is unsure if we really need this here
1643
  drupal_page_is_cacheable(FALSE);
1644

  
1645
  // Messages not set when DB connection fails.
1646
  return isset($_SESSION['cdm']['ws_debug']) ? $_SESSION['cdm']['ws_debug'] : NULL;
1560 1647
}
1561 1648

  
1562 1649
/**
......
2005 2092
}
2006 2093

  
2007 2094
/**
2008
 * Implementation of theme_status_messages($display = NULL).
2095
 * Adds java script to create and enable a toggler for the cdm webservice debug block content.
2009 2096
 *
2010
 * @see includes/theme.inc
2011
 *
2012
 * @return void
2013 2097
 */
2014
function _add_status_message_toggler() {
2015
  static $isAdded = FALSE;
2016
  if (!$isAdded) {
2017
    drupal_add_js(
2018
    'jQuery(document).ready(function($){
2019
       $(\'.messages.debug\').before( \'<h6 class="messages_toggler debug">Debug Messages (click to toggle)</h6>\' );
2020
       $(\'.messages_toggler\').click(function(){
2021
         $(this).next().slideToggle(\'fast\');
2022
         return false;
2023
       }).next().hide();
2024
     });', array('type' => 'inline'));
2098
function _add_js_ws_debug() {
2099

  
2100
  drupal_add_js(drupal_get_path('module', 'cdm_dataportal') . '/js/DataTables-1.9.4/media/js/jquery.dataTables.min.js',
2101
    array(
2102
      'type' => 'file',
2103
      'weight' => JS_LIBRARY,
2104
      'cache' => TRUE)
2105
    );
2106

  
2107
  drupal_add_js(drupal_get_path('module', 'cdm_dataportal') . '/js/colorbox/jquery.colorbox-min.js',
2108
    array(
2109
      'type' => 'file',
2110
      'weight' => JS_LIBRARY,
2111
      'cache' => TRUE)
2112
    );
2113
  drupal_add_css(drupal_get_path('module', 'cdm_dataportal') . '/js/colorbox/colorbox.css');
2114
  drupal_add_css(drupal_get_path('module', 'cdm_dataportal') . '/js/DataTables-1.9.4/media/css/cdm_debug_table.css');
2115

  
2116
  drupal_add_js(drupal_get_path('module', 'cdm_dataportal') . '/js/ws_debug_block.js',
2117
    array(
2118
      'type' => 'file',
2119
      'weight' => JS_LIBRARY,
2120
      'cache' => TRUE)
2121
    );
2025 2122

  
2026
    $isAdded = TRUE;
2027
  }
2028 2123
}
2029 2124

  
2030 2125
/**

Also available in: Unified diff