|
| 1 | +/*! |
| 2 | + * Copyright 2024 Google LLC. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import * as firestore from '@google-cloud/firestore'; |
| 18 | +import {google} from '../protos/firestore_v1_proto_api'; |
| 19 | +import {Serializer} from './serializer'; |
| 20 | +import IPlanSummary = google.firestore.v1.IPlanSummary; |
| 21 | +import IExecutionStats = google.firestore.v1.IExecutionStats; |
| 22 | +import IExplainMetrics = google.firestore.v1.IExplainMetrics; |
| 23 | + |
| 24 | +/** |
| 25 | + * PlanSummary contains information about the planning stage of a query. |
| 26 | + * |
| 27 | + * @class PlanSummary |
| 28 | + */ |
| 29 | +export class PlanSummary implements firestore.PlanSummary { |
| 30 | + /** |
| 31 | + * @private |
| 32 | + * @internal |
| 33 | + */ |
| 34 | + constructor(readonly indexesUsed: Record<string, unknown>[]) {} |
| 35 | + |
| 36 | + /** |
| 37 | + * @private |
| 38 | + * @internal |
| 39 | + */ |
| 40 | + static _fromProto( |
| 41 | + plan: IPlanSummary | null | undefined, |
| 42 | + serializer: Serializer |
| 43 | + ): PlanSummary { |
| 44 | + const indexes: Record<string, unknown>[] = []; |
| 45 | + if (plan && plan.indexesUsed) { |
| 46 | + for (const index of plan.indexesUsed) { |
| 47 | + indexes.push(serializer.decodeGoogleProtobufStruct(index)); |
| 48 | + } |
| 49 | + } |
| 50 | + return new PlanSummary(indexes); |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +/** |
| 55 | + * ExecutionStats contains information about the execution of a query. |
| 56 | + * |
| 57 | + * @class ExecutionStats |
| 58 | + */ |
| 59 | +export class ExecutionStats implements firestore.ExecutionStats { |
| 60 | + /** |
| 61 | + * @private |
| 62 | + * @internal |
| 63 | + */ |
| 64 | + constructor( |
| 65 | + readonly resultsReturned: number, |
| 66 | + readonly executionDuration: firestore.Duration, |
| 67 | + readonly readOperations: number, |
| 68 | + readonly debugStats: Record<string, unknown> |
| 69 | + ) {} |
| 70 | + |
| 71 | + /** |
| 72 | + * @private |
| 73 | + * @internal |
| 74 | + */ |
| 75 | + static _fromProto( |
| 76 | + stats: IExecutionStats | null | undefined, |
| 77 | + serializer: Serializer |
| 78 | + ): ExecutionStats | null { |
| 79 | + if (stats) { |
| 80 | + return new ExecutionStats( |
| 81 | + Number(stats.resultsReturned), |
| 82 | + { |
| 83 | + seconds: Number(stats.executionDuration?.seconds), |
| 84 | + nanoseconds: Number(stats.executionDuration?.nanos), |
| 85 | + }, |
| 86 | + Number(stats.readOperations), |
| 87 | + serializer.decodeGoogleProtobufStruct(stats.debugStats) |
| 88 | + ); |
| 89 | + } |
| 90 | + return null; |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +/** |
| 95 | + * ExplainMetrics contains information about planning and execution of a query. |
| 96 | + * |
| 97 | + * @class ExplainMetrics |
| 98 | + */ |
| 99 | +export class ExplainMetrics implements firestore.ExplainMetrics { |
| 100 | + /** |
| 101 | + * @private |
| 102 | + * @internal |
| 103 | + */ |
| 104 | + constructor( |
| 105 | + readonly planSummary: PlanSummary, |
| 106 | + readonly executionStats: ExecutionStats | null |
| 107 | + ) {} |
| 108 | + |
| 109 | + /** |
| 110 | + * @private |
| 111 | + * @internal |
| 112 | + */ |
| 113 | + static _fromProto( |
| 114 | + metrics: IExplainMetrics, |
| 115 | + serializer: Serializer |
| 116 | + ): ExplainMetrics { |
| 117 | + return new ExplainMetrics( |
| 118 | + PlanSummary._fromProto(metrics.planSummary, serializer), |
| 119 | + ExecutionStats._fromProto(metrics.executionStats, serializer) |
| 120 | + ); |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +/** |
| 125 | + * ExplainResults contains information about planning, execution, and results |
| 126 | + * of a query. |
| 127 | + * |
| 128 | + * @class ExplainResults |
| 129 | + */ |
| 130 | +export class ExplainResults<T> implements firestore.ExplainResults<T> { |
| 131 | + /** |
| 132 | + * @private |
| 133 | + * @internal |
| 134 | + */ |
| 135 | + constructor( |
| 136 | + readonly metrics: ExplainMetrics, |
| 137 | + readonly snapshot: T | null |
| 138 | + ) {} |
| 139 | +} |
0 commit comments