factor out database init as separate install step

This commit is contained in:
Seb Bacon 2011-04-07 10:24:22 +01:00
parent 8dfa04434f
commit 14f733bd9f
4 changed files with 33 additions and 7 deletions

View File

@ -50,16 +50,22 @@ Installation
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.
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``::
paster loadanalytics --config=../ckan/development.ini
(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
information about the most popular packages at:
@ -69,8 +75,8 @@ Installation
website is on the package page, where number of downloads are
displayed next to each resource.
6. Consider putting the import command as a daily cron job, or
remember to run it by hand!
7. Consider running the import command reguarly as a cron job, or
remember to run it by hand, or your statistics won't get updated.
Testing
=======

View File

@ -4,7 +4,6 @@ from pylons import config as pylonsconfig
from ckan.lib.cli import CkanCommand
from gdata.analytics import client
import ckan.model as model
from sqlalchemy.orm import sessionmaker
import dbutil
@ -13,6 +12,24 @@ PACKAGE_URL = '/package/' # XXX get from routes...
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):
"""Parse data from Google Analytics API and store it in a local
database
@ -47,7 +64,6 @@ class LoadAnalytics(CkanCommand):
def save_ga_data(self, packages_data):
"""Save tuples of packages_data to the database
"""
dbutil.init_tables()
for identifier, visits in packages_data.items():
recently = visits.get('recent', 0)
ever = visits.get('ever', 0)

View File

@ -30,5 +30,6 @@ setup(
[paste.paster_command]
loadanalytics = ckanext.googleanalytics.commands:LoadAnalytics
initdb = ckanext.googleanalytics.commands:InitDB
""",
)

View File

@ -8,6 +8,7 @@ from ckan.tests import conf_dir, url_for, CreateTestData
from mockgoogleanalytics import runmockserver
from ckanext.googleanalytics.commands import LoadAnalytics
from ckanext.googleanalytics.commands import InitDB
from ckanext.googleanalytics import dbutil
from ckanext.googleanalytics.gasnippet import gacode
@ -73,6 +74,7 @@ class xTestLoadCommand(TestCase):
assert code in response.body
def test_top_packages(self):
InitDB("initdb").run([]) # set up database tables
command = LoadAnalytics("loadanalytics")
command.TEST_HOST = MockClient('localhost', 6969)
command.CONFIG = self.config
@ -83,6 +85,7 @@ class xTestLoadCommand(TestCase):
self.assertEquals(resources[0][1], 4)
def test_download_count_inserted(self):
InitDB("initdb").run([]) # set up database tables
command = LoadAnalytics("loadanalytics")
command.TEST_HOST = MockClient('localhost', 6969)
command.CONFIG = self.config