Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
sgiehl committed Nov 18, 2017
0 parents commit 22c3e16
Show file tree
Hide file tree
Showing 15 changed files with 455 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
tests/UI/processed-ui-screenshots/
tests/System/processed/
34 changes: 34 additions & 0 deletions API.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php
/**
* Piwik - free/libre analytics platform
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*
*/
namespace Piwik\Plugins\IPReports;

use Piwik\Archive;
use Piwik\Network\IP;
use Piwik\Piwik;

/**
* The IP Reports API lets you access reports for your visitors IP-Addresses.
*
* @method static \Piwik\Plugins\IPReports\API getInstance()
*/
class API extends \Piwik\Plugin\API
{
public function getIPs($idSite, $period, $date, $segment = false)
{
Piwik::checkUserHasViewAccess($idSite);
$archive = Archive::build($idSite, $period, $date, $segment);
$dataTable = $archive->getDataTable(Archiver::IPREPORTS_RECORD_NAME);
$dataTable->filter('GroupBy', array('label', function($value) {
return IP::fromBinaryIP($value)->toString();
}));
$dataTable->queueFilter('ReplaceColumnNames');
$dataTable->queueFilter('ReplaceSummaryRowLabel');
return $dataTable;
}
}
39 changes: 39 additions & 0 deletions Archiver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php
/**
* Piwik - free/libre analytics platform
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*
*/
namespace Piwik\Plugins\IPReports;

use Piwik\Metrics;

class Archiver extends \Piwik\Plugin\Archiver
{
const IPREPORTS_RECORD_NAME = 'IPReports_ips';
const IPREPORTS_FIELD = 'location_ip';

public function aggregateDayReport()
{
$metrics = $this->getLogAggregator()->getMetricsFromVisitByDimension(self::IPREPORTS_FIELD)->asDataTable();
$report = $metrics->getSerialized($this->maximumRows, null, Metrics::INDEX_NB_VISITS);
$this->getProcessor()->insertBlobRecord(self::IPREPORTS_RECORD_NAME, $report);
}

public function aggregateMultipleReports()
{
$columnsAggregationOperation = null;

$this->getProcessor()->aggregateDataTableRecords(
array(self::IPREPORTS_RECORD_NAME),
$this->maximumRows,
$maximumRowsInSubDataTable = null,
$columnToSortByBeforeTruncation = null,
$columnsAggregationOperation,
$columnsToRenameAfterAggregation = null,
$countRowsRecursive = array()
);
}
}
18 changes: 18 additions & 0 deletions Columns/IP.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
/**
* Piwik - free/libre analytics platform
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*
*/
namespace Piwik\Plugins\IPReports\Columns;

use Piwik\Plugin\Dimension\VisitDimension;

class IP extends VisitDimension
{
protected $category = 'UserCountry_VisitLocation';
protected $nameSingular = 'IPReports_IP';
protected $namePlural = 'IPReports_IPs';
}
18 changes: 18 additions & 0 deletions IPReports.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
/**
* Piwik - Open source web analytics
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
namespace Piwik\Plugins\IPReports;

use Piwik\Plugin;

/**
*
*/
class IPReports extends Plugin
{

}
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Piwik IP Reports Plugin

