Skip to content

Commit 5fda017

Browse files
committed
Add all stages in autocomplete
1 parent 8912b2a commit 5fda017

File tree

4 files changed

+175
-82
lines changed

4 files changed

+175
-82
lines changed

src/aggregation.json

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
{
2+
"stages": [
3+
{
4+
"name": "$addFields",
5+
"description": "Adds new fields to documents. Similar to $project, $addFields reshapes each document in the stream; specifically, by adding new fields to output documents that contain both the existing fields from the input documents and the newly added fields. $set is an alias for $addFields."
6+
},
7+
{
8+
"name": "$bucket",
9+
"description": "Categorizes incoming documents into groups, called buckets, based on a specified expression and bucket boundaries."
10+
},
11+
{
12+
"name": "$bucketAuto",
13+
"description": "Categorizes incoming documents into a specific number of groups, called buckets, based on a specified expression. Bucket boundaries are automatically determined in an attempt to evenly distribute the documents into the specified number of buckets."
14+
},
15+
{
16+
"name": "$changeStream",
17+
"description": "Returns a Change Stream cursor for the collection. This stage can only occur once in an aggregation pipeline and it must occur as the first stage."
18+
},
19+
{
20+
"name": "$changeStreamSplitLargeEvent",
21+
"description": "Splits large change stream events that exceed 16 MB into smaller fragments returned in a change stream cursor. You can only use $changeStreamSplitLargeEvent in a $changeStream pipeline and it must be the final stage in the pipeline."
22+
},
23+
{ "name": "$collStats", "description": "Returns statistics regarding a collection or view." },
24+
{
25+
"name": "$count",
26+
"description": "Returns a count of the number of documents at this stage of the aggregation pipeline. Distinct from the $count aggregation accumulator."
27+
},
28+
{
29+
"name": "$densify",
30+
"description": "Creates new documents in a sequence of documents where certain values in a field are missing."
31+
},
32+
{ "name": "$documents", "description": "Returns literal documents from input expressions." },
33+
{
34+
"name": "$facet",
35+
"description": "Processes multiple aggregation pipelines within a single stage on the same set of input documents. Enables the creation of multi-faceted aggregations capable of characterizing data across multiple dimensions, or facets, in a single stage.",
36+
"insertText": "\"\\$facet\": {\n\t${1:outputFieldN}$0: [ ${2:stageN}, ${3:...} ]\n}"
37+
},
38+
{ "name": "$fill", "description": "Populates null and missing field values within documents." },
39+
{
40+
"name": "$geoNear",
41+
"description": "Returns an ordered stream of documents based on the proximity to a geospatial point. Incorporates the functionality of $match, $sort, and $limit for geospatial data. The output documents include an additional distance field and can include a location identifier field."
42+
},
43+
{
44+
"name": "$graphLookup",
45+
"description": "Performs a recursive search on a collection. To each output document, adds a new array field that contains the traversal results of the recursive search for that document."
46+
},
47+
{
48+
"name": "$group",
49+
"description": "Groups input documents by a specified identifier expression and applies the accumulator expression(s), if specified, to each group. Consumes all input documents and outputs one document per each distinct group. The output documents only contain the identifier field and, if specified, accumulated fields."
50+
},
51+
{ "name": "$indexStats", "description": "Returns statistics regarding the use of each index for the collection." },
52+
{
53+
"name": "$limit",
54+
"description": "Passes the first n documents unmodified to the pipeline where n is the specified limit. For each input document, outputs either one document (for the first n documents) or zero documents (after the first n documents)."
55+
},
56+
{
57+
"name": "$listSampledQueries",
58+
"description": "Lists sampled queries for all collections or a specific collection."
59+
},
60+
{
61+
"name": "$listSearchIndexes",
62+
"description": "Returns information about existing Atlas Search indexes on a specified collection."
63+
},
64+
{
65+
"name": "$listSessions",
66+
"description": "Lists all sessions that have been active long enough to propagate to the system.sessions collection."
67+
},
68+
{
69+
"name": "$lookup",
70+
"description": "Performs a left outer join to another collection in the same database to filter in documents from the \"joined\" collection for processing.",
71+
"insertText": "\"\\$lookup\": {\n\t\"from\": ${1:collection}$0,\n\t\"localField\": ${2:field},\n\t\"foreignField\": ${3:field},\n\t\"as\": ${4:result}\n}"
72+
},
73+
{
74+
"name": "$match",
75+
"description": "Filters the document stream to allow only matching documents to pass unmodified into the next pipeline stage. $match uses standard MongoDB queries. For each input document, outputs either one document (a match) or zero documents (no match)."
76+
},
77+
{
78+
"name": "$merge",
79+
"description": "Writes the resulting documents of the aggregation pipeline to a collection. The stage can incorporate (insert new documents, merge documents, replace documents, keep existing documents, fail the operation, process documents with a custom update pipeline) the results into an output collection. To use the $merge stage, it must be the last stage in the pipeline."
80+
},
81+
{
82+
"name": "$out",
83+
"description": "Writes the resulting documents of the aggregation pipeline to a collection. To use the $out stage, it must be the last stage in the pipeline."
84+
},
85+
{ "name": "$planCacheStats", "description": "Returns plan cache information for a collection." },
86+
{
87+
"name": "$project",
88+
"description": "Reshapes each document in the stream, such as by adding new fields or removing existing fields. For each input document, outputs one document. See also $unset for removing existing fields."
89+
},
90+
{
91+
"name": "$querySettings",
92+
"description": "Returns query settings previously added with setQuerySettings. New in version 8.0."
93+
},
94+
{
95+
"name": "$queryStats",
96+
"description": "Returns runtime statistics for recorded queries. WARNING: The $queryStats aggregation stage is unsupported and is not guaranteed to be stable in a future release. Don't build functionality that relies on a specific output format of this stage, since the output may change in a future release."
97+
},
98+
{
99+
"name": "$redact",
100+
"description": "Reshapes each document in the stream by restricting the content for each document based on information stored in the documents themselves. Incorporates the functionality of $project and $match. Can be used to implement field level redaction. For each input document, outputs either one or zero documents."
101+
},
102+
{
103+
"name": "$replaceRoot",
104+
"description": "Replaces a document with the specified embedded document. The operation replaces all existing fields in the input document, including the _id field. Specify a document embedded in the input document to promote the embedded document to the top level. $replaceWith is an alias for $replaceRoot stage."
105+
},
106+
{
107+
"name": "$replaceWith",
108+
"description": "Replaces a document with the specified embedded document. The operation replaces all existing fields in the input document, including the _id field. Specify a document embedded in the input document to promote the embedded document to the top level. $replaceWith is an alias for $replaceRoot stage."
109+
},
110+
{ "name": "$sample", "description": "Randomly selects the specified number of documents from its input." },
111+
{
112+
"name": "$search",
113+
"description": "Performs a full-text search of the field or fields in an Atlas collection. $search is only available for MongoDB Atlas clusters, and is not available for self-managed deployments. To learn more, see Atlas Search Aggregation Pipeline Stages."
114+
},
115+
{
116+
"name": "$searchMeta",
117+
"description": "Returns different types of metadata result documents for the Atlas Search query against an Atlas collection. $searchMeta is only available for MongoDB Atlas clusters, and is not available for self-managed deployments. To learn more, see Atlas Search Aggregation Pipeline Stages."
118+
},
119+
{
120+
"name": "$set",
121+
"description": "Adds new fields to documents. Similar to $project, $set reshapes each document in the stream; specifically, by adding new fields to output documents that contain both the existing fields from the input documents and the newly added fields. $set is an alias for $addFields stage."
122+
},
123+
{
124+
"name": "$setWindowFields",
125+
"description": "Groups documents into windows and applies one or more operators to the documents in each window. New in version 5.0."
126+
},
127+
{
128+
"name": "$skip",
129+
"description": "Skips the first n documents where n is the specified skip number and passes the remaining documents unmodified to the pipeline. For each input document, outputs either zero documents (for the first n documents) or one document (if after the first n documents)."
130+
},
131+
{
132+
"name": "$sort",
133+
"description": "Reorders the document stream by a specified sort key. Only the order changes; the documents remain unmodified. For each input document, outputs one document."
134+
},
135+
{
136+
"name": "$sortByCount",
137+
"description": "Groups incoming documents based on the value of a specified expression, then computes the count of documents in each distinct group."
138+
},
139+
{
140+
"name": "$unionWith",
141+
"description": "Performs a union of two collections; i.e. combines pipeline results from two collections into a single result set."
142+
},
143+
{
144+
"name": "$unset",
145+
"description": "Removes/excludes fields from documents. $unset is an alias for $project stage that removes fields."
146+
},
147+
{
148+
"name": "$unwind",
149+
"description": "Deconstructs an array field from the input documents to output a document for each element. Each output document replaces the array with an element value. For each input document, outputs n documents where n is the number of array elements and can be zero for an empty array."
150+
},
151+
{
152+
"name": "$vectorSearch",
153+
"description": "Performs an ANN or ENN search on a vector in the specified field of an Atlas collection. $vectorSearch is only available for MongoDB Atlas clusters running MongoDB v6.0.11 or higher, and is not available for self-managed deployments. New in version 7.0.2."
154+
}
155+
]
156+
}

