Grafana Generate Snapshots avatar
Grafana Generate Snapshots
Deprecated
View all Actors
This Actor is deprecated

This Actor is unavailable because the developer has decided to deprecate it. Would you like to try a similar Actor instead?

See alternative Actors
Grafana Generate Snapshots

Grafana Generate Snapshots

defensivedepth/grafana-generate-snapshots

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.

Dockerfile

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

package.json

1{
2    "name": "apify-project",
3    "version": "0.0.1",
4    "description": "",
5    "author": "It's not you it's me",
6    "license": "ISC",
7    "dependencies": {
8        "apify": "0.21.10",
9        "apify-client": "0.6.0",
10        "puppeteer": "latest",
11        "jquery": "latest",
12        "cheerio": "latest"
13    },
14    "scripts": {
15        "start": "node main.js"
16    }
17}

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});
Developer
Maintained by Community
Categories