attachmentSettings.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. import { ReactiveCache } from '/imports/reactiveCache';
  2. import { TAPi18n } from '/imports/i18n';
  3. import { Meteor } from 'meteor/meteor';
  4. import { Session } from 'meteor/session';
  5. import { Tracker } from 'meteor/tracker';
  6. import { ReactiveVar } from 'meteor/reactive-var';
  7. import { BlazeComponent } from 'meteor/peerlibrary:blaze-components';
  8. import { Chart } from 'chart.js';
  9. // Global reactive variables for attachment settings
  10. const attachmentSettings = {
  11. loading: new ReactiveVar(false),
  12. showStorageSettings: new ReactiveVar(false),
  13. showMigration: new ReactiveVar(false),
  14. showMonitoring: new ReactiveVar(false),
  15. // Storage configuration
  16. filesystemPath: new ReactiveVar(''),
  17. attachmentsPath: new ReactiveVar(''),
  18. avatarsPath: new ReactiveVar(''),
  19. gridfsEnabled: new ReactiveVar(false),
  20. s3Enabled: new ReactiveVar(false),
  21. s3Endpoint: new ReactiveVar(''),
  22. s3Bucket: new ReactiveVar(''),
  23. s3Region: new ReactiveVar(''),
  24. s3SslEnabled: new ReactiveVar(false),
  25. s3Port: new ReactiveVar(443),
  26. // Migration settings
  27. migrationBatchSize: new ReactiveVar(10),
  28. migrationDelayMs: new ReactiveVar(1000),
  29. migrationCpuThreshold: new ReactiveVar(70),
  30. migrationProgress: new ReactiveVar(0),
  31. migrationStatus: new ReactiveVar('idle'),
  32. migrationLog: new ReactiveVar(''),
  33. // Monitoring data
  34. totalAttachments: new ReactiveVar(0),
  35. filesystemAttachments: new ReactiveVar(0),
  36. gridfsAttachments: new ReactiveVar(0),
  37. s3Attachments: new ReactiveVar(0),
  38. totalSize: new ReactiveVar(0),
  39. filesystemSize: new ReactiveVar(0),
  40. gridfsSize: new ReactiveVar(0),
  41. s3Size: new ReactiveVar(0),
  42. // Migration state
  43. isMigrationRunning: new ReactiveVar(false),
  44. isMigrationPaused: new ReactiveVar(false),
  45. migrationQueue: new ReactiveVar([]),
  46. currentMigration: new ReactiveVar(null)
  47. };
  48. // Main attachment settings component
  49. BlazeComponent.extendComponent({
  50. onCreated() {
  51. this.loading = attachmentSettings.loading;
  52. this.showStorageSettings = attachmentSettings.showStorageSettings;
  53. this.showMigration = attachmentSettings.showMigration;
  54. this.showMonitoring = attachmentSettings.showMonitoring;
  55. // Set default sub-menu state
  56. this.showStorageSettings.set(true);
  57. this.showMigration.set(false);
  58. this.showMonitoring.set(false);
  59. // Load initial data
  60. this.loadStorageConfiguration();
  61. this.loadMigrationSettings();
  62. this.loadMonitoringData();
  63. },
  64. events() {
  65. return [
  66. {
  67. 'click a.js-attachment-storage-settings': this.switchToStorageSettings,
  68. 'click a.js-attachment-migration': this.switchToMigration,
  69. 'click a.js-attachment-monitoring': this.switchToMonitoring,
  70. }
  71. ];
  72. },
  73. switchToStorageSettings(event) {
  74. this.switchMenu(event, 'storage-settings');
  75. this.showStorageSettings.set(true);
  76. this.showMigration.set(false);
  77. this.showMonitoring.set(false);
  78. },
  79. switchToMigration(event) {
  80. this.switchMenu(event, 'attachment-migration');
  81. this.showStorageSettings.set(false);
  82. this.showMigration.set(true);
  83. this.showMonitoring.set(false);
  84. },
  85. switchToMonitoring(event) {
  86. this.switchMenu(event, 'attachment-monitoring');
  87. this.showStorageSettings.set(false);
  88. this.showMigration.set(false);
  89. this.showMonitoring.set(true);
  90. },
  91. switchMenu(event, targetId) {
  92. const target = $(event.target);
  93. if (!target.hasClass('active')) {
  94. this.loading.set(true);
  95. $('.side-menu li.active').removeClass('active');
  96. target.parent().addClass('active');
  97. // Load data based on target
  98. if (targetId === 'storage-settings') {
  99. this.loadStorageConfiguration();
  100. } else if (targetId === 'attachment-migration') {
  101. this.loadMigrationSettings();
  102. } else if (targetId === 'attachment-monitoring') {
  103. this.loadMonitoringData();
  104. }
  105. this.loading.set(false);
  106. }
  107. },
  108. loadStorageConfiguration() {
  109. Meteor.call('getAttachmentStorageConfiguration', (error, result) => {
  110. if (!error && result) {
  111. attachmentSettings.filesystemPath.set(result.filesystemPath || '');
  112. attachmentSettings.attachmentsPath.set(result.attachmentsPath || '');
  113. attachmentSettings.avatarsPath.set(result.avatarsPath || '');
  114. attachmentSettings.gridfsEnabled.set(result.gridfsEnabled || false);
  115. attachmentSettings.s3Enabled.set(result.s3Enabled || false);
  116. attachmentSettings.s3Endpoint.set(result.s3Endpoint || '');
  117. attachmentSettings.s3Bucket.set(result.s3Bucket || '');
  118. attachmentSettings.s3Region.set(result.s3Region || '');
  119. attachmentSettings.s3SslEnabled.set(result.s3SslEnabled || false);
  120. attachmentSettings.s3Port.set(result.s3Port || 443);
  121. }
  122. });
  123. },
  124. loadMigrationSettings() {
  125. Meteor.call('getAttachmentMigrationSettings', (error, result) => {
  126. if (!error && result) {
  127. attachmentSettings.migrationBatchSize.set(result.batchSize || 10);
  128. attachmentSettings.migrationDelayMs.set(result.delayMs || 1000);
  129. attachmentSettings.migrationCpuThreshold.set(result.cpuThreshold || 70);
  130. attachmentSettings.migrationStatus.set(result.status || 'idle');
  131. attachmentSettings.migrationProgress.set(result.progress || 0);
  132. }
  133. });
  134. },
  135. loadMonitoringData() {
  136. Meteor.call('getAttachmentMonitoringData', (error, result) => {
  137. if (!error && result) {
  138. attachmentSettings.totalAttachments.set(result.totalAttachments || 0);
  139. attachmentSettings.filesystemAttachments.set(result.filesystemAttachments || 0);
  140. attachmentSettings.gridfsAttachments.set(result.gridfsAttachments || 0);
  141. attachmentSettings.s3Attachments.set(result.s3Attachments || 0);
  142. attachmentSettings.totalSize.set(result.totalSize || 0);
  143. attachmentSettings.filesystemSize.set(result.filesystemSize || 0);
  144. attachmentSettings.gridfsSize.set(result.gridfsSize || 0);
  145. attachmentSettings.s3Size.set(result.s3Size || 0);
  146. }
  147. });
  148. }
  149. }).register('attachmentSettings');
  150. // Storage settings component
  151. BlazeComponent.extendComponent({
  152. onCreated() {
  153. this.filesystemPath = attachmentSettings.filesystemPath;
  154. this.attachmentsPath = attachmentSettings.attachmentsPath;
  155. this.avatarsPath = attachmentSettings.avatarsPath;
  156. this.gridfsEnabled = attachmentSettings.gridfsEnabled;
  157. this.s3Enabled = attachmentSettings.s3Enabled;
  158. this.s3Endpoint = attachmentSettings.s3Endpoint;
  159. this.s3Bucket = attachmentSettings.s3Bucket;
  160. this.s3Region = attachmentSettings.s3Region;
  161. this.s3SslEnabled = attachmentSettings.s3SslEnabled;
  162. this.s3Port = attachmentSettings.s3Port;
  163. },
  164. events() {
  165. return [
  166. {
  167. 'click button.js-test-s3-connection': this.testS3Connection,
  168. 'click button.js-save-s3-settings': this.saveS3Settings,
  169. 'change input#s3-secret-key': this.updateS3SecretKey
  170. }
  171. ];
  172. },
  173. testS3Connection() {
  174. const secretKey = $('#s3-secret-key').val();
  175. if (!secretKey) {
  176. alert(TAPi18n.__('s3-secret-key-required'));
  177. return;
  178. }
  179. Meteor.call('testS3Connection', { secretKey }, (error, result) => {
  180. if (error) {
  181. alert(TAPi18n.__('s3-connection-failed') + ': ' + error.reason);
  182. } else {
  183. alert(TAPi18n.__('s3-connection-success'));
  184. }
  185. });
  186. },
  187. saveS3Settings() {
  188. const secretKey = $('#s3-secret-key').val();
  189. if (!secretKey) {
  190. alert(TAPi18n.__('s3-secret-key-required'));
  191. return;
  192. }
  193. Meteor.call('saveS3Settings', { secretKey }, (error, result) => {
  194. if (error) {
  195. alert(TAPi18n.__('s3-settings-save-failed') + ': ' + error.reason);
  196. } else {
  197. alert(TAPi18n.__('s3-settings-saved'));
  198. $('#s3-secret-key').val(''); // Clear the password field
  199. }
  200. });
  201. },
  202. updateS3SecretKey(event) {
  203. // This method can be used to validate the secret key format
  204. const secretKey = event.target.value;
  205. // Add validation logic here if needed
  206. }
  207. }).register('storageSettings');
  208. // Migration component
  209. BlazeComponent.extendComponent({
  210. onCreated() {
  211. this.migrationBatchSize = attachmentSettings.migrationBatchSize;
  212. this.migrationDelayMs = attachmentSettings.migrationDelayMs;
  213. this.migrationCpuThreshold = attachmentSettings.migrationCpuThreshold;
  214. this.migrationProgress = attachmentSettings.migrationProgress;
  215. this.migrationStatus = attachmentSettings.migrationStatus;
  216. this.migrationLog = attachmentSettings.migrationLog;
  217. this.isMigrationRunning = attachmentSettings.isMigrationRunning;
  218. this.isMigrationPaused = attachmentSettings.isMigrationPaused;
  219. // Subscribe to migration updates
  220. this.subscription = Meteor.subscribe('attachmentMigrationStatus');
  221. // Set up reactive updates
  222. this.autorun(() => {
  223. const status = attachmentSettings.migrationStatus.get();
  224. if (status === 'running') {
  225. this.isMigrationRunning.set(true);
  226. } else {
  227. this.isMigrationRunning.set(false);
  228. }
  229. });
  230. },
  231. onDestroyed() {
  232. if (this.subscription) {
  233. this.subscription.stop();
  234. }
  235. },
  236. events() {
  237. return [
  238. {
  239. 'click button.js-migrate-all-to-filesystem': () => this.startMigration('filesystem'),
  240. 'click button.js-migrate-all-to-gridfs': () => this.startMigration('gridfs'),
  241. 'click button.js-migrate-all-to-s3': () => this.startMigration('s3'),
  242. 'click button.js-pause-migration': this.pauseMigration,
  243. 'click button.js-resume-migration': this.resumeMigration,
  244. 'click button.js-stop-migration': this.stopMigration,
  245. 'change input#migration-batch-size': this.updateBatchSize,
  246. 'change input#migration-delay-ms': this.updateDelayMs,
  247. 'change input#migration-cpu-threshold': this.updateCpuThreshold
  248. }
  249. ];
  250. },
  251. startMigration(targetStorage) {
  252. const batchSize = parseInt($('#migration-batch-size').val()) || 10;
  253. const delayMs = parseInt($('#migration-delay-ms').val()) || 1000;
  254. const cpuThreshold = parseInt($('#migration-cpu-threshold').val()) || 70;
  255. Meteor.call('startAttachmentMigration', {
  256. targetStorage,
  257. batchSize,
  258. delayMs,
  259. cpuThreshold
  260. }, (error, result) => {
  261. if (error) {
  262. alert(TAPi18n.__('migration-start-failed') + ': ' + error.reason);
  263. } else {
  264. this.addToLog(TAPi18n.__('migration-started') + ': ' + targetStorage);
  265. }
  266. });
  267. },
  268. pauseMigration() {
  269. Meteor.call('pauseAttachmentMigration', (error, result) => {
  270. if (error) {
  271. alert(TAPi18n.__('migration-pause-failed') + ': ' + error.reason);
  272. } else {
  273. this.addToLog(TAPi18n.__('migration-paused'));
  274. }
  275. });
  276. },
  277. resumeMigration() {
  278. Meteor.call('resumeAttachmentMigration', (error, result) => {
  279. if (error) {
  280. alert(TAPi18n.__('migration-resume-failed') + ': ' + error.reason);
  281. } else {
  282. this.addToLog(TAPi18n.__('migration-resumed'));
  283. }
  284. });
  285. },
  286. stopMigration() {
  287. if (confirm(TAPi18n.__('migration-stop-confirm'))) {
  288. Meteor.call('stopAttachmentMigration', (error, result) => {
  289. if (error) {
  290. alert(TAPi18n.__('migration-stop-failed') + ': ' + error.reason);
  291. } else {
  292. this.addToLog(TAPi18n.__('migration-stopped'));
  293. }
  294. });
  295. }
  296. },
  297. updateBatchSize(event) {
  298. const value = parseInt(event.target.value);
  299. if (value >= 1 && value <= 100) {
  300. attachmentSettings.migrationBatchSize.set(value);
  301. }
  302. },
  303. updateDelayMs(event) {
  304. const value = parseInt(event.target.value);
  305. if (value >= 100 && value <= 10000) {
  306. attachmentSettings.migrationDelayMs.set(value);
  307. }
  308. },
  309. updateCpuThreshold(event) {
  310. const value = parseInt(event.target.value);
  311. if (value >= 10 && value <= 90) {
  312. attachmentSettings.migrationCpuThreshold.set(value);
  313. }
  314. },
  315. addToLog(message) {
  316. const timestamp = new Date().toISOString();
  317. const currentLog = attachmentSettings.migrationLog.get();
  318. const newLog = `[${timestamp}] ${message}\n${currentLog}`;
  319. attachmentSettings.migrationLog.set(newLog);
  320. }
  321. }).register('attachmentMigration');
  322. // Monitoring component
  323. BlazeComponent.extendComponent({
  324. onCreated() {
  325. this.totalAttachments = attachmentSettings.totalAttachments;
  326. this.filesystemAttachments = attachmentSettings.filesystemAttachments;
  327. this.gridfsAttachments = attachmentSettings.gridfsAttachments;
  328. this.s3Attachments = attachmentSettings.s3Attachments;
  329. this.totalSize = attachmentSettings.totalSize;
  330. this.filesystemSize = attachmentSettings.filesystemSize;
  331. this.gridfsSize = attachmentSettings.gridfsSize;
  332. this.s3Size = attachmentSettings.s3Size;
  333. // Subscribe to monitoring updates
  334. this.subscription = Meteor.subscribe('attachmentMonitoringData');
  335. // Set up chart
  336. this.autorun(() => {
  337. this.updateChart();
  338. });
  339. },
  340. onDestroyed() {
  341. if (this.subscription) {
  342. this.subscription.stop();
  343. }
  344. },
  345. events() {
  346. return [
  347. {
  348. 'click button.js-refresh-monitoring': this.refreshMonitoring,
  349. 'click button.js-export-monitoring': this.exportMonitoring
  350. }
  351. ];
  352. },
  353. refreshMonitoring() {
  354. Meteor.call('refreshAttachmentMonitoringData', (error, result) => {
  355. if (error) {
  356. alert(TAPi18n.__('monitoring-refresh-failed') + ': ' + error.reason);
  357. }
  358. });
  359. },
  360. exportMonitoring() {
  361. Meteor.call('exportAttachmentMonitoringData', (error, result) => {
  362. if (error) {
  363. alert(TAPi18n.__('monitoring-export-failed') + ': ' + error.reason);
  364. } else {
  365. // Download the exported data
  366. const blob = new Blob([JSON.stringify(result, null, 2)], { type: 'application/json' });
  367. const url = URL.createObjectURL(blob);
  368. const a = document.createElement('a');
  369. a.href = url;
  370. a.download = 'wekan-attachment-monitoring.json';
  371. document.body.appendChild(a);
  372. a.click();
  373. document.body.removeChild(a);
  374. URL.revokeObjectURL(url);
  375. }
  376. });
  377. },
  378. updateChart() {
  379. const ctx = document.getElementById('storage-distribution-chart');
  380. if (!ctx) return;
  381. const filesystemCount = this.filesystemAttachments.get();
  382. const gridfsCount = this.gridfsAttachments.get();
  383. const s3Count = this.s3Attachments.get();
  384. if (this.chart) {
  385. this.chart.destroy();
  386. }
  387. this.chart = new Chart(ctx, {
  388. type: 'doughnut',
  389. data: {
  390. labels: [
  391. TAPi18n.__('filesystem-storage'),
  392. TAPi18n.__('gridfs-storage'),
  393. TAPi18n.__('s3-storage')
  394. ],
  395. datasets: [{
  396. data: [filesystemCount, gridfsCount, s3Count],
  397. backgroundColor: [
  398. '#28a745',
  399. '#007bff',
  400. '#ffc107'
  401. ]
  402. }]
  403. },
  404. options: {
  405. responsive: true,
  406. maintainAspectRatio: false,
  407. plugins: {
  408. legend: {
  409. position: 'bottom'
  410. }
  411. }
  412. }
  413. });
  414. }
  415. }).register('attachmentMonitoring');
  416. // Export the attachment settings for use in other components
  417. export { attachmentSettings };