config_ctrl.ts 2.2 KB

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