|
| 1 | +import json |
| 2 | +from typing import Any |
| 3 | + |
| 4 | +import click |
| 5 | +import requests |
| 6 | + |
| 7 | +from ddev.cli.application import Application |
| 8 | +from ddev.cli.size.utils.common_funcs import get_org, get_valid_platforms |
| 9 | + |
| 10 | + |
| 11 | +@click.command() |
| 12 | +@click.option( |
| 13 | + "--dd-org", |
| 14 | + type=str, |
| 15 | + required=True, |
| 16 | + help="Datadog organization name taken from your config file e.g. 'default'", |
| 17 | +) |
| 18 | +@click.pass_obj |
| 19 | +def create_dashboard( |
| 20 | + app: Application, |
| 21 | + dd_org: str, |
| 22 | +) -> None: |
| 23 | + """ |
| 24 | + Creates a Datadog dashboard to visualize size metrics for integrations and dependencies. |
| 25 | + A new dashboard is created on each run. This command does not send data to Datadog. |
| 26 | + To send metrics, use: `ddev size status --to-dd-org <org>`. |
| 27 | + """ |
| 28 | + try: |
| 29 | + config_file_info = get_org(app, dd_org) |
| 30 | + if 'api_key' not in config_file_info: |
| 31 | + raise RuntimeError("No API key found in config file") |
| 32 | + if 'app_key' not in config_file_info: |
| 33 | + raise RuntimeError("No APP key found in config file") |
| 34 | + if 'site' not in config_file_info: |
| 35 | + raise RuntimeError("No site found in config file") |
| 36 | + headers = { |
| 37 | + "DD-API-KEY": config_file_info["api_key"], |
| 38 | + "DD-APPLICATION-KEY": config_file_info["app_key"], |
| 39 | + "Content-Type": "application/json", |
| 40 | + } |
| 41 | + |
| 42 | + payload = { |
| 43 | + "title": "Disk Usage Status for Integrations and Dependencies", |
| 44 | + "layout_type": "ordered", |
| 45 | + "widgets": create_json(app), |
| 46 | + } |
| 47 | + |
| 48 | + response = requests.post( |
| 49 | + f"https://api.{config_file_info['site']}/api/v1/dashboard", |
| 50 | + headers=headers, |
| 51 | + data=json.dumps(payload), |
| 52 | + ) |
| 53 | + |
| 54 | + resp_json = response.json() |
| 55 | + if "Forbidden" in str(resp_json.get("errors", [])): |
| 56 | + raise PermissionError("Access denied: your APP key doesn't have permission to create dashboards.") |
| 57 | + print(f"Dashboard URL: https://app.{config_file_info['site']}{resp_json['url']}") |
| 58 | + except Exception as e: |
| 59 | + app.abort(str(e)) |
| 60 | + |
| 61 | + |
| 62 | +def create_json(app: Application) -> list[dict[str, Any]]: |
| 63 | + valid_platforms = get_valid_platforms(app.repo.path) |
| 64 | + widgets: list[dict[str, Any]] = [] |
| 65 | + |
| 66 | + for size_type in ["compressed", "uncompressed"]: |
| 67 | + for platform in valid_platforms: |
| 68 | + # Treemap widget |
| 69 | + widgets.append( |
| 70 | + { |
| 71 | + "definition": { |
| 72 | + "type": "treemap", |
| 73 | + "title": f"{size_type.capitalize()} sizes in {platform}", |
| 74 | + "requests": [ |
| 75 | + { |
| 76 | + "queries": [ |
| 77 | + { |
| 78 | + "data_source": "metrics", |
| 79 | + "name": "query2", |
| 80 | + "query": f"avg:datadog.agent_integrations.size_analyzer.{size_type}" |
| 81 | + f"{{platform:{platform}}} by {{name_type,name}}", |
| 82 | + "aggregator": "last", |
| 83 | + } |
| 84 | + ], |
| 85 | + "response_format": "scalar", |
| 86 | + "style": {"palette": "classic"}, |
| 87 | + "formulas": [ |
| 88 | + { |
| 89 | + "formula": "query2", |
| 90 | + "number_format": { |
| 91 | + "unit": { |
| 92 | + "type": "canonical_unit", |
| 93 | + "unit_name": "byte_in_binary_bytes_family", |
| 94 | + } |
| 95 | + }, |
| 96 | + } |
| 97 | + ], |
| 98 | + } |
| 99 | + ], |
| 100 | + } |
| 101 | + } |
| 102 | + ) |
| 103 | + # Timeseries widget |
| 104 | + widgets.append( |
| 105 | + { |
| 106 | + "definition": { |
| 107 | + "title": f"Timeline of {size_type} sizes in {platform}", |
| 108 | + "type": "timeseries", |
| 109 | + "requests": [ |
| 110 | + { |
| 111 | + "response_format": "timeseries", |
| 112 | + "queries": [ |
| 113 | + { |
| 114 | + "name": "query1", |
| 115 | + "data_source": "metrics", |
| 116 | + "query": f"sum:datadog.agent_integrations.size_analyzer.{size_type}" |
| 117 | + f"{{platform:{platform}}}", |
| 118 | + } |
| 119 | + ], |
| 120 | + "formulas": [ |
| 121 | + { |
| 122 | + "formula": "query1", |
| 123 | + "number_format": { |
| 124 | + "unit": { |
| 125 | + "type": "canonical_unit", |
| 126 | + "unit_name": "byte_in_binary_bytes_family", |
| 127 | + } |
| 128 | + }, |
| 129 | + } |
| 130 | + ], |
| 131 | + "style": { |
| 132 | + "palette": "dog_classic", |
| 133 | + "order_by": "values", |
| 134 | + "line_type": "solid", |
| 135 | + "line_width": "normal", |
| 136 | + }, |
| 137 | + "display_type": "line", |
| 138 | + } |
| 139 | + ], |
| 140 | + } |
| 141 | + } |
| 142 | + ) |
| 143 | + |
| 144 | + return widgets |
0 commit comments