3
0
Fork 0
mirror of https://github.com/farmOS/farmOS.git synced 2024-02-23 11:37:38 +01:00

Add a farm metrics module to collect high level metrics #230

This commit is contained in:
Michael Stenta 2020-02-26 11:18:43 -05:00
commit 383dbc77ae
11 changed files with 205 additions and 96 deletions

View file

@ -45,5 +45,6 @@ dependencies[] = farm_log
dependencies[] = farm_log_activity
dependencies[] = farm_log_observation
dependencies[] = farm_menu
dependencies[] = farm_metrics
dependencies[] = farm_people
dependencies[] = farm_ui

View file

@ -926,6 +926,13 @@ function farm_update_7051(&$sandbox) {
}
}
/**
* Install the Farm Metrics module.
*/
function farm_update_7052(&$sandbox) {
_farm_update_enable_modules(array('farm_metrics'));
}
/**
* Update helper function: enable modules.
*/

View file

@ -1,13 +1,13 @@
<?php
/**
* @file
* Farm dashboard hooks implemented by farm calendar module.
* Farm metrics hooks implemented by farm calendar module.
*/
/**
* Implements hook_farm_dashboard_metrics().
* Implements hook_farm_metrics().
*/
function farm_area_farm_dashboard_metrics() {
function farm_area_farm_metrics() {
// Start with empty metrics.
$metrics = array();
@ -15,7 +15,7 @@ function farm_area_farm_dashboard_metrics() {
// Count the number of areas.
$areas = db_query("SELECT COUNT(tid) FROM {taxonomy_term_data} t LEFT JOIN {taxonomy_vocabulary} v ON t.vid = v.vid WHERE v.machine_name = 'farm_areas'")->fetchField();
if (!empty($areas)) {
$metrics[] = array(
$metrics['areas'] = array(
'label' => t('Areas'),
'value' => $areas,
'link' => 'farm/areas',
@ -51,7 +51,7 @@ function farm_area_farm_dashboard_metrics() {
}
// Add a metric.
$metrics[] = array(
$metrics[$area_type] = array(
'label' => t('@area_type area', array('@area_type' => $label)),
'value' => $total_area,
'link' => 'farm/areas',

View file

@ -50,28 +50,6 @@ function hook_farm_dashboard_panes() {
);
}
/**
* Defines farm dashboard metrics.
*
* @return array
* Returns an array of metrics to be displays on the dashboard. Each should
* be an array containing the following keys:
* - label: Translated metric label.
* - value: The metric's value.
* - link: A path to link the value to.
* - weight: Weight for ordering (optional - defaults to alphabetical).
*/
function hook_farm_dashboard_metrics() {
return array(
array(
'label' => t('Example'),
'value' => '100',
'link' => 'farm/example',
'weight' => -10,
),
);
}
/**
* @}
*/

View file

@ -1,62 +0,0 @@
<?php
/**
* @file
* Farm dashboard hooks implemented by farm_dashboard module.
*/
/**
* Implements hook_farm_dashboard_panes().
*/
function farm_dashboard_farm_dashboard_panes() {
return array(
'farm_dashboard_metrics' => array(
'title' => t('Metrics'),
'callback' => 'farm_dashboard_metrics',
'group' => 'metrics',
),
);
}
/**
* Metrics dashboard callback.
*/
function farm_dashboard_metrics() {
// Ask modules for metrics.
$metrics = module_invoke_all('farm_dashboard_metrics');
if (empty($metrics)) {
return 'No metrics available.';
}
// Make sure all metrics have defaults set.
$defaults = array(
'label' => '',
'value' => '0',
'link' => 'farm',
'weight' => 0,
);
foreach ($metrics as $key => $metric) {
$metrics[$key] = array_merge($defaults, $metric);
}
// Sort the metrics by weight ascending, value descending.
$weight_index = array();
$value_index = array();
foreach ($metrics as $key => $row) {
$weight_index[$key] = $row['weight'];
$value_index[$key] = $row['value'];
}
array_multisort($weight_index, SORT_ASC, $value_index, SORT_DESC, $metrics);
// Iterate through the metrics and build rendered metrics.
$rendered_metrics = array();
foreach ($metrics as $metric) {
$rendered_metrics[] = '<li role="presentation"><a href="' . url($metric['link']) . '">' . $metric['label'] . ' <span class="badge">' . $metric['value'] . '</span></a></li>';
}
// Build and return the final output.
$output = '<ul class="nav nav-pills" role="tablist">';
$output .= implode('', $rendered_metrics);
$output .= '</ul>';
return $output;
}

View file

@ -8,9 +8,6 @@
* Implements hook_hook_info().
*/
function farm_dashboard_hook_info() {
$hooks['farm_dashboard_metrics'] = array(
'group' => 'farm_dashboard',
);
$hooks['farm_dashboard_panes'] = array(
'group' => 'farm_dashboard',
);

View file

@ -0,0 +1,51 @@
<?php
/**
* @file
* Hooks provided by farm_metrics.
*
* This file contains no working PHP code; it exists to provide additional
* documentation for doxygen as well as to document hooks in the standard
* Drupal manner.
*/
/**
* @defgroup farm_metrics Farm metrics module integrations.
*
* Module integrations with the farm_metrics module.
*/
/**
* @defgroup farm_metrics_hooks Farm metrics's hooks
* @{
* Hooks that can be implemented by other modules in order to extend farm_metrics.
*/
/**
* Defines farm metrics.
*
* @return array
* Returns an array of farm metrics. The key should be a unique metric
* name, and each should be an array of metric values including the
* following keys:
* - label: Translated metric label.
* - value: The metric's value.
* - link: A path to link the value to.
* - weight: Weight for ordering (optional - defaults to alphabetical).
*/
function hook_farm_metrics() {
$metrics = array();
$metrics['example'] = array(
'label' => t('Example'),
'value' => '100',
'link' => 'farm/example',
'weight' => -10,
);
return $metrics;
}
/**
* @}
*/

View file

@ -0,0 +1,46 @@
<?php
/**
* @file
* Farm dashboard hooks implemented by farm_metrics module.
*/
/**
* Implements hook_farm_dashboard_panes().
*/
function farm_metrics_farm_dashboard_panes() {
$panes = array();
if (user_access('access farm metrics')) {
$panes['farm_metrics'] = array(
'title' => t('Metrics'),
'callback' => 'farm_metrics_dashboard_pane',
'group' => 'metrics',
);
}
return $panes;
}
/**
* Metrics dashboard callback.
*/
function farm_metrics_dashboard_pane() {
// Load metrics.
$metrics = farm_metrics();
// If no metrics are available, show a message.
if (empty($metrics)) {
return 'No metrics available.';
}
// Iterate through the metrics and build rendered metrics.
$rendered_metrics = array();
foreach ($metrics as $metric) {
$rendered_metrics[] = '<li role="presentation"><a href="' . url($metric['link']) . '">' . $metric['label'] . ' <span class="badge">' . $metric['value'] . '</span></a></li>';
}
// Build and return the final output.
$output = '<ul class="nav nav-pills" role="tablist">';
$output .= implode('', $rendered_metrics);
$output .= '</ul>';
return $output;
}

View file

@ -0,0 +1,4 @@
name = Farm Metrics
description = Allow modules to define high level metrics.
core = 7.x
package = farmOS

View file

@ -0,0 +1,87 @@
<?php
/**
* @file
* Farm metrics module.
*/
/**
* Implements hook_hook_info().
*/
function farm_metrics_hook_info() {
$hooks['farm_metrics'] = array(
'group' => 'farm_metrics',
);
return $hooks;
}
/**
* Implements hook_permission().
*/
function farm_metrics_permission() {
$perms = array(
'access farm metrics' => array(
'title' => t('Access farm metrics'),
),
);
return $perms;
}
/**
* Implements hook_farm_access_perms().
*/
function farm_metrics_farm_access_perms($role) {
$perms = array();
// Access farm metrics.
$perms[] = 'access farm metrics';
return $perms;
}
/**
* Implements hook_farm_info().
*/
function farm_metrics_farm_info() {
if (user_access('access farm metrics')) {
return array(
'metrics' => farm_metrics(),
);
}
}
/**
* Returns a sorted array of all metrics.
*/
function farm_metrics() {
// Ask modules for metrics.
$metrics = module_invoke_all('farm_metrics');
// If no metrics are available, return an empty array.
if (empty($metrics)) {
return array();
}
// Make sure all metrics have defaults set.
$defaults = array(
'label' => '',
'value' => '0',
'link' => 'farm',
'weight' => 0,
);
foreach ($metrics as $key => $metric) {
$metrics[$key] = array_merge($defaults, $metric);
}
// Sort the metrics by weight ascending, value descending.
$weight_index = array();
$value_index = array();
foreach ($metrics as $key => $row) {
$weight_index[$key] = $row['weight'];
$value_index[$key] = $row['value'];
}
array_multisort($weight_index, SORT_ASC, $value_index, SORT_DESC, $metrics);
// Return metrics.
return $metrics;
}

View file

@ -2,13 +2,13 @@
/**
* @file
* Farm dashboard hooks implemented by farm_ui module.
* Farm metrics hooks implemented by farm_ui module.
*/
/**
* Implements hook_farm_dashboard_metrics().
* Implements hook_farm_metrics().
*/
function farm_ui_farm_dashboard_metrics() {
function farm_ui_farm_metrics() {
/**
* @todo
@ -35,7 +35,7 @@ function farm_ui_farm_dashboard_metrics() {
}
// Build a metric.
$metrics[] = array(
$metrics[$type] = array(
'label' => $info['label_plural'],
'value' => $count,
'link' => farm_ui_view_page_path($info['view']),