Grafana Generate Snapshots avatar
Grafana Generate Snapshots

Deprecated

Pricing

Pay per usage

Go to Store
Grafana Generate Snapshots

Grafana Generate Snapshots

Deprecated

Developed by

defensivedepth

defensivedepth

Maintained by Community

Generates a Grafana Dashboard Snapshot. To use, edit the Grafana login URL in the source and rebuild the actor. Then call the API with valid Grafana credentials and target report URL in the body. The output will be the snapshot URL. This actor must use images with Puppeteer.

0.0 (0)

Pricing

Pay per usage

2

Total users

29

Monthly users

1

Last modified

3 years ago

Dockerfile

# This is a template for a Dockerfile used to run acts in Actor system.
# The base image name below is set during the act build, based on user settings.
# IMPORTANT: The base image must set a correct working directory, such as /usr/src/app or /home/user
FROM apify/actor-node-chrome:v0.21.10
# Second, copy just package.json and package-lock.json since it should be
# the only file that affects "npm install" in the next step, to speed up the build
COPY package*.json ./
# Install NPM packages, skip optional and development dependencies to
# keep the image small. Avoid logging too much and print the dependency
# tree for debugging
RUN npm --quiet set progress=false \
&& npm install --only=prod --no-optional \
&& echo "Installed NPM packages:" \
&& (npm list --all || true) \
&& echo "Node.js version:" \
&& node --version \
&& echo "NPM version:" \
&& npm --version
# Copy source code to container
# Do this in the last step, to have fast build if only the source code changed
COPY --chown=myuser:myuser . ./
# NOTE: The CMD is already defined by the base image.
# Uncomment this for local node inspector debugging:
# CMD [ "node", "--inspect=0.0.0.0:9229", "main.js" ]

package.json

{
"name": "apify-project",
"version": "0.0.1",
"description": "",
"author": "It's not you it's me",
"license": "ISC",
"dependencies": {
"apify": "0.21.10",
"apify-client": "0.6.0",
"puppeteer": "latest",
"jquery": "latest",
"cheerio": "latest"
},
"scripts": {
"start": "node main.js"
}
}

main.js

1const Apify = require('apify');
2const utils = require('apify/build/utils');
3const ApifyClient = require('apify-client');
4require('puppeteer');
5const path = require('path')
6require('jquery');
7const cheerio = require('cheerio')
8
9const humanDelay = ms => (Math.random() + 1) * ms;
10
11const sleepPromised = ms => new Promise(resolve => setTimeout(resolve, ms));
12
13const saveScreen = async (page, key) => {
14 const screenshotBuffer = await page.screenshot();
15 await Apify.setValue(key, screenshotBuffer, { contentType: 'image/png' });
16 const html = await page.evaluate('document.documentElement.outerHTML');
17 await Apify.setValue(key+'txt', html, { contentType: 'text/html' });
18};
19
20const opts = {
21 args: [
22 '--disable-web-security',
23 ],
24 //proxyUrl : ''
25};
26
27var browser = {};
28
29const setCookies = async (page) => {
30
31}
32
33const pageGoto = async (page, url, timeout, retryCount) => {
34 //if !(page) await page = await browser.newPage();
35 let done = '';
36 let counter = 0;
37 await setCookies(page);
38 do {
39 try {
40 //console.log('BEFORE REQ')
41 //console.log(url);
42 var response = await page.goto(url,{ waitUntil: 'networkidle2',timeout: timeout });
43 //console.log('AFTER REQ')
44 done = '';
45 }
46 catch (error){
47 console.log(error)
48 counter += 1;
49 done = error;
50 await page.close();
51 if (counter>1){
52 browser = await Apify.launchPuppeteer(opts);
53 console.log('new browser');
54 }
55 page = await browser.newPage();
56 await new Promise((resolve) => setTimeout(resolve, 3000));
57 console.log('RETRY Error')
58 }
59 if (response) {
60 if (response.status()!==200){
61 await page.close();
62 if (counter>1){
63 browser = await Apify.launchPuppeteer(opts);
64 console.log('new browser');
65 }
66 page = await browser.newPage();
67 counter += 1;
68 done = '403';
69 await new Promise((resolve) => setTimeout(resolve, 3000));
70 console.log('RETRY '+response.status())
71 }
72 }
73 } while (done!=='' && counter < retryCount)
74
75 if (done!==''){
76 console.log('RETURN NULL')
77 return null;
78 }
79 else
80 {
81 return page;
82 }
83}
84
85Apify.main(async () => {
86 var results = [];
87
88 const input = await Apify.getValue('INPUT')
89
90 var userName = input.userName;
91 var password = input.password;
92 var url = input.url;
93
94 console.log('Launching Puppeteer...');
95 browser = await Apify.launchPuppeteer(opts);
96 var page = await browser.newPage();
97
98 await page.setUserAgent('Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; .NET4.0E; .NET4.0C; .NET CLR 3.5.30729; .NET CLR 2.0.50727; .NET CLR 3.0.30729; InfoPath.3; MALCJS; rv:11.0) like Gecko');
99 page = await pageGoto(page,"https://YourGrafanaDomain.com/login", 100000, 10);
100 if (page!==null){
101 console.log('logIn');
102 await page.type('[name="username"]', userName, { delay: 100 });
103 await page.type('[id="inputPassword"]', password, { delay: 100 });
104 page.click('[type="submit"]');
105 await page.waitForNavigation({ waitUntil: 'networkidle2' });
106 console.log('after logIn submit');
107 }
108
109 page = await pageGoto(page,url, 100000, 10);
110
111 page.click('[bs-tooltip="\'Share dashboard\'"]');
112 try {
113 await page.waitForSelector('a[class="gf-tabs-link"]');
114 }
115 catch (error) {
116 await saveScreen(page,'errr');
117 throw error;
118 }
119
120 page.click('a[class="gf-tabs-link"]');
121
122 try {
123 await page.waitForSelector('[ng-click="createSnapshot()"]');
124 }
125 catch (error) {
126 await saveScreen(page,'errr');
127 throw error;
128 }
129
130 page.click('[ng-click="createSnapshot()"]');
131
132 try {
133 await page.waitForSelector('a[class="large share-modal-link"]');
134 }
135 catch (error) {
136 await saveScreen(page,'errr');
137 throw error;
138 }
139
140 let href = await page.$eval('a[class="large share-modal-link"]', a => a.href)
141
142 await Apify.setValue('OUTPUT', href);
143
144 console.log('Done.');
145});