All files / src/operations set_profiling_level.ts

100% Statements 24/24
100% Branches 6/6
100% Functions 3/3
100% Lines 24/24

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73  135x       135x 135x   135x     135x                         135x           168x 168x 168x   53x 53x   9x 9x   97x 97x   9x 9x     168x       212x               124x   124x 9x           115x 115x      
import type { Db } from '../db';
import { MongoInvalidArgumentError } from '../error';
import type { Server } from '../sdam/server';
import type { ClientSession } from '../sessions';
import { type TimeoutContext } from '../timeout';
import { enumToString } from '../utils';
import { CommandOperation, type CommandOperationOptions } from './command';
 
const levelValues = new Set(['off', 'slow_only', 'all']);
 
/** @public */
export const ProfilingLevel = Object.freeze({
  off: 'off',
  slowOnly: 'slow_only',
  all: 'all'
} as const);
 
/** @public */
export type ProfilingLevel = (typeof ProfilingLevel)[keyof typeof ProfilingLevel];
 
/** @public */
export type SetProfilingLevelOptions = CommandOperationOptions;
 
/** @internal */
export class SetProfilingLevelOperation extends CommandOperation<ProfilingLevel> {
  override options: SetProfilingLevelOptions;
  level: ProfilingLevel;
  profile: 0 | 1 | 2;
 
  constructor(db: Db, level: ProfilingLevel, options: SetProfilingLevelOptions) {
    super(db, options);
    this.options = options;
    switch (level) {
      case ProfilingLevel.off:
        this.profile = 0;
        break;
      case ProfilingLevel.slowOnly:
        this.profile = 1;
        break;
      case ProfilingLevel.all:
        this.profile = 2;
        break;
      default:
        this.profile = 0;
        break;
    }
 
    this.level = level;
  }
 
  override get commandName() {
    return 'profile' as const;
  }
 
  override async execute(
    server: Server,
    session: ClientSession | undefined,
    timeoutContext: TimeoutContext
  ): Promise<ProfilingLevel> {
    const level = this.level;
 
    if (!levelValues.has(level)) {
      throw new MongoInvalidArgumentError(
        `Profiling level must be one of "${enumToString(ProfilingLevel)}"`
      );
    }
 
    // TODO(NODE-3483): Determine error to put here
    await super.executeCommand(server, session, { profile: this.profile }, timeoutContext);
    return level;
  }
}