src/autocomplete.tsx

Lines changed: 15 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,15 @@
11
import { useRef, useEffect } from 'react';
22
import { type Monaco, type monacoTypes } from '@grafana/ui';
33
import { languages } from 'monaco-editor';
4-
import { DataSourceApi } from '@grafana/data';
4+
import aggregationData from './aggregation.json';
55

66
// Supports JSON only right now
77
class CompletionProvider implements monacoTypes.languages.CompletionItemProvider {
8-
constructor(
9-
private readonly datasource: DataSourceApi,
10-
private readonly monaco: Monaco,
11-
private readonly editor: monacoTypes.editor.IStandaloneCodeEditor,
12-
) {}
8+
constructor(private readonly editor: monacoTypes.editor.IStandaloneCodeEditor) {}
139

1410
provideCompletionItems(
1511
model: monacoTypes.editor.ITextModel,
1612
position: monacoTypes.Position,
17-
context: monacoTypes.languages.CompletionContext,
18-
token: monacoTypes.CancellationToken,
1913
): monacoTypes.languages.ProviderResult<monacoTypes.languages.CompletionList> {
2014
if (this.editor.getModel()?.id !== model.id) {
2115
return { suggestions: [] };
@@ -46,81 +40,23 @@ class CompletionProvider implements monacoTypes.languages.CompletionItemProvider
4640
endColumn: word.endColumn,
4741
};
4842

43+
const suggestions: languages.CompletionItem[] = aggregationData['stages'].map((s) => ({
44+
label: `"${s['name']}"`,
45+
kind: languages.CompletionItemKind.Function,
46+
insertText: s['insertText'] ? s['insertText'] : `"\\${s['name']}": {\n\t$0\n}`,
47+
range: range,
48+
detail: 'stage',
49+
documentation: s['description'],
50+
insertTextRules: languages.CompletionItemInsertTextRule.InsertAsSnippet,
51+
}));
52+
4953
return {
50-
suggestions: [
51-
{
52-
label: '"$match"',
53-
kind: languages.CompletionItemKind.Function,
54-
insertText: '"\\$match": {\n\t${1:query}$0\n}',
55-
range: range,
56-
detail: 'stage',
57-
documentation: 'Filters documents based on a specified query predicate.',
58-
insertTextRules: languages.CompletionItemInsertTextRule.InsertAsSnippet,
59-
},
60-
{
61-
label: '"$project"',
62-
kind: languages.CompletionItemKind.Function,
63-
insertText: '"\\$project": {\n\t${1:specification(s)}$0\n}',
64-
range: range,
65-
detail: 'stage',
66-
documentation: 'Passes along the documents with the requested fields to the next stage in the pipeline.',
67-
insertTextRules: languages.CompletionItemInsertTextRule.InsertAsSnippet,
68-
},
69-
{
70-
label: '"$limit"',
71-
kind: languages.CompletionItemKind.Function,
72-
insertText: '"\\$limit": ${1:number}',
73-
range: range,
74-
detail: 'stage',
75-
insertTextRules: languages.CompletionItemInsertTextRule.InsertAsSnippet,
76-
},
77-
{
78-
label: '"$lookup"',
79-
kind: languages.CompletionItemKind.Function,
80-
insertText:
81-
'"\\$lookup": {\n\t"from": ${1:collection}$0,\n\t"localField": ${2:field},\n\t"foreignField": ${3:field},\n\t"as": ${4:result}\n}',
82-
range: range,
83-
detail: 'stage',
84-
insertTextRules: languages.CompletionItemInsertTextRule.InsertAsSnippet,
85-
},
86-
{
87-
label: '"$sort"',
88-
kind: languages.CompletionItemKind.Function,
89-
insertText: '"\\$sort": {\n\t${1:field1}$0: ${2:sortOrder}\n}',
90-
range: range,
91-
detail: 'stage',
92-
insertTextRules: languages.CompletionItemInsertTextRule.InsertAsSnippet,
93-
},
94-
{
95-
label: '"$facet"',
96-
kind: languages.CompletionItemKind.Function,
97-
insertText: '"\\$facet": {\n\t${1:outputFieldN}$0: [ ${2:stageN}, ${3:...} ]\n}',
98-
range: range,
99-
detail: 'stage',
100-
insertTextRules: languages.CompletionItemInsertTextRule.InsertAsSnippet,
101-
},
102-
{
103-
label: '"$addFields"',
104-
kind: languages.CompletionItemKind.Function,
105-
insertText: '"\\$addFields": {\n\t${1:newField}: ${2:expression}$0, ${3:...}\n}',
106-
range: range,
107-
detail: 'stage',
108-
insertTextRules: languages.CompletionItemInsertTextRule.InsertAsSnippet,
109-
},
110-
{
111-
label: '"$count"',
112-
kind: languages.CompletionItemKind.Function,
113-
insertText: '"\\$count": "${1:string}"',
114-
range: range,
115-
detail: 'stage',
116-
insertTextRules: languages.CompletionItemInsertTextRule.InsertAsSnippet,
117-
},
118-
],
54+
suggestions: suggestions,
11955
};
12056
}
12157
}
12258

