utils.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. import async from "async";
  2. import { useHasPermission, getUserPermissions } from "../hooks/hasPermission";
  3. // eslint-disable-next-line
  4. import moduleManager from "../../index";
  5. const UtilsModule = moduleManager.modules.utils;
  6. const WSModule = moduleManager.modules.ws;
  7. export default {
  8. getModules: useHasPermission("utils.getModules", function getModules(session, cb) {
  9. async.waterfall(
  10. [
  11. next => {
  12. next(null, UtilsModule.moduleManager.modules);
  13. },
  14. (modules, next) => {
  15. next(
  16. null,
  17. Object.keys(modules).map(moduleName => {
  18. const module = modules[moduleName];
  19. return {
  20. name: module.name,
  21. status: module.status,
  22. stage: module.stage,
  23. jobsInQueue: module.jobQueue.lengthQueue(),
  24. jobsInProgress: module.jobQueue.lengthRunning(),
  25. jobsPaused: module.jobQueue.lengthPaused(),
  26. concurrency: module.jobQueue.concurrency
  27. };
  28. })
  29. );
  30. }
  31. ],
  32. async (err, modules) => {
  33. if (err && err !== true) {
  34. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  35. this.log("ERROR", "GET_MODULES", `User ${session.userId} failed to get modules. '${err}'`);
  36. cb({ status: "error", message: err });
  37. } else {
  38. this.log("SUCCESS", "GET_MODULES", `User ${session.userId} has successfully got the modules info.`);
  39. cb({
  40. status: "success",
  41. message: "Successfully got modules.",
  42. data: { modules }
  43. });
  44. }
  45. }
  46. );
  47. }),
  48. getModule: useHasPermission("utils.getModules", function getModule(session, moduleName, cb) {
  49. async.waterfall(
  50. [
  51. next => {
  52. next(null, UtilsModule.moduleManager.modules[moduleName]);
  53. },
  54. ({ jobStatistics, jobQueue }, next) => {
  55. const taskMapFn = runningTask => ({
  56. name: runningTask.job.name,
  57. uniqueId: runningTask.job.uniqueId,
  58. status: runningTask.job.status,
  59. priority: runningTask.priority,
  60. parentUniqueId: runningTask.job.parentJob?.uniqueId ?? "N/A",
  61. parentName: runningTask.job.parentJob?.name ?? "N/A"
  62. });
  63. next(null, {
  64. jobStatistics,
  65. runningTasks: jobQueue.runningTasks.map(taskMapFn),
  66. pausedTasks: jobQueue.pausedTasks.map(taskMapFn),
  67. queuedTasks: jobQueue.queue.map(taskMapFn)
  68. });
  69. }
  70. ],
  71. async (err, data) => {
  72. if (err && err !== true) {
  73. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  74. this.log("ERROR", "GET_MODULE", `User ${session.userId} failed to get module. '${err}'`);
  75. cb({ status: "error", message: err });
  76. } else {
  77. this.log("SUCCESS", "GET_MODULE", `User ${session.userId} has successfully got the module info.`);
  78. cb({
  79. status: "success",
  80. message: "Successfully got module info.",
  81. data
  82. });
  83. }
  84. }
  85. );
  86. }),
  87. getRooms(session, cb) {
  88. WSModule.runJob("GET_ROOMS_FOR_SOCKET", { socketId: session.socketId })
  89. .then(response => {
  90. this.log("SUCCESS", "GET_ROOMS", `User ${session.userId} has successfully got the module info.`);
  91. cb({
  92. status: "success",
  93. message: "Successfully got rooms.",
  94. data: {
  95. rooms: response
  96. }
  97. });
  98. })
  99. .catch(async err => {
  100. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  101. this.log("ERROR", "GET_ROOMS", `Failed to get rooms. '${err}'`);
  102. cb({ status: "error", message: err });
  103. });
  104. },
  105. /**
  106. * Get permissions
  107. * @param {object} session - the session object automatically added by socket.io
  108. * @param {string} stationId - optional, the station id
  109. * @param {Function} cb - gets called with the result
  110. */
  111. async getPermissions(session, stationId, cb) {
  112. const callback = cb || stationId;
  113. async.waterfall(
  114. [
  115. next => {
  116. getUserPermissions(session.userId, cb ? stationId : null)
  117. .then(permissions => {
  118. next(null, permissions);
  119. })
  120. .catch(() => {
  121. next(null, {});
  122. });
  123. }
  124. ],
  125. async (err, permissions) => {
  126. if (err) {
  127. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  128. this.log("ERROR", "GET_PERMISSIONS", `Fetching permissions failed. "${err}"`);
  129. return callback({ status: "error", message: err });
  130. }
  131. this.log("SUCCESS", "GET_PERMISSIONS", "Fetching permissions was successful.");
  132. return callback({ status: "success", data: { permissions } });
  133. }
  134. );
  135. }
  136. };