noImplicitAny: Sub 3000 errors (#17821)

* noImplicitAny Stackdriver

* Sub 3000 noImplicitAny

* Update error count limit

* Add DataQueryRequest type
This commit is contained in:
Tobias Skarhed
2019-07-01 11:11:57 +02:00
committed by Torkel Ödegaard
parent bd4a7ddf3a
commit 4e27ba9646
38 changed files with 388 additions and 302 deletions

View File

@@ -4,6 +4,7 @@ import {
createResetHandler,
PasswordFieldEnum,
} from '../../../features/datasources/utils/passwordHandlers';
import DatasourceSrv from 'app/features/plugins/datasource_srv';
export class PostgresConfigCtrl {
static templateUrl = 'partials/config.html';
@@ -15,7 +16,7 @@ export class PostgresConfigCtrl {
onPasswordChange: ReturnType<typeof createChangeHandler>;
/** @ngInject */
constructor($scope, datasourceSrv) {
constructor($scope: any, datasourceSrv: DatasourceSrv) {
this.datasourceSrv = datasourceSrv;
this.current.jsonData.sslmode = this.current.jsonData.sslmode || 'verify-full';
this.current.jsonData.postgresVersion = this.current.jsonData.postgresVersion || 903;
@@ -30,13 +31,13 @@ export class PostgresConfigCtrl {
return;
}
this.datasourceSrv.loadDatasource(this.current.name).then(ds => {
return ds.getVersion().then(version => {
this.datasourceSrv.loadDatasource(this.current.name).then((ds: any) => {
return ds.getVersion().then((version: any) => {
version = Number(version[0].text);
// timescaledb is only available for 9.6+
if (version >= 906) {
ds.getTimescaleDBVersion().then(version => {
ds.getTimescaleDBVersion().then((version: any) => {
if (version.length === 1) {
this.current.jsonData.timescaledb = true;
}

View File

@@ -1,6 +1,10 @@
import _ from 'lodash';
import ResponseParser from './response_parser';
import PostgresQuery from 'app/plugins/datasource/postgres/postgres_query';
import { IQService } from 'angular';
import { BackendSrv } from 'app/core/services/backend_srv';
import { TemplateSrv } from 'app/features/templating/template_srv';
import { TimeSrv } from 'app/features/dashboard/services/TimeSrv';
export class PostgresDatasource {
id: any;
@@ -11,7 +15,13 @@ export class PostgresDatasource {
interval: string;
/** @ngInject */
constructor(instanceSettings, private backendSrv, private $q, private templateSrv, private timeSrv) {
constructor(
instanceSettings: { name: any; id?: any; jsonData?: any },
private backendSrv: BackendSrv,
private $q: IQService,
private templateSrv: TemplateSrv,
private timeSrv: TimeSrv
) {
this.name = instanceSettings.name;
this.id = instanceSettings.id;
this.jsonData = instanceSettings.jsonData;
@@ -20,7 +30,7 @@ export class PostgresDatasource {
this.interval = (instanceSettings.jsonData || {}).timeInterval || '1m';
}
interpolateVariable = (value, variable) => {
interpolateVariable = (value: string, variable: { multi: any; includeAll: any }) => {
if (typeof value === 'string') {
if (variable.multi || variable.includeAll) {
return this.queryModel.quoteLiteral(value);
@@ -39,7 +49,7 @@ export class PostgresDatasource {
return quotedValues.join(',');
};
query(options) {
query(options: any) {
const queries = _.filter(options.targets, target => {
return target.hide !== true;
}).map(target => {
@@ -72,7 +82,7 @@ export class PostgresDatasource {
.then(this.responseParser.processQueryResult);
}
annotationQuery(options) {
annotationQuery(options: any) {
if (!options.annotation.rawQuery) {
return this.$q.reject({
message: 'Query missing in annotation definition',
@@ -96,10 +106,10 @@ export class PostgresDatasource {
queries: [query],
},
})
.then(data => this.responseParser.transformAnnotationResponse(options, data));
.then((data: any) => this.responseParser.transformAnnotationResponse(options, data));
}
metricFindQuery(query, optionalOptions) {
metricFindQuery(query: string, optionalOptions: { variable?: any }) {
let refId = 'tempvar';
if (optionalOptions && optionalOptions.variable && optionalOptions.variable.name) {
refId = optionalOptions.variable.name;
@@ -125,7 +135,7 @@ export class PostgresDatasource {
method: 'POST',
data: data,
})
.then(data => this.responseParser.parseMetricFindQueryResult(refId, data));
.then((data: any) => this.responseParser.parseMetricFindQueryResult(refId, data));
}
getVersion() {
@@ -138,10 +148,10 @@ export class PostgresDatasource {
testDatasource() {
return this.metricFindQuery('SELECT 1', {})
.then(res => {
.then((res: any) => {
return { status: 'success', message: 'Database Connection OK' };
})
.catch(err => {
.catch((err: any) => {
console.log(err);
if (err.data && err.data.message) {
return { status: 'error', message: err.data.message };

View File

@@ -1,5 +1,7 @@
import QueryModel from './postgres_query';
export class PostgresMetaQuery {
constructor(private target, private queryModel) {}
constructor(private target: { table: string; timeColumn: string }, private queryModel: QueryModel) {}
getOperators(datatype: string) {
switch (datatype) {
@@ -19,7 +21,7 @@ export class PostgresMetaQuery {
}
// quote identifier as literal to use in metadata queries
quoteIdentAsLiteral(value) {
quoteIdentAsLiteral(value: string) {
return this.queryModel.quoteLiteral(this.queryModel.unquoteIdentifier(value));
}

View File

@@ -1,4 +1,6 @@
import _ from 'lodash';
import { TemplateSrv } from 'app/features/templating/template_srv';
import { ScopedVars } from '@grafana/ui';
export default class PostgresQuery {
target: any;
@@ -6,7 +8,7 @@ export default class PostgresQuery {
scopedVars: any;
/** @ngInject */
constructor(target, templateSrv?, scopedVars?) {
constructor(target: any, templateSrv?: TemplateSrv, scopedVars?: ScopedVars) {
this.target = target;
this.templateSrv = templateSrv;
this.scopedVars = scopedVars;
@@ -35,7 +37,7 @@ export default class PostgresQuery {
}
// remove identifier quoting from identifier to use in metadata queries
unquoteIdentifier(value) {
unquoteIdentifier(value: string) {
if (value[0] === '"' && value[value.length - 1] === '"') {
return value.substring(1, value.length - 1).replace(/""/g, '"');
} else {
@@ -43,15 +45,15 @@ export default class PostgresQuery {
}
}
quoteIdentifier(value) {
quoteIdentifier(value: any) {
return '"' + String(value).replace(/"/g, '""') + '"';
}
quoteLiteral(value) {
quoteLiteral(value: any) {
return "'" + String(value).replace(/'/g, "''") + "'";
}
escapeLiteral(value) {
escapeLiteral(value: any) {
return String(value).replace(/'/g, "''");
}
@@ -63,7 +65,7 @@ export default class PostgresQuery {
return this.target.metricColumn !== 'none';
}
interpolateQueryStr(value, variable, defaultFormatFn) {
interpolateQueryStr(value: any, variable: { multi: any; includeAll: any }, defaultFormatFn: any) {
// if no multi or include all do not regexEscape
if (!variable.multi && !variable.includeAll) {
return this.escapeLiteral(value);
@@ -77,7 +79,7 @@ export default class PostgresQuery {
return escapedValues.join(',');
}
render(interpolate?) {
render(interpolate?: any) {
const target = this.target;
// new query with no table set yet
@@ -146,7 +148,7 @@ export default class PostgresQuery {
return query;
}
buildValueColumn(column) {
buildValueColumn(column: any) {
let query = '';
const columnName: any = _.find(column, (g: any) => g.type === 'column');

View File

@@ -5,6 +5,8 @@ import { QueryCtrl } from 'app/plugins/sdk';
import { SqlPart } from 'app/core/components/sql_part/sql_part';
import PostgresQuery from './postgres_query';
import sqlPart from './sql_part';
import { auto, IQService } from 'angular';
import { TemplateSrv } from 'app/features/templating/template_srv';
export interface QueryMeta {
sql: string;
@@ -40,7 +42,13 @@ export class PostgresQueryCtrl extends QueryCtrl {
groupAdd: any;
/** @ngInject */
constructor($scope, $injector, private templateSrv, private $q, private uiSegmentSrv) {
constructor(
$scope: any,
$injector: auto.IInjectorService,
private templateSrv: TemplateSrv,
private $q: IQService,
private uiSegmentSrv: any
) {
super($scope, $injector);
this.target = this.target;
this.queryModel = new PostgresQuery(this.target, templateSrv, this.panel.scopedVars);
@@ -57,7 +65,7 @@ export class PostgresQueryCtrl extends QueryCtrl {
this.target.rawQuery = true;
} else {
this.target.rawSql = defaultQuery;
this.datasource.metricFindQuery(this.metaBuilder.findMetricTable()).then(result => {
this.datasource.metricFindQuery(this.metaBuilder.findMetricTable()).then((result: any) => {
if (result.length > 0) {
this.target.table = result[0].text;
let segment = this.uiSegmentSrv.newSegment(this.target.table);
@@ -187,7 +195,7 @@ export class PostgresQueryCtrl extends QueryCtrl {
}
}
resetPlusButton(button) {
resetPlusButton(button: { html: any; value: any }) {
const plusButton = this.uiSegmentSrv.newPlusButton();
button.html = plusButton.html;
button.value = plusButton.value;
@@ -211,7 +219,7 @@ export class PostgresQueryCtrl extends QueryCtrl {
this.metricColumnSegment.value = segment.value;
this.target.metricColumn = 'none';
const task1 = this.datasource.metricFindQuery(this.metaBuilder.buildColumnQuery('time')).then(result => {
const task1 = this.datasource.metricFindQuery(this.metaBuilder.buildColumnQuery('time')).then((result: any) => {
// check if time column is still valid
if (result.length > 0 && !_.find(result, (r: any) => r.text === this.target.timeColumn)) {
const segment = this.uiSegmentSrv.newSegment(result[0].text);
@@ -220,7 +228,7 @@ export class PostgresQueryCtrl extends QueryCtrl {
}
return this.timeColumnChanged(false);
});
const task2 = this.datasource.metricFindQuery(this.metaBuilder.buildColumnQuery('value')).then(result => {
const task2 = this.datasource.metricFindQuery(this.metaBuilder.buildColumnQuery('value')).then((result: any) => {
if (result.length > 0) {
this.target.select = [[{ type: 'column', params: [result[0].text] }]];
this.updateProjection();
@@ -241,31 +249,33 @@ export class PostgresQueryCtrl extends QueryCtrl {
timeColumnChanged(refresh?: boolean) {
this.target.timeColumn = this.timeColumnSegment.value;
return this.datasource.metricFindQuery(this.metaBuilder.buildDatatypeQuery(this.target.timeColumn)).then(result => {
if (result.length === 1) {
if (this.target.timeColumnType !== result[0].text) {
this.target.timeColumnType = result[0].text;
}
let partModel;
if (this.queryModel.hasUnixEpochTimecolumn()) {
partModel = sqlPart.create({ type: 'macro', name: '$__unixEpochFilter', params: [] });
} else {
partModel = sqlPart.create({ type: 'macro', name: '$__timeFilter', params: [] });
return this.datasource
.metricFindQuery(this.metaBuilder.buildDatatypeQuery(this.target.timeColumn))
.then((result: any) => {
if (result.length === 1) {
if (this.target.timeColumnType !== result[0].text) {
this.target.timeColumnType = result[0].text;
}
let partModel;
if (this.queryModel.hasUnixEpochTimecolumn()) {
partModel = sqlPart.create({ type: 'macro', name: '$__unixEpochFilter', params: [] });
} else {
partModel = sqlPart.create({ type: 'macro', name: '$__timeFilter', params: [] });
}
if (this.whereParts.length >= 1 && this.whereParts[0].def.type === 'macro') {
// replace current macro
this.whereParts[0] = partModel;
} else {
this.whereParts.splice(0, 0, partModel);
}
}
if (this.whereParts.length >= 1 && this.whereParts[0].def.type === 'macro') {
// replace current macro
this.whereParts[0] = partModel;
} else {
this.whereParts.splice(0, 0, partModel);
this.updatePersistedParts();
if (refresh !== false) {
this.panelCtrl.refresh();
}
}
this.updatePersistedParts();
if (refresh !== false) {
this.panelCtrl.refresh();
}
});
});
}
getMetricColumnSegments() {
@@ -280,7 +290,7 @@ export class PostgresQueryCtrl extends QueryCtrl {
this.panelCtrl.refresh();
}
onDataReceived(dataList) {
onDataReceived(dataList: any) {
this.lastQueryMeta = null;
this.lastQueryError = null;
@@ -290,7 +300,7 @@ export class PostgresQueryCtrl extends QueryCtrl {
}
}
onDataError(err) {
onDataError(err: any) {
if (err.data && err.data.results) {
const queryRes = err.data.results[this.target.refId];
if (queryRes) {
@@ -300,8 +310,8 @@ export class PostgresQueryCtrl extends QueryCtrl {
}
}
transformToSegments(config) {
return results => {
transformToSegments(config: { addNone?: any; addTemplateVars?: any; templateQuoter?: any }) {
return (results: any) => {
const segments = _.map(results, segment => {
return this.uiSegmentSrv.newSegment({
value: segment.text,
@@ -335,15 +345,15 @@ export class PostgresQueryCtrl extends QueryCtrl {
};
}
findAggregateIndex(selectParts) {
findAggregateIndex(selectParts: any) {
return _.findIndex(selectParts, (p: any) => p.def.type === 'aggregate' || p.def.type === 'percentile');
}
findWindowIndex(selectParts) {
findWindowIndex(selectParts: any) {
return _.findIndex(selectParts, (p: any) => p.def.type === 'window' || p.def.type === 'moving_window');
}
addSelectPart(selectParts, item, subItem) {
addSelectPart(selectParts: any[], item: { value: any }, subItem: { type: any; value: any }) {
let partType = item.value;
if (subItem && subItem.type) {
partType = subItem.type;
@@ -415,7 +425,7 @@ export class PostgresQueryCtrl extends QueryCtrl {
this.panelCtrl.refresh();
}
removeSelectPart(selectParts, part) {
removeSelectPart(selectParts: any, part: { def: { type: string } }) {
if (part.def.type === 'column') {
// remove all parts of column unless its last column
if (this.selectParts.length > 1) {
@@ -430,7 +440,7 @@ export class PostgresQueryCtrl extends QueryCtrl {
this.updatePersistedParts();
}
handleSelectPartEvent(selectParts, part, evt) {
handleSelectPartEvent(selectParts: any, part: { def: any }, evt: { name: any }) {
switch (evt.name) {
case 'get-param-options': {
switch (part.def.type) {
@@ -462,7 +472,7 @@ export class PostgresQueryCtrl extends QueryCtrl {
}
}
handleGroupPartEvent(part, index, evt) {
handleGroupPartEvent(part: any, index: any, evt: { name: any }) {
switch (evt.name) {
case 'get-param-options': {
return this.datasource
@@ -486,7 +496,7 @@ export class PostgresQueryCtrl extends QueryCtrl {
}
}
addGroup(partType, value) {
addGroup(partType: string, value: string) {
let params = [value];
if (partType === 'time') {
params = ['$__interval', 'none'];
@@ -515,7 +525,7 @@ export class PostgresQueryCtrl extends QueryCtrl {
this.updatePersistedParts();
}
removeGroup(part, index) {
removeGroup(part: { def: { type: string } }, index: number) {
if (part.def.type === 'time') {
// remove aggregations
this.selectParts = _.map(this.selectParts, (s: any) => {
@@ -532,7 +542,7 @@ export class PostgresQueryCtrl extends QueryCtrl {
this.updatePersistedParts();
}
handleWherePartEvent(whereParts, part, evt, index) {
handleWherePartEvent(whereParts: any, part: any, evt: any, index: any) {
switch (evt.name) {
case 'get-param-options': {
switch (evt.param.name) {
@@ -598,7 +608,7 @@ export class PostgresQueryCtrl extends QueryCtrl {
return this.$q.when(options);
}
addWhereAction(part, index) {
addWhereAction(part: any, index: any) {
switch (this.whereAdd.type) {
case 'macro': {
const partModel = sqlPart.create({ type: 'macro', name: this.whereAdd.value, params: [] });
@@ -623,7 +633,7 @@ export class PostgresQueryCtrl extends QueryCtrl {
getGroupOptions() {
return this.datasource
.metricFindQuery(this.metaBuilder.buildColumnQuery('group'))
.then(tags => {
.then((tags: any) => {
const options = [];
if (!this.queryModel.hasTimeGroup()) {
options.push(this.uiSegmentSrv.newSegment({ type: 'time', value: 'time($__interval,none)' }));
@@ -647,7 +657,7 @@ export class PostgresQueryCtrl extends QueryCtrl {
this.panelCtrl.refresh();
}
handleQueryError(err) {
handleQueryError(err: any): any[] {
this.error = err.message || 'Failed to issue metric query';
return [];
}

View File

@@ -1,10 +1,11 @@
import _ from 'lodash';
import { IQService } from 'angular';
export default class ResponseParser {
constructor(private $q) {}
constructor(private $q: IQService) {}
processQueryResult(res) {
const data = [];
processQueryResult(res: any) {
const data: any[] = [];
if (!res.data.results) {
return { data: data };
@@ -37,7 +38,7 @@ export default class ResponseParser {
return { data: data };
}
parseMetricFindQueryResult(refId, results) {
parseMetricFindQueryResult(refId: string, results: any) {
if (!results || results.data.length === 0 || results.data.results[refId].meta.rowCount === 0) {
return [];
}
@@ -54,7 +55,7 @@ export default class ResponseParser {
return this.transformToSimpleList(rows);
}
transformToKeyValueList(rows, textColIndex, valueColIndex) {
transformToKeyValueList(rows: any, textColIndex: number, valueColIndex: number) {
const res = [];
for (let i = 0; i < rows.length; i++) {
@@ -69,7 +70,7 @@ export default class ResponseParser {
return res;
}
transformToSimpleList(rows) {
transformToSimpleList(rows: any[][]) {
const res = [];
for (let i = 0; i < rows.length; i++) {
@@ -86,7 +87,7 @@ export default class ResponseParser {
});
}
findColIndex(columns, colName) {
findColIndex(columns: any[], colName: string) {
for (let i = 0; i < columns.length; i++) {
if (columns[i].text === colName) {
return i;
@@ -96,7 +97,7 @@ export default class ResponseParser {
return -1;
}
containsKey(res, key) {
containsKey(res: any, key: any) {
for (let i = 0; i < res.length; i++) {
if (res[i].text === key) {
return true;
@@ -105,7 +106,7 @@ export default class ResponseParser {
return false;
}
transformAnnotationResponse(options, data) {
transformAnnotationResponse(options: any, data: any) {
const table = data.data.results[options.annotation.name].tables[0];
let timeColumnIndex = -1;

View File

@@ -1,12 +1,14 @@
import { PostgresDatasource } from '../datasource';
import { CustomVariable } from 'app/features/templating/custom_variable';
import { toUtc, dateTime } from '@grafana/ui/src/utils/moment_wrapper';
import { BackendSrv } from 'app/core/services/backend_srv';
import { IQService } from 'angular';
describe('PostgreSQLDatasource', () => {
const instanceSettings = { name: 'postgresql' };
const backendSrv = {};
const templateSrv = {
const templateSrv: any = {
replace: jest.fn(text => text),
};
const raw = {
@@ -25,11 +27,17 @@ describe('PostgreSQLDatasource', () => {
} as any;
beforeEach(() => {
ctx.ds = new PostgresDatasource(instanceSettings, backendSrv, {}, templateSrv, ctx.timeSrvMock);
ctx.ds = new PostgresDatasource(
instanceSettings,
backendSrv as BackendSrv,
{} as IQService,
templateSrv,
ctx.timeSrvMock
);
});
describe('When performing annotationQuery', () => {
let results;
let results: any;
const annotationName = 'MyAnno';
@@ -66,7 +74,7 @@ describe('PostgreSQLDatasource', () => {
ctx.backendSrv.datasourceRequest = jest.fn(options => {
return Promise.resolve({ data: response, status: 200 });
});
ctx.ds.annotationQuery(options).then(data => {
ctx.ds.annotationQuery(options).then((data: any) => {
results = data;
});
});
@@ -86,7 +94,7 @@ describe('PostgreSQLDatasource', () => {
});
describe('When performing metricFindQuery', () => {
let results;
let results: any;
const query = 'select * from atable';
const response = {
results: {
@@ -109,7 +117,7 @@ describe('PostgreSQLDatasource', () => {
ctx.backendSrv.datasourceRequest = jest.fn(options => {
return Promise.resolve({ data: response, status: 200 });
});
ctx.ds.metricFindQuery(query).then(data => {
ctx.ds.metricFindQuery(query).then((data: any) => {
results = data;
});
});
@@ -122,7 +130,7 @@ describe('PostgreSQLDatasource', () => {
});
describe('When performing metricFindQuery with key, value columns', () => {
let results;
let results: any;
const query = 'select * from atable';
const response = {
results: {
@@ -145,7 +153,7 @@ describe('PostgreSQLDatasource', () => {
ctx.backendSrv.datasourceRequest = jest.fn(options => {
return Promise.resolve({ data: response, status: 200 });
});
ctx.ds.metricFindQuery(query).then(data => {
ctx.ds.metricFindQuery(query).then((data: any) => {
results = data;
});
});
@@ -160,7 +168,7 @@ describe('PostgreSQLDatasource', () => {
});
describe('When performing metricFindQuery with key, value columns and with duplicate keys', () => {
let results;
let results: any;
const query = 'select * from atable';
const response = {
results: {
@@ -183,7 +191,7 @@ describe('PostgreSQLDatasource', () => {
ctx.backendSrv.datasourceRequest = jest.fn(options => {
return Promise.resolve({ data: response, status: 200 });
});
ctx.ds.metricFindQuery(query).then(data => {
ctx.ds.metricFindQuery(query).then((data: any) => {
results = data;
});
//ctx.$rootScope.$apply();

View File

@@ -1,7 +1,9 @@
import PostgresQuery from '../postgres_query';
import { TemplateSrv } from 'app/features/templating/template_srv';
describe('PostgresQuery', () => {
const templateSrv = {
// @ts-ignore
const templateSrv: TemplateSrv = {
replace: jest.fn(text => text),
};
@@ -142,7 +144,7 @@ describe('PostgresQuery', () => {
});
describe('When generating complete statement', () => {
const target = {
const target: any = {
timeColumn: 't',
table: 'table',
select: [[{ type: 'column', params: ['value'] }]],

View File

@@ -1,8 +1,8 @@
import { SqlPartDef, SqlPart } from 'app/core/components/sql_part/sql_part';
const index = [];
const index: any[] = [];
function createPart(part): any {
function createPart(part: any): any {
const def = index[part.type];
if (!def) {
return null;