123-
export function useAutocomplete(datasource: DataSourceApi) {
59+
export function useAutocomplete() {
12460
const autocompleteDisposeFun = useRef<(() => void) | null>(null);
12561
useEffect(() => {
12662
return () => {
@@ -129,7 +65,7 @@ export function useAutocomplete(datasource: DataSourceApi) {
12965
}, []);
13066

13167
return (editor: monacoTypes.editor.IStandaloneCodeEditor, monaco: Monaco) => {
132-
const provider = new CompletionProvider(datasource, monaco, editor);
68+
const provider = new CompletionProvider(editor);
13369
const { dispose } = monaco.languages.registerCompletionItemProvider('json', provider);
13470
autocompleteDisposeFun.current = dispose;
13571
};

src/components/QueryEditor.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,11 @@ const languageOptions: Array<SelectableValue<string>> = [
4343
{ label: 'JavaScript Shadow', value: QueryLanguage.JAVASCRIPT_SHADOW, description: 'JavaScript with Evaluation' },
4444
];
4545

46-
export function QueryEditor({ query, onChange, app, datasource }: Props) {
46+
export function QueryEditor({ query, onChange, app }: Props) {
4747
const codeEditorRef = useRef<monacoTypes.editor.IStandaloneCodeEditor | null>(null);
4848
const [queryTextError, setQueryTextError] = useState<string | null>(null);
4949
const [isOpen, setIsOpen] = useState(false);
50-
const setupAutocompleteFn = useAutocomplete(datasource);
50+
const setupAutocompleteFn = useAutocomplete();
5151

5252
const [maxTimeMSText, setMaxTimeMSText] = useState<string>(
5353
query.aggregateMaxTimeMS ? query.aggregateMaxTimeMS.toString() : '',

tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22
"compilerOptions": {
33
"jsx": "react"
44
},
5-
"extends": "./.config/tsconfig.json"
5+
"extends": "./.config/tsconfig.json",
6+
"resolveJsonModule": true
67
}

0 commit comments

Comments
 (0)