The fetch_data() method is the core of your custom data source. It fetches your data and returns it in a format that Alpha Insights can understand and display.
public function fetch_data( WPDAI_Data_Warehouse $data_warehouse )
There is a single required parameter: the data warehouse instance. Filters (including date range) are not passed as a separate argument; use the data warehouse methods below to get them.
The data warehouse instance. Use it to:
$data_warehouse->get_date_from(), $data_warehouse->get_date_to()$data_warehouse->get_filter( 'date_format_display' ), $data_warehouse->get_filter() for all filters$data_warehouse->get_data_by_date_range_container()$data_warehouse->get_data( 'orders', 'totals' )See Data Warehouse API Reference for all available methods.
Must return an array with the following optional keys (all keys are optional, but must match the structure if provided):
array(
'totals' => array(...), // Aggregated totals/metrics
'categorized_data' => array(...), // Data grouped by categories
'data_table' => array(...), // Tabular data for data table widgets
'data_by_date' => array(...), // Time-series data keyed by date
'total_db_records' => 0, // Number of records fetched
)
// execution_time and memory_usage are added automatically by the warehouse
Aggregated totals/metrics. Keys are metric names, values are numeric values.
'totals' => array(
'total_custom_metric' => 1250.50,
'total_custom_count' => 42,
'average_custom_value' => 29.77,
'custom_percentage' => 15.5,
)
Important: Metric keys here must match keys defined in get_data_mapping() 'totals' section (if you provide mapping).
Data grouped by categories for pie charts, doughnut charts, bar charts, etc. Structure:
'categorized_data' => array(
'category_key' => array(
'label' => 'Category Display Name', // Optional: display name
'total_custom_metric' => 500.25, // Metric values
'total_custom_count' => 15,
// Additional metrics...
),
'another_category' => array(
'label' => 'Another Category',
'total_custom_metric' => 750.75,
'total_custom_count' => 20,
),
)
Notes:
get_data_mapping() 'categorized_data' 'metric_fields'Tabular data for data table widgets. Structure:
'data_table' => array(
'table_key' => array(
array(
'id' => 1,
'name' => 'Item 1',
'value' => 125.50,
'category' => 'category_a',
'date' => '2024-01-01',
),
array(
'id' => 2,
'name' => 'Item 2',
'value' => 250.75,
'category' => 'category_b',
'date' => '2024-01-02',
),
// More rows...
),
)
Notes:
get_data_mapping() 'data_table' sectionTime-series data keyed by date. This is used for line charts, area charts, and time-based bar charts.
CRITICAL: Always initialize metrics using the date range container from the data warehouse to ensure proper date alignment:
$date_range_container = $data_warehouse->get_data_by_date_range_container();
'data_by_date' => array(
'metric_key' => $date_range_container, // Initialize with container
// Then populate with your data
)
Complete example:
$date_range_container = $data_warehouse->get_data_by_date_range_container();
$data_by_date = array(
'total_custom_metric_by_date' => $date_range_container,
'total_custom_count_by_date' => $date_range_container,
);
// Populate with actual data
foreach ( array_keys( $date_range_container ) as $date_key ) {
// Fetch or calculate your data for this date
$data_by_date['total_custom_metric_by_date'][ $date_key ] = /* your value */;
$data_by_date['total_custom_count_by_date'][ $date_key ] = /* your value */;
}
Why use the date container?
Important: Metric keys must match keys defined in get_data_mapping() 'data_by_date' section (if you provide mapping).
The number of database records processed. Used for performance tracking and debugging.
'total_db_records' => 150
DO NOT include this in your return array. Execution time is automatically measured and added by the data warehouse.
The data warehouse automatically tracks execution time by:
microtime(true) before calling your fetch_data() methodmicrotime(true) after your method returnsYou do not need to manually track or return execution time. If you include it in your return array, it will be ignored and replaced with the automatically calculated value.
DO NOT include this in your return array. Memory usage is automatically measured and added by the data warehouse (in bytes).
The data warehouse automatically tracks memory usage by:
memory_get_usage(true) before calling your fetch_data() methodmemory_get_usage(true) after your method returnsThis provides accurate memory consumption metrics for performance monitoring and debugging.
Important: You do NOT need to manually track performance metrics. The data warehouse automatically tracks:
These metrics are automatically measured before and after your fetch_data() method is called and are automatically added to the stored data structure. Do not include them in your return array.
$date_from = $data_warehouse->get_date_from();
$date_to = $data_warehouse->get_date_to();
$date_format_display = $data_warehouse->get_filter( 'date_format_display', 'day' );
Always initialize time-series metrics with the date range container:
$date_range_container = $data_warehouse->get_data_by_date_range_container();
$data_by_date['my_metric'] = $date_range_container;
// Then populate with data
Execution time and memory usage are automatically tracked. Do not include them in your return array:
// DON'T do this:
return array(
'totals' => $totals,
'execution_time' => microtime( true ) - $start_time, // ❌ Don't include
'memory_usage' => $memory_used, // ❌ Don't include
);
// DO this instead:
return array(
'totals' => $totals,
// execution_time and memory_usage are automatically added
);
global $wpdb;
$date_from = $data_warehouse->get_date_from();
$date_to = $data_warehouse->get_date_to();
$results = $wpdb->get_results( $wpdb->prepare(
"SELECT * FROM {$wpdb->prefix}my_table WHERE date >= %s AND date <= %s",
$date_from,
$date_to
) );
try {
// Fetch data
$data = fetch_from_api();
} catch ( Exception $e ) {
// Log error and return empty structure
error_log( 'Custom data source error: ' . $e->getMessage() );
return array(
'totals' => array(),
// ... other empty arrays
);
}
public function fetch_data( WPDAI_Data_Warehouse $data_warehouse ) {
// Get date range from warehouse
$date_from = $data_warehouse->get_date_from();
$date_to = $data_warehouse->get_date_to();
// Get date range container
$date_range_container = $data_warehouse->get_data_by_date_range_container();
// Fetch data from database
global $wpdb;
$results = $wpdb->get_results( $wpdb->prepare(
"SELECT * FROM {$wpdb->prefix}my_table WHERE date >= %s AND date <= %s",
$date_from,
$date_to
) );
// Build totals
$totals = array(
'total_metric' => 0,
'total_count' => 0,
);
// Build data_by_date
$data_by_date = array(
'metric_by_date' => $date_range_container,
'count_by_date' => $date_range_container,
);
// Process results
foreach ( $results as $row ) {
// Update totals
$totals['total_metric'] += floatval( $row->metric_value );
$totals['total_count']++;
// Update data_by_date
$date_key = date( 'Y-m-d', strtotime( $row->date ) );
if ( isset( $data_by_date['metric_by_date'][ $date_key ] ) ) {
$data_by_date['metric_by_date'][ $date_key ] += floatval( $row->metric_value );
$data_by_date['count_by_date'][ $date_key ]++;
}
}
// Return data structure (execution_time and memory_usage are added by the warehouse)
return array(
'totals' => $totals,
'data_by_date' => $data_by_date,
'total_db_records' => count( $results ),
);
}