[![Build Status](https://travis-ci.org/sgiehl/piwik-plugin-IPReports.png?branch=master)](https://travis-ci.org/sgiehl/piwik-plugin-IPReports) [![Flattr this git repo](http://api.flattr.com/button/flattr-badge-large.png)](https://flattr.com/submit/auto?user_id=sgiehl&url=https://github.com/sgiehl/piwik-plugin-IPReports&title=Piwik%20Plugin%20IP%20Reports&language=&tags=github&category=software)

## Description

This plugin adds an additional report "Top IP-Addresses" to Piwik.

### Requirements

[Piwik](https://github.com/piwik/piwik) 3.0.0-b1 or higher is required.

## Support

Please direct any feedback to [[email protected]](mailto:[email protected])

## Contribute

Feel free to create issues and pull requests.

33 changes: 33 additions & 0 deletions Reports/GetIPs.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
/**
* Piwik - free/libre analytics platform
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*
*/
namespace Piwik\Plugins\IPReports\Reports;

use Piwik\Piwik;
use Piwik\Plugin\Report;
use Piwik\Plugin\ViewDataTable;
use Piwik\Plugins\IPReports\Columns\IP;

class GetIPs extends Report
{
protected function init()
{
$this->categoryId = 'General_Visitors';
$this->dimension = new IP();
$this->name = Piwik::translate('IPReports_WidgetIPs');
$this->order = 50;

$this->subcategoryId = 'UserCountry_SubmenuLocations';
}

public function configureView(ViewDataTable $view)
{
$view->requestConfig->filter_limit = 5;
$view->config->addTranslation('label', $this->dimension->getName());
}
}
7 changes: 7 additions & 0 deletions lang/en.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"IPReports": {
"IP": "IP-Address",
"IPs": "IP-Addresses",
"WidgetIPs": "Top IP-Addresses"
}
}
29 changes: 29 additions & 0 deletions plugin.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "IPReports",
"version": "0.1.0",
"description": "Adds new IP reports to Piwik.",
"keywords": ["ip", "ips", "report"],
"license": "GPL v3+",
"homepage": "http://github.com/sgiehl/piwik-plugin-IPReports",
"require": {
"piwik": ">=3.0.0,<4.0.0-b1",
"php": ">=5.5.0"
},
"support": {
"email": "[email protected]",
"issues": "https://github.com/sgiehl/piwik-plugin-IPReports/issues",
"source": "https://github.com/sgiehl/piwik-plugin-IPReports"
},
"authors": [
{
"name": "Stefan Giehl",
"email": "[email protected]",
"homepage": "http://github.com/sgiehl"
}
],
"donate": {
"paypal": "[email protected]",
"flattr": "https://flattr.com/thing/3787742/sgiehlpiwik-plugin-IPReports-on-GitHub",
"bitcoin": "1NxyWNfsjSNQULGxPVJUkgf8D3rpdJEgM6"
}
}
Binary file added screenshots/report.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
65 changes: 65 additions & 0 deletions tests/Fixtures/IPFixture.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php
/**
* Piwik - free/libre analytics platform
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*
*/

namespace Piwik\Plugins\IPReports\tests\Fixtures;

use Piwik\Tests\Framework\Fixture;
use Piwik\Date;

class IPFixture extends Fixture
{
public $dateTime = '2014-09-04 00:00:00';
public $idSite = 1;

public function setUp()
{
$this->setUpWebsite();
$this->trackVisits();
}

private function setUpWebsite()
{
if (!self::siteCreated($this->idSite)) {
$idSite = self::createWebsite($this->dateTime);
$this->assertSame($this->idSite, $idSite);
}
}

private function getIPAddresses() {
return array(
'192.23.33.55', '234.6.45.44', '9.9.9.9', '4.33.44.154',
'2003:f6:93bf:907:9ec7:a6ff:fe29:27de', '234.6.45.44', 'fe80::74e6:b5f3:fe92:830e',
'92.34.0.87', '5.88.3.45', 'fe80:0000:0000:0000:0202:b3ff:fe1e:8329'
);
}

private function trackVisits() {

$tracker = self::getTracker(
$this->idSite,
$this->dateTime,
$defaultInit = false
);
$tracker->setTokenAuth(self::getTokenAuth());

$hour = 1;
foreach ($this->getIPAddresses() as $ip) {

$tracker->setForceVisitDateTime(
Date::factory($this->dateTime)->addHour($hour++)->getDatetime()
);

$tracker->setIp($ip);

self::checkResponse($tracker->doTrackPageView("Viewing homepage"));
}

}

}
70 changes: 70 additions & 0 deletions tests/System/ApiTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php
/**
* Piwik - free/libre analytics platform
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*
*/
namespace Piwik\Plugins\IPReports\tests\System;


use Piwik\Plugins\IPReports\tests\Fixtures\IPFixture;
use Piwik\Tests\Framework\TestCase\SystemTestCase;

/**
* @package Piwik\Plugins\IPReports\tests
* @group Plugins
* @group IPReports
*/
class ApiTest extends SystemTestCase {

public static $fixture = null;

public static function getOutputPrefix()
{
return '';
}

/**
* @param $api
* @param $params
* @dataProvider getApiForTesting
*/
public function testApi($api, $params)
{
$this->runApiTests($api, $params);
}

/**
* @return array
*/
public function getApiForTesting()
{
$apiToCall = array(
"IPReports.getIPs",
);

$apiToTest = array();

$apiToTest[] = array(
$apiToCall,
array(
'idSite' => self::$fixture->idSite,
'date' => self::$fixture->dateTime,
'periods' => array('day')
)
);

return $apiToTest;
}


public static function getPathToTestDirectory()
{
return dirname(__FILE__);
}

}

ApiTest::$fixture = new IPFixture();
Loading

0 comments on commit 22c3e16

Please sign in to comment.