Skip to content

Commit

Permalink
Merge pull request #5 from swarzesherz/beta
Browse files Browse the repository at this point in the history
Congiguración de las graficas fuera del view
  • Loading branch information
fabiobatalha committed Oct 21, 2015
2 parents 7ccbc95 + 92a28d1 commit 388eb42
Show file tree
Hide file tree
Showing 5 changed files with 355 additions and 187 deletions.
5 changes: 5 additions & 0 deletions analytics/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from pyramid.settings import aslist, asbool

from analytics import controller
from analytics import charts_config
from thrift_clients import clients

from analytics.views_website import cache_region as views_website_cache_region
Expand All @@ -24,6 +25,9 @@ def add_publicationstats(request):
def add_articlemeta(request):
return controller.articlemeta(settings['articlemeta'])

def add_chartsconfig(request):
return charts_config.chartsconfig(request)

config.include('pyramid_mako')
config.add_static_view('static', 'static', cache_max_age=3600)
config.add_route('index_web', '/')
Expand Down Expand Up @@ -52,6 +56,7 @@ def add_articlemeta(request):
config.add_request_method(add_accessstats, 'accessstats', reify=True)
config.add_request_method(add_articlemeta, 'articlemeta', reify=True)
config.add_request_method(add_publicationstats, 'publicationstats', reify=True)
config.add_request_method(add_chartsconfig, 'chartsconfig', reify=True)


