KustoQueryField.tsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. import _ from 'lodash';
  2. import Plain from 'slate-plain-serializer';
  3. import QueryField from './query_field';
  4. // import debounce from './utils/debounce';
  5. // import {getNextCharacter} from './utils/dom';
  6. import debounce from 'app/features/explore/utils/debounce';
  7. import { getNextCharacter } from 'app/features/explore/utils/dom';
  8. import { KEYWORDS, functionTokens, operatorTokens, grafanaMacros } from './kusto/kusto';
  9. // import '../sass/editor.base.scss';
  10. const TYPEAHEAD_DELAY = 100;
  11. interface Suggestion {
  12. text: string;
  13. deleteBackwards?: number;
  14. type?: string;
  15. }
  16. interface SuggestionGroup {
  17. label: string;
  18. items: Suggestion[];
  19. prefixMatch?: boolean;
  20. skipFilter?: boolean;
  21. }
  22. interface KustoSchema {
  23. Databases: {
  24. Default?: KustoDBSchema;
  25. };
  26. Plugins?: any[];
  27. }
  28. interface KustoDBSchema {
  29. Name?: string;
  30. Functions?: any;
  31. Tables?: any;
  32. }
  33. const defaultSchema = () => ({
  34. Databases: {
  35. Default: {}
  36. }
  37. });
  38. const cleanText = s => s.replace(/[{}[\]="(),!~+\-*/^%]/g, '').trim();
  39. const wrapText = text => ({ text });
  40. export default class KustoQueryField extends QueryField {
  41. fields: any;
  42. events: any;
  43. schema: KustoSchema;
  44. constructor(props, context) {
  45. super(props, context);
  46. this.schema = defaultSchema();
  47. this.onTypeahead = debounce(this.onTypeahead, TYPEAHEAD_DELAY);
  48. }
  49. componentDidMount() {
  50. super.componentDidMount();
  51. this.fetchSchema();
  52. }
  53. onTypeahead = () => {
  54. const selection = window.getSelection();
  55. if (selection.anchorNode) {
  56. const wrapperNode = selection.anchorNode.parentElement;
  57. if (wrapperNode === null) {
  58. return;
  59. }
  60. const editorNode = wrapperNode.closest('.slate-query-field');
  61. if (!editorNode || this.state.value.isBlurred) {
  62. // Not inside this editor
  63. return;
  64. }
  65. // DOM ranges
  66. const range = selection.getRangeAt(0);
  67. const text = selection.anchorNode.textContent;
  68. if (text === null) {
  69. return;
  70. }
  71. const offset = range.startOffset;
  72. let prefix = cleanText(text.substr(0, offset));
  73. // Model ranges
  74. const modelOffset = this.state.value.anchorOffset;
  75. const modelPrefix = this.state.value.anchorText.text.slice(0, modelOffset);
  76. // Determine candidates by context
  77. let suggestionGroups: SuggestionGroup[] = [];
  78. const wrapperClasses = wrapperNode.classList;
  79. let typeaheadContext: string | null = null;
  80. if (wrapperClasses.contains('function-context')) {
  81. typeaheadContext = 'context-function';
  82. if (this.fields) {
  83. suggestionGroups = this._getKeywordSuggestions();
  84. } else {
  85. this._fetchFields();
  86. return;
  87. }
  88. } else if (modelPrefix.match(/(where\s$)/i)) {
  89. typeaheadContext = 'context-where';
  90. const fullQuery = Plain.serialize(this.state.value);
  91. const table = this.getTableFromContext(fullQuery);
  92. if (table) {
  93. suggestionGroups = this.getWhereSuggestions(table);
  94. } else {
  95. return;
  96. }
  97. } else if (modelPrefix.match(/(,\s*$)/)) {
  98. typeaheadContext = 'context-multiple-fields';
  99. if (this.fields) {
  100. suggestionGroups = this._getKeywordSuggestions();
  101. } else {
  102. this._fetchFields();
  103. return;
  104. }
  105. } else if (modelPrefix.match(/(from\s$)/i)) {
  106. typeaheadContext = 'context-from';
  107. if (this.events) {
  108. suggestionGroups = this._getKeywordSuggestions();
  109. } else {
  110. this._fetchEvents();
  111. return;
  112. }
  113. } else if (modelPrefix.match(/(^select\s\w*$)/i)) {
  114. typeaheadContext = 'context-select';
  115. if (this.fields) {
  116. suggestionGroups = this._getKeywordSuggestions();
  117. } else {
  118. this._fetchFields();
  119. return;
  120. }
  121. } else if (modelPrefix.match(/from\s\S+\s\w*$/i)) {
  122. prefix = '';
  123. typeaheadContext = 'context-since';
  124. suggestionGroups = this._getKeywordSuggestions();
  125. // } else if (modelPrefix.match(/\d+\s\w*$/)) {
  126. // typeaheadContext = 'context-number';
  127. // suggestionGroups = this._getAfterNumberSuggestions();
  128. } else if (modelPrefix.match(/ago\b/i) || modelPrefix.match(/facet\b/i) || modelPrefix.match(/\$__timefilter\b/i)) {
  129. typeaheadContext = 'context-timeseries';
  130. suggestionGroups = this._getKeywordSuggestions();
  131. } else if (prefix && !wrapperClasses.contains('argument')) {
  132. typeaheadContext = 'context-builtin';
  133. suggestionGroups = this._getKeywordSuggestions();
  134. } else if (Plain.serialize(this.state.value) === '') {
  135. typeaheadContext = 'context-new';
  136. if (this.schema) {
  137. suggestionGroups = this._getInitialSuggestions();
  138. } else {
  139. this.fetchSchema();
  140. setTimeout(this.onTypeahead, 0);
  141. return;
  142. }
  143. }
  144. let results = 0;
  145. prefix = prefix.toLowerCase();
  146. const filteredSuggestions = suggestionGroups.map(group => {
  147. if (group.items && prefix && !group.skipFilter) {
  148. group.items = group.items.filter(c => c.text.length >= prefix.length);
  149. if (group.prefixMatch) {
  150. group.items = group.items.filter(c => c.text.toLowerCase().indexOf(prefix) === 0);
  151. } else {
  152. group.items = group.items.filter(c => c.text.toLowerCase().indexOf(prefix) > -1);
  153. }
  154. }
  155. results += group.items.length;
  156. return group;
  157. })
  158. .filter(group => group.items.length > 0);
  159. // console.log('onTypeahead', selection.anchorNode, wrapperClasses, text, offset, prefix, typeaheadContext);
  160. this.setState({
  161. typeaheadPrefix: prefix,
  162. typeaheadContext,
  163. typeaheadText: text,
  164. suggestions: results > 0 ? filteredSuggestions : [],
  165. });
  166. }
  167. }
  168. applyTypeahead(change, suggestion) {
  169. const { typeaheadPrefix, typeaheadContext, typeaheadText } = this.state;
  170. let suggestionText = suggestion.text || suggestion;
  171. const move = 0;
  172. // Modify suggestion based on context
  173. const nextChar = getNextCharacter();
  174. if (suggestion.type === 'function') {
  175. if (!nextChar || nextChar !== '(') {
  176. suggestionText += '(';
  177. }
  178. } else if (typeaheadContext === 'context-function') {
  179. if (!nextChar || nextChar !== ')') {
  180. suggestionText += ')';
  181. }
  182. } else {
  183. if (!nextChar || nextChar !== ' ') {
  184. suggestionText += ' ';
  185. }
  186. }
  187. this.resetTypeahead();
  188. // Remove the current, incomplete text and replace it with the selected suggestion
  189. const backward = suggestion.deleteBackwards || typeaheadPrefix.length;
  190. const text = cleanText(typeaheadText);
  191. const suffixLength = text.length - typeaheadPrefix.length;
  192. const offset = typeaheadText.indexOf(typeaheadPrefix);
  193. const midWord = typeaheadPrefix && ((suffixLength > 0 && offset > -1) || suggestionText === typeaheadText);
  194. const forward = midWord ? suffixLength + offset : 0;
  195. return change
  196. .deleteBackward(backward)
  197. .deleteForward(forward)
  198. .insertText(suggestionText)
  199. .move(move)
  200. .focus();
  201. }
  202. // private _getFieldsSuggestions(): SuggestionGroup[] {
  203. // return [
  204. // {
  205. // prefixMatch: true,
  206. // label: 'Fields',
  207. // items: this.fields.map(wrapText)
  208. // },
  209. // {
  210. // prefixMatch: true,
  211. // label: 'Variables',
  212. // items: this.props.templateVariables.map(wrapText)
  213. // }
  214. // ];
  215. // }
  216. // private _getAfterFromSuggestions(): SuggestionGroup[] {
  217. // return [
  218. // {
  219. // skipFilter: true,
  220. // label: 'Events',
  221. // items: this.events.map(wrapText)
  222. // },
  223. // {
  224. // prefixMatch: true,
  225. // label: 'Variables',
  226. // items: this.props.templateVariables
  227. // .map(wrapText)
  228. // .map(suggestion => {
  229. // suggestion.deleteBackwards = 0;
  230. // return suggestion;
  231. // })
  232. // }
  233. // ];
  234. // }
  235. // private _getAfterSelectSuggestions(): SuggestionGroup[] {
  236. // return [
  237. // {
  238. // prefixMatch: true,
  239. // label: 'Fields',
  240. // items: this.fields.map(wrapText)
  241. // },
  242. // {
  243. // prefixMatch: true,
  244. // label: 'Functions',
  245. // items: FUNCTIONS.map((s: any) => { s.type = 'function'; return s; })
  246. // },
  247. // {
  248. // prefixMatch: true,
  249. // label: 'Variables',
  250. // items: this.props.templateVariables.map(wrapText)
  251. // }
  252. // ];
  253. // }
  254. private _getKeywordSuggestions(): SuggestionGroup[] {
  255. return [
  256. {
  257. prefixMatch: true,
  258. label: 'Keywords',
  259. items: KEYWORDS.map(wrapText)
  260. },
  261. {
  262. prefixMatch: true,
  263. label: 'Operators',
  264. items: operatorTokens
  265. },
  266. {
  267. prefixMatch: true,
  268. label: 'Functions',
  269. items: functionTokens.map((s: any) => { s.type = 'function'; return s; })
  270. },
  271. {
  272. prefixMatch: true,
  273. label: 'Macros',
  274. items: grafanaMacros.map((s: any) => { s.type = 'function'; return s; })
  275. },
  276. {
  277. prefixMatch: true,
  278. label: 'Tables',
  279. items: _.map(this.schema.Databases.Default.Tables, (t: any) => ({ text: t.Name }))
  280. }
  281. ];
  282. }
  283. private _getInitialSuggestions(): SuggestionGroup[] {
  284. return [
  285. {
  286. prefixMatch: true,
  287. label: 'Tables',
  288. items: _.map(this.schema.Databases.Default.Tables, (t: any) => ({ text: t.Name }))
  289. }
  290. ];
  291. // return [
  292. // {
  293. // prefixMatch: true,
  294. // label: 'Keywords',
  295. // items: KEYWORDS.map(wrapText)
  296. // },
  297. // {
  298. // prefixMatch: true,
  299. // label: 'Operators',
  300. // items: operatorTokens.map((s: any) => { s.type = 'function'; return s; })
  301. // },
  302. // {
  303. // prefixMatch: true,
  304. // label: 'Functions',
  305. // items: functionTokens.map((s: any) => { s.type = 'function'; return s; })
  306. // },
  307. // {
  308. // prefixMatch: true,
  309. // label: 'Macros',
  310. // items: grafanaMacros.map((s: any) => { s.type = 'function'; return s; })
  311. // }
  312. // ];
  313. }
  314. private getWhereSuggestions(table: string): SuggestionGroup[] {
  315. const tableSchema = this.schema.Databases.Default.Tables[table];
  316. if (tableSchema) {
  317. return [
  318. {
  319. prefixMatch: true,
  320. label: 'Fields',
  321. items: _.map(tableSchema.OrderedColumns, (f: any) => ({
  322. text: f.Name,
  323. hint: f.Type
  324. }))
  325. }
  326. ];
  327. } else {
  328. return [];
  329. }
  330. }
  331. private getTableFromContext(query: string) {
  332. const tablePattern = /^\s*(\w+)\s*|/g;
  333. const normalizedQuery = normalizeQuery(query);
  334. const match = tablePattern.exec(normalizedQuery);
  335. if (match && match.length > 1 && match[0] && match[1]) {
  336. return match[1];
  337. } else {
  338. return null;
  339. }
  340. }
  341. private async _fetchEvents() {
  342. // const query = 'events';
  343. // const result = await this.request(query);
  344. // if (result === undefined) {
  345. // this.events = [];
  346. // } else {
  347. // this.events = result;
  348. // }
  349. // setTimeout(this.onTypeahead, 0);
  350. //Stub
  351. this.events = [];
  352. }
  353. private async _fetchFields() {
  354. // const query = 'fields';
  355. // const result = await this.request(query);
  356. // this.fields = result || [];
  357. // setTimeout(this.onTypeahead, 0);
  358. // Stub
  359. this.fields = [];
  360. }
  361. private async fetchSchema() {
  362. let schema = await this.props.getSchema();
  363. if (schema) {
  364. if (schema.Type === 'AppInsights') {
  365. schema = castSchema(schema);
  366. }
  367. this.schema = schema;
  368. } else {
  369. this.schema = defaultSchema();
  370. }
  371. }
  372. }
  373. /**
  374. * Cast schema from App Insights to default Kusto schema
  375. */
  376. function castSchema(schema) {
  377. const defaultSchemaTemplate = defaultSchema();
  378. defaultSchemaTemplate.Databases.Default = schema;
  379. return defaultSchemaTemplate;
  380. }
  381. function normalizeQuery(query: string): string {
  382. const commentPattern = /\/\/.*$/gm;
  383. let normalizedQuery = query.replace(commentPattern, '');
  384. normalizedQuery = normalizedQuery.replace('\n', ' ');
  385. return normalizedQuery;
  386. }