config_ctrl.ts 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import _ from 'lodash';
  2. import {
  3. createChangeHandler,
  4. createResetHandler,
  5. PasswordFieldEnum,
  6. } from '../../../features/datasources/utils/passwordHandlers';
  7. import DatasourceSrv from 'app/features/plugins/datasource_srv';
  8. export class PostgresConfigCtrl {
  9. static templateUrl = 'partials/config.html';
  10. current: any;
  11. datasourceSrv: any;
  12. showTimescaleDBHelp: boolean;
  13. onPasswordReset: ReturnType<typeof createResetHandler>;
  14. onPasswordChange: ReturnType<typeof createChangeHandler>;
  15. /** @ngInject */
  16. constructor($scope: any, datasourceSrv: DatasourceSrv) {
  17. this.datasourceSrv = datasourceSrv;
  18. this.current.jsonData.sslmode = this.current.jsonData.sslmode || 'verify-full';
  19. this.current.jsonData.postgresVersion = this.current.jsonData.postgresVersion || 903;
  20. this.showTimescaleDBHelp = false;
  21. this.autoDetectFeatures();
  22. this.onPasswordReset = createResetHandler(this, PasswordFieldEnum.Password);
  23. this.onPasswordChange = createChangeHandler(this, PasswordFieldEnum.Password);
  24. }
  25. autoDetectFeatures() {
  26. if (!this.current.id) {
  27. return;
  28. }
  29. this.datasourceSrv.loadDatasource(this.current.name).then((ds: any) => {
  30. return ds.getVersion().then((version: any) => {
  31. version = Number(version[0].text);
  32. // timescaledb is only available for 9.6+
  33. if (version >= 906) {
  34. ds.getTimescaleDBVersion().then((version: any) => {
  35. if (version.length === 1) {
  36. this.current.jsonData.timescaledb = true;
  37. }
  38. });
  39. }
  40. const major = Math.trunc(version / 100);
  41. const minor = version % 100;
  42. let name = String(major);
  43. if (version < 1000) {
  44. name = String(major) + '.' + String(minor);
  45. }
  46. if (!_.find(this.postgresVersions, (p: any) => p.value === version)) {
  47. this.postgresVersions.push({ name: name, value: version });
  48. }
  49. this.current.jsonData.postgresVersion = version;
  50. });
  51. });
  52. }
  53. toggleTimescaleDBHelp() {
  54. this.showTimescaleDBHelp = !this.showTimescaleDBHelp;
  55. }
  56. // the value portion is derived from postgres server_version_num/100
  57. postgresVersions = [
  58. { name: '9.3', value: 903 },
  59. { name: '9.4', value: 904 },
  60. { name: '9.5', value: 905 },
  61. { name: '9.6', value: 906 },
  62. { name: '10', value: 1000 },
  63. ];
  64. }