config.add_subscriber('analytics.subscribers.add_renderer_globals',
Expand Down
319 changes: 319 additions & 0 deletions analytics/charts_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,319 @@
# coding: utf-8

def chartsconfig(request):
return ChartsConfig(request)

class ChartsConfig(object):
def __init__(self, request):
self.request = request

@property
def _(self):
return self.request.translate

@property
def highchart(self):

_higchart = {
'chart': {'type': 'line', 'backgroundColor': 'transparent'},
'yAxis' : {'min': 0, 'labels' :{'format': '{value}'}},
'legend': {'align': 'center', 'highlightSeries': {'enabled': True}},
'credits': {'href': 'http://www.scielo.org', 'text': self._(u'Fuente: SciELO.org')}
}

return _higchart

def publication_article_references(self, data):

chart = self.highchart
chart['chart']['type'] = 'column'
chart['title'] = {'text': self._(u'Distribuição de número de referências bibliográficas dos documentos')}
chart['xAxis'] = {
'categories': data['categories'],
'title': {'text': self._(u'Referências bibliográficas')}
}
chart['legend'] = {'enabled': False}
chart['series'] = data['series']
chart['yAxis']['title'] = {'text': self._(u'Número de documentos') }
chart['tooltip'] = {
'headerFormat': '',
'pointFormat': u'<span style="color:{point.color}">\u25CF</span> ' + self._(u'%s documentos com %s referências bibliográficas' % ('<strong>{point.y}</strong>', '<strong>{point.category}</strong>')),
'followPointer': True
}

return {'options': chart}

def publication_article_authors(self, data):

chart = self.highchart
chart['chart']['type'] = 'column'
chart['title'] = {'text': self._(u'Distribuição de número de autores dos documentos')}
chart['xAxis'] = {
'categories': data['categories'],
'title': {'text': self._(u'Número de autores')}
}
chart['legend'] = {'enabled': False}
chart['series'] = data['series']
chart['yAxis']['title'] = {'text': self._(u'Número de documentos') }
chart['tooltip'] = {
'headerFormat': '',
'pointFormat': u'<span style="color:{point.color}">\u25CF</span> ' + self._(u'%s documentos com %s autores' % ('<strong>{point.y}</strong>', '<strong>{point.category}</strong>')),
'followPointer': True
}

return {'options': chart}

def publication_article_affiliations(self, data):

chart = self.highchart
chart['chart']['type'] = 'column'
chart['title'] = {'text': self._(u'Distribuição de países de afiliação dos documentos')}
chart['xAxis'] = {
'categories': data['categories'],
'title': {'text': self._(u'País de afiliação')}
}
chart['plotOptions'] = {'series': {'colorByPoint': True}}
chart['legend'] = {'enabled': False}
chart['series'] = data['series']
chart['yAxis']['title'] = {'text': self._(u'Número de documentos') }
chart['tooltip'] = {
'headerFormat': '',
'pointFormat': u'<span style="color:{point.color}">\u25CF</span> ' + self._(u'País de afiliação') + ' <strong>{point.category}</strong><br>' + self._(u'Documentos') + ': <strong>{point.y}</strong>',
'followPointer': True
}

return {'options': chart}

def publication_article_year(self, data):

chart = self.highchart
chart['chart']['type'] = 'column'
chart['title'] = {'text': self._(u'Distribuição de ano de publicação dos documentos')}
chart['xAxis'] = {
'categories': data['categories'],
'title': {'text': self._(u'Ano de publicação')}
}
chart['legend'] = {'enabled': False}
chart['series'] = data['series']
chart['yAxis']['title'] = {'text': self._(u'Número de documentos') }
chart['tooltip'] = {
'headerFormat': '',
'pointFormat': u'<span style="color:{point.color}">\u25CF</span> ' + self._(u'Ano de publicação') + ' <strong>{point.category}</strong><br>' + self._(u'Documentos') + ': <strong>{point.y}</strong>',
'followPointer': True
}

return {'options': chart}

def publication_article_languages(self, data):

chart = self.highchart
chart['chart']['type'] = 'column'
chart['title'] = {'text': self._(u'Distribuição de idiomas dos documentos')}
chart['xAxis'] = {
'categories': data['categories'],
'title': {'text': self._(u'Idiomas de publicação')}
}
chart['plotOptions'] = {'series': {'colorByPoint': True}}
chart['legend'] = {'enabled': False}
chart['series'] = data['series']
chart['yAxis']['title'] = {'text': self._(u'Número de documentos') }
chart['tooltip'] = {
'headerFormat': '',
'pointFormat': u'<span style="color:{point.color}">\u25CF</span> ' + self._(u'Idioma do documento') + ' <strong>{point.category}</strong><br>' + self._(u'Documentos') + ': <strong>{point.y}</strong>',
'followPointer': True
}

return {'options': chart}

def publication_journal_status(self, data):

chart = self.highchart
chart['chart']['type'] = 'column'
chart['title'] = {'text': self._(u'Distribuição de periódicos por situação atual de publicação no SciELO')}
chart['xAxis'] = {
'categories': data['categories'],
'title': {'text': self._(u'Situação da publicação')}
}
chart['legend'] = {'enabled': False}
chart['series'] = data['series']
chart['plotOptions'] = {'series': {'colorByPoint': True}}
chart['yAxis']['title'] = {'text': self._(u'Número de periódicos') }
chart['tooltip'] = {
'headerFormat': '',
'pointFormat': u'<span style="color:{point.color}">\u25CF</span> ' + self._(u'Situação da publicação') + ' <strong>{point.category}</strong><br>' + self._(u'Periódicos') + ': <strong>{point.y}</strong>',
'followPointer': True
}

return {'options': chart}

def publication_journal_year(self, data):

chart = self.highchart
chart['chart']['type'] = 'column'
chart['title'] = {'text': self._(u'Distribuição de periódicos por ano de inclusão no SciELO')}
chart['xAxis'] = {
'categories': data['categories'],
'title': {'text': self._(u'Ano de inclusão')}
}
chart['legend'] = {'enabled': False}
chart['series'] = data['series']
chart['yAxis']['title'] = {'text': self._(u'Número de periódicos') }
chart['tooltip'] = {
'headerFormat': '',
'pointFormat': u'<span style="color:{point.color}">\u25CF</span> ' + self._(u'Ano de inclusão') + ' <strong>{point.category}</strong><br>' + self._(u'Periódicos') + ': <strong>{point.y}</strong>',
'followPointer': True
}

return {'options': chart}

def publication_article_subject_areas(self, data):

chart = self.highchart
chart['chart']['type'] = 'column'
chart['title'] = {'text': self._(u'Distribuição de área temática dos documentos')}
chart['xAxis'] = {
'categories': data['categories'],
'title': {'text': self._(u'Areas temáticas')}
}
chart['plotOptions'] = {'series': {'colorByPoint': True}}
chart['legend'] = {'enabled': False}
chart['series'] = data['series']
chart['yAxis']['title'] = {'text': self._(u'Número de documentos') }
chart['tooltip'] = {
'headerFormat': '',
'pointFormat': u'<span style="color:{point.color}">\u25CF</span> ' + self._(u'Área temática') + ' <strong>{point.category}</strong><br>' + self._(u'Documentos') + ': <strong>{point.y}</strong>'
}

return {'options': chart}

def publication_article_document_type(self, data):

chart = self.highchart
chart['chart']['type'] = 'column'
chart['title'] = {'text': self._(u'Distribuição por tipo de documento')}
chart['xAxis'] = {
'categories': data['categories'],
'title': {'text': self._(u'Tipos de documentos')}
}
chart['plotOptions'] = {'series': {'colorByPoint': True}}
chart['legend'] = {'enabled': False}
chart['series'] = data['series']
chart['yAxis']['title'] = {'text': self._(u'Número de documentos') }
chart['tooltip'] = {
'headerFormat': '',
'pointFormat': u'<span style="color:{point.color}">\u25CF</span> ' + self._(u'Tipo de documento') + ' <strong>{point.category}</strong><br>' + self._(u'Documentos') + ': <strong>{point.y}</strong>',
'followPointer': True
}

return {'options': chart}

def publication_article_licenses(self, data):

chart = self.highchart
chart['chart']['type'] = 'column'
chart['title'] = {'text': self._(u'Distribuição de licença de uso dos documentos')}
chart['xAxis'] = {
'categories': [x.upper() for x in data['categories']],
'title': {'text': self._(u'Licenças de uso')}
}
chart['plotOptions'] = {'series': {'colorByPoint': True}}
chart['legend'] = {'enabled': False}
chart['series'] = data['series']
chart['yAxis']['title'] = {'text': self._(u'Número de documentos') }
chart['tooltip'] = {
'headerFormat': '',
'pointFormat': u'<span style="color:{point.color}">\u25CF</span> ' + self._(u'Licença') + ' <strong>{point.category}</strong><br>' + self._(u'Documentos') + ': <strong>{point.y}</strong>',
'followPointer': True
}

return {'options': chart}

def publication_journal_subject_areas(self, data):

chart = self.highchart
chart['chart']['type'] = 'column'
chart['title'] = {'text': self._(u'Distribuição de área temática dos periódicos')}
chart['xAxis'] = {
'categories': data['categories'],
'title': {'text': self._(u'Areas temáticas')}
}
chart['legend'] = {'enabled': False}
chart['series'] = data['series']
chart['plotOptions'] = {'series': {'colorByPoint': True}}
chart['yAxis']['title'] = {'text': self._(u'Número de periódicos') }
chart['tooltip'] = {
'headerFormat': '',
'pointFormat': u'<span style="color:{point.color}">\u25CF</span> ' + self._(u'Área temática') + ' <strong>{point.category}</strong><br>' + self._(u'Periódicos') + ': <strong>{point.y}</strong>'
}

return {'options': chart}

def publication_journal_licenses(self, data):

chart = self.highchart
chart['chart']['type'] = 'column'
chart['title'] = {'text': self._(u'Distribuição de licença de uso dos periódicos')}
chart['xAxis'] = {
'categories': [x.upper() for x in data['categories']],
'title': {'text': self._(u'Licenças de uso')}
}
chart['legend'] = {'enabled': False}
chart['series'] = data['series']
chart['plotOptions'] = {'series': {'colorByPoint': True}}
chart['yAxis']['title'] = {'text': self._(u'Número de periódicos') }
chart['tooltip'] = {
'headerFormat': '',
'pointFormat': u'<span style="color:{point.color}">\u25CF</span> ' + self._(u'Licença') + ' <strong>{point.category}</strong><br>' + self._(u'Periódicos') + ': <strong>{point.y}</strong>',
'followPointer': True
}

return {'options': chart}

def bymonthandyear(self, data):

chart = self.highchart
chart['title'] = {'text': self._(u'Total de acessos por ano e mês')}
chart['xAxis'] = {'categories': data['categories']}
chart['series'] = data['series']
chart['yAxis']['title'] = {'text': self._(u'Acessos') }
chart['tooltip'] = {
'headerFormat': '',
'pointFormat': u'<span style="color:{point.color}">\u25CF</span> {point.category}<br>' + self._(u'Acessos a') + ' <strong>{series.name}</strong>: {point.y}',
'followPointer': True
}

return {'options': chart}

def documenttype(self, data):

chart = self.highchart
chart['chart']['type'] = 'pie'
chart['title'] = {'text': self._('Total de accessos por tipo de documento')}
chart['series'] = data['series']
chart['tooltip'] = {
'headerFormat': '',
'pointFormat': u'<span style="color:{point.color}">\u25CF</span> '+ self._(u'%s acessos a documentos do tipo %s' % ('<strong>{point.y}</strong>', '<strong>{point.name}</strong>')),
'followPointer': True
}

return {'options': chart}

def lifetime(self, data):

charts = []
for item in data:
chart = self.highchart
chart['chart']['type'] = 'column'
chart['legend'] = {'enabled': False}
chart['title'] = {'text': self._(u'Vida útil de artigos por número de acessos em ') + item['series'][0]['name']}
chart['xAxis'] = {'categories': item['categories']}
chart['series'] = item['series']
chart['yAxis']['title'] = {'text': self._(u'Acessos') + item['series'][0]['name'] }
chart['tooltip'] = {
'headerFormat': '',
'pointFormat': u'<span style="color:{point.color}">\u25CF</span> '+ self._(u'Ano de referência') +' <strong>{series.name}</strong><br>' + self._(u'%s acessos aos documentos do ano %s' % ('<strong>{point.y}</strong>', '<strong>{point.category}</strong>')),
'followPointer': True
}
charts.append(chart)

return charts
11 changes: 8 additions & 3 deletions analytics/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,9 @@ def _compute_general(self, query_result, field):
return {"series": series, "categories": categories}

@cache_region.cache_on_arguments()
def general(self, index, field, code, collection, size=0):
def general(self, index, field, code, collection, size=0, sort_term = None):

sort_term = sort_term if sort_term in ['asc', 'desc'] else None

body = {
"query": {
Expand All @@ -173,6 +175,9 @@ def general(self, index, field, code, collection, size=0):
}
}

if sort_term:
body['aggs'][field]['terms']['order'] = {"_term": sort_term}

code_type = self._code_type(code)

if code_type:
Expand Down Expand Up @@ -748,7 +753,7 @@ def access_lifetime(self, code, collection, date_range_start=None, date_range_en
"field": "access_year",
"size": 0,
"order": {
"access_total": "desc"
"_term": "desc"
}
},
"aggs": {
Expand All @@ -762,7 +767,7 @@ def access_lifetime(self, code, collection, date_range_start=None, date_range_en
"field": "publication_year",
"size": 30,
"order": {
"access_total": "desc"
"_term": "desc"
}
},
"aggs": {
Expand Down
3 changes: 2 additions & 1 deletion analytics/templates/website/base.mako
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,8 @@
<script>$('.collapse').collapse()</script>
<script type="text/javascript">
Highcharts.setOptions({
colors: ['#3366CC', '#DC3912', '#FF9900', '#109618', '#990099', '#0099C6', '#DD4477', '#66AA00', '#B82E2E', '#316395', '#22AA99', '#AAAA11', '#6633CC', '#E67300', '#8B0707', '#651067']
colors: ['#3366CC', '#DC3912', '#FF9900', '#109618', '#990099', '#0099C6', '#DD4477', '#66AA00', '#B82E2E', '#316395', '#22AA99', '#AAAA11', '#6633CC', '#E67300', '#8B0707', '#651067'],
lang: {decimalPoint: '.', thousandsSep: ','}
});
</script>
<script>
Expand Down
Loading

0 comments on commit 388eb42

Please sign in to comment.