Пример #1
0
 public function execute()
 {
     ob_start();
     $apisrv_stats = OkapiServiceRunner::call('services/apisrv/stats', new OkapiInternalRequest(new OkapiInternalConsumer(), null, array()));
     $active_apps_count = Db::select_value("\n            select count(distinct s.consumer_key)\n            from\n                okapi_stats_hourly s,\n                okapi_consumers c\n            where\n                s.consumer_key = c.`key`\n                and s.period_start > date_add(now(), interval -7 day)\n        ");
     $weekly_stats = Db::select_row("\n            select\n                sum(s.http_calls) as total_http_calls,\n                sum(s.http_runtime) as total_http_runtime\n            from okapi_stats_hourly s\n            where\n                s.consumer_key != 'internal' -- we don't want to exclude 'anonymous' nor 'facade'\n                and s.period_start > date_add(now(), interval -7 day)\n        ");
     print "Hello! This is your weekly summary of OKAPI usage.\n\n";
     print "Apps active this week: " . $active_apps_count . " out of " . $apisrv_stats['apps_count'] . ".\n";
     print "Total of " . $weekly_stats['total_http_calls'] . " requests were made (" . sprintf("%01.1f", $weekly_stats['total_http_runtime']) . " seconds).\n\n";
     $consumers = Db::select_all("\n            select\n                s.consumer_key,\n                c.name,\n                sum(s.http_calls) as http_calls,\n                sum(s.http_runtime) as http_runtime\n            from\n                okapi_stats_hourly s\n                left join okapi_consumers c\n                    on s.consumer_key = c.`key`\n            where s.period_start > date_add(now(), interval -7 day)\n            group by s.consumer_key\n            having sum(s.http_calls) > 0\n            order by sum(s.http_calls) desc\n        ");
     print "== Consumers ==\n\n";
     print "Consumer name                         Calls     Runtime\n";
     print "----------------------------------- ------- -----------\n";
     foreach ($consumers as $row) {
         $name = $row['name'];
         if ($row['consumer_key'] == 'anonymous') {
             $name = "Anonymous (Level 0 Authentication)";
         } elseif ($row['consumer_key'] == 'facade') {
             $name = "Internal usage via Facade";
         }
         if (mb_strlen($name) > 35) {
             $name = mb_substr($name, 0, 32) . "...";
         }
         print self::mb_str_pad($name, 35, " ", STR_PAD_RIGHT);
         print str_pad($row['http_calls'], 8, " ", STR_PAD_LEFT);
         print str_pad(sprintf("%01.2f", $row['http_runtime']), 11, " ", STR_PAD_LEFT) . "s\n";
     }
     print "\n";
     $methods = Db::select_all("\n            select\n                s.service_name,\n                sum(s.http_calls) as http_calls,\n                sum(s.http_runtime) as http_runtime\n            from okapi_stats_hourly s\n            where s.period_start > date_add(now(), interval -7 day)\n            group by s.service_name\n            having sum(s.http_calls) > 0\n            order by sum(s.http_calls) desc\n        ");
     print "== Methods ==\n\n";
     print "Service name                          Calls     Runtime      Avg\n";
     print "----------------------------------- ------- ----------- --------\n";
     foreach ($methods as $row) {
         $name = $row['service_name'];
         if (mb_strlen($name) > 35) {
             $name = mb_substr($name, 0, 32) . "...";
         }
         print self::mb_str_pad($name, 35, " ", STR_PAD_RIGHT);
         print str_pad($row['http_calls'], 8, " ", STR_PAD_LEFT);
         print str_pad(sprintf("%01.2f", $row['http_runtime']), 11, " ", STR_PAD_LEFT) . "s";
         print str_pad(sprintf("%01.4f", $row['http_calls'] > 0 ? $row['http_runtime'] / $row['http_calls'] : 0), 8, " ", STR_PAD_LEFT) . "s\n";
     }
     print "\n";
     $oauth_users = Db::select_all("\n            select\n                c.name,\n                count(*) as users\n            from\n                okapi_authorizations a,\n                okapi_consumers c\n            where a.consumer_key = c.`key`\n            group by a.consumer_key\n            having count(*) >= 5\n            order by count(*) desc;\n        ");
     print "== Current OAuth usage by Consumers with at least 5 users ==\n\n";
     print "Consumer name                         Users\n";
     print "----------------------------------- -------\n";
     foreach ($oauth_users as $row) {
         $name = $row['name'];
         if (mb_strlen($name) > 35) {
             $name = mb_substr($name, 0, 32) . "...";
         }
         print self::mb_str_pad($name, 35, " ", STR_PAD_RIGHT);
         print str_pad($row['users'], 8, " ", STR_PAD_LEFT) . "\n";
     }
     print "\n";
     print "This report includes requests from external consumers and those made via\n";
     print "Facade class (used by OC code). It does not include methods used by OKAPI\n";
     print "internally (i.e. while running cronjobs). Runtimes do not include HTTP\n";
     print "request handling overhead.\n";
     $message = ob_get_clean();
     Okapi::mail_admins("Weekly OKAPI usage report", $message);
 }
