{"id":18550294,"url":"https://github.com/xp-forge/pivot","last_synced_at":"2025-05-15T10:08:59.327Z","repository":{"id":31815004,"uuid":"35381668","full_name":"xp-forge/pivot","owner":"xp-forge","description":"Pivot table","archived":false,"fork":false,"pushed_at":"2024-03-24T13:11:53.000Z","size":35,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-12T23:02:12.554Z","etag":null,"topics":["pivot-tables","xp-framework"],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/xp-forge.png","metadata":{"files":{"readme":"README.md","changelog":"ChangeLog.md","contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-05-10T17:54:11.000Z","updated_at":"2022-02-28T21:24:19.000Z","dependencies_parsed_at":"2022-08-17T18:55:32.207Z","dependency_job_id":null,"html_url":"https://github.com/xp-forge/pivot","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xp-forge%2Fpivot","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xp-forge%2Fpivot/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xp-forge%2Fpivot/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xp-forge%2Fpivot/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/xp-forge","download_url":"https://codeload.github.com/xp-forge/pivot/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254319722,"owners_count":22051075,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["pivot-tables","xp-framework"],"created_at":"2024-11-06T21:04:07.973Z","updated_at":"2025-05-15T10:08:59.234Z","avatar_url":"https://github.com/xp-forge.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"Pivot table\n===========\n\n[![Build status on GitHub](https://github.com/xp-forge/pivot/workflows/Tests/badge.svg)](https://github.com/xp-forge/pivot/actions)\n[![XP Framework Module](https://raw.githubusercontent.com/xp-framework/web/master/static/xp-framework-badge.png)](https://github.com/xp-framework/core)\n[![BSD Licence](https://raw.githubusercontent.com/xp-framework/web/master/static/licence-bsd.png)](https://github.com/xp-framework/core/blob/master/LICENCE.md)\n[![Requires PHP 7.0+](https://raw.githubusercontent.com/xp-framework/web/master/static/php-7_0plus.svg)](http://php.net/)\n[![Supports PHP 8.0+](https://raw.githubusercontent.com/xp-framework/web/master/static/php-8_0plus.svg)](http://php.net/)\n[![Latest Stable Version](https://poser.pugx.org/xp-forge/pivot/version.png)](https://packagist.org/packages/xp-forge/pivot)\n\nWorking with [pivot tables](https://en.wikipedia.org/wiki/Pivot_table)\n\nExample\n-------\nGiven the following input, e.g. from a logfile:\n\n```\n2015-05-10 00:00:09 OK: 304 100 bytes\n2015-05-10 00:00:48 GOOD: 200 102 bytes (ETag: 214ceb4b-980-3a7bbd9630480)\n2015-05-10 03:00:49 ERROR: 404 512 bytes (Not found)\n2015-05-11 00:00:17 OK: 304 102 bytes\n2015-05-11 02:01:01 ERROR: 500 0 bytes (Internal Server Error)\n2015-05-11 02:01:02 ERROR: 500 256 bytes (Internal Server Error)\n...\n```\n\nWe will parse this using `sscanf()`, transforming the lines into arrays like the following:\n\n```php\n[\"2015-05-10\", \"00:00:48\", \"GOOD\", 200, 95, \"ETag: 214ceb4b-980-3a7bbd9630480\"]\n```\n\nWe can the load this into our pivot table using the array offsets (*if we had a map, we could use its string keys; for objects we'll pass references to the getters and for more complex situations we can pass closures*). Putting it together, we get the following:\n\n```php\nuse io\\streams\\{TextReader, FileInputStream};\nuse util\\data\\PivotCreation;\n\n$pivot= (new PivotCreation())\n  -\u003egroupingBy(2)        // category\n  -\u003egroupingBy(3)        // code\n  -\u003espreadingBy(0)       // date\n  -\u003esumming(4, 'bytes')  // bytes\n  -\u003ecreate()\n);\n\n$reader= new TextReader(new FileInputStream('measures.log'));\nwhile (null !== ($line= $reader-\u003ereadLine())) {\n  $pivot-\u003eadd(sscanf($line, '%[0-9-] %[0-9:] %[^:]: %d %d bytes (%[^)])'));\n}\n```\n\nThe resulting table will look something like this (using \"b:\" as an abbreviation for *bytes* - this becomes relevant once we sum on multiple columns):\n\n```\n.------------------------------------------------------- ~ ---------------------------.\n|                    || Columns                             |                         |\n|                    ||--------------------------------- ~ -|                         |\n| Category  | Count  || 2015-05-10    | 2015-05-11    |- ~ -| Sum        | Average    |\n|-----------|--------||---------------|---------------|- ~ -|------------|------------|\n| OK        | 2      || 1, b:100      | 1, b:102      |- ~ -| b:202      | b:101      |\n| GOOD      | 1      || 1, b:102      |               |- ~ -| b:102      | b:102      |\n| ERROR     | 3      || 2, b:512      | 1, b:256      |- ~ -| b:768      | b:256      |\n| ^- client | ^- 1   || ^- 1, b:512   |               |- ~ -| ^- b:512   | ^- b:512   |\n|   ^- 404  |   ^- 1 ||   ^- 1, b:512 |               |- ~ -|   ^- b:512 |   ^- b:512 |\n| ^- server | ^- 2   || ^- 1, b:0     | ^- 1, b:256   |- ~ -| ^- b:256   | ^- b:128   |\n|   ^- 500  |   ^- 2 ||   ^- 1, b:0   |   ^- 1, b:256 |- ~ -|   ^- b:256 |  ^- b:128  |\n|-----------|--------||---------------|---------------|- ~ -|------------|------------|\n| Total     | 6      || b:714         | b:358         |- ~ -| b:1072     | b:178.7    |\n`------------------------------------------------------- ~ ---------------------------´\n```\n\n### Accessing values in a pivot\n\nThe number of records grouped by the grouping columns can be retrieved via `count()`. The aggregates can be accessed by passing the category to the respective methods. \n\n```php\n$count= $pivot-\u003ecount('OK');                                // 2\n$count= $pivot-\u003ecount();                                    // 6\n\n$count= $pivot-\u003erecords('2015-05-10', 'OK');                // 1\n$count= $pivot-\u003erecords('2015-05-10');                      // 4\n\n$transferred= $pivot-\u003ecolumn('2015-05-10', 'OK')['bytes'];  // 100\n$transferred= $pivot-\u003ecolumn('2015-05-10')['bytes'];        // 714\n\n$transferred= $pivot-\u003esum('OK')['bytes'];                   // 202\n$transferred= $pivot-\u003esum()['bytes'];                       // 1072\n\n$average= $pivot-\u003eaverage('OK')['bytes'];                   // 101.0\n$average= $pivot-\u003eaverage()['bytes'];                       // 178.7\n```\n\n### Drill down\n\nWe can dril down by the categories we grouped on by using the `rows()` method. To calculate the distribution of categories in percent of the total, we'll use the `count()` method.\n\n```php\n$rows= $pivot-\u003erows();                         // ['OK', 'GOOD', 'ERROR']\n\n// OK: 2 / 6 = 33.3%\n// GOOD: 1 / 6 = 16.7%\n// ERROR: 3 / 6 = 50.0%\n$total= $pivot-\u003ecount();\nforeach ($rows as $cat) {\n  $count= $pivot-\u003ecount($cat);\n  printf(\"%s: %d / %d = %.1f%%\\n\", $cat, $count, $total, $count / $total * 100);\n}\n\n// client: 1\n// server: 2\nforeach ($pivot-\u003erows('ERROR') as $code) {\n  printf(\"ERROR %s: %dx\\n\", $row, $pivot-\u003ecount('ERROR', $code));\n}\n```\n\nIt can also interesting to see a development over time, so we'll drill down based on the columsn instead.\n\n```php\n$columns= $pivot-\u003ecolumns();                   // ['2015-05-10', '2015-05-11']\n\n// 2015-05-10: 714 / 1072 bytes = 66.6%\n// 2015-05-11: 358 / 1072 bytes = 33.4%\n$total= $pivot-\u003etotal()['bytes'];\nforeach ($columns as $date) {\n  $bytes= $pivot-\u003ecolumn($date)['bytes'];\n  printf(\"%s: %d / %d bytes = %.1f%%\\n\", $date, $bytes, $total, $bytes / $total * 100);\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxp-forge%2Fpivot","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fxp-forge%2Fpivot","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxp-forge%2Fpivot/lists"}