queryUtils.ts 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import { cloneDeep } from 'lodash';
  2. import InfluxQueryModel from './influx_query_model';
  3. import { InfluxQuery } from './types';
  4. // FIXME: these functions are a beginning of a refactoring of influx_query_model.ts
  5. // into a simpler approach with full typescript types.
  6. // later we should be able to migrate the unit-tests
  7. // that relate to these functions here, and then perhaps even move the implementation
  8. // to this place
  9. export function buildRawQuery(query: InfluxQuery): string {
  10. const queryCopy = cloneDeep(query); // the query-model mutates the query
  11. const model = new InfluxQueryModel(queryCopy);
  12. return model.render(false);
  13. }
  14. export function normalizeQuery(query: InfluxQuery): InfluxQuery {
  15. // we return the original query if there is no need to update it
  16. if (
  17. query.policy !== undefined &&
  18. query.resultFormat !== undefined &&
  19. query.orderByTime !== undefined &&
  20. query.tags !== undefined &&
  21. query.groupBy !== undefined &&
  22. query.select !== undefined
  23. ) {
  24. return query;
  25. }
  26. // FIXME: we should move the whole normalizeQuery logic here,
  27. // and then have influxQueryModel call this function,
  28. // to concentrate the whole logic here
  29. const queryCopy = cloneDeep(query); // the query-model mutates the query
  30. return new InfluxQueryModel(queryCopy).target;
  31. }
  32. export function addNewSelectPart(query: InfluxQuery, type: string, index: number): InfluxQuery {
  33. const queryCopy = cloneDeep(query); // the query-model mutates the query
  34. const model = new InfluxQueryModel(queryCopy);
  35. model.addSelectPart(model.selectModels[index], type);
  36. return model.target;
  37. }
  38. export function removeSelectPart(query: InfluxQuery, partIndex: number, index: number): InfluxQuery {
  39. const queryCopy = cloneDeep(query); // the query-model mutates the query
  40. const model = new InfluxQueryModel(queryCopy);
  41. const selectModel = model.selectModels[index];
  42. model.removeSelectPart(selectModel, selectModel[partIndex]);
  43. return model.target;
  44. }
  45. export function changeSelectPart(
  46. query: InfluxQuery,
  47. listIndex: number,
  48. partIndex: number,
  49. newParams: string[]
  50. ): InfluxQuery {
  51. // we need to make shallow copy of `query.select` down to `query.select[listIndex][partIndex]`
  52. const newSel = [...(query.select ?? [])];
  53. newSel[listIndex] = [...newSel[listIndex]];
  54. newSel[listIndex][partIndex] = {
  55. ...newSel[listIndex][partIndex],
  56. params: newParams,
  57. };
  58. return { ...query, select: newSel };
  59. }
  60. export function addNewGroupByPart(query: InfluxQuery, type: string): InfluxQuery {
  61. const queryCopy = cloneDeep(query); // the query-model mutates the query
  62. const model = new InfluxQueryModel(queryCopy);
  63. model.addGroupBy(type);
  64. return model.target;
  65. }
  66. export function removeGroupByPart(query: InfluxQuery, partIndex: number): InfluxQuery {
  67. const queryCopy = cloneDeep(query); // the query-model mutates the query
  68. const model = new InfluxQueryModel(queryCopy);
  69. model.removeGroupByPart(model.groupByParts[partIndex], partIndex);
  70. return model.target;
  71. }
  72. export function changeGroupByPart(query: InfluxQuery, partIndex: number, newParams: string[]): InfluxQuery {
  73. // we need to make shallow copy of `query.groupBy` down to `query.groupBy[partIndex]`
  74. const newGroupBy = [...(query.groupBy ?? [])];
  75. newGroupBy[partIndex] = {
  76. ...newGroupBy[partIndex],
  77. params: newParams,
  78. };
  79. return { ...query, groupBy: newGroupBy };
  80. }