Skip to content
Extraits de code Groupes Projets
Valider 1a6ab307 rédigé par diosmosis's avatar diosmosis
Parcourir les fichiers

Refs #4200, started documentation of DataTable class.

parent 4535508d
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
...@@ -27,121 +27,83 @@ use ReflectionClass; ...@@ -27,121 +27,83 @@ use ReflectionClass;
require_once PIWIK_INCLUDE_PATH . '/core/Common.php'; require_once PIWIK_INCLUDE_PATH . '/core/Common.php';
/** /**
* * The primary data structure used to store analytics data in Piwik.
* ---- DataTable *
* A DataTable is a data structure used to store complex tables of data. * ### The Basics
* *
* A DataTable is composed of multiple DataTable\Row. * DataTables consist of rows and each row consists of columns. A column value can be
* A DataTable can be applied one or several DataTable_Filter. * be a numeric, string or array.
* A DataTable can be given to a DataTable_Renderer that would export the data under a given format (XML, HTML, etc.). *
* * DataTables are hierarchical data structures. Each row can also contain an additional
* A DataTable has the following features: * nested sub-DataTable.
* - serializable to be stored in the DB *
* - loadable from the serialized version * Both DataTables and DataTable rows can hold **metadata**. _DataTable metadata_ is information
* - efficient way of loading data from an external source (from a PHP array structure) * regarding all the data, such as the site or period that the data is for. _Row metadata_
* - very simple interface to get data from the table * is information regarding that row, such as a browser logo or website URL.
* *
* ---- DataTable\Row * Finally, DataTables all contain a special _summary_ row.
* A DataTableRow in the table is defined by *
* - multiple columns (a label, multiple values, ...) * ### Populating DataTables
* - optional metadata *
* - optional - a sub DataTable associated to this row * Data can be added to DataTables in a couple different ways. You can either:
* *
* Simple row example: * 1. create rows one by one and add them through [addRow](#addRow) then truncate if desired,
* - columns = array( 'label' => 'Firefox', * 2. create an array of DataTable\Row instances or an array of arrays and add them using
* 'visitors' => 155, * [addRowsFromArray](#addRowsFromArray) or [addRowsFromSimpleArray](#addRowsFromSimpleArray)
* 'pages' => 214, * then truncate if desired,
* 'bounce_rate' => 67) * 3. or set the maximum number of allowed rows (with [setMaximumAllowedRows](#setMaximumAllowedRows))
* - metadata = array('logo' => '/plugins/UserSettings/images/browsers/FF.gif') * and add rows one by one.
* - no sub DataTable *
* * If you want to eventually truncate your data (standard practice for all Piwik plugins),
* A more complex example would be a DataTable\Row that is associated to a sub DataTable. * the third method is the most memory efficient. It is, unfortunately, not always possible
* For example, for the row of the search engine Google, * to use since it requires that the data be sorted before adding.
* we want to get the list of keywords associated, with their statistics. *
* - columns = array( 'label' => 'Google', * ### Manipulating DataTables
* 'visits' => 1550, *
* 'visits_length' => 514214, * There are two main ways to manipulate a DataTable. You can either:
* 'returning_visits' => 77) *
* - metadata = array( 'logo' => '/plugins/Referrers/images/searchEngines/google.com.png', * 1. manually iterate through each row and manipulate the data,
* 'url' => 'http://google.com') * 2. or you can use predefined Filters.
* - DataTable = DataTable containing several DataTable\Row containing the keywords information for this search engine *
* Example of one DataTable\Row * A Filter is a class that has a 'filter' method which will manipulate a DataTable in
* - the keyword columns specific to this search engine = * some way. There are several predefined Filters that allow you to do common things,
* array( 'label' => 'Piwik', // the keyword * such as,
* 'visitors' => 155, // Piwik has been searched on Google by 155 visitors *
* 'pages' => 214 // Visitors coming from Google with the kwd Piwik have seen 214 pages * - add a new column to each row,
* ) * - add new metadata to each row,
* - the keyword metadata = array() // nothing here, but we could imagining storing the URL of the search in Google for example * - modify an existing column value for each row,
* - no subTable * - sort an entire DataTable,
* * - and more.
* *
* ---- DataTable_Filter * Using these Filters instead of writing your own code will increase code clarity and
* A DataTable_Filter is a applied to a DataTable and so * reduce code redundancy. Additionally, Filters have the advantage that they can be
* can filter information in the multiple DataTable\Row. * applied to DataTable\Map instances. So you can visit every DataTable in a DataTable\Map
* * without having to write a recursive visiting function.
* For example a DataTable_Filter can: *
* - remove rows from the table, * Note: Anonymous functions can be used as DataTable Filters.
* for example the rows' labels that do not match a given searched pattern *
* for example the rows' values that are less than a given percentage (low population) * ### Applying Filters
* - return a subset of the DataTable *
* for example a function that apply a limit: $offset, $limit * Filters can be applied now (via [filter](#filter)), or they can be applied later (via
* - add / remove columns * [queueFilter](#queueFilter)).
* for example adding a column that gives the percentage of a given value *
* - add some metadata * Filters that sort rows or manipulate the number of rows should be applied right away.
* for example the 'logo' path if the filter detects the logo * Non-essential, presentation filters should be queued.
* - edit the value, the label *
* - change the rows order * See also:
* for example if we want to sort by Label alphabetical order, or by any column value *
* * - ArchiveProcessor — to learn how DataTables are persisted.
* When several DataTable_Filter are to be applied to a DataTable they are applied sequentially. * - DataTable\Renderer — to learn how DataTable data is exported to XML, JSON, etc.
* A DataTable_Filter is assigned a priority. * - DataTable\Filter — to see all core Filters.
* For example, filters that * - DataTable\Manager — to learn how DataTables are loaded.
* - sort rows should be applied with the highest priority *
* - remove rows should be applied with a high priority as they prune the data and improve performance. * ### Examples
* *
* ---- Code example * **Populating a DataTable**
* * **Serializing & unserializing**
* $table = new DataTable(); * **Filtering for an API method**
* $table->addRowsFromArray( array(...) ); * ??? TODO
* *
* # sort the table by visits asc
* $filter = new DataTable_Filter_Sort( $table, 'visits', 'asc');
* $tableFiltered = $filter->getTableFiltered();
*
* # add a filter to select only the website with a label matching '*.com' (regular expression)
* $filter = new DataTable_Filter_Pattern( $table, 'label', '*(.com)');
* $tableFiltered = $filter->getTableFiltered();
*
* # keep the 20 elements from offset 15
* $filter = new DataTable_Filter_Limit( $tableFiltered, 15, 20);
* $tableFiltered = $filter->getTableFiltered();
*
* # add a column computing the percentage of visits
* # params = table, column containing the value, new column name to add, number of total visits to use to compute the %
* $filter = new DataTable_Filter_AddColumnPercentage( $tableFiltered, 'visits', 'visits_percentage', 2042);
* $tableFiltered = $filter->getTableFiltered();
*
* # we get the table as XML
* $xmlOutput = new DataTable_Exporter_Xml( $table );
* $xmlOutput->setHeader( ... );
* $xmlOutput->setColumnsToExport( array('visits', 'visits_percent', 'label') );
* $XMLstring = $xmlOutput->getOutput();
*
*
* ---- Other (ideas)
* We can also imagine building a DataTable_Compare which would take N DataTable that have the same
* structure and would compare them, by computing the percentages of differences, etc.
*
* For example
* DataTable1 = [ keyword1, 1550 visits]
* [ keyword2, 154 visits ]
* DataTable2 = [ keyword1, 1004 visits ]
* [ keyword3, 659 visits ]
* DataTable_Compare = result of comparison of table1 with table2
* [ keyword1, +154% ]
* [ keyword2, +1000% ]
* [ keyword3, -430% ]
*
* @package Piwik * @package Piwik
* @subpackage DataTable * @subpackage DataTable
* *
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter