From 085430cb53864f124a0c0b3ec230b01165546898 Mon Sep 17 00:00:00 2001 From: Maxime Beauchemin Date: Sat, 16 Jan 2016 00:05:55 -0800 Subject: [PATCH] Doing some refactoring --- panoramix/models.py | 15 ++++++----- panoramix/static/panoramix.js | 25 +++++++++++++++++- panoramix/static/widgets/viz_bignumber.js | 3 ++- panoramix/static/widgets/viz_nvd3.js | 31 ++++------------------- panoramix/templates/panoramix/base.html | 1 + panoramix/viz.py | 13 ++-------- 6 files changed, 42 insertions(+), 46 deletions(-) diff --git a/panoramix/models.py b/panoramix/models.py index cc214a1e97..63e3690ce4 100644 --- a/panoramix/models.py +++ b/panoramix/models.py @@ -412,7 +412,7 @@ class SqlaTable(Model, Queryable, AuditMixinNullable): "Datetime column not provided as part table configuration") dttm_expr = cols[granularity].expression if dttm_expr: - timestamp = ColumnClause(dttm_expr, is_literal=True).label('timestamp') + timestamp = literal_column(dttm_expr).label('timestamp') else: timestamp = literal_column(granularity).label('timestamp') metrics_exprs = [ @@ -437,8 +437,8 @@ class SqlaTable(Model, Queryable, AuditMixinNullable): col = cols[s] expr = col.expression if expr: - outer = ColumnClause(expr, is_literal=True).label(s) - inner = ColumnClause(expr, is_literal=True).label('__' + s) + outer = literal_column(expr).label(s) + inner = literal_column(expr).label('__' + s) else: outer = column(s).label(s) inner = column(s).label('__' + s) @@ -462,15 +462,16 @@ class SqlaTable(Model, Queryable, AuditMixinNullable): if not columns: qry = qry.group_by(*groupby_exprs) + tf = '%Y-%m-%d %H:%M:%S.%f' time_filter = [ - timestamp >= from_dttm.isoformat(), - timestamp <= to_dttm.isoformat(), + timestamp >= from_dttm.strftime(tf), + timestamp <= to_dttm.strftime(tf), ] inner_time_filter = copy(time_filter) if inner_from_dttm: - inner_time_filter[0] = timestamp >= inner_from_dttm.isoformat() + inner_time_filter[0] = timestamp >= inner_from_dttm.strftime(tf) if inner_to_dttm: - inner_time_filter[1] = timestamp <= inner_to_dttm.isoformat() + inner_time_filter[1] = timestamp <= inner_to_dttm.strftime(tf) where_clause_and = [] having_clause_and = [] for col, op, eq in filter: diff --git a/panoramix/static/panoramix.js b/panoramix/static/panoramix.js index ef833bc98c..9e8996f007 100644 --- a/panoramix/static/panoramix.js +++ b/panoramix/static/panoramix.js @@ -3,6 +3,28 @@ var px = (function() { var visualizations = {}; var dashboard = undefined; + function UTC(dttm){ + return v = new Date(dttm.getUTCFullYear(), dttm.getUTCMonth(), dttm.getUTCDate(), dttm.getUTCHours(), dttm.getUTCMinutes(), dttm.getUTCSeconds()); + } + var tickMultiFormat = d3.time.format.multi([ + [".%L", function(d) { return d.getMilliseconds(); }], // If there are millisections, show only them + [":%S", function(d) { return d.getSeconds(); }], // If there are seconds, show only them + ["%a %b %d, %I:%M %p", function(d) { return d.getMinutes()!=0; }], // If there are non-zero minutes, show Date, Hour:Minute [AM/PM] + ["%a %b %d, %I %p", function(d) { return d.getHours() != 0; }], // If there are hours that are multiples of 3, show date and AM/PM + ["%a %b %d, %Y", function(d) { return d.getDate() != 1; }], // If not the first of the month, do "month day, year." + ["%B %Y", function(d) { return d.getMonth() != 0 && d.getDate() == 1; }], // If the first of the month, do "month day, year." + ["%Y", function(d) { return true; }] // fall back on month, year + ]); + function formatDate(dttm) { + var d = UTC(new Date(dttm)); + //d = new Date(d.getTime() - 1 * 60 * 60 * 1000); + return tickMultiFormat(d); + } + colors = [ + "#FF5A5F", "#007A87", "#7B0051", "#00D1C1", "#8CE071", "#FFB400", + "#FFAA91", "#B4A76C", "#9CA299", "#565A5C" + ]; + var Slice = function(data, dashboard){ var timer; var token = $('#' + data.token); @@ -119,7 +141,6 @@ var px = (function() { addFilter: function(slice_id, filters) { this.filters[slice_id] = filters; this.refreshExcept(slice_id); - console.log(this.filters); }, readFilters: function() { // Returns a list of human readable active filters @@ -424,5 +445,7 @@ var px = (function() { druidify: druidify, initExploreView: initExploreView, initDashboardView: initDashboardView, + formatDate: formatDate, + colors: colors, } })(); diff --git a/panoramix/static/widgets/viz_bignumber.js b/panoramix/static/widgets/viz_bignumber.js index 361d083f33..95d8717dd4 100644 --- a/panoramix/static/widgets/viz_bignumber.js +++ b/panoramix/static/widgets/viz_bignumber.js @@ -98,7 +98,8 @@ px.registerViz('big_number', function(slice) { var x_axis = d3.svg.axis() .scale(scale_x) .orient('bottom') - .ticks(4); + .ticks(4) + .tickFormat(px.formatDate); g.call(x_axis); g.attr('transform', 'translate(0,' + (height - margin) + ')'); diff --git a/panoramix/static/widgets/viz_nvd3.js b/panoramix/static/widgets/viz_nvd3.js index b2025760dc..c84da4f514 100644 --- a/panoramix/static/widgets/viz_nvd3.js +++ b/panoramix/static/widgets/viz_nvd3.js @@ -2,27 +2,6 @@ function viz_nvd3(slice) { var chart = undefined; var data = {}; - function UTC(dttm){ - return v = new Date(dttm.getUTCFullYear(), dttm.getUTCMonth(), dttm.getUTCDate(), dttm.getUTCHours(), dttm.getUTCMinutes(), dttm.getUTCSeconds()); - } - var tickMultiFormat = d3.time.format.multi([ - [".%L", function(d) { return d.getMilliseconds(); }], // If there are millisections, show only them - [":%S", function(d) { return d.getSeconds(); }], // If there are seconds, show only them - ["%a %b %d, %I:%M %p", function(d) { return d.getMinutes()!=0; }], // If there are non-zero minutes, show Date, Hour:Minute [AM/PM] - ["%a %b %d, %I %p", function(d) { return d.getHours() != 0; }], // If there are hours that are multiples of 3, show date and AM/PM - ["%a %b %d, %Y", function(d) { return d.getDate() != 1; }], // If not the first of the month, do "month day, year." - ["%B %Y", function(d) { return d.getMonth() != 0 && d.getDate() == 1; }], // If the first of the month, do "month day, year." - ["%Y", function(d) { return true; }] // fall back on month, year - ]); - function formatDate(dttm) { - var d = UTC(new Date(dttm)); - //d = new Date(d.getTime() - 1 * 60 * 60 * 1000); - return tickMultiFormat(d); - } - colors = [ - "#FF5A5F", "#007A87", "#7B0051", "#00D1C1", "#8CE071", "#FFB400", - "#FFAA91", "#B4A76C", "#9CA299", "#565A5C" - ]; var refresh = function() { $.getJSON(slice.jsonEndpoint(), function(payload) { var fd = payload.form_data; @@ -36,7 +15,7 @@ function viz_nvd3(slice) { chart.lines2.xScale(d3.time.scale.utc()); chart.x2Axis .showMaxMin(fd.x_axis_showminmax) - .tickFormat(formatDate) + .tickFormat(px.formatDate) .staggerLabels(true); } else { chart = nv.models.lineChart() @@ -47,7 +26,7 @@ function viz_nvd3(slice) { chart.interpolate(fd.line_interpolation); chart.xAxis .showMaxMin(fd.x_axis_showminmax) - .tickFormat(formatDate) + .tickFormat(px.formatDate) .staggerLabels(true); chart.showLegend(fd.show_legend); chart.yAxis.tickFormat(d3.format('.3s')); @@ -66,7 +45,7 @@ function viz_nvd3(slice) { .groupSpacing(0.1); chart.xAxis .showMaxMin(false) - .tickFormat(formatDate) + .tickFormat(px.formatDate) .staggerLabels(true); chart.showLegend(fd.show_legend); chart.stacked(fd.bar_stacked); @@ -104,7 +83,7 @@ function viz_nvd3(slice) { chart.xScale(d3.time.scale.utc()); chart.xAxis .showMaxMin(false) - .tickFormat(formatDate) + .tickFormat(px.formatDate) .staggerLabels(true); chart.showLegend(fd.show_legend); chart.yAxis.tickFormat(d3.format('.3p')); @@ -137,7 +116,7 @@ function viz_nvd3(slice) { chart.xScale(d3.time.scale.utc()); chart.xAxis .showMaxMin(false) - .tickFormat(formatDate) + .tickFormat(px.formatDate) .staggerLabels(true); chart.showLegend(fd.show_legend); chart.yAxis.tickFormat(d3.format('.3s')); diff --git a/panoramix/templates/panoramix/base.html b/panoramix/templates/panoramix/base.html index bb9bd3299a..323e6ad9d3 100644 --- a/panoramix/templates/panoramix/base.html +++ b/panoramix/templates/panoramix/base.html @@ -9,6 +9,7 @@ {% endblock %} {% block tail_js %} + diff --git a/panoramix/viz.py b/panoramix/viz.py index 458245babb..776fb25f81 100644 --- a/panoramix/viz.py +++ b/panoramix/viz.py @@ -289,7 +289,6 @@ class TableViz(BaseViz): ] is_timeseries = False js_files = [ - 'lib/d3.min.js', 'lib/dataTables/jquery.dataTables.min.js', 'lib/dataTables/dataTables.bootstrap.js', 'widgets/viz_table.js', @@ -437,7 +436,6 @@ class WordCloudViz(BaseViz): ) },) js_files = [ - 'lib/d3.min.js', 'lib/d3.layout.cloud.js', 'widgets/viz_wordcloud.js', ] @@ -463,7 +461,6 @@ class NVD3Viz(BaseViz): verbose_name = "Base NVD3 Viz" is_timeseries = False js_files = [ - 'lib/d3.min.js', 'lib/nvd3/nv.d3.min.js', 'widgets/viz_nvd3.js', ] @@ -547,7 +544,6 @@ class BigNumberViz(BaseViz): verbose_name = "Big Number" is_timeseries = True js_files = [ - 'lib/d3.min.js', 'widgets/viz_bignumber.js', ] css_files = [ @@ -562,6 +558,7 @@ class BigNumberViz(BaseViz): 'metric', 'compare_lag', 'compare_suffix', + 'y_axis_format', ) },) @@ -584,7 +581,6 @@ class BigNumberViz(BaseViz): form_data = self.form_data df = self.get_df() df = df.sort(columns=df.columns[0]) - df['timestamp'] = df[[0]].astype(np.int64) // 10**9 compare_lag = form_data.get("compare_lag", "") compare_lag = int(compare_lag) if compare_lag and compare_lag.isdigit() else 0 d = { @@ -592,7 +588,7 @@ class BigNumberViz(BaseViz): 'compare_lag': compare_lag, 'compare_suffix': form_data.get('compare_suffix', ''), } - return json.dumps(d) + return dumps(d) class NVD3TimeSeriesViz(NVD3Viz): @@ -865,7 +861,6 @@ class SunburstViz(BaseViz): verbose_name = "Sunburst" is_timeseries = False js_files = [ - 'lib/d3.min.js', 'widgets/viz_sunburst.js'] css_files = ['widgets/viz_sunburst.css'] fieldsets = ( @@ -931,7 +926,6 @@ class SankeyViz(BaseViz): verbose_name = "Sankey" is_timeseries = False js_files = [ - 'lib/d3.min.js', 'lib/d3-sankey.js', 'widgets/viz_sankey.js'] css_files = ['widgets/viz_sankey.css'] @@ -973,7 +967,6 @@ class DirectedForceViz(BaseViz): verbose_name = "Directed Force Layout" is_timeseries = False js_files = [ - 'lib/d3.min.js', 'widgets/viz_directed_force.js'] css_files = ['widgets/viz_directed_force.css'] fieldsets = ( @@ -1019,7 +1012,6 @@ class WorldMapViz(BaseViz): verbose_name = "World Map" is_timeseries = False js_files = [ - 'lib/d3.min.js', 'lib/topojson.min.js', 'lib/datamaps.all.js', 'widgets/viz_world_map.js'] @@ -1098,7 +1090,6 @@ class FilterBoxViz(BaseViz): verbose_name = "Filters" is_timeseries = False js_files = [ - 'lib/d3.min.js', 'widgets/viz_filter_box.js'] css_files = [ 'widgets/viz_filter_box.css']