mirror of
https://github.com/farmOS/farmOS.git
synced 2024-02-23 11:37:38 +01:00
Create farm_metrics.json endpoint to expose metrics over API.
This commit is contained in:
parent
6a114b7fc8
commit
ec82d5d9d6
2 changed files with 71 additions and 0 deletions
21
modules/farm/farm_metrics/farm_metrics.farm_access.inc
Normal file
21
modules/farm/farm_metrics/farm_metrics.farm_access.inc
Normal file
|
@ -0,0 +1,21 @@
|
|||
<?php
|
||||
/**
|
||||
* @file
|
||||
* Farm access hooks implemented by farm_metrics module.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Implements hook_farm_access_perms().
|
||||
*/
|
||||
function farm_metrics_farm_access_perms($role) {
|
||||
// Load the list of farm roles.
|
||||
$roles = farm_access_roles();
|
||||
|
||||
// If this role has 'config' access, grant area generator access.
|
||||
if (!empty($roles[$role]['access']['config'])) {
|
||||
return array('access farm metrics');
|
||||
}
|
||||
else {
|
||||
return array();
|
||||
}
|
||||
}
|
|
@ -13,3 +13,53 @@ function farm_metrics_hook_info() {
|
|||
);
|
||||
return $hooks;
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_permission().
|
||||
*/
|
||||
function farm_metrics_permission() {
|
||||
$perms = array(
|
||||
'access farm metrics' => array(
|
||||
'title' => t('Access the farmOS metrics endpoint'),
|
||||
),
|
||||
);
|
||||
return $perms;
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_menu().
|
||||
*/
|
||||
function farm_metrics_menu() {
|
||||
|
||||
// General farm metrics JSON endpoint.
|
||||
$items['farm_metrics.json'] = array(
|
||||
'page callback' => 'farm_metrics_info',
|
||||
'access arguments' => array('access farm metrics'),
|
||||
'type' => MENU_CALLBACK,
|
||||
);
|
||||
|
||||
return $items;
|
||||
}
|
||||
|
||||
/**
|
||||
* Farm metrics API callback.
|
||||
*/
|
||||
function farm_metrics_info() {
|
||||
|
||||
// Ask modules for metrics.
|
||||
$metrics = module_invoke_all('farm_metrics');
|
||||
|
||||
// Make sure all metrics have defaults set.
|
||||
$defaults = array(
|
||||
'label' => '',
|
||||
'value' => '0',
|
||||
'link' => 'farm',
|
||||
'weight' => 0,
|
||||
);
|
||||
foreach ($metrics as $key => $metric) {
|
||||
$label = $metric['label'];
|
||||
$metrics[$key] = array_merge($defaults, $metric);
|
||||
}
|
||||
|
||||
drupal_json_output($metrics);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue