Skip to content

Commit

Permalink
Merge pull request #3430 from open-data/query_performance
Browse files Browse the repository at this point in the history
Query performance improvements.
  • Loading branch information
amercader committed Feb 14, 2017
2 parents 2b1ede3 + 5d6949c commit cb498fc
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 13 deletions.
1 change: 0 additions & 1 deletion ckan/model/resource.py
Expand Up @@ -189,7 +189,6 @@ def activity_stream_detail(self, activity_id, activity_type):
),
)
},
order_by=[resource_table.c.package_id],
extension=[vdm.sqlalchemy.Revisioner(resource_revision_table),
extension.PluginMapperExtension(),
],
Expand Down
42 changes: 33 additions & 9 deletions ckan/model/user.py
Expand Up @@ -9,7 +9,7 @@
from passlib.hash import pbkdf2_sha512
from sqlalchemy.sql.expression import or_
from sqlalchemy.orm import synonym
from sqlalchemy import types, Column, Table
from sqlalchemy import types, Column, Table, func
import vdm.sqlalchemy

import meta
Expand Down Expand Up @@ -194,21 +194,45 @@ def as_dict(self):
def number_of_edits(self):
# have to import here to avoid circular imports
import ckan.model as model
revisions_q = meta.Session.query(model.Revision)
revisions_q = revisions_q.filter_by(author=self.name)
return revisions_q.count()

# Get count efficiently without spawning the SQLAlchemy subquery
# wrapper. Reset the VDM-forced order_by on timestamp.
return meta.Session.execute(
meta.Session.query(
model.Revision
).filter_by(
author=self.name
).statement.with_only_columns(
[func.count()]
).order_by(
None
)
).scalar()

def number_created_packages(self, include_private_and_draft=False):
# have to import here to avoid circular imports
import ckan.model as model
q = meta.Session.query(model.Package)\
.filter_by(creator_user_id=self.id)

# Get count efficiently without spawning the SQLAlchemy subquery
# wrapper. Reset the VDM-forced order_by on timestamp.
q = meta.Session.query(
model.Package
).filter_by(
creator_user_id=self.id
)

if include_private_and_draft:
q = q.filter(model.Package.state != 'deleted')
else:
q = q.filter_by(state='active')\
.filter_by(private=False)
return q.count()
q = q.filter_by(state='active', private=False)

return meta.Session.execute(
q.statement.with_only_columns(
[func.count()]
).order_by(
None
)
).scalar()

def activate(self):
''' Activate the user '''
Expand Down
3 changes: 2 additions & 1 deletion ckan/templates/package/resources.html
Expand Up @@ -11,8 +11,9 @@
{% block primary_content_inner %}
{% if pkg.resources %}
<ul class="resource-list"{% if has_reorder %} data-module="resource-reorder" data-module-id="{{ pkg.id }}"{% endif %}>
{% set can_edit = h.check_access('package_update', {'id':pkg.id }) %}
{% for resource in pkg.resources %}
{% snippet 'package/snippets/resource_item.html', pkg=pkg, res=resource, url_is_edit=true %}
{% snippet 'package/snippets/resource_item.html', pkg=pkg, res=resource, url_is_edit=true, can_edit=can_edit %}
{% endfor %}
</ul>
{% else %}
Expand Down
1 change: 0 additions & 1 deletion ckan/templates/package/snippets/resource_item.html
@@ -1,4 +1,3 @@
{% set can_edit = h.check_access('package_update', {'id':pkg.id }) %}
{% set url_action = 'resource_edit' if url_is_edit and can_edit else 'resource_read' %}
{% set url = h.url_for(controller='package', action=url_action, id=pkg.name, resource_id=res.id) %}

Expand Down
3 changes: 2 additions & 1 deletion ckan/templates/package/snippets/resources_list.html
Expand Up @@ -15,8 +15,9 @@ <h3>{{ _('Data and Resources') }}</h3>
{% if resources %}
<ul class="{% block resource_list_class %}resource-list{% endblock %}">
{% block resource_list_inner %}
{% set can_edit = h.check_access('package_update', {'id':pkg.id }) %}
{% for resource in resources %}
{% snippet 'package/snippets/resource_item.html', pkg=pkg, res=resource %}
{% snippet 'package/snippets/resource_item.html', pkg=pkg, res=resource, can_edit=can_edit %}
{% endfor %}
{% endblock %}
</ul>
Expand Down

0 comments on commit cb498fc

Please sign in to comment.