vizPlugins.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. export type JsonPrimitive = string | number | boolean | null;
  20. export type JsonValue = JsonPrimitive | JsonObject | JsonArray;
  21. export type JsonArray = JsonValue[];
  22. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  23. export type JsonObject = { [member: string]: any };
  24. export interface Slice {
  25. slice_id: number;
  26. form_data: {
  27. viz_type: string;
  28. };
  29. }
  30. export interface Dashboard {
  31. slices: Slice[];
  32. }
  33. const V1_PLUGINS = [
  34. 'box_plot',
  35. 'echarts_timeseries',
  36. 'word_cloud',
  37. 'pie',
  38. 'table',
  39. 'big_number',
  40. 'big_number_total',
  41. ];
  42. export const DASHBOARD_CHART_ALIAS_PREFIX = 'getChartData_';
  43. export function isLegacyChart(vizType: string): boolean {
  44. return !V1_PLUGINS.includes(vizType);
  45. }
  46. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  47. export function isLegacyResponse(response: any): boolean {
  48. return !response.result;
  49. }
  50. export function getSliceIdFromRequestUrl(url: string) {
  51. const address = new URL(url);
  52. const query = address.searchParams.get('form_data');
  53. return query?.match(/\d+/)?.[0];
  54. }
  55. export function getChartDataRouteForSlice(slice: Slice) {
  56. const vizType = slice.form_data.viz_type;
  57. const isLegacy = isLegacyChart(vizType);
  58. const formData = encodeURIComponent(`{"slice_id":${slice.slice_id}}`);
  59. if (isLegacy) {
  60. return `**/superset/explore_json/?*${formData}*`;
  61. }
  62. return `**/api/v1/chart/data?*${formData}*`;
  63. }
  64. export function getChartAlias(slice: Slice): string {
  65. const alias = `${DASHBOARD_CHART_ALIAS_PREFIX}${slice.slice_id}_${slice.form_data.viz_type}`;
  66. const route = getChartDataRouteForSlice(slice);
  67. cy.intercept('POST', route).as(alias);
  68. return `@${alias}`;
  69. }
  70. export function getChartAliases(slices: Slice[]): string[] {
  71. return Array.from(slices).map(getChartAlias);
  72. }
  73. export function interceptChart({
  74. sliceId,
  75. legacy = false,
  76. method = 'POST',
  77. }: {
  78. sliceId?: number;
  79. legacy?: boolean;
  80. method?: 'POST' | 'GET';
  81. }) {
  82. const urlBase = legacy ? '**/superset/explore_json/' : '**/api/v1/chart/data';
  83. let url;
  84. if (sliceId) {
  85. const encodedFormData = encodeURIComponent(
  86. JSON.stringify({ slice_id: sliceId }),
  87. );
  88. url = `${urlBase}?form_data=${encodedFormData}*`;
  89. } else {
  90. url = `${urlBase}**`;
  91. }
  92. return cy.intercept(method, url);
  93. }