(payload)
| 23 | } |
| 24 | |
| 25 | async function createChart(payload) { |
| 26 | let { |
| 27 | project_id, dataset_id, spec, team_id, |
| 28 | name, legend, type, subType, displayLegend, pointRadius, |
| 29 | dataLabels, includeZeros, timeInterval, stacked, horizontal, |
| 30 | xLabelTicks, showGrowth, invertGrowth, mode, maxValue, minValue, ranges, |
| 31 | xAxis, xAxisOperation, yAxis, yAxisOperation, dateField, dateFormat, |
| 32 | conditions, formula, seriesConfiguration, |
| 33 | } = payload; |
| 34 | |
| 35 | if (!project_id) { |
| 36 | throw new Error("project_id is required to create a chart"); |
| 37 | } |
| 38 | |
| 39 | if (!name) { |
| 40 | throw new Error("name is required to create a chart"); |
| 41 | } |
| 42 | |
| 43 | if (!team_id) { |
| 44 | throw new Error("team_id is required to create a chart"); |
| 45 | } |
| 46 | |
| 47 | // Provide default chart spec if not provided |
| 48 | const defaultSpec = { |
| 49 | type: "line", |
| 50 | title: "AI Generated Chart", |
| 51 | timeInterval: "day", |
| 52 | chartSize: 2, |
| 53 | displayLegend: true, |
| 54 | pointRadius: 0, |
| 55 | dataLabels: false, |
| 56 | includeZeros: true, |
| 57 | stacked: false, |
| 58 | horizontal: false, |
| 59 | xLabelTicks: "default", |
| 60 | showGrowth: false, |
| 61 | invertGrowth: false, |
| 62 | mode: "chart", |
| 63 | options: {} |
| 64 | }; |
| 65 | |
| 66 | let chartSpec = spec || defaultSpec; |
| 67 | |
| 68 | try { |
| 69 | // Get the dataset to get its legend for default values |
| 70 | const normalizedTeamId = normalizeTeamId(team_id); |
| 71 | const dataset = await requireDatasetForTeam(dataset_id, normalizedTeamId); |
| 72 | let dataRequest = null; |
| 73 | if (Array.isArray(dataset.DataRequests) && dataset.DataRequests.length > 0) { |
| 74 | dataRequest = dataset.DataRequests[0]; |
| 75 | } else if (dataset.main_dr_id) { |
| 76 | dataRequest = await db.DataRequest.findByPk(dataset.main_dr_id); |
| 77 | } else { |
| 78 | dataRequest = await db.DataRequest.findOne({ where: { dataset_id } }); |
| 79 | } |
| 80 | const chartSanitization = removeCompiledMetricAccumulation({ |
| 81 | configuration: dataRequest?.configuration, |
| 82 | type, |
no test coverage detected