Пример #2
0
 public static function call(OkapiRequest $request)
 {
     # Read the parameters.
     $acodes = $request->get_parameter('acodes');
     if (!$acodes) {
         throw new ParamMissing('acodes');
     }
     $acodes = explode("|", $acodes);
     $langpref = $request->get_parameter('langpref');
     if (!$langpref) {
         $langpref = "en";
     }
     $langpref = explode("|", $langpref);
     $fields = $request->get_parameter('fields');
     if (!$fields) {
         $fields = "name";
     }
     $fields = explode("|", $fields);
     foreach ($fields as $field) {
         if (!in_array($field, self::$valid_field_names)) {
             throw new InvalidParam('fields', "'{$field}' is not a valid field code.");
         }
     }
     $forward_compatible = $request->get_parameter('forward_compatible');
     if (!$forward_compatible) {
         $forward_compatible = "true";
     }
     if (!in_array($forward_compatible, array("true", "false"))) {
         throw new InvalidParam('forward_compatible');
     }
     $forward_compatible = $forward_compatible == "true";
     # Load the attributes (all of them).
     require_once 'attr_helper.inc.php';
     $attrdict = AttrHelper::get_attrdict();
     # For each A-code, check if it exists, filter its fields and add it
     # to the results.
     $results = array();
     foreach ($acodes as $acode) {
         /* Please note, that the $attr variable from the $attrdict dictionary
          * below is NOT fully compatible with the interface of the "attribute"
          * method. Some of $attr's fields are private and should not be exposed,
          * other fields don't exist and have to be added dynamically! */
         if (isset($attrdict[$acode])) {
             $attr = $attrdict[$acode];
         } elseif ($forward_compatible) {
             $attr = AttrHelper::get_unknown_placeholder($acode);
         } else {
             $results[$acode] = null;
             continue;
         }
         # Fill langpref-specific fields.
         $attr['name'] = Okapi::pick_best_language($attr['names'], $langpref);
         $attr['description'] = Okapi::pick_best_language($attr['descriptions'], $langpref);
         # Fill some other fields (not kept in the cached attrdict).
         $attr['is_locally_used'] = $attr['internal_id'] !== null;
         $attr['is_deprecated'] = $attr['is_discontinued'];
         // deprecated and undocumetned field, see issue 70
         # Add to results.
         $results[$acode] = $attr;
     }
     # If the user wanted local_icon_urls, fetch them now. (We cannot cache them
     # in the $attrdict because currently we have no way of knowing then they
     # change.)
     if (in_array('local_icon_url', $fields)) {
         $tmp = Db::select_all("\n                select id, icon_large\n                from cache_attrib\n            ");
         $map = array();
         foreach ($tmp as &$row_ref) {
             $map[$row_ref['id']] =& $row_ref;
         }
         $prefix = Settings::get('SITE_URL');
         foreach ($results as &$attr_ref) {
             $internal_id = $attr_ref['internal_id'];
             if (isset($map[$internal_id])) {
                 $row = $map[$internal_id];
                 $attr_ref['local_icon_url'] = $prefix . $row['icon_large'];
             } else {
                 $attr_ref['local_icon_url'] = null;
             }
         }
     }
     # Filter the fields.
     foreach ($results as &$attr_ref) {
         $clean_row = array();
         foreach ($fields as $field) {
             $clean_row[$field] = $attr_ref[$field];
         }
         $attr_ref = $clean_row;
     }
     return Okapi::formatted_response($request, $results);
 }