From f5793e9a7ac530d074c77b995414acb6ad6136c9 Mon Sep 17 00:00:00 2001 From: Aditya Toshniwal Date: Tue, 9 Aug 2022 15:42:16 +0530 Subject: [PATCH] Fixed SonarQube issues. --- web/pgadmin/about/static/js/about.js | 8 +-- .../misc/statistics/static/js/Statistics.jsx | 30 ++------ web/pgadmin/static/js/clipboard.js | 4 +- web/pgadmin/static/js/size_prettify.js | 31 --------- web/pgadmin/static/js/utils.js | 27 ++++++++ .../static/js/import_export_servers.js | 5 -- .../javascript/size_prettify_spec.js | 68 ------------------- 7 files changed, 35 insertions(+), 138 deletions(-) delete mode 100644 web/pgadmin/static/js/size_prettify.js delete mode 100644 web/regression/javascript/size_prettify_spec.js diff --git a/web/pgadmin/about/static/js/about.js b/web/pgadmin/about/static/js/about.js index 0fc6814e9..4fb34b93e 100644 --- a/web/pgadmin/about/static/js/about.js +++ b/web/pgadmin/about/static/js/about.js @@ -11,7 +11,6 @@ import React from 'react'; import gettext from 'sources/gettext'; import Notify from '../../../static/js/helpers/Notifier'; import pgAdmin from 'sources/pgadmin'; -import pgBrowser from 'top/browser/static/js/browser'; import AboutComponent from './AboutComponent'; import current_user from 'pgadmin.user_management.current_user'; @@ -25,11 +24,6 @@ class About { return About.instance; } - constructor(pgAdmin, pgBrowser) { - this.pgAdmin = pgAdmin; - this.pgBrowser = pgBrowser; - } - init() { if (this.initialized) return; @@ -55,7 +49,7 @@ class About { } } -pgAdmin.About = About.getInstance(pgAdmin, pgBrowser); +pgAdmin.About = About.getInstance(); module.exports = { About: About, diff --git a/web/pgadmin/misc/statistics/static/js/Statistics.jsx b/web/pgadmin/misc/statistics/static/js/Statistics.jsx index 7ba600235..4da011dda 100644 --- a/web/pgadmin/misc/statistics/static/js/Statistics.jsx +++ b/web/pgadmin/misc/statistics/static/js/Statistics.jsx @@ -15,10 +15,11 @@ import PropTypes from 'prop-types'; import Notify from '../../../../static/js/helpers/Notifier'; import getApiInstance from 'sources/api_instance'; import { makeStyles } from '@material-ui/core/styles'; -import sizePrettify from 'sources/size_prettify'; import { getURL } from '../../../static/utils/utils'; import Loader from 'sources/components/Loader'; import EmptyPanelMessage from '../../../../static/js/components/EmptyPanelMessage'; +import { compareSizeVals, toPrettySize } from '../../../../static/js/utils'; + const useStyles = makeStyles((theme) => ({ emptyPanel: { minHeight: '100%', @@ -64,28 +65,7 @@ function getColumn(data, singleLineStatistics) { resizable: true, disableGlobalFilter: false, sortType: ((rowA, rowB, id) => { - let val1 = rowA.values[id]; - let val2 = rowB.values[id]; - const sizes = ['bytes', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']; - sizes.some((t, i) => { - if (!_.isNull(rowA.values[id]) && typeof (rowA.values[id]) == 'string' && rowA.values[id].indexOf(t) > -1) { - val1 = (parseInt(rowA.values[id]) * Math.pow(1024, i)); - } - - if (!_.isNull(rowB.values[id]) && typeof (rowB.values[id]) == 'string' && rowB.values[id].indexOf(t) > -1) { - val2 = parseInt(rowB.values[id]) * Math.pow(1024, i); - } - - }); - - if ((val1) > (val2) || _.isNull(val2)) { - return 1; - } - if ((val2) > (val1) || _.isNull(val1)) { - return -1; - } - return 0; - + return compareSizeVals(rowA.values[id], rowB.values[id]); }) }; }else{ @@ -133,7 +113,7 @@ function getTableData(res, node) { // Prettify the field values if (!_.isEmpty(node.statsPrettifyFields)) { node.statsPrettifyFields.forEach((field) => { - row[field] = sizePrettify(row[field]); + row[field] = toPrettySize(row[field]); }); } nodeStats.push({ ...row, icon: '' }); @@ -159,7 +139,7 @@ function createSingleLineStatistics(data, prettifyFields) { if (row && row[name]) { value = _.indexOf(prettifyFields, name) != -1 - ? sizePrettify(row[name]) + ? toPrettySize(row[name]) : row[name]; } else { value = null; diff --git a/web/pgadmin/static/js/clipboard.js b/web/pgadmin/static/js/clipboard.js index 747c4444e..379a10648 100644 --- a/web/pgadmin/static/js/clipboard.js +++ b/web/pgadmin/static/js/clipboard.js @@ -1,6 +1,6 @@ -export function copyToClipboard(text) { +export async function copyToClipboard(text) { try { - navigator.clipboard.writeText(text); + await navigator.clipboard.writeText(text); } catch { /* Suppress error */ console.error('Does not have clipboard acccess'); diff --git a/web/pgadmin/static/js/size_prettify.js b/web/pgadmin/static/js/size_prettify.js deleted file mode 100644 index b22417d2d..000000000 --- a/web/pgadmin/static/js/size_prettify.js +++ /dev/null @@ -1,31 +0,0 @@ -///////////////////////////////////////////////////////////// -// -// pgAdmin 4 - PostgreSQL Tools -// -// Copyright (C) 2013 - 2022, The pgAdmin Development Team -// This software is released under the PostgreSQL Licence -// -////////////////////////////////////////////////////////////// - -define([], - function () { - return 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]; - } - }; - }); diff --git a/web/pgadmin/static/js/utils.js b/web/pgadmin/static/js/utils.js index df764656f..49d797b6d 100644 --- a/web/pgadmin/static/js/utils.js +++ b/web/pgadmin/static/js/utils.js @@ -13,6 +13,7 @@ import gettext from 'sources/gettext'; import 'wcdocker'; import Notify from './helpers/Notifier'; import { hasTrojanSource } from 'anti-trojan-source'; +import convert from 'convert-units'; var wcDocker = window.wcDocker; @@ -494,3 +495,29 @@ export function downloadBlob(blob, fileName) { } document.body.removeChild(link); } + +export function toPrettySize(rawSize) { + try { + let conVal = convert(rawSize).from('B').toBest(); + conVal.val = Math.round(conVal.val * 100) / 100; + return `${conVal.val} ${conVal.unit}`; + } + catch { + return ''; + } +} + +export function compareSizeVals(val1, val2) { + const parseAndConvert = (val)=>{ + try { + let [size, unit] = val.split(' '); + return convert(size).from(unit.toUpperCase()).to('B'); + } catch { + return -1; + } + }; + val1 = parseAndConvert(val1); + val2 = parseAndConvert(val2); + if(val1 > val2) return 1; + return (val1 < val2 ? -1 : 0); +} diff --git a/web/pgadmin/tools/import_export_servers/static/js/import_export_servers.js b/web/pgadmin/tools/import_export_servers/static/js/import_export_servers.js index b367c593a..e137ae2a0 100644 --- a/web/pgadmin/tools/import_export_servers/static/js/import_export_servers.js +++ b/web/pgadmin/tools/import_export_servers/static/js/import_export_servers.js @@ -24,11 +24,6 @@ export default class ImportExportServersModule { return ImportExportServersModule.instance; } - constructor(pgAdmin, pgBrowser) { - this.pgAdmin = pgAdmin; - this.pgBrowser = pgBrowser; - } - init() { if (this.initialized) return; diff --git a/web/regression/javascript/size_prettify_spec.js b/web/regression/javascript/size_prettify_spec.js deleted file mode 100644 index 0ac9c7e97..000000000 --- a/web/regression/javascript/size_prettify_spec.js +++ /dev/null @@ -1,68 +0,0 @@ -////////////////////////////////////////////////////////////////////////// -// -// pgAdmin 4 - PostgreSQL Tools -// -// Copyright (C) 2013 - 2022, 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'); - }); - - }); - - }); -});