opusdev/vector-similarity-api
1
1import { type Document } from '../bson';2import { type Connection } from '../cmap/connection';3import { MongoDBResponse } from '../cmap/wire_protocol/responses';4import type { Db } from '../db';5import { MongoInvalidArgumentError } from '../error';6import { enumToString } from '../utils';7import { CommandOperation, type CommandOperationOptions } from './command';8 9const levelValues = new Set(['off', 'slow_only', 'all']);10 11/** @public */12export const ProfilingLevel = Object.freeze({13 off: 'off',14 slowOnly: 'slow_only',15 all: 'all'16} as const);17 18/** @public */19export type ProfilingLevel = (typeof ProfilingLevel)[keyof typeof ProfilingLevel];20 21/** @public */22export type SetProfilingLevelOptions = Omit<CommandOperationOptions, 'rawData'>;23 24/** @internal */25export class SetProfilingLevelOperation extends CommandOperation<ProfilingLevel> {26 override SERVER_COMMAND_RESPONSE_TYPE = MongoDBResponse;27 override options: SetProfilingLevelOptions;28 level: ProfilingLevel;29 profile: 0 | 1 | 2;30 31 constructor(db: Db, level: ProfilingLevel, options: SetProfilingLevelOptions) {32 super(db, options);33 this.options = options;34 switch (level) {35 case ProfilingLevel.off:36 this.profile = 0;37 break;38 case ProfilingLevel.slowOnly:39 this.profile = 1;40 break;41 case ProfilingLevel.all:42 this.profile = 2;43 break;44 default:45 this.profile = 0;46 break;47 }48 49 this.level = level;50 }51 52 override get commandName() {53 return 'profile' as const;54 }55 56 override buildCommandDocument(_connection: Connection): Document {57 const level = this.level;58 59 if (!levelValues.has(level)) {60 // TODO(NODE-3483): Determine error to put here61 throw new MongoInvalidArgumentError(62 `Profiling level must be one of "${enumToString(ProfilingLevel)}"`63 );64 }65 66 return { profile: this.profile };67 }68 69 override handleOk(70 _response: InstanceType<typeof this.SERVER_COMMAND_RESPONSE_TYPE>71 ): ProfilingLevel {72 return this.level;73 }74}75 