diff --git a/README.rst b/README.rst index ee1001d..29ef3df 100644 --- a/README.rst +++ b/README.rst @@ -50,16 +50,22 @@ Installation to specify something like ``.mydomain.com``. See `Google's documentation `_ 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 ======= diff --git a/ckanext/googleanalytics/commands.py b/ckanext/googleanalytics/commands.py index 0954f60..e200c3c 100644 --- a/ckanext/googleanalytics/commands.py +++ b/ckanext/googleanalytics/commands.py @@ -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) diff --git a/setup.py b/setup.py index 53dd9f7..6764221 100644 --- a/setup.py +++ b/setup.py @@ -30,5 +30,6 @@ setup( [paste.paster_command] loadanalytics = ckanext.googleanalytics.commands:LoadAnalytics + initdb = ckanext.googleanalytics.commands:InitDB """, ) diff --git a/tests/test_general.py b/tests/test_general.py index a3b9fad..1913a60 100644 --- a/tests/test_general.py +++ b/tests/test_general.py @@ -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