Skip to content

Commit

Permalink
Merge pull request #1420 from AgID/develop
Browse files Browse the repository at this point in the history
Better website activation check
  • Loading branch information
pdavide authored May 24, 2022
2 parents 111bb25 + f187530 commit d1adcbe
Show file tree
Hide file tree
Showing 17 changed files with 242 additions and 135 deletions.
26 changes: 18 additions & 8 deletions app/Contracts/AnalyticsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,29 +191,39 @@ public function loginAndRedirectUser(string $userLogin, string $hashedPassword,
public function setWebsiteAccess(string $userLogin, int $access, string $idSites): void;

/**
* Check wether there is some tracking data for a given website.
*
* @param string $idSite the Analytics Service website ID
* @param int $minutes the minutes period
*
* @throws CommandErrorException if command is unsuccessful
* @throws AnalyticsServiceException if unable to connect the Analytics Service
*
* @return int the live number of website visits
* @return bool wether the site has collected any visits
*/
public function getLiveVisits(string $idSite, int $minutes): int;
public function isActive(string $idSite): bool;

/**
* Get total number of visits for a specified site
* registered in the Analytics Service.
* Get settings for a specified website.
*
* @param string $idSite the Analytics Service website ID
*
* @throws CommandErrorException if command is unsuccessful
* @throws AnalyticsServiceException if unable to connect the Analytics Service
*
* @return array the settings for the website
*/
public function getSiteSettings(string $idSite): array;

/**
* @param string $idSite the Analytics Service website ID
* @param string $from the date range
* @param int $minutes the minutes period
*
* @throws CommandErrorException if command is unsuccessful
* @throws AnalyticsServiceException if unable to connect the Analytics Service
*
* @return int the total reported website visits
* @return int the live number of website visits
*/
public function getSiteTotalVisitsFrom(string $idSite, string $from): int;
public function getLiveVisits(string $idSite, int $minutes): int;

/**
* Get the number of visits for a specified site
Expand Down
55 changes: 35 additions & 20 deletions app/Services/MatomoService.php
Original file line number Diff line number Diff line change
Expand Up @@ -428,55 +428,70 @@ public function setWebsiteAccess(string $userLogin, int $access, string $idSites
}

/**
* Check wether there is some tracking data for a given website.
*
* @param string $idSite the Analytics Service website ID
* @param int $minutes the minutes period
*
* @throws CommandErrorException if command is unsuccessful
* @throws AnalyticsServiceException if unable to connect the Analytics Service
*
* @return int the live number of website visits
* @return bool wether the site has collected any visits
*/
public function getLiveVisits(string $idSite, int $minutes): int
public function isActive(string $idSite): bool
{
$params = [
'method' => 'Live.getCounters',
'method' => 'WaiCustom.isActive',
'idSite' => $idSite,
'lastMinutes' => $minutes,
'token_auth' => $this->tokenAuth,
];
$response = $this->apiCall($params);
if (!empty($response)) {
return $response[0]['visits'];
}

return 0;
return $this->apiCall($params)['value'];
}

/**
* Get total number of visits for a specified site
* registered in the Analytics Service.
* Get settings for a specified website.
*
* @param string $idSite the Analytics Service website ID
* @param string $from the date range
*
* @throws CommandErrorException if command is unsuccessful
* @throws AnalyticsServiceException if unable to connect the Analytics Service
*
* @return int the total reported website visits
* @return array the settings for the website
*/
// TODO: Needs rework to verify that the requested range is available
public function getSiteTotalVisitsFrom(string $idSite, string $from): int
public function getSiteSettings(string $idSite): array
{
$params = [
'method' => 'VisitsSummary.get',
'method' => 'SitesManager.getSiteSettings',
'idSite' => $idSite,
'token_auth' => $this->tokenAuth,
];

return $this->apiCall($params);
}

/**
* @param string $idSite the Analytics Service website ID
* @param int $minutes the minutes period
*
* @throws CommandErrorException if command is unsuccessful
* @throws AnalyticsServiceException if unable to connect the Analytics Service
*
* @return int the live number of website visits
*/
public function getLiveVisits(string $idSite, int $minutes): int
{
$params = [
'method' => 'Live.getCounters',
'idSite' => $idSite,
'period' => 'range',
'date' => $from . ',' . now()->format('Y-m-d'),
'lastMinutes' => $minutes,
'token_auth' => $this->tokenAuth,
];
$response = $this->apiCall($params);
if (!empty($response)) {
return $response[0]['visits'];
}

return $response['nb_visits'] ?? 0;
return 0;
}

/**
Expand Down
23 changes: 16 additions & 7 deletions app/Services/SingleDigitalGatewayService.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,25 @@
class SingleDigitalGatewayService
{
/**
* Local service URL.
* Base service URL.
*
* @var string the local URL
* @var string the base service URL
*/
protected $serviceBaseUri;

/**
* Public service URL.
* Unique ID endpoint.
*
* @var string the public URL
* @var string the unique ID endpoint
*/
protected $servicePublicUrl;
protected $serviceUniqueIdEndpoint;

/**
* Stats IS endpoint.
*
* @var string the stats IS endpoint
*/
protected $serviceStatsIsEndpoint;

/**
* SSL verification flag.
Expand All @@ -52,6 +59,8 @@ class SingleDigitalGatewayService
public function __construct()
{
$this->serviceBaseUri = config('single-digital-gateway-service.api_public_url');
$this->serviceUniqueIdEndpoint = config('single-digital-gateway-service.api_uniqueid_endpoint');
$this->serviceStatsIsEndpoint = config('single-digital-gateway-service.api_stats_is_endpoint');
$this->SSLVerify = config('single-digital-gateway-service.ssl_verify');
$this->apiKey = config('single-digital-gateway-service.api_key');
$this->storageDisk = config('single-digital-gateway-service.storage_disk');
Expand All @@ -65,7 +74,7 @@ public function __construct()
*/
public function getUniqueID(): string
{
return $this->apiCall('unique-id');
return $this->apiCall($this->serviceUniqueIdEndpoint);
}

/**
Expand Down Expand Up @@ -97,7 +106,7 @@ public function sendStatisticsInformation($dataset): void
$requestDatetime = Carbon::now()->format('Y-m-d_H-i-s');
Storage::disk($this->storageDisk)->put($this->storageDirectory . "/requests/req_{$requestDatetime}.json", json_encode($dataset, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) . PHP_EOL);

$response = $this->apiCall('statistics/information-services', 'POST', [], (array) $dataset);
$response = $this->apiCall($this->serviceStatsIsEndpoint, 'POST', [], (array) $dataset);

$responseDatetime = Carbon::now()->format('Y-m-d_H-i-s');
Storage::disk($this->storageDisk)->put($this->storageDirectory . "/responses/res_{$responseDatetime}.json", json_encode(json_decode($response), JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) . PHP_EOL);
Expand Down
5 changes: 1 addition & 4 deletions app/Traits/ActivatesWebsite.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,7 @@ public function hasActivated(Website $website): bool
{
$analyticsService = app()->make('analytics-service');

$liveVisits = $analyticsService->getLiveVisits($website->analytics_id, 60);
$totalVisits = $analyticsService->getSiteTotalVisitsFrom($website->analytics_id, $website->created_at->format('Y-m-d'));

return $liveVisits > 0 || $totalVisits > 0;
return $analyticsService->isActive($website->analytics_id);
}

/**
Expand Down
3 changes: 3 additions & 0 deletions build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@
<token key="ANALYTICS_PUBLIC_URL" value="${ANALYTICS_PUBLIC_URL}" />
<token key="ANALYTICS_PUBLIC_DASHBOARD_ID" value="${MATOMO_PUBLIC_ROLLUP_ID}" />
<token key="SDG_API_PUBLIC_URL" value="${SDG_API_PUBLIC_URL}" />
<token key="SDG_API_UNIQUEID_ENDPOINT" value="${SDG_API_UNIQUEID_ENDPOINT}" />
<token key="SDG_API_STATS_IS_ENDPOINT" value="${SDG_API_STATS_IS_ENDPOINT}" />
<token key="SDG_API_SSL_VERIFY" value="${SDG_API_SSL_VERIFY}" />
<token key="SDG_API_KEY" value="${SDG_API_KEY}" />
<token key="SDG_URLS_FILE_FORMAT" value="${SDG_URLS_FILE_FORMAT}" />
Expand All @@ -140,6 +142,7 @@
<token key="MATOMO_PHPREDIS_VERSION" value="${MATOMO_PHPREDIS_VERSION}" />
<token key="MATOMO_WAI_THEME_VERSION" value="${MATOMO_WAI_THEME_VERSION}" />
<token key="MATOMO_PLUGIN_RESTRICTANONYMOUSACCESS_VERSION" value="${MATOMO_PLUGIN_RESTRICTANONYMOUSACCESS_VERSION}" />
<token key="MATOMO_PLUGIN_WAICUSTOM_VERSION" value="${MATOMO_PLUGIN_WAICUSTOM_VERSION}" />
<token key="MATOMO_PLUGIN_SUPERUSERONLYRESTRICTIONS_VERSION" value="${MATOMO_PLUGIN_SUPERUSERONLYRESTRICTIONS_VERSION}" />
<token key="MATOMO_PLUGIN_SUPERUSERONLYRESTRICTIONS_ENABLED" value="${MATOMO_PLUGIN_SUPERUSERONLYRESTRICTIONS_ENABLED}" />
<token key="ANALYTICS_API_PUBLIC_DOMAIN" value="${ANALYTICS_API_PUBLIC_DOMAIN}" />
Expand Down
2 changes: 2 additions & 0 deletions config/single-digital-gateway-service.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
return [
'api_key' => env('SDG_API_KEY'),
'api_public_url' => env('SDG_API_PUBLIC_URL'),
'api_uniqueid_endpoint' => env('SDG_API_UNIQUEID_ENDPOINT'),
'api_stats_is_endpoint' => env('SDG_API_STATS_IS_ENDPOINT'),
'ssl_verify' => env('SDG_API_SSL_VERIFY'),
'storage_directory' => env('SDG_STORAGE_DIRECTORY', 'sdg'),
'storage_disk' => env('SDG_STORAGE_DISK', 'persistent'),
Expand Down
1 change: 1 addition & 0 deletions containers/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ services:
- MATOMO_PLUGIN_VISITORGENERATOR_VERSION=${MATOMO_PLUGIN_VISITORGENERATOR_VERSION}
- MATOMO_PLUGIN_RESETDATABASE_VERSION=${MATOMO_PLUGIN_RESETDATABASE_VERSION}
- MATOMO_PLUGIN_RESTRICTANONYMOUSACCESS_VERSION=${MATOMO_PLUGIN_RESTRICTANONYMOUSACCESS_VERSION}
- MATOMO_PLUGIN_WAICUSTOM_VERSION=${MATOMO_PLUGIN_WAICUSTOM_VERSION}
- MATOMO_PLUGIN_SUPERUSERONLYRESTRICTIONS_VERSION=${MATOMO_PLUGIN_SUPERUSERONLYRESTRICTIONS_VERSION}
- MATOMO_PLUGIN_SUPERUSERONLYRESTRICTIONS_ENABLED=${MATOMO_PLUGIN_SUPERUSERONLYRESTRICTIONS_ENABLED}
- MATOMO_PLUGIN_CUSTOMPDFREPORT_VERSION=${MATOMO_PLUGIN_CUSTOMPDFREPORT_VERSION}
Expand Down
11 changes: 11 additions & 0 deletions containers/matomo/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ RUN mkdir -p /opt/matomo
ARG MATOMO_VERSION
ARG MATOMO_WAI_THEME_VERSION
ARG MATOMO_PLUGIN_RESTRICTANONYMOUSACCESS_VERSION
ARG MATOMO_PLUGIN_WAICUSTOM_VERSION
ARG MATOMO_PLUGIN_SUPERUSERONLYRESTRICTIONS_VERSION
ARG MATOMO_PLUGIN_UPDATEEXPOSEDFQDNLIST_VERSION
ARG MATOMO_PLUGIN_DISABLESITETRACKING_VERSION
Expand Down Expand Up @@ -49,6 +50,16 @@ RUN curl -fsSL -o WAIMatomoTheme.tar.gz \
&& tar -xf WAIMatomoTheme.tar.gz -C /opt/matomo/plugins \
&& rm -rf WAIMatomoTheme.tar.gz

RUN if [ -n "$MATOMO_PLUGIN_WAICUSTOM_VERSION" ]; then \
curl -fsSL -o WaiCustom.zip \
"https://github.com/AgID/wai-matomo-plugin-WaiCustom/archive/${MATOMO_PLUGIN_WAICUSTOM_VERSION}.zip" \
&& unzip WaiCustom.zip -d /opt/matomo/plugins \
&& mv /opt/matomo/plugins/wai-matomo-plugin-WaiCustom-${MATOMO_PLUGIN_WAICUSTOM_VERSION} /opt/matomo/plugins/WaiCustom \
&& rm -rf WaiCustom.zip \
&& sed -i -E -e 's/(@MATOMO_PLUGINS@)/Plugins[] = "WaiCustom"\n\1/g' /opt/matomo/config/config.ini.php \
&& sed -i -E -e 's/(@MATOMO_PLUGINS_INSTALLED@)/PluginsInstalled[] = "WaiCustom"\n\1/g' /opt/matomo/config/config.ini.php \
;fi

RUN if [ -n "$MATOMO_PLUGIN_SUPERUSERONLYRESTRICTIONS_VERSION" ]; then \
curl -fsSL -o SuperUserOnlyRestrictions.zip \
"https://github.com/AgID/wai-matomo-plugin-SuperUserOnlyRestrictions/archive/${MATOMO_PLUGIN_SUPERUSERONLYRESTRICTIONS_VERSION}.zip" \
Expand Down
5 changes: 4 additions & 1 deletion env/build.properties.example
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,8 @@ MATOMO_PLUGIN_QUEUEDTRACKING_VERSION=3.3.6
MATOMO_PLUGIN_PROTECTTRACKID_VERSION=1.0.0
MATOMO_PLUGIN_CUSTOMDIMENSIONS_VERSION=3.1.10
MATOMO_PLUGIN_LOGINFILTERIP_VERSION=0.1.0
MATOMO_PLUGIN_RESTRICTANONYMOUSACCESS_VERSION=0.2.0
MATOMO_PLUGIN_RESTRICTANONYMOUSACCESS_VERSION=0.3.0
MATOMO_PLUGIN_WAICUSTOM_VERSION=0.2.0
MATOMO_PLUGIN_SUPERUSERONLYRESTRICTIONS_VERSION=1.0.0
MATOMO_PLUGIN_SUPERUSERONLYRESTRICTIONS_ENABLED=0
MATOMO_PLUGIN_TRACKERHOSTNAME_VERSION=0.1.0
Expand Down Expand Up @@ -284,6 +285,8 @@ ANALYTICS_WIDGETS_BASE_URL=/
# ---------------------------------------

SDG_API_PUBLIC_URL=
SDG_API_UNIQUEID_ENDPOINT=
SDG_API_STATS_IS_ENDPOINT=
SDG_API_SSL_VERIFY=false
SDG_API_KEY=
SDG_STORAGE_DIRECTORY=sdg
Expand Down
5 changes: 4 additions & 1 deletion env/build.properties.testing
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,8 @@ MATOMO_PLUGIN_QUEUEDTRACKING_VERSION=3.3.6
MATOMO_PLUGIN_PROTECTTRACKID_VERSION=1.0.0
MATOMO_PLUGIN_CUSTOMDIMENSIONS_VERSION=3.1.10
MATOMO_PLUGIN_LOGINFILTERIP_VERSION=0.1.0
MATOMO_PLUGIN_RESTRICTANONYMOUSACCESS_VERSION=0.2.0
MATOMO_PLUGIN_RESTRICTANONYMOUSACCESS_VERSION=0.3.0
MATOMO_PLUGIN_WAICUSTOM_VERSION=0.2.0
MATOMO_PLUGIN_SUPERUSERONLYRESTRICTIONS_VERSION=1.0.0
MATOMO_PLUGIN_SUPERUSERONLYRESTRICTIONS_ENABLED=0
MATOMO_PLUGIN_TRACKERHOSTNAME_VERSION=0.1.0
Expand Down Expand Up @@ -283,6 +284,8 @@ ANALYTICS_WIDGETS_BASE_URL=/
# ----------------------

SDG_API_PUBLIC_URL=
SDG_API_UNIQUEID_ENDPOINT=
SDG_API_STATS_IS_ENDPOINT=
SDG_API_SSL_VERIFY=false
SDG_API_KEY=
SDG_STORAGE_DIRECTORY=sdg
Expand Down
1 change: 1 addition & 0 deletions env/env-containers.template
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ MATOMO_PLUGIN_CUSTOMPDFREPORT_VERSION=@MATOMO_PLUGIN_CUSTOMPDFREPORT_VERSION@
MATOMO_PLUGIN_VISITORGENERATOR_VERSION=@MATOMO_PLUGIN_VISITORGENERATOR_VERSION@
MATOMO_PLUGIN_RESETDATABASE_VERSION=@MATOMO_PLUGIN_RESETDATABASE_VERSION@
MATOMO_PLUGIN_RESTRICTANONYMOUSACCESS_VERSION=@MATOMO_PLUGIN_RESTRICTANONYMOUSACCESS_VERSION@
MATOMO_PLUGIN_WAICUSTOM_VERSION=@MATOMO_PLUGIN_WAICUSTOM_VERSION@
MATOMO_PLUGIN_SUPERUSERONLYRESTRICTIONS_VERSION=@MATOMO_PLUGIN_SUPERUSERONLYRESTRICTIONS_VERSION@
MATOMO_PLUGIN_SUPERUSERONLYRESTRICTIONS_ENABLED=@MATOMO_PLUGIN_SUPERUSERONLYRESTRICTIONS_ENABLED@
MATOMO_WAI_URL=@HOSTNAME@
Expand Down
2 changes: 2 additions & 0 deletions env/env-dusk.template
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ ANALYTICS_PUBLIC_DASHBOARD_ID=
ANALYTICS_CRON_ARCHIVING_ENABLED=@MATOMO_CRON_ARCHIVING_ENABLED@

SDG_API_PUBLIC_URL=@SDG_API_PUBLIC_URL@
SDG_API_UNIQUEID_ENDPOINT=@SDG_API_UNIQUEID_ENDPOINT@
SDG_API_STATS_IS_ENDPOINT=@SDG_API_STATS_IS_ENDPOINT@
SDG_API_SSL_VERIFY=@SDG_API_SSL_VERIFY@
SDG_API_KEY=@SDG_API_KEY@
SDG_STORAGE_DIRECTORY=@SDG_STORAGE_DIRECTORY@
Expand Down
2 changes: 2 additions & 0 deletions env/env-laravel.template
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ ANALYTICS_PUBLIC_DASHBOARD_ID=@ANALYTICS_PUBLIC_DASHBOARD_ID@
ANALYTICS_CRON_ARCHIVING_ENABLED=@MATOMO_CRON_ARCHIVING_ENABLED@

SDG_API_PUBLIC_URL=@SDG_API_PUBLIC_URL@
SDG_API_UNIQUEID_ENDPOINT=@SDG_API_UNIQUEID_ENDPOINT@
SDG_API_STATS_IS_ENDPOINT=@SDG_API_STATS_IS_ENDPOINT@
SDG_API_SSL_VERIFY=@SDG_API_SSL_VERIFY@
SDG_API_KEY=@SDG_API_KEY@
SDG_STORAGE_DIRECTORY=@SDG_STORAGE_DIRECTORY@
Expand Down
Loading

0 comments on commit d1adcbe

Please sign in to comment.