factor out database init as separate install step
This commit is contained in:
parent
8dfa04434f
commit
14f733bd9f
16
README.rst
16
README.rst
|
@ -50,16 +50,22 @@ Installation
|
||||||
to specify something like ``.mydomain.com``. See `Google's
|
to specify something like ``.mydomain.com``. See `Google's
|
||||||
documentation <http://code.google.com/apis/analytics/docs/gaJS/gaJSApiDomainDirectory.html#_gat.GA_Tracker_._setDomainName>`_ for more info.
|
documentation <http://code.google.com/apis/analytics/docs/gaJS/gaJSApiDomainDirectory.html#_gat.GA_Tracker_._setDomainName>`_ for more info.
|
||||||
|
|
||||||
3. Wait a day or so for some stats to be recorded in Google
|
3. Run the following command from ``src/ckanext-googleanalytics`` to
|
||||||
|
set up the required database tables (of course, altering the
|
||||||
|
``--config`` option to point to your site config file)::
|
||||||
|
|
||||||
4. Import Google stats by running the following command from
|
paster initdb --config=../ckan/development.ini
|
||||||
|
|
||||||
|
4. Wait a while for some stats to be recorded in Google
|
||||||
|
|
||||||
|
5. Import Google stats by running the following command from
|
||||||
``src/ckanext-googleanalytics``::
|
``src/ckanext-googleanalytics``::
|
||||||
|
|
||||||
paster loadanalytics --config=../ckan/development.ini
|
paster loadanalytics --config=../ckan/development.ini
|
||||||
|
|
||||||
(Of course, pointing config at your specific site config)
|
(Of course, pointing config at your specific site config)
|
||||||
|
|
||||||
5. Look at some stats within CKAN
|
6. Look at some stats within CKAN
|
||||||
|
|
||||||
Once your GA account has gathered some data, you can see some basic
|
Once your GA account has gathered some data, you can see some basic
|
||||||
information about the most popular packages at:
|
information about the most popular packages at:
|
||||||
|
@ -69,8 +75,8 @@ Installation
|
||||||
website is on the package page, where number of downloads are
|
website is on the package page, where number of downloads are
|
||||||
displayed next to each resource.
|
displayed next to each resource.
|
||||||
|
|
||||||
6. Consider putting the import command as a daily cron job, or
|
7. Consider running the import command reguarly as a cron job, or
|
||||||
remember to run it by hand!
|
remember to run it by hand, or your statistics won't get updated.
|
||||||
|
|
||||||
Testing
|
Testing
|
||||||
=======
|
=======
|
||||||
|
|
|
@ -4,7 +4,6 @@ from pylons import config as pylonsconfig
|
||||||
from ckan.lib.cli import CkanCommand
|
from ckan.lib.cli import CkanCommand
|
||||||
from gdata.analytics import client
|
from gdata.analytics import client
|
||||||
import ckan.model as model
|
import ckan.model as model
|
||||||
from sqlalchemy.orm import sessionmaker
|
|
||||||
|
|
||||||
import dbutil
|
import dbutil
|
||||||
|
|
||||||
|
@ -13,6 +12,24 @@ PACKAGE_URL = '/package/' # XXX get from routes...
|
||||||
DEFAULT_RESOURCE_URL_TAG = '/downloads/'
|
DEFAULT_RESOURCE_URL_TAG = '/downloads/'
|
||||||
|
|
||||||
|
|
||||||
|
class InitDB(CkanCommand):
|
||||||
|
"""Initialise the local stats database tables
|
||||||
|
"""
|
||||||
|
summary = __doc__.split('\n')[0]
|
||||||
|
usage = __doc__
|
||||||
|
max_args = 0
|
||||||
|
min_args = 0
|
||||||
|
|
||||||
|
def command(self):
|
||||||
|
self._load_config()
|
||||||
|
# funny dance we need to do to make sure we've got a
|
||||||
|
# configured session
|
||||||
|
model.Session.remove()
|
||||||
|
model.Session.configure(bind=model.meta.engine)
|
||||||
|
dbutil.init_tables()
|
||||||
|
log.info("Set up statistics tables in main database")
|
||||||
|
|
||||||
|
|
||||||
class LoadAnalytics(CkanCommand):
|
class LoadAnalytics(CkanCommand):
|
||||||
"""Parse data from Google Analytics API and store it in a local
|
"""Parse data from Google Analytics API and store it in a local
|
||||||
database
|
database
|
||||||
|
@ -47,7 +64,6 @@ class LoadAnalytics(CkanCommand):
|
||||||
def save_ga_data(self, packages_data):
|
def save_ga_data(self, packages_data):
|
||||||
"""Save tuples of packages_data to the database
|
"""Save tuples of packages_data to the database
|
||||||
"""
|
"""
|
||||||
dbutil.init_tables()
|
|
||||||
for identifier, visits in packages_data.items():
|
for identifier, visits in packages_data.items():
|
||||||
recently = visits.get('recent', 0)
|
recently = visits.get('recent', 0)
|
||||||
ever = visits.get('ever', 0)
|
ever = visits.get('ever', 0)
|
||||||
|
|
1
setup.py
1
setup.py
|
@ -30,5 +30,6 @@ setup(
|
||||||
|
|
||||||
[paste.paster_command]
|
[paste.paster_command]
|
||||||
loadanalytics = ckanext.googleanalytics.commands:LoadAnalytics
|
loadanalytics = ckanext.googleanalytics.commands:LoadAnalytics
|
||||||
|
initdb = ckanext.googleanalytics.commands:InitDB
|
||||||
""",
|
""",
|
||||||
)
|
)
|
||||||
|
|
|
@ -8,6 +8,7 @@ from ckan.tests import conf_dir, url_for, CreateTestData
|
||||||
|
|
||||||
from mockgoogleanalytics import runmockserver
|
from mockgoogleanalytics import runmockserver
|
||||||
from ckanext.googleanalytics.commands import LoadAnalytics
|
from ckanext.googleanalytics.commands import LoadAnalytics
|
||||||
|
from ckanext.googleanalytics.commands import InitDB
|
||||||
from ckanext.googleanalytics import dbutil
|
from ckanext.googleanalytics import dbutil
|
||||||
from ckanext.googleanalytics.gasnippet import gacode
|
from ckanext.googleanalytics.gasnippet import gacode
|
||||||
|
|
||||||
|
@ -73,6 +74,7 @@ class xTestLoadCommand(TestCase):
|
||||||
assert code in response.body
|
assert code in response.body
|
||||||
|
|
||||||
def test_top_packages(self):
|
def test_top_packages(self):
|
||||||
|
InitDB("initdb").run([]) # set up database tables
|
||||||
command = LoadAnalytics("loadanalytics")
|
command = LoadAnalytics("loadanalytics")
|
||||||
command.TEST_HOST = MockClient('localhost', 6969)
|
command.TEST_HOST = MockClient('localhost', 6969)
|
||||||
command.CONFIG = self.config
|
command.CONFIG = self.config
|
||||||
|
@ -83,6 +85,7 @@ class xTestLoadCommand(TestCase):
|
||||||
self.assertEquals(resources[0][1], 4)
|
self.assertEquals(resources[0][1], 4)
|
||||||
|
|
||||||
def test_download_count_inserted(self):
|
def test_download_count_inserted(self):
|
||||||
|
InitDB("initdb").run([]) # set up database tables
|
||||||
command = LoadAnalytics("loadanalytics")
|
command = LoadAnalytics("loadanalytics")
|
||||||
command.TEST_HOST = MockClient('localhost', 6969)
|
command.TEST_HOST = MockClient('localhost', 6969)
|
||||||
command.CONFIG = self.config
|
command.CONFIG = self.config
|
||||||
|
|
Loading…
Reference in New Issue