Skip to content

Commit

Permalink
Merge pull request #119 from noirbizarre/default-settings
Browse files Browse the repository at this point in the history
Consolidate and expose default settings
  • Loading branch information
noirbizarre authored Mar 7, 2019
2 parents 790625a + 2c3e48d commit 98520a0
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 8 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## Current (in progress)

- Nothing yet
- Consolidate and expose default settings [#119](https://github.com/opendatateam/udata-piwik/pull/119)

## 1.3.2 (2019-01-14)

Expand Down
13 changes: 6 additions & 7 deletions udata_piwik/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,15 @@

from flask import current_app

log = logging.getLogger(__name__)
from . import settings

DEFAULT_TRACK_TIMEOUT = 60 # in seconds
DEFAULT_ANALYZE_TIMEOUT = 60 * 5 # in seconds
log = logging.getLogger(__name__)


def analyze(method, **kwargs):
"""Retrieve JSON stats from Piwik for a given `method` and parameters."""
base_url = '{0}://{1}/index.php'.format(
current_app.config.get('PIWIK_SCHEME', 'http'),
current_app.config.get('PIWIK_SCHEME', settings.PIWIK_SCHEME),
current_app.config['PIWIK_URL'],
)
data = {
Expand All @@ -35,15 +34,15 @@ def analyze(method, **kwargs):
kwargs['date'] = dt
data.update(kwargs)
timeout = current_app.config.get('PIWIK_ANALYZE_TIMEOUT',
DEFAULT_ANALYZE_TIMEOUT)
settings.PIWIK_ANALYZE_TIMEOUT)
r = requests.get(base_url, params=data, timeout=timeout)
return r.json()


def track(url, **kwargs):
"""Track a request to a given `url` by issuing a POST against Piwik."""
base_url = '{0}://{1}/piwik.php'.format(
current_app.config.get('PIWIK_SCHEME', 'http'),
current_app.config.get('PIWIK_SCHEME', settings.PIWIK_SCHEME),
current_app.config['PIWIK_URL'],
)
data = {
Expand All @@ -54,5 +53,5 @@ def track(url, **kwargs):
}
data.update(kwargs)
timeout = current_app.config.get('PIWIK_TRACK_TIMEOUT',
DEFAULT_TRACK_TIMEOUT)
settings.PIWIK_TRACK_TIMEOUT)
requests.post(base_url, data=data, timeout=timeout)
33 changes: 33 additions & 0 deletions udata_piwik/settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
'''
Default settings for udata-piwik
'''
# Piwik/Matomo instance URL
PIWIK_URL = None

# Scheme used to make Piwik/Matomo API calls (http or https)
PIWIK_SCHEME = 'http'

# Piwik/Matomo site ID
PIWIK_ID = 1

# Authentication token from Piwik/Matomo
PIWIK_AUTH = '<32-chars-auth-token-from-piwik>'

# Piwik/Matomo Goals mapping
# All keys are required and should exists on instance
PIWIK_GOALS = {
# 'NEW_DATASET': 1,
# 'NEW_REUSE': 2,
# 'NEW_FOLLOW': 3,
# 'SHARE': 4,
# 'RESOURCE_DOWNLOAD': 5,
# 'RESOURCE_REDIRECT': 6,
}

# Piwik/Matomo reporting analyse timeout in seconds
PIWIK_ANALYZE_TIMEOUT = 60 * 5

# Piwik/Matomo tracking submission timeout in seconds
PIWIK_TRACK_TIMEOUT = 60

0 comments on commit 98520a0

Please sign in to comment.