Skip to content

Commit

Permalink
Gráfico de calor de acessos
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiobatalha committed Jun 30, 2017
1 parent 5ff70e6 commit 53f9e87
Show file tree
Hide file tree
Showing 7 changed files with 228 additions and 1 deletion.
1 change: 1 addition & 0 deletions analytics/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ def add_chartsconfig(request):
config.add_route('accesses_bydocumenttype', '/ajx/accesses/bydocumenttype')
config.add_route('accesses_bymonthandyear', '/ajx/accesses/bymonthandyear')
config.add_route('accesses_lifetime', '/ajx/accesses/lifetime')
config.add_route('accesses_heat', '/ajx/accesses/heat')
config.add_route('publication_size_web', '/w/publication/size')
config.add_route('publication_size', '/ajx/publication/size')
config.add_route('publication_journal_web', '/w/publication/journal')
Expand Down
33 changes: 33 additions & 0 deletions analytics/charts_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,39 @@ def highchart(self):

return _highchart

def access_heat(self, data):

chart = self.highchart

chart['chart']['type'] = 'heatmap'
chart['xAxis'] = {'categories': data['categories_x']}
chart['yAxis'] = {'categories': data['categories_y']}
chart['series'] = [{'data': data['series']}]
chart['colorAxis'] = {
"min": 0,
"minColor": '#FFFFFF',
"maxColor": '#003d99'

}
chart['legend'] = {
"align": "right",
"layout": "vertical",
"margin": 0,
"verticalAlign": 'top',
"y": 25,
"symbolHeight": 280
}

chart['plotOptions'] = {
"heatmap": {
"turboThreshold": len(data['series'])
}
}
chart['yAxis']['title'] = {'text': self._(u'Ano de acesso aos documentos')}
chart['xAxis']['title'] = {'text': self._(u'Ano de publicação de documentos')}

return {'options': chart}

def bibliometrics_cited_and_citing_years_heat(self, data):

