def _store(redis, pk, dimension, metric, period, dt, count, method='set', rank=False): # Keep a list of graphs per pk key = keyify(pk, dimension, Period.get(period).interval, metric) # Store pk dimensions dimension_key = keyify('dimensions', pk) dimension_json = keyify(dimension) if not dimension_json in _added_dimensions[dimension_key]: redis.sadd(dimension_key, dimension_json) _added_dimensions[dimension_key].append(dimension_json) # Store dimensional subdimensions if dimension != '_': subdimension_key = keyify('subdimensions', pk, parent(dimension)) if not dimension_json in _added_subdimensions[subdimension_key]: redis.sadd(subdimension_key, dimension_json) _added_subdimensions[subdimension_key].append(dimension_json) if method == 'set': new_val = float(count) redis.hset(key, dt, new_val) elif method == 'incr': new_val = redis.execute_command('HINCRBYFLOAT', key, dt, float(count)) if rank and (isinstance(try_loads(pk), list) or dimension != '_'): if isinstance(pk, list) and dimension == '_': tgt_pk = parent(pk) tgt_dimension = dimension else: tgt_pk = pk tgt_dimension = parent(dimension) rank_key = keyify('rank', tgt_pk, tgt_dimension, Period.get(period).interval, dt, metric) redis.zadd(rank_key, dimension_json, new_val) return new_val
def totals(cls, pk, dimensions=None, metrics=None, periods=None): if not periods: periods = DEFAULT_PERIODS if not isinstance(periods, list): periods = [periods] metrics = metrics or ["hits"] if not isinstance(metrics, list): metrics = [metrics] ratios = [] for metric in metrics: if "/" in metric: metrics.remove(metric) ratios.append(metric) metrics += metric.split("/") d = {} for p in periods: p_data = cls.plotpoints(pk, dimensions, metrics, period=str(p)) p_totals = dict() for dim in p_data.keys(): p_totals[dim] = dict() for met, vals in p_data[dim].items(): p_totals[dim][met] = sum([v for k, v in vals.items() if Period.get(p).flatten(k)]) for rat in ratios: top, bot = parse_formula(rat) topt, bott = p_totals[dim][top], p_totals[dim][bot] p_totals[dim][rat] = bott and topt / bott or 0 d[str(p)] = p_totals return d
def _ranked(redis, pk, parent_dimension, metric, period, ats, start=0, size=10, sort_dir=None): top, bot = parse_formula(metric) rank_keyify = lambda ats, met: keyify('rank', pk, parent_dimension, Period.get(period).interval, ats, met) final_rank_key = rank_keyify(ats, metric) def squash_ats(met): if len(ats) > 1: map(lambda at: redis.zremrangebyscore(rank_keyify(at, met), 0, 0), ats) redis.zunionstore(rank_keyify(ats, met), map(lambda at: rank_keyify(at, met), ats)) squash_ats(top) if bot: squash_ats(bot) top_key, bot_key = rank_keyify(ats, top), rank_keyify(ats, bot) redis.execute_command("eval", """ for key_i, key_n in ipairs(redis.call("zrange", KEYS[2], 0, -1)) do local top_s = tonumber(redis.call("zscore", KEYS[1], key_n)) local bot_s = tonumber(redis.call("zscore", KEYS[2], key_n)) if top_s and bot_s and bot_s > 0 then redis.call("zadd", KEYS[3], top_s/bot_s, key_n) end end """, 3, top_key, bot_key, final_rank_key) redis.zremrangebyscore(final_rank_key, 0, 0) return redis.zrange(final_rank_key, start, start + size, desc=not sort_dir or sort_dir.upper() in ['-', 'DESC', 'HIGH'])
def __call__(self, *args, **kwargs): from whale import Whale from periods import Period if len(args) and args[0] == Whale or issubclass(args[0], Whale): args = args[1:] clear_cache = kwargs.pop("unmemoize", False) self.get_cache() if "period" in kwargs: p = Period.get(kwargs["period"]) kwargs["period"] = str(p) ttl = int(p.interval) / 5 else: ttl = 60 key_name = self.keyify(args, kwargs) if clear_cache: self.cache.delete(key_name) try: return json.loads(self.cache[key_name]) except KeyError: value = self.func(Whale, *args, **kwargs) self.cache[key_name] = json.dumps(value) self.cache.expire(key_name, ttl) return value except TypeError: # uncachable -- for instance, passing a list as an argument. # Better to not cache than to blow up entirely. return self.func(Whale, *args, **kwargs)
def scalar_plotpoints( cls, pk, dimensions=None, metrics=None, depth=0, period=None, flot_time=False, points_type=dict ): metrics = metrics or ["hits"] if isinstance(metrics, basestring): metrics = [metrics] period = Period.get(period) sparse = cls.whale_driver().retrieve(pk, dimensions, metrics, period=period) nonsparse = defaultdict(dict) if flot_time: points_type = list for dim, mets in sparse.items(): for met, points in mets.items(): dts = period.datetimes_strs() nonsparse[dim][met] = [] for dt in dts: if flot_time: dt_t = to_flot_time(Period.parse_dt_str(dt)) else: dt_t = dt value = points[dt] if dt in points else 0 nonsparse[dim][met].append([dt_t, float(value)]) nonsparse[dim][met] = points_type(nonsparse[dim][met]) if depth > 0: for sub in cls.get_subdimensions(pk, dimensions): nonsparse = dict( nonsparse.items() + cls.plotpoints( pk, sub, metrics, depth=depth - 1, period=period, flot_time=flot_time, points_type=points_type ).items() ) return nonsparse
def retrieve(self, pk, dimensions, metrics, period=None, dt=None): nested = defaultdict(dict) period = str(Period.get(period)) for dimension in map(maybe_dumps, iterate_dimensions(dimensions)): for metric in map(maybe_dumps, metrics): hash_key = keyify(pk, dimension, period, metric) value_dict = self.hgetall(hash_key) nested[dimension][metric] = dict([(k, float(v)) for k, v in value_dict.items()]) return dict(nested)
def update_count_to(cls, pk, dimensions='_', metrics=None, period=False, at=False, rank=False): period = Period.get(period) at = at or cls.now() dt = period.flatten_str(at) pipe = cls.whale_driver().pipeline(transaction=False) for (metric, i) in metrics.iteritems(): _store(pipe, pk, dimensions, metric, period, dt, i, rank=rank) pipe.execute()
def _retrieve(redis, pk, dimensions, metrics, period=None, dt=None): nested = defaultdict(dict) interval = Period.get(period).interval for dimension in iterate_dimensions(dimensions)+['_']: for metric in metrics: if ':' in metric: metric_name = metric.split(':')[0] else: metric_name = metric hash_key = keyify(pk, dimension, interval, metric_name) value_dict = redis.hgetall(hash_key) nested[maybe_dumps(dimension)][maybe_dumps(metric)] = dict([ (k, float(v)) for k, v in value_dict.items()]) return dict(nested)
def table_graph(): from periods import Period params = { 'tzoffset': g('tzoffset', 0.0), 'period': g('period', str(Period.get(None))), } debug = g('debug', False) table = g('table', '') height = g('height', '300px') delay = g('delay', 5000) hwurl = req.GET.get('hwurl', '/' or req.url.split('table_graph.js')[0]) include_string = \ "document.write(\"<scr\" + \"ipt type='text/javascript' src='%sjs/jquery.min.js'></script>\");"%hwurl include_string += \ "document.write(\"<scr\" + \"ipt type='text/javascript' src='%sjs/hailwhale.js'></script>\");"%hwurl include_string += \ "document.write(\"<scr\" + \"ipt type='text/javascript' src='%sjs/d3.js'></script>\");"%hwurl include_string += \ "document.write(\"<scr\" + \"ipt type='text/javascript' src='%sjs/nvd3.js'></script>\");"%hwurl return_string = ''' appended=false;\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 init_graphs =function() {{ $.hailwhale('{hwurl}').graph_tables('{table}', {options});\n }} setTimeout(init_graphs, {delay}); if(ui_loaded_funcs) ui_loaded_funcs.init_graphs = init_graphs; }});\n }} }} jqinit();\n '''.format(include_string=include_string, table=table, delay=delay, hwurl=hwurl, options=util.maybe_dumps(params)) return return_string
def reasons_for(cls, pk, formula="value/hits", known_data=None, period=None, recursive=True): metric, denomenator = parse_formula(formula) period = Period.get(period) pk_base, decision, option = pk base = "_" best = worst = None ranks = cls.cached_rank(pk, formula=formula, dimension=base, period=period, recursive=recursive, points=False) overall = cls.cached_rank( [pk_base, decision], formula=formula, dimension=base, period=period, recursive=recursive, points=False ) parent_score = overall[base]["score"] parent_count = overall[base]["count"] ranks[base]["effect"] = ranks[base]["count"] * ranks[base]["difference"] def delta(info): diff = info["score"] - parent_score info["value_diff"] = info["value"] - overall[base]["value"] info["difference"] += diff if math.fabs(diff) > 0 and info["count"] > 0: info["effect"] += diff * info["count"] info["significance"] = ((0.5 * info["effect"]) ** 2) / parent_count else: info["effect"] = 0 info["significance"] = 0 return info known_dimensions = iterate_dimensions(known_data) for dim, info in ranks.items(): ranks[dim] = info = delta(info) if try_loads(dim) in known_dimensions and info["important"]: best_score = best and ranks[best]["score"] worst_score = worst and ranks[worst]["score"] if info["score"] > best_score: best = dim if info["score"] < worst_score: worst = dim i = { "good": best and ranks[best] or {}, "bad": worst and ranks[worst] or {}, #'ranks': ranks, "base": ranks[base], "parent": overall[base], } i["high"] = i["good"].get("difference", 0) i["high_sig"] = i["good"].get("significance", 0) > 4 i["low"] = i["bad"].get("difference", 0) i["low_sig"] = i["bad"].get("significance", 0) > 4 return i
def _retrieve(redis, pk, dimensions, metrics, period=None, dt=None): nested = defaultdict(dict) interval = Period.get(period).interval for dimension in iterate_dimensions(dimensions) + ['_']: for metric in metrics: if ':' in metric: metric_name = metric.split(':')[0] else: metric_name = metric hash_key = keyify(pk, dimension, interval, metric_name) value_dict = redis.hgetall(hash_key) nested[maybe_dumps(dimension)][maybe_dumps(metric)] = dict([ (k, float(v)) for k, v in value_dict.items() ]) return dict(nested)
def table_graph(): from periods import Period params = { 'tzoffset': g('tzoffset', 0.0), 'period': g('period', str(Period.get(None))), } debug = g('debug', False) table = g('table', '') height = g('height', '300px') delay = g('delay', 5000) hwurl = req.GET.get('hwurl', '/' or req.url.split('table_graph.js')[0]) include_string = \ "document.write(\"<scr\" + \"ipt type='text/javascript' src='%sjs/jquery.min.js'></script>\");"%hwurl include_string += \ "document.write(\"<scr\" + \"ipt type='text/javascript' src='%sjs/hailwhale.js'></script>\");"%hwurl include_string += \ "document.write(\"<scr\" + \"ipt type='text/javascript' src='%sjs/d3.js'></script>\");"%hwurl include_string += \ "document.write(\"<scr\" + \"ipt type='text/javascript' src='%sjs/nvd3.js'></script>\");"%hwurl return_string = ''' appended=false;\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 init_graphs =function() {{ $.hailwhale('{hwurl}').graph_tables('{table}', {options});\n }} setTimeout(init_graphs, {delay}); if(ui_loaded_funcs) ui_loaded_funcs.init_graphs = init_graphs; }});\n }} }} jqinit();\n '''.format( include_string=include_string, table=table, delay=delay, hwurl=hwurl, options=util.maybe_dumps(params)) return return_string
def reasons_for(cls, pk, formula='value/hits', known_data=None, period=None, recursive=True): metric, denomenator = parse_formula(formula) period = Period.get(period) pk_base, decision, option = pk base = '_' best = worst = None ranks = cls.cached_rank(pk, formula=formula, dimension=base, period=period, recursive=recursive, points=False) overall = cls.cached_rank([pk_base, decision], formula=formula, dimension=base, period=period, recursive=recursive, points=False) parent_score = overall[base]['score'] parent_count = overall[base]['count'] ranks[base]['effect'] = ranks[base]['count'] * ranks[base]['difference'] def delta(info): diff = info['score'] - parent_score info['value_diff'] = info['value'] - overall[base]['value'] info['difference'] += diff if math.fabs(diff) > 0 and info['count'] > 0: info['effect'] += diff * info['count'] info['significance'] = ((.5 * info['effect']) ** 2) / parent_count else: info['effect'] = 0 info['significance'] = 0 return info known_dimensions = iterate_dimensions(known_data) for dim, info in ranks.items(): ranks[dim] = info = delta(info) if try_loads(dim) in known_dimensions and info['important']: best_score = best and ranks[best]['score'] worst_score = worst and ranks[worst]['score'] if info['score'] > best_score: best = dim if info['score'] < worst_score: worst = dim i = {'good': best and ranks[best] or {}, 'bad': worst and ranks[worst] or {}, #'ranks': ranks, 'base': ranks[base], 'parent': overall[base]} i['high'] = i['good'].get('difference', 0) i['high_sig'] = i['good'].get('significance', 0) > 4 i['low'] = i['bad'].get('difference', 0) i['low_sig'] = i['bad'].get('significance', 0) > 4 return i
def _ranked(redis, pk, parent_dimension, metric, period, ats, start=0, size=10, sort_dir=None): top, bot = parse_formula(metric) rank_keyify = lambda ats, met: keyify('rank', pk, parent_dimension, Period.get(period).interval, ats, met ) final_rank_key = rank_keyify(ats, metric) def squash_ats(met): if len(ats) > 1: map(lambda at: redis.zremrangebyscore(rank_keyify(at, met), 0, 0), ats) redis.zunionstore(rank_keyify(ats, met), map(lambda at: rank_keyify(at, met), ats)) squash_ats(top) if bot: squash_ats(bot) top_key, bot_key = rank_keyify(ats, top), rank_keyify(ats, bot) redis.execute_command( "eval", """ for key_i, key_n in ipairs(redis.call("zrange", KEYS[2], 0, -1)) do local top_s = tonumber(redis.call("zscore", KEYS[1], key_n)) local bot_s = tonumber(redis.call("zscore", KEYS[2], key_n)) if top_s and bot_s and bot_s > 0 then redis.call("zadd", KEYS[3], top_s/bot_s, key_n) end end """, 3, top_key, bot_key, final_rank_key) redis.zremrangebyscore(final_rank_key, 0, 0) return redis.zrange(final_rank_key, start, start + size, desc=not sort_dir or sort_dir.upper() in ['-', 'DESC', 'HIGH'])
def plotpoints(cls, pk, dimensions=None, metrics=None, depth=0, period=None, flot_time=False, points_type=dict): metrics = metrics or ['hits'] if isinstance(metrics, basestring): metrics = [metrics] period = Period.get(period) dts = period.datetimes_strs() nonsparse = defaultdict(defaultdict) # Hardwire time-based metrics for lulz time_metrics = {'second': 1, 'minute': 60, 'hour': 3600, 'day': 3600*24, 'week': 3600*24*7} #for t_m, factor in time_metrics.items(): # if t_m in metrics: # metrics.remove(t_m) # for dimension in dimensions: # nonsparse[dimension][t_m] = list() # for dt in dts: # if flot_time: # dt = to_flot_time(Period.parse_dt_str(dt)) # nonsparse[dimension][t_m].append([dt, period.interval / factor]) # nonsparse[dimension][t_m] = points_type(nonsparse[dimension][t_m]) # Pull the plotpoints that exist from Redis sparse = cls.whale_driver().retrieve(pk, dimensions, metrics, period=period) for dimensions, metrics in sparse.items(): for metric, points in metrics.items(): #if metric in time_metrics: continue nonsparse[dimensions][metric] = [] for dt in dts: if flot_time: dt = to_flot_time(Period.parse_dt_str(dt)) value = points[dt] if dt in points else 0 nonsparse[dimensions][metric].append([dt, float(value)]) nonsparse[dimensions][metric] = points_type(nonsparse[dimensions][metric]) if depth > 0: for sub in cls.get_subdimensions(pk, dimensions): nonsparse = dict(nonsparse.items() + cls.plotpoints(pk, sub, metrics, depth=depth - 1, period=period, flot_time=flot_time, points_type=points_type).items()) return nonsparse
def totals(cls, pk, dimensions=None, metrics=None, periods=None): if not periods: periods = DEFAULT_PERIODS if not isinstance(periods, list): periods = [periods] metrics = metrics or ['hits'] if not isinstance(metrics, list): metrics = [metrics] d = {} for p in periods: p_data = cls.plotpoints(pk, dimensions, metrics, period=str(p)) p_totals = dict() for dim, mets in p_data.items(): p_totals[dim] = dict() for met, vals in mets.items(): p_totals[dim][met] = sum([ v for k, v in vals.items() if Period.get(p).flatten(k)]) d[str(p)] = p_totals d['alltime'] = cls.whale_driver().retrieve( pk, dimensions, metrics, period='all') return d
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 reasons_for(cls, pk, formula='value/hits', known_data=None, period=None, recursive=True): metric, denomenator = parse_formula(formula) period = Period.get(period) pk_base, decision, option = pk base = '_' best = worst = None ranks = cls.cached_rank(pk, formula=formula, dimension=base, period=period, recursive=recursive, points=False) overall = cls.cached_rank([pk_base, decision], formula=formula, dimension=base, period=period, recursive=recursive, points=False) parent_score = overall[base]['score'] parent_count = overall[base]['count'] ranks[base][ 'effect'] = ranks[base]['count'] * ranks[base]['difference'] def delta(info): diff = info['score'] - parent_score info['value_diff'] = info['value'] - overall[base]['value'] info['difference'] += diff if math.fabs(diff) > 0 and info['count'] > 0: info['effect'] += diff * info['count'] info['significance'] = ( (.5 * info['effect'])**2) / parent_count else: info['effect'] = 0 info['significance'] = 0 return info known_dimensions = iterate_dimensions(known_data) for dim, info in ranks.items(): ranks[dim] = info = delta(info) if try_loads(dim) in known_dimensions and info['important']: best_score = best and ranks[best]['score'] worst_score = worst and ranks[worst]['score'] if info['score'] > best_score: best = dim if info['score'] < worst_score: worst = dim i = { 'good': best and ranks[best] or {}, 'bad': worst and ranks[worst] or {}, #'ranks': ranks, 'base': ranks[base], 'parent': overall[base] } i['high'] = i['good'].get('difference', 0) i['high_sig'] = i['good'].get('significance', 0) > 4 i['low'] = i['bad'].get('difference', 0) i['low_sig'] = i['bad'].get('significance', 0) > 4 return i
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