def g(name, default=None, coerce_to=True): val = util.try_loads(req.GET.get(name, default)) try: if coerce_to is True: coerce_to = type(default) if coerce_to: if coerce_to is list and isinstance(val, basestring): val = [val, ] elif coerce_to is list and type(val) == dict: pass else: val = coerce_to(val) except Exception as e: pass if val in ['', [], {}, '""', '\"\"', [u''], [u'""']]: return default return val
def g(name, default=None, coerce_to=True): val = util.try_loads(req.GET.get(name, default)) try: if coerce_to is True: coerce_to = type(default) if coerce_to: if coerce_to is list and isinstance(val, basestring): val = [ val, ] elif coerce_to is list and type(val) == dict: pass else: val = coerce_to(val) except Exception as e: pass if val in ['', [], {}, '""', '\"\"', [u''], [u'""']]: return default return val
def graph(): from periods import Period params = {'pk': g('pk', '_', False), 'dimension': g('dimension', '_', False), 'metric': g('metric', 'hits', False), 'depth': g('depth', 0), 'tzoffset': g('tzoffset', 0.0), 'period': g('period', str(Period.get(None))), 'area': g('area', ''), } pk = params['pk'] dimension = params['dimension'] metric = params['metric'] period = Period.get(params['period']) debug = g('debug', False) parent_div = g('parent_div', 'hailwhale_graphs') table = g('table', False) height = g('height', '300px') params['title'] = g('title', '') if not params['title']: pkname = g('pk', '') dimname = util.try_loads(g('dimension', 'Overall')) dimname = isinstance(dimname, list) and dimname[-1] or dimname params['title'] = '%s [%s]' % (util.maybe_dumps(pkname), util.maybe_dumps(dimname)) if isinstance(table, basestring): table = table.lower() == 'true' hwurl = req.GET.get('hwurl', req.url.split('graph.js')[0]) params['autoupdate'] = g('live', True) params['interval'] = g('interval', 6000) graph_id = hashlib.md5(str(params)).hexdigest() include_string = \ "document.write(\"<scr\" + \"ipt type='text/javascript' src='%sjs/jquery.min.js'></script>\");"%hwurl if table: try: columns = int(g('table', 6, int)) except: columns = 6 pps = Whale.plotpoints(pk, dimension, metric, period=period, depth=params['depth']) dates = [p for p in Period.get(period).datetimes_strs()][(-1*columns - 1):] table_str = ''' $('#{id} .table').html('<table style="width: 100%"> <tr> <th></th> <th></th> {columns} </tr> '''.strip().format(id=graph_id,columns=' '.join([ '<th>%s</th>'%date.replace('00:00:00 ', '') for date in dates])) dimensions = pps.keys() if '_' in dimensions: dimensions.remove('_') dimensions = ['_'] + dimensions for dimension_counter, dimension in enumerate(dimensions): checked = 'off' if dimension_counter < 10: checked = 'on' if dimension == '_': if params['depth']: continue dimension_name = '<b>Overall</b>' else: dimension_name = dimension.capitalize() table_str += ''' <tr> <td><input id="" style="display: none" type="checkbox" value="{checked}" name="checkbox-{pk}-{dimension}"></td> <td>{dimension_name}</td> {columns} </tr> '''.format(pk=pk, dimension=dimension, checked=checked, dimension_name=dimension_name, columns=' '.join([ "<td>%s</td>"%int(pps[dimension][metric][date]) for date in dates])).strip() table_str += '''</table>');''' else: table_str = '' include_string = \ "document.write(\"<scr\" + \"ipt type='text/javascript' src='%sjs/hailwhale.min.js'></script>\");"%hwurl return_string = ''' appended=false;\n document.write('<div id="{id}"><div class="graph" style="height: {height}"></div><div class="table"></div></div>');\n function jqinit() {{\n if(typeof(jQuery) == 'undefined' || typeof(jQuery.hailwhale) == 'undefined') {{\n if(!appended) {{\n appended = true;\n {include_string}\n }}\n setTimeout(jqinit, 250);\n }} else {{\n $(function() {{\n $.hailwhale('{hwurl}').add_graph('{id} .graph', {options});\n {table_str} }});\n }} }} jqinit();\n '''.format(parent_div=parent_div, include_string=include_string, hwurl=hwurl, table_str=table_str, height=height, id=graph_id, options=util.maybe_dumps(params)) return return_string
def graph(): from periods import Period params = { 'pk': g('pk', '_', False), 'dimension': g('dimension', '_', False), 'metric': g('metric', 'hits', False), 'depth': g('depth', 0), 'tzoffset': g('tzoffset', 0.0), 'period': g('period', str(Period.get(None))), 'area': g('area', ''), } pk = params['pk'] dimension = params['dimension'] metric = params['metric'] period = Period.get(params['period']) debug = g('debug', False) parent_div = g('parent_div', 'hailwhale_graphs') table = g('table', False) height = g('height', '300px') params['title'] = g('title', '') if not params['title']: pkname = g('pk', '') dimname = util.try_loads(g('dimension', 'Overall')) dimname = isinstance(dimname, list) and dimname[-1] or dimname params['title'] = '%s [%s]' % (util.maybe_dumps(pkname), util.maybe_dumps(dimname)) if isinstance(table, basestring): table = table.lower() == 'true' hwurl = req.GET.get('hwurl', req.url.split('graph.js')[0]) params['autoupdate'] = g('live', True) params['interval'] = g('interval', 6000) graph_id = hashlib.md5(str(params)).hexdigest() include_string = \ "document.write(\"<scr\" + \"ipt type='text/javascript' src='%sjs/jquery.min.js'></script>\");"%hwurl if table: try: columns = int(g('table', 6, int)) except: columns = 6 pps = Whale.plotpoints(pk, dimension, metric, period=period, depth=params['depth']) dates = [p for p in Period.get(period).datetimes_strs() ][(-1 * columns - 1):] table_str = ''' $('#{id} .table').html('<table style="width: 100%"> <tr> <th></th> <th></th> {columns} </tr> '''.strip().format(id=graph_id, columns=' '.join([ '<th>%s</th>' % date.replace('00:00:00 ', '') for date in dates ])) dimensions = pps.keys() if '_' in dimensions: dimensions.remove('_') dimensions = ['_'] + dimensions for dimension_counter, dimension in enumerate(dimensions): checked = 'off' if dimension_counter < 10: checked = 'on' if dimension == '_': if params['depth']: continue dimension_name = '<b>Overall</b>' else: dimension_name = dimension.capitalize() table_str += ''' <tr> <td><input id="" style="display: none" type="checkbox" value="{checked}" name="checkbox-{pk}-{dimension}"></td> <td>{dimension_name}</td> {columns} </tr> '''.format(pk=pk, dimension=dimension, checked=checked, dimension_name=dimension_name, columns=' '.join([ "<td>%s</td>" % int(pps[dimension][metric][date]) for date in dates ])).strip() table_str += '''</table>');''' else: table_str = '' include_string = \ "document.write(\"<scr\" + \"ipt type='text/javascript' src='%sjs/hailwhale.min.js'></script>\");"%hwurl return_string = ''' appended=false;\n document.write('<div id="{id}"><div class="graph" style="height: {height}"></div><div class="table"></div></div>');\n function jqinit() {{\n if(typeof(jQuery) == 'undefined' || typeof(jQuery.hailwhale) == 'undefined') {{\n if(!appended) {{\n appended = true;\n {include_string}\n }}\n setTimeout(jqinit, 250);\n }} else {{\n $(function() {{\n $.hailwhale('{hwurl}').add_graph('{id} .graph', {options});\n {table_str} }});\n }} }} jqinit();\n '''.format(parent_div=parent_div, include_string=include_string, hwurl=hwurl, table_str=table_str, height=height, id=graph_id, options=util.maybe_dumps(params)) return return_string
def graph(): params = {'pk': g('pk', '_', False), 'dimension': g('dimension', '_', False), 'metric': g('metric', 'hits', False), 'depth': g('depth', 0), 'tzoffset': g('tzoffset', 0.0), 'period': g('period', '3600x86400'), 'area': g('area', ''), } pk = params['pk'] dimension = params['dimension'] period = params['period'] debug = g('debug', False) parent_div = g('parent_div', 'hailwhale_graphs') hide_table = g('hide_table', False) height = g('height', '400px') params['title'] = g('title', '') if not params['title']: pkname = g('pk', '') dimname = util.try_loads(g('dimension', 'Overall')) dimname = isinstance(dimname, list) and dimname[-1] or dimname params['title'] = '%s [%s]' % (util.maybe_dumps(pkname), util.maybe_dumps(dimname)) length, interval = [int(part) for part in period.split('x')] if isinstance(hide_table, basestring): hide_table = hide_table.lower() == 'true' hwurl = req.url.split('graph.js')[0] params['autoupdate'] = g('live', True) include_string = \ "document.write(\"<scr\" + \"ipt type='text/javascript' src='%sjs/jquery.min.js'></script>\");"%hwurl if hide_table: table_str = ''' $('#{parent_div}').append('<table> <tr> <th></th> <th>Dimension Name</th> </tr> '''.strip() dimensions = [item['dimension'] for item in Whale().rank(pk, formula).values()] for dimension_counter, dimension in enumerate(dimensions): checked = 'off' if dimension_counter < 10: checked = 'on' table_str += ''' <tr> <td><input id="" type="checkbox" value="{checked}" name="checkbox-{pk}-{dimension}"></td> <td>{dimension}</td> </tr> '''.format(pk=pk, dimension=dimension, checked=checked) table_str += '''</table>');''' else: table_str = '' debug_return_string = ''' appended=false;\n document.write('<div id="{id}" style="height: {height}"></div>');\n function jqinit() {{\n if(typeof(jQuery) == 'undefined') {{\n if(!appended) {{\n appended = true;\n {include_string}\n }}\n setTimeout(jqinit, 250);\n }} else {{\n $(function() {{\n // Nest a few of these, very poor form \n $.getScript('{hwurl}js/highcharts.src.js', function() {{\n $.getScript('{hwurl}js/d3.js', function() {{\n $.getScript('{hwurl}js/hailwhale.js', function() {{\n $.hailwhale('{hwurl}').add_graph('{id}', {options});\n {table_str} }});\n }});\n }});\n }});\n }} }} jqinit();\n '''.format(parent_div=parent_div, include_string=include_string, hwurl=hwurl, table_str=table_str, height=height, id=hashlib.md5(str(params)).hexdigest(), options=util.maybe_dumps(params)) include_string = \ "document.write(\"<scr\" + \"ipt type='text/javascript' src='%sjs/hailwhale.min.js'></script>\");"%hwurl return_string = ''' appended=false;\n document.write('<div id="{id}" style="height: {height}"></div>');\n function jqinit() {{\n if(typeof(jQuery) == 'undefined' || typeof(jQuery.hailwhale) == 'undefined') {{\n if(!appended) {{\n appended = true;\n {include_string}\n }}\n setTimeout(jqinit, 250);\n }} else {{\n $(function() {{\n $.hailwhale('{hwurl}').add_graph('{id}', {options});\n {table_str} }});\n }} }} jqinit();\n '''.format(parent_div=parent_div, include_string=include_string, hwurl=hwurl, table_str=table_str, height=height, id=hashlib.md5(str(params)).hexdigest(), options=util.maybe_dumps(params)) return debug and debug_return_string or return_string