chart = self.highchart
Expand Down
150 changes: 150 additions & 0 deletions analytics/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -2597,3 +2597,153 @@ def access_by_month_and_year(self, code, collection, py_range, sa_scope, la_scop
computed = self._compute_access_by_month_and_year(query_result)

return query_result if raw else computed

@staticmethod
def _compute_access_heat(query_result):
data = {}
data['citing_list'] = []
data['categories_x'] = set()
data['categories_y'] = set()
data['series'] = []

temp_dict = {}
for xitem in query_result['aggregations']['publication_year']['buckets']:
if not int(xitem['key']) >= 1900 or not int(xitem['key']) <= datetime.now().year:
continue
data['categories_x'].add(xitem['key'])
temp_dict.setdefault(xitem['key'], {})
for yitem in xitem['access_year']['buckets']:
if not int(yitem['key']) >= 1900 or not int(yitem['key']) <= datetime.now().year:
continue
data['categories_y'].add(yitem['key'])
temp_dict[xitem['key']].setdefault(yitem['key'], yitem['access_total']['value'])

data['categories_x'] = sorted(data['categories_x'])
data['categories_y'] = sorted(data['categories_y'])

x = 0
for itemx in data['categories_x']:
y = 0
for itemy in data['categories_y']:
data['series'].append([x, y, temp_dict.get(itemx, {}).get(itemy, 0)])
y += 1
x += 1

return data

@cache_region.cache_on_arguments()
def access_heat(self, code, collection, py_range, sa_scope, la_scope, date_range_start=None, date_range_end=None, raw=False):

body = {"query": {"filtered": {}}}

end = datetime.now()
start = end - timedelta(365*3)

date_range_start = date_range_start or start.isoformat()[0:10]
date_range_end = date_range_end or end.isoformat()[0:10]

fltr = {
"filter": {
"bool": {
"must": [
{
"range": {
"publication_year": {
"gte": py_range[0],
"lte": py_range[1]
}
}
}, {
"exists": {
"field": "document_title"
}
}, {
"terms": {
"subject_areas": sa_scope
}
}, {
"terms": {
"languages": la_scope
}
}
]
}
}
}

query = {
"query": {
"bool": {
"must": [
{
"match": {
"collection": collection
}
},
{
"range": {
"access_date": {
"gte": date_range_start,
"lte": date_range_end
}
}
}
]
}
}
}

aggs = {
"aggs": {
"publication_year": {
"terms": {
"field": "publication_year",
"size": 0,
"order": {
"_term": "asc"
}
},
"aggs": {
"access_year": {
"terms": {
"field": "access_year",
"size": 0,
"order": {
"_term": "asc"
}
},
"aggs": {
"access_total": {
"sum": {
"field": "access_total"
}
}
}
}
}
}
}
}

body['query']['filtered'].update(fltr)
body['query']['filtered'].update(query)
body.update(aggs)

code_type = self._code_type(code)

if code_type:
query["query"]["bool"]["must"].append({
"match": {
code_type: code
}
})

query_parameters = [
clients.accessstats_thrift.kwargs('size', '0')
]

query_result = json.loads(self.client.search(json.dumps(body), query_parameters))

computed = self._compute_access_heat(query_result)

return query_result if raw else computed
3 changes: 3 additions & 0 deletions analytics/templates/website/accesses.mako
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
<div class="chart">
<%include file="access_by_month_and_year.mako"/>
</div>
<div class="chart">
<%include file="accesses_heat_chart.mako"/>
</div>
<div class="chart">
<%include file="access_lifetime.mako"/>
</div>
Expand Down
27 changes: 27 additions & 0 deletions analytics/templates/website/accesses_heat_chart.mako
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
## coding: utf-8
<div id="accesses_heat_chart" style="width:100%; height:400px;">
<span id="accesses_heat_chart">
<img src="/static/images/loading.gif" />
<h5>${_(u'loading')}</h5>
</span>
</div>
<script language="javascript">
$("#loading_accesses_heat_chart").show();
$(document).ready(function() {
var url = "${request.route_url('accesses_heat')}?code=${selected_code}&collection=${selected_collection_code}&range_start=${range_start}&range_end=${range_end}&py_range=${'-'.join(py_range)}&callback=?";
$.getJSON(url, function(data) {
data['options']['title'] = {'text': "${_(u'Acessos aos documentos')}"};
data['options']['subtitle'] = {'text': '${selected_journal or selected_collection["name"]}'};
data['options']['tooltip'] = {
'formatter': function () {
return '${_(u"Documentos publicados em")} <b>' + this.series.xAxis.categories[this.point.x] + '</b> ${_(u"tiveram")} <b>' + this.point.value + '</b> ${_(u"acessos em")} <b>' + this.series.yAxis.categories[this.point.y];
}
}
Highcharts.chart('accesses_heat_chart', data['options']);
$("#loading_accesses_heat_chart").hide();
});
});
</script>
13 changes: 13 additions & 0 deletions analytics/views_ajax.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,3 +357,16 @@ def lifetime(request):
data_chart = request.stats.access.access_lifetime(data['selected_code'], data['selected_collection_code'], data['py_range'], data['sa_scope'], data['la_scope'], range_start, range_end)

return request.chartsconfig.lifetime(data_chart)

@view_config(route_name='accesses_heat', request_method='GET', renderer='jsonp')
@base_data_manager
def accesses_heat(request):

data = request.data_manager

range_start = request.GET.get('range_start', None)
range_end = request.GET.get('range_end', None)

data = request.stats.access.access_heat(data['selected_code'], data['selected_collection_code'], data['py_range'], data['sa_scope'], data['la_scope'], range_start, range_end)

return request.chartsconfig.access_heat(data)
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

setup(
name="analytics",
version='1.18.4',
version='1.19.4',
description="A analytics frontend for SciELO usage and publication statistics",
author="SciELO",
author_email="[email protected]",
Expand Down

0 comments on commit 53f9e87

Please sign in to comment.