|
"script" : "\/\/ Configuration\nconst url = \"http:\/\/grafana.example.com\";\nconst token = \"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\";\nconst orgId = 1;\nconst dashboardName = \"widgets\";\nconst dashboardId = \"xxxxxxxxxxxxxx\";\nconst tz = \"America\/Chicago\";\nconst darkMode = true;\nconst deviceSize = \"393x852\";\n\nparams = config.runsInWidget\n ? args.widgetParameter?.split(\",\") : []\n\nconst opts = {\n type: params[0] ? params[0] : \"md\",\n panel: params[1] ? params[1] : \"1\",\n from: params[2] ? params[2] : \"now-30m\",\n to: params[3] ? params[3] : \"now\",\n title: params[4] ? params[4] : \"Grafana\",\n align: params[5] ? params[5] : \"c\",\n};\n\n\/\/ Scriptable limits displayed image resolution to a max of 500x500\nconst imageSizes = {\n sm: new Size(474, 474),\n md: new Size(500, 210),\n lg: new Size(500, 500),\n re: new Size(480, 216),\n ci: new Size(216, 216),\n};\n\nconst widgetSizes = {\n \/\/ iOS sizes\n \"430x932\": {\n sm: new Size(170, 170),\n md: new Size(364, 170),\n lg: new Size(364, 382),\n ci: new Size(76, 76),\n re: new Size(172, 76),\n },\n \"428x926\": {\n sm: new Size(170, 170),\n md: new Size(364, 170),\n lg: new Size(364, 382),\n ci: new Size(76, 76),\n re: new Size(172, 76),\n },\n \"414x896\": {\n sm: new Size(169, 169),\n md: new Size(360, 169),\n lg: new Size(360, 379),\n ci: new Size(76, 76),\n re: new Size(160, 72),\n },\n \"414x736\": {\n sm: new Size(159, 159),\n md: new Size(348, 157),\n lg: new Size(348, 357),\n ci: new Size(76, 76),\n re: new Size(170, 76),\n },\n \"393x852\": {\n sm: new Size(158, 158),\n md: new Size(338, 158),\n lg: new Size(338, 354),\n ci: new Size(72, 72),\n re: new Size(160, 72),\n },\n \"390x844\": {\n sm: new Size(158, 158),\n md: new Size(338, 158),\n lg: new Size(338, 354),\n ci: new Size(72, 72),\n re: new Size(160, 72),\n },\n \"375x812\": {\n sm: new Size(155, 155),\n md: new Size(329, 155),\n lg: new Size(329, 345),\n ci: new Size(72, 72),\n re: new Size(157, 72),\n },\n \"375x667\": {\n sm: new Size(148, 148),\n md: new Size(321, 148),\n lg: new Size(321, 324),\n ci: new Size(68, 68),\n re: new Size(153, 68),\n },\n \"360x780\": {\n sm: new Size(155, 155),\n md: new Size(329, 155),\n lg: new Size(329, 345),\n ci: new Size(72, 72),\n re: new Size(157, 72),\n },\n \"320x568\": {\n sm: new Size(141, 141),\n md: new Size(292, 141),\n lg: new Size(292, 311),\n ci: null,\n re: null,\n },\n \/\/ iPadOS sizes\n \"768x1024\": {\n sm: new Size(141, 141),\n md: new Size(305.5, 141),\n lg: new Size(305.5, 305.5),\n lg: new Size(634.5, 305.5),\n xl: new Size(634.5, 305.5),\n },\n \"744x1133\": {\n sm: new Size(141, 141),\n md: new Size(305.5, 141),\n lg: new Size(305.5, 305.5),\n lg: new Size(634.5, 305.5),\n xl: new Size(634.5, 305.5),\n },\n \"810x1080\": {\n sm: new Size(146, 146),\n md: new Size(320.5, 146),\n lg: new Size(320.5, 320.5),\n lg: new Size(669, 320.5),\n xl: new Size(669, 320.5),\n },\n \"820x1180\": {\n sm: new Size(155, 155),\n md: new Size(342, 155),\n lg: new Size(342, 342),\n lg: new Size(715.5, 342),\n xl: new Size(715.5, 342),\n },\n \"834x1112\": {\n sm: new Size(150, 150),\n md: new Size(327.5, 150),\n lg: new Size(327.5, 327.5),\n lg: new Size(682, 327.5),\n xl: new Size(682, 327.5),\n },\n \"834x1194\": {\n sm: new Size(155, 155),\n md: new Size(342, 155),\n lg: new Size(342, 342),\n lg: new Size(715.5, 342),\n xl: new Size(715.5, 342),\n },\n \"954x1373\": {\n sm: new Size(162, 162),\n md: new Size(350, 162),\n lg: new Size(350, 350),\n lg: new Size(726, 350),\n xl: new Size(726, 350),\n },\n \"970x1389\": {\n sm: new Size(162, 162),\n md: new Size(350, 162),\n lg: new Size(350, 350),\n lg: new Size(726, 350),\n xl: new Size(726, 350),\n },\n \"1024x1366\": {\n sm: new Size(170, 170),\n md: new Size(378.5, 170),\n lg: new Size(378.5, 378.5),\n lg: new Size(795, 378.5),\n xl: new Size(795, 378.5),\n },\n \"1192x1590\": {\n sm: new Size(188, 188),\n md: new Size(412, 188),\n lg: new Size(412, 412),\n lg: new Size(860, 412),\n xl: new Size(860, 412),\n },\n\n \/\/ Apple watch sizes\n \"304x139\": { sm: new Size(304, 139) },\n \"330x145\": { sm: new Size(330, 145) },\n \"346x153\": { sm: new Size(346, 153) },\n \"368x161\": { sm: new Size(368, 161) },\n \"382x163\": { sm: new Size(382, 163) },\n};\n\n\/\/ Get widget dimensions based on device size\nsize = widgetSizes[deviceSize][opts.type];\n\n\/\/ Get image dimensions based on widget type\nconst imageSize = imageSizes[opts.type];\nconst theme = darkMode ? \"dark\" : \"light\";\n\n\/\/ Main container\nconst list = new ListWidget();\nlist.setPadding(0, 0, 0, 0);\nlist.backgroundColor = new Color(darkMode ? \"#111216\" : \"#F4F5F5\");\nlist.url = `${url}\/d\/${dashboardId}\/${dashboardName}?orgId=${orgId}&viewPanel=${opts.panel}&from=${opts.from}&to=${opts.to}&kiosk`;\n\n\/\/ Exclude lock screen widgets\nif (!(opts.type == \"re\" || opts.type == \"ci\")) {\n list.addSpacer(5);\n const textStack = list.addStack();\n\n \/\/ In order for spacers to function correly, the stack must be the full width of the widget\n textStack.size = new Size(size.width, 0);\n textStack.setPadding(5, 0, 0, 0);\n\n \/\/ Add flexible or fixed spacer based on alignment\n \/\/ Text is centered horizontally in stacks by default\n if (opts.align == \"l\") textStack.addSpacer(15);\n if (opts.align == \"r\") textStack.addSpacer(null);\n\n const text = textStack.addText(opts.title);\n text.font = Font.mediumSystemFont(12);\n text.textColor = new Color(darkMode ? \"#ccccdc\" : \"#24292e\");\n\n if (opts.align == \"l\") textStack.addSpacer(null);\n if (opts.align == \"r\") textStack.addSpacer(15);\n}\n\n\/\/ Image container\nconst imageStack = list.addStack();\nimageStack.size = new Size(size.width, 0);\n\nconst image_url = `${url}\/render\/d-solo\/${dashboardId}\/${dashboardName}?orgId=${orgId}&panelId=${opts.panel}&from=${opts.from}&to=${opts.to}&width=${imageSize.width}&height=${imageSize.height}&theme=${theme}&tz=${tz}`;\n\nconst req = new Request(image_url);\nreq.headers = {\n Authorization: `Bearer ${token}`,\n};\n\nconst image = await req.loadImage();\nconst imageWidget = imageStack.addImage(image);\n\nif (opts.type == \"re\") imageWidget.cornerRadius = 12;\n\nif (opts.type == \"ci\") imageWidget.cornerRadius = 100;\n\nif (config.runsInWidget) {\n Script.setWidget(list);\n} else {\n switch (opts.type) {\n case \"sm\":\n list.presentSmall();\n break;\n case \"md\":\n list.presentMedium();\n break;\n case \"lg\":\n list.presentLarge();\n break;\n case \"re\":\n list.presentAccessoryRectangular();\n break;\n case \"ci\":\n list.presentAccessoryCircular();\n break;\n case \"li\":\n list.presentAccessoryInline();\n break;\n }\n}\n\nScript.complete();", |