attachmentSettings.js 15 KB

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