Spaces:
Sleeping
Sleeping
File size: 4,757 Bytes
5fdb69e |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
{
"cells": [
{
"cell_type": "markdown",
"id": "de3b5d4c",
"metadata": {},
"source": [
"# 🧠 Grafana Dashboard Summarizer\n",
"Simulate reading a Grafana dashboard JSON and summarize its panels using GPT or plain logic."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0abf3aaf",
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"from dotenv import load_dotenv\n",
"from IPython.display import Markdown, display\n",
"from openai import OpenAI\n",
"import json\n",
"import pandas as pd"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ad82ca65",
"metadata": {},
"outputs": [],
"source": [
"\n",
"\n",
"with open(\"mock_grafana_dashboard.json\", \"r\") as f:\n",
" data = json.load(f)\n",
"\n",
"dashboard = data[\"dashboard\"]\n",
"panels = dashboard[\"panels\"]\n",
"print(f\"Dashboard Title: {dashboard['title']}\")\n",
"print(f\"Total Panels: {len(panels)}\\n\")\n",
"for p in panels:\n",
" print(f\"- {p['title']} ({p['type']})\")\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1bf45c0f",
"metadata": {},
"outputs": [],
"source": [
"# Extracting panel data\n",
"\n",
"panel_data = []\n",
"for p in panels:\n",
" thresholds = p.get(\"fieldConfig\", {}).get(\"defaults\", {}).get(\"thresholds\", {}).get(\"steps\", [])\n",
" panel_data.append({\n",
" \"Title\": p[\"title\"],\n",
" \"Type\": p[\"type\"],\n",
" \"Unit\": p.get(\"fieldConfig\", {}).get(\"defaults\", {}).get(\"unit\", \"N/A\"),\n",
" \"Thresholds\": thresholds\n",
" })\n",
"\n",
"df = pd.DataFrame(panel_data)\n",
"df\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "90b67133",
"metadata": {},
"outputs": [],
"source": [
"\n",
"summary_prompt = f\"\"\"\n",
"You are a helpful assistant summarizing a system monitoring dashboard.\n",
"\n",
"Dashboard: {dashboard['title']}\n",
"Panels:\n",
"\"\"\"\n",
"for idx, row in df.iterrows():\n",
" summary_prompt += f\"- {row['Title']} [{row['Type']}] - Unit: {row['Unit']}, Thresholds: {row['Thresholds']}\\n\"\n",
"\n",
"print(summary_prompt)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "69a4208c",
"metadata": {},
"outputs": [],
"source": [
"\n",
"\n",
"load_dotenv(override=True)\n",
"api_key = os.getenv('OPENAI_API_KEY')\n",
"# Check the key\n",
"\n",
"if not api_key:\n",
" print(\"No API key was found - please head over to the troubleshooting notebook in this folder to identify & fix!\")\n",
"elif not api_key.startswith(\"sk-proj-\"):\n",
" print(\"An API key was found, but it doesn't start sk-proj-; please check you're using the right key - see troubleshooting notebook\")\n",
"elif api_key.strip() != api_key:\n",
" print(\"An API key was found, but it looks like it might have space or tab characters at the start or end - please remove them - see troubleshooting notebook\")\n",
"else:\n",
" print(\"API key found and looks good so far!\")\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2eee5a32",
"metadata": {},
"outputs": [],
"source": [
"openai = OpenAI()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "660eedb7",
"metadata": {},
"outputs": [],
"source": [
"def summarize():\n",
" response = openai.chat.completions.create(\n",
" model=\"gpt-4o-mini\",\n",
" messages=[\n",
" {\"role\": \"system\", \"content\": \"You are a Grafana dashboard summarizer.\"},\n",
" {\"role\": \"user\", \"content\": summary_prompt}\n",
" ]\n",
")\n",
" return response.choices[0].message.content"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "55f57d56",
"metadata": {},
"outputs": [],
"source": [
"\n",
"summary = summarize()\n",
"display(Markdown(summary))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "10dbfd6c",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "arunllms",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.11"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
|