Fix sorting of sizes on the statistics views by sorting raw values and prettifying on the client side. Includes Jasmine tests for the prettyfying function. Fixes #2315

This commit is contained in:
Khushboo Vashi 2017-05-08 11:29:13 +01:00 committed by Dave Page
parent c32bf7780e
commit 94b7fc483a
18 changed files with 169 additions and 39 deletions

View File

@ -627,6 +627,7 @@ function($, _, S, pgAdmin, pgBrowser, Alertify) {
canDropCascade: true,
hasDepends: true,
hasStatistics: true,
statsPrettifyFields: ['Index size'],
Init: function() {
/* Avoid multiple registration of menus */
if (this.initialized)

View File

@ -19,6 +19,7 @@ function($, _, S, pgAdmin, pgBrowser, alertify) {
hasSQL: true,
hasDepends: true,
hasStatistics: true,
statsPrettifyFields: ['Index size'],
parent_type: 'table',
canDrop: true,
canDropCascade: true,

View File

@ -12,7 +12,8 @@ function($, _, S, pgAdmin, pgBrowser, Backform, alertify) {
sqlAlterHelp: 'sql-alterindex.html',
sqlCreateHelp: 'sql-createindex.html',
columns: ['name', 'description'],
hasStatistics: true
hasStatistics: true,
statsPrettifyFields: ['Size', 'Index size']
});
};
@ -215,6 +216,7 @@ function($, _, S, pgAdmin, pgBrowser, Backform, alertify) {
hasSQL: true,
hasDepends: true,
hasStatistics: true,
statsPrettifyFields: ['Size', 'Index size'],
Init: function() {
/* Avoid mulitple registration of menus */
if (this.initialized)

View File

@ -4,12 +4,12 @@ SELECT
idx_tup_fetch AS {{ conn|qtIdent(_('Index tuples fetched')) }},
idx_blks_read AS {{ conn|qtIdent(_('Index blocks read')) }},
idx_blks_hit AS {{ conn|qtIdent(_('Index blocks hit')) }},
pg_size_pretty(pg_relation_size({{ exid }}::OID)) AS {{ conn|qtIdent(_('Index size')) }}
pg_relation_size({{ exid }}::OID) AS {{ conn|qtIdent(_('Index size')) }}
{#=== Extended stats ===#}
{% if is_pgstattuple %}
,version AS {{ conn|qtIdent(_('Version')) }},
tree_level AS {{ conn|qtIdent(_('Tree level')) }},
pg_size_pretty(index_size) AS {{ conn|qtIdent(_('Index size')) }},
index_size AS {{ conn|qtIdent(_('Index size')) }},
root_block_no AS {{ conn|qtIdent(_('Root block no')) }},
internal_pages AS {{ conn|qtIdent(_('Internal pages')) }},
leaf_pages AS {{ conn|qtIdent(_('Leaf pages')) }},

View File

@ -3,7 +3,7 @@ SELECT
idx_scan AS {{ conn|qtIdent(_('Index scans')) }},
idx_tup_read AS {{ conn|qtIdent(_('Index tuples read')) }},
idx_tup_fetch AS {{ conn|qtIdent(_('Index tuples fetched')) }},
pg_size_pretty(pg_relation_size(indexrelid)) AS {{ conn|qtIdent(_('Size')) }}
pg_relation_size(indexrelid) AS {{ conn|qtIdent(_('Size')) }}
FROM
pg_stat_all_indexes stat
JOIN pg_class cls ON cls.oid=indexrelid

View File

@ -4,12 +4,12 @@ SELECT
idx_tup_fetch AS {{ conn|qtIdent(_('Index tuples fetched')) }},
idx_blks_read AS {{ conn|qtIdent(_('Index blocks read')) }},
idx_blks_hit AS {{ conn|qtIdent(_('Index blocks hit')) }},
pg_size_pretty(pg_relation_size({{ idx }}::OID)) AS {{ conn|qtIdent(_('Index size')) }}
pg_relation_size({{ idx }}::OID) AS {{ conn|qtIdent(_('Index size')) }}
{#=== Extended stats ===#}
{% if is_pgstattuple %}
,version AS {{ conn|qtIdent(_('Version')) }},
tree_level AS {{ conn|qtIdent(_('Tree level')) }},
pg_size_pretty(index_size) AS {{ conn|qtIdent(_('Index size')) }},
index_size AS {{ conn|qtIdent(_('Index size')) }},
root_block_no AS {{ conn|qtIdent(_('Root block no')) }},
internal_pages AS {{ conn|qtIdent(_('Internal pages')) }},
leaf_pages AS {{ conn|qtIdent(_('Leaf pages')) }},

View File

@ -4,12 +4,12 @@ SELECT
idx_tup_fetch AS {{ conn|qtIdent(_('Index tuples fetched')) }},
idx_blks_read AS {{ conn|qtIdent(_('Index blocks read')) }},
idx_blks_hit AS {{ conn|qtIdent(_('Index blocks hit')) }},
pg_size_pretty(pg_relation_size({{ cid }}::OID)) AS {{ conn|qtIdent(_('Index size')) }}
pg_relation_size({{ cid }}::OID) AS {{ conn|qtIdent(_('Index size')) }}
{#=== Extended stats ===#}
{% if is_pgstattuple %}
,version AS {{ conn|qtIdent(_('Version')) }},
tree_level AS {{ conn|qtIdent(_('Tree level')) }},
pg_size_pretty(index_size) AS {{ conn|qtIdent(_('Index size')) }},
index_size AS {{ conn|qtIdent(_('Index size')) }},
root_block_no AS {{ conn|qtIdent(_('Root block no')) }},
internal_pages AS {{ conn|qtIdent(_('Internal pages')) }},
leaf_pages AS {{ conn|qtIdent(_('Leaf pages')) }},

View File

@ -13,7 +13,10 @@ function($, _, S, pgAdmin, pgBrowser, alertify) {
label: '{{ _('Tables') }}',
type: 'coll-table',
columns: ['name', 'relowner', 'description'],
hasStatistics: true
hasStatistics: true,
statsPrettifyFields: ['Size', 'Indexes size', 'Table size',
'Toast table size', 'Tuple length',
'Dead tuple length', 'Free space']
});
};
@ -25,6 +28,9 @@ function($, _, S, pgAdmin, pgBrowser, alertify) {
hasSQL: true,
hasDepends: true,
hasStatistics: true,
statsPrettifyFields: ['Size', 'Indexes size', 'Table size',
'Toast table size', 'Tuple length',
'Dead tuple length', 'Free space'],
sqlAlterHelp: 'sql-altertable.html',
sqlCreateHelp: 'sql-createtable.html',
dialogHelp: '{{ url_for('help.static', filename='table_dialog.html') }}',

View File

@ -14,12 +14,12 @@ SELECT
autovacuum_count AS {{ conn|qtIdent(_('Autovacuum counter')) }},
analyze_count AS {{ conn|qtIdent(_('Analyze counter')) }},
autoanalyze_count AS {{ conn|qtIdent(_('Autoanalyze counter')) }},
pg_size_pretty(pg_relation_size(st.relid)
pg_relation_size(st.relid)
+ CASE WHEN cl.reltoastrelid = 0 THEN 0 ELSE pg_relation_size(cl.reltoastrelid)
+ COALESCE((SELECT SUM(pg_relation_size(indexrelid))
FROM pg_index WHERE indrelid=cl.reltoastrelid)::int8, 0) END
+ COALESCE((SELECT SUM(pg_relation_size(indexrelid))
FROM pg_index WHERE indrelid=st.relid)::int8, 0)) AS {{ conn|qtIdent(_('Size')) }}
FROM pg_index WHERE indrelid=st.relid)::int8, 0) AS {{ conn|qtIdent(_('Size')) }}
FROM
pg_stat_all_tables st
JOIN

View File

@ -25,23 +25,23 @@ SELECT
autovacuum_count AS {{ conn|qtIdent(_('Autovacuum counter')) }},
analyze_count AS {{ conn|qtIdent(_('Analyze counter')) }},
autoanalyze_count AS {{ conn|qtIdent(_('Autoanalyze counter')) }},
pg_size_pretty(pg_relation_size(stat.relid)) AS {{ conn|qtIdent(_('Table size')) }},
CASE WHEN cl.reltoastrelid = 0 THEN NULL ELSE pg_size_pretty(pg_relation_size(cl.reltoastrelid)
pg_relation_size(stat.relid) AS {{ conn|qtIdent(_('Table size')) }},
CASE WHEN cl.reltoastrelid = 0 THEN NULL ELSE pg_relation_size(cl.reltoastrelid)
+ COALESCE((SELECT SUM(pg_relation_size(indexrelid))
FROM pg_index WHERE indrelid=cl.reltoastrelid)::int8, 0))
FROM pg_index WHERE indrelid=cl.reltoastrelid)::int8, 0)
END AS {{ conn|qtIdent(_('Toast table size')) }},
pg_size_pretty(COALESCE((SELECT SUM(pg_relation_size(indexrelid))
FROM pg_index WHERE indrelid=stat.relid)::int8, 0))
COALESCE((SELECT SUM(pg_relation_size(indexrelid))
FROM pg_index WHERE indrelid=stat.relid)::int8, 0)
AS {{ conn|qtIdent(_('Indexes size')) }}
{% if is_pgstattuple %}
{#== EXTENDED STATS ==#}
,tuple_count AS {{ conn|qtIdent(_('Tuple count')) }},
pg_size_pretty(tuple_len) AS {{ conn|qtIdent(_('Tuple length')) }},
tuple_len AS {{ conn|qtIdent(_('Tuple length')) }},
tuple_percent AS {{ conn|qtIdent(_('Tuple percent')) }},
dead_tuple_count AS {{ conn|qtIdent(_('Dead tuple count')) }},
pg_size_pretty(dead_tuple_len) AS {{ conn|qtIdent(_('Dead tuple length')) }},
dead_tuple_len AS {{ conn|qtIdent(_('Dead tuple length')) }},
dead_tuple_percent AS {{ conn|qtIdent(_('Dead tuple percent')) }},
pg_size_pretty(free_space) AS {{ conn|qtIdent(_('Free space')) }},
free_space AS {{ conn|qtIdent(_('Free space')) }},
free_percent AS {{ conn|qtIdent(_('Free percent')) }}
FROM
pgstattuple('{{schema_name}}.{{table_name}}'), pg_stat_all_tables stat

View File

@ -12,7 +12,8 @@ function($, _, S, pgAdmin, pgBrowser, Alertify) {
label: '{{ _('Databases') }}',
type: 'coll-database',
columns: ['name', 'datowner', 'comments'],
hasStatistics: true
hasStatistics: true,
statsPrettifyFields: ['Size', 'Size of temporary files']
});
};
@ -26,6 +27,7 @@ function($, _, S, pgAdmin, pgBrowser, Alertify) {
hasSQL: true,
hasDepends: true,
hasStatistics: true,
statsPrettifyFields: ['Size', 'Size of temporary files'],
canDrop: function(node) {
return node.canDrop;
},

View File

@ -17,11 +17,11 @@ SELECT
slave.confl_bufferpin AS {{ conn|qtIdent(_('Bufferpin conflicts')) }},
slave.confl_deadlock AS {{ conn|qtIdent(_('Deadlock conflicts')) }},
temp_files AS {{ conn|qtIdent(_("Temporary files")) }},
pg_size_pretty(temp_bytes) AS {{ conn|qtIdent(_("Size of temporary files")) }},
temp_bytes AS {{ conn|qtIdent(_("Size of temporary files")) }},
deadlocks AS {{ conn|qtIdent(_("Deadlocks")) }},
blk_read_time AS {{ conn|qtIdent(_("Block read time")) }},
blk_write_time AS {{ conn|qtIdent(_("Block write time")) }},
pg_size_pretty(pg_database_size(db.datid)) AS {{ conn|qtIdent(_('Size')) }}
pg_database_size(db.datid) AS {{ conn|qtIdent(_('Size')) }}
FROM
pg_stat_database db
LEFT JOIN pg_stat_database_conflicts slave ON db.datid=slave.datid

View File

@ -16,7 +16,7 @@ SELECT
slave.confl_snapshot AS {{ conn|qtIdent(_('Snapshot conflicts')) }},
slave.confl_bufferpin AS {{ conn|qtIdent(_('Bufferpin conflicts')) }},
slave.confl_deadlock AS {{ conn|qtIdent(_('Deadlock conflicts')) }},
pg_size_pretty(pg_database_size(db.datid)) AS {{ conn|qtIdent(_('Size')) }}
pg_database_size(db.datid) AS {{ conn|qtIdent(_('Size')) }}
FROM
pg_stat_database db
LEFT JOIN pg_stat_database_conflicts slave ON db.datid=slave.datid

View File

@ -9,7 +9,8 @@ function($, _, S, pgAdmin, pgBrowser, alertify) {
label: '{{ _('Tablespaces') }}',
type: 'coll-tablespace',
columns: ['name', 'spcuser', 'description'],
hasStatistics: true
hasStatistics: true,
statsPrettifyFields: ['Size']
});
};
@ -25,6 +26,7 @@ function($, _, S, pgAdmin, pgBrowser, alertify) {
canDrop: true,
hasDepends: true,
hasStatistics: true,
statsPrettifyFields: ['Size'],
Init: function() {
/* Avoid mulitple registration of menus */
if (this.initialized)

View File

@ -1,9 +1,9 @@
{### SQL to fetch tablespace object stats ###}
{% if tsid %}
SELECT pg_size_pretty(pg_tablespace_size({{ tsid|qtLiteral }}::OID)) AS {{ conn|qtIdent(_('Size')) }}
SELECT pg_tablespace_size({{ tsid|qtLiteral }}::OID) AS {{ conn|qtIdent(_('Size')) }}
{% else %}
SELECT ts.spcname AS {{ conn|qtIdent(_('Name')) }},
pg_size_pretty(pg_tablespace_size(ts.oid)) AS {{ conn|qtIdent(_('Size')) }}
pg_tablespace_size(ts.oid) AS {{ conn|qtIdent(_('Size')) }}
FROM
pg_catalog.pg_tablespace ts;
{% endif %}

View File

@ -1,7 +1,7 @@
define([
'underscore', 'underscore.string', 'jquery', 'pgadmin.browser', 'backgrid',
'alertify', 'wcdocker', 'pgadmin.backgrid', 'pgadmin.alertifyjs', 'pgadmin.browser.messages',
], function(_, S, $, pgBrowser, Backgrid, Alertify) {
'alertify', 'sources/size_prettify'
], function(_, S, $, pgBrowser, Backgrid, Alertify, sizePrettify) {
if (pgBrowser.NodeStatistics)
return pgBrowser.NodeStatistics;
@ -12,6 +12,25 @@ define([
return pgBrowser.NodeStatistics;
}
var SizeFormatter = Backgrid.SizeFormatter = function () {};
_.extend(SizeFormatter.prototype, {
/**
Takes a raw value from a model and returns the human readable formatted
string for display.
@member Backgrid.SizeFormatter
@param {*} rawData
@param {Backbone.Model} model Used for more complicated formatting
@return {*}
*/
fromRaw: function (rawData, model) {
return sizePrettify(rawData);
},
toRaw: function (formattedData, model) {
return formattedData;
}
});
var PGBooleanCell = Backgrid.Extension.SwitchCell.extend({
defaults: _.extend({}, Backgrid.Extension.SwitchCell.prototype.defaults)
}),
@ -200,9 +219,9 @@ define([
if (res.data) {
var data = res.data;
if (node.hasCollectiveStatistics || data['rows'].length > 1) {
self.__createMultiLineStatistics.call(self, data);
self.__createMultiLineStatistics.call(self, data, node.statsPrettifyFields);
} else {
self.__createSingleLineStatistics.call(self, data);
self.__createSingleLineStatistics.call(self, data, node.statsPrettifyFields);
}
if (self.grid) {
@ -294,24 +313,29 @@ define([
}
},
__createMultiLineStatistics: function(data) {
__createMultiLineStatistics: function(data, prettifyFields) {
var rows = data['rows'],
columns = data['columns'];
this.columns = [];
for (var idx in columns) {
var c = columns[idx];
this.columns.push({
editable: false,
name: c['name'],
cell: typeCellMapper[c['type_code']] || 'string'
});
var rawColumn = columns[idx],
col = {
editable: false,
name: rawColumn['name'],
cell: typeCellMapper[rawColumn['type_code']] || 'string'
};
if (_.indexOf(prettifyFields, rawColumn['name']) != -1) {
col['formatter'] = SizeFormatter
}
this.columns.push(col);
}
this.collection.reset(rows);
},
__createSingleLineStatistics: function(data) {
__createSingleLineStatistics: function(data, prettifyFields) {
var row = data['rows'][0],
columns = data['columns']
res = [];
@ -322,7 +346,7 @@ define([
res.push({
'statistics': name,
// Check if row is undefined?
'value': row && row[name] ? row[name] : null
'value': row && row[name] ? ((_.indexOf(prettifyFields, name) != -1) ? sizePrettify(row[name]) : row[name]) : null
});
}

View File

@ -0,0 +1,24 @@
define([],
function () {
var sizePrettify = function (rawSize) {
var size = Math.abs(rawSize),
limit = 10 * 1024,
limit2 = limit - 1,
cnt = 0,
sizeUnits = ['bytes', 'kB', 'MB', 'GB', 'TB', 'PB'];
if (size < limit)
return size + ' ' + sizeUnits[cnt]; // return in bytes format
else
{
do {
size = size / 1024;
cnt += 1;
} while (size > limit2);
return Math.round(size) + ' ' + sizeUnits[cnt];
}
};
return sizePrettify;
});

View File

@ -0,0 +1,68 @@
//////////////////////////////////////////////////////////////////////////
//
// pgAdmin 4 - PostgreSQL Tools
//
// Copyright (C) 2013 - 2017, The pgAdmin Development Team
// This software is released under the PostgreSQL Licence
//
//////////////////////////////////////////////////////////////////////////
define(["sources/size_prettify"], function (sizePrettify) {
describe("sizePrettify", function () {
describe("when size is 0", function () {
it("returns 0 bytes", function () {
expect(sizePrettify(0)).toEqual("0 bytes");
});
});
describe("when size >= 10kB and size < 10 MB", function () {
it("returns size in kB", function () {
expect(sizePrettify(10240)).toEqual("10 kB");
});
it("returns size in kB", function () {
expect(sizePrettify(99999)).toEqual("98 kB");
});
});
describe("when size >= 10MB and size < 10 GB", function () {
it("returns size in MB", function () {
expect(sizePrettify(10485760)).toEqual("10 MB");
});
it("returns size in MB", function () {
expect(sizePrettify(44040192)).toEqual("42 MB");
});
});
describe("when size >= 10GB and size < 10 TB", function () {
it("returns size in GB", function () {
expect(sizePrettify(10737418240)).toEqual("10 GB");
});
it("returns size in GB", function () {
expect(sizePrettify(10736344498176)).toEqual("9999 GB");
});
});
describe("when size >= 10TB and size < 10 PB", function () {
it("returns size in TB", function () {
expect(sizePrettify(10995116277760)).toEqual("10 TB");
});
it("returns size in TB", function () {
expect(sizePrettify(29995116277760)).toEqual("27 TB");
});
});
describe("when size >= 10 PB", function () {
it("returns size in PB", function () {
expect(sizePrettify(11258999068426200)).toEqual("10 PB");
});
});
});
});