set_profiling_level.ts 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import type { Db } from '../db';
  2. import { MongoInvalidArgumentError, MongoRuntimeError } from '../error';
  3. import type { Server } from '../sdam/server';
  4. import type { ClientSession } from '../sessions';
  5. import type { Callback } from '../utils';
  6. import { enumToString } from '../utils';
  7. import { CommandCallbackOperation, type CommandOperationOptions } from './command';
  8. const levelValues = new Set(['off', 'slow_only', 'all']);
  9. /** @public */
  10. export const ProfilingLevel = Object.freeze({
  11. off: 'off',
  12. slowOnly: 'slow_only',
  13. all: 'all'
  14. } as const);
  15. /** @public */
  16. export type ProfilingLevel = (typeof ProfilingLevel)[keyof typeof ProfilingLevel];
  17. /** @public */
  18. export type SetProfilingLevelOptions = CommandOperationOptions;
  19. /** @internal */
  20. export class SetProfilingLevelOperation extends CommandCallbackOperation<ProfilingLevel> {
  21. override options: SetProfilingLevelOptions;
  22. level: ProfilingLevel;
  23. profile: 0 | 1 | 2;
  24. constructor(db: Db, level: ProfilingLevel, options: SetProfilingLevelOptions) {
  25. super(db, options);
  26. this.options = options;
  27. switch (level) {
  28. case ProfilingLevel.off:
  29. this.profile = 0;
  30. break;
  31. case ProfilingLevel.slowOnly:
  32. this.profile = 1;
  33. break;
  34. case ProfilingLevel.all:
  35. this.profile = 2;
  36. break;
  37. default:
  38. this.profile = 0;
  39. break;
  40. }
  41. this.level = level;
  42. }
  43. override executeCallback(
  44. server: Server,
  45. session: ClientSession | undefined,
  46. callback: Callback<ProfilingLevel>
  47. ): void {
  48. const level = this.level;
  49. if (!levelValues.has(level)) {
  50. return callback(
  51. new MongoInvalidArgumentError(
  52. `Profiling level must be one of "${enumToString(ProfilingLevel)}"`
  53. )
  54. );
  55. }
  56. // TODO(NODE-3483): Determine error to put here
  57. super.executeCommandCallback(server, session, { profile: this.profile }, (err, doc) => {
  58. if (err == null && doc.ok === 1) return callback(undefined, level);
  59. return err != null
  60. ? callback(err)
  61. : callback(new MongoRuntimeError('Error with profile command'));
  62. });
  63. }
  64. }