set_profiling_level.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. 'use strict';
  2. const CommandOperation = require('./command');
  3. const levelValues = new Set(['off', 'slow_only', 'all']);
  4. class SetProfilingLevelOperation extends CommandOperation {
  5. constructor(db, level, options) {
  6. let profile = 0;
  7. if (level === 'off') {
  8. profile = 0;
  9. } else if (level === 'slow_only') {
  10. profile = 1;
  11. } else if (level === 'all') {
  12. profile = 2;
  13. }
  14. super(db, options);
  15. this.level = level;
  16. this.profile = profile;
  17. }
  18. _buildCommand() {
  19. const profile = this.profile;
  20. // Set up the profile number
  21. const command = { profile };
  22. return command;
  23. }
  24. execute(callback) {
  25. const level = this.level;
  26. if (!levelValues.has(level)) {
  27. return callback(new Error('Error: illegal profiling level value ' + level));
  28. }
  29. super.execute((err, doc) => {
  30. if (err == null && doc.ok === 1) return callback(null, level);
  31. return err != null
  32. ? callback(err, null)
  33. : callback(new Error('Error with profile command'), null);
  34. });
  35. }
  36. }
  37. module.exports = SetProfilingLevelOperation;