Adding a force-directed graph of web hits

As you have seen from the other recipes throughout the book, we have used many of the normal everyday visualizations seen commonly in spreadsheets and presentations. As data intelligence tools such as Splunk push the boundaries of getting data into a user's hands, there is a need to deliver and represent data via new and unique visualizations.

This recipe will show you how to install the Splunk web framework and create a force-directed graph (FDG) of relationships between web page hits that will be populated as a new dashboard in the Operational Intelligence application.

Getting ready

To step through this recipe, you will need a running Splunk Enterprise server, with the sample data loaded from Chapter 1, Play Time – Getting Data In. You should be familiar with navigating the Splunk user interface and using the Splunk search language. Some basic knowledge of JavaScript is recommended.

You will also need to download the Splunk Web Framework Toolkit app from Splunkbase. This app can be found here: https://splunkbase.splunk.com/app/1613/.

How to do it...

Follow the steps in this recipe to add a force-directed graph that illustrates web hits:

  1. Log in to your Splunk server.
  2. From the Apps menu in the upper left-hand corner of the home screen, click on the gear icon:
    How to do it...
  3. The Apps settings page will load. Then click on the Install App from file button.
  4. Click on the Choose File button and select the app file that was previously downloaded from Splunkbase. Then click the Upload button:
    How to do it...
  5. Select the Operational Intelligence application from the Apps menu:
    How to do it...
  6. Click on the Administration menu and then click the Dashboards menu item.
  7. Click on the Create New Dashboard button.
  8. In the Create New Dashboard window, enter Web Hits in the Title field and select Shared in App in the Permissions field.
  9. Click on Create Dashboard:
    How to do it...
  10. Then click on Done:
    How to do it...
  11. Click on the Edit button and select the Convert to HTML menu item:
    How to do it...
  12. Select the Replace Current option and click on Convert Dashboard:
    How to do it...
  13. Click on the Edit HTML button:
    How to do it...
  14. In the dashboard editor form, locate the HTML <div> tag with the data-role=main value and update it to look like the following code:
    <div class="dashboard-body container-fluid main-section-body" data-role="main">
        <div class="dashboard-header clearfix">
            <h2>Web Hits</h2>
       </div>
        <div class="dashboard-row dashboard-row1">
            <div class="dashboard-cell" style="width: 100%;">
                <div class="dashboard-panel clearfix">
                    <div class="panel-element-row">
                        <div class="dashboard-element chart"                      id="element1" style="width: 100%">
                            <div class="panel-head">
                                <h3>Webpage relationship</h3>
                            </div>
                            <div class="panel-body">
                            <div id="fd-chart"></div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
  15. Locate the require.config statement just below and insert the additional path that will help Splunk find the toolkit components. Add the app path as follows (remember to add a comma to the preceding array item):
    require.config({
        baseUrl: "{{SPLUNKWEB_URL_PREFIX}}/static/js",
        waitSeconds: 0, // Disable require.js load timeout
        paths:
        {
            "app": "../app"
        }
    });
  16. Locate the require portion of the JavaScript just below and add this item to the array (remember to add a comma to the preceding array item):
    "app/splunk_wftoolkit/components/forcedirected/forcedirected"
    
  17. Below the function statement, add ForceDirected (remember to add a comma to the preceding array item):
    PostProcessManager,
    UrlTokenModel,
    ForceDirected
    
  18. Locate the SEARCH MANAGERS section of the JavaScript and add the following code:
    var search1 = new SearchManager({
        "id": "search1",
        "status_buckets": 0,
        "latest_time": "now",
        "search": " index=main sourcetype=access_combined | rex     field=referer ".*(?<sourcepage>/.*?)$" | eval group=uri_path | stats count by     sourcepage, uri_path, group",
        "cancelOnUnload": true,
        "earliest_time": "-6h",
        "app": utils.getCurrentApp(),
        "auto_cancel": 0,
        "preview": true
    }, {tokens: true, tokenNamespace: "submitted"});
    
  19. Locate the VIEWS: VISUALIZATION ELEMENTS section of the JavaScript (close to the bottom of the page) and add the following code:
    var forcegraph = new ForceDirected({
            'id' : 'fd1',
            'managerid' : 'search1',
            'el' : $('#fd-chart')
        }).render();
    
  20. Click on Save, and you will be taken back to the dashboard.
  21. After a short while, you should see the force-directed graph load on your dashboard. This shows the relationships between pages and page transitions:
How to do it...

How it works...

Splunk provides the Web Framework Toolkit app to help demonstrate how different libraries can be used to create visualizations such as the force-directed graph. This app comes with prebuilt D3.js visualizations that we can use in our own apps. Force-directed graphs are visualizations that render the connectivity and clustering between objects. The typical force-directed drawing algorithm places similar objects in closer proximity to each other and unrelated objects farther apart.

In order to use the toolkit and FDGs, you first had to install the necessary app. You could copy the required libraries over to the Operational Intelligence application if your app is going to be distributed as it reduces dependency on other apps being installed.

The dashboard had to be in the HTML format for the FDG to be inserted, so you created an empty dashboard in SimpleXML and then converted it to HTML. You then added the HTML code that will hold the FDG, and ensured that it was set with all the correct CSS classes in order to ensure that the formatting matches the app.

The majority of the dashboard functionality comes in the JavaScript section. You first added references to the libraries that you copied into the Operational Intelligence app from the Web Framework Toolkit. You then added a search manager object that controls the searching functionality and returns the data to the graph object. Finally, you added an object that will hold the FDG and told it which HTML entity it is to be put in.

Note

A good walkthrough of HTML can be found on the Splunk developer website at http://dev.splunk.com/view/webframework-htmldashboards/SP-CAAAETK.

Additionally, to learn more about D3.js and the types of visualizations that you can create with it, visit http://d3js.org/.

There's more…

Splunk is very flexible, and there are a few tweaks we can make to improve or modify the behavior of the dashboard.

Changing the time range on the search manager

The search manager JavaScript object controls and coordinates the search requests, and as such, it can be modified with different settings to control the different aspects of its behavior.

One of the parameters that can be modified easily is the time range. This can be done by hardcoding a new value or linking it with other controls such as dropdowns or textboxes.

To change the search time range from the previous 6 hours to the current business week, update the search manager attributes of latest_time and earliest_time as shown in the following code:

var search1 = new SearchManager({
    "id": "search1",
    "status_buckets": 0,
    "latest_time": "+7d@w6",
    "search": "index=main sourcetype=access_combined | rex field=referer ".*(?<sourcepage>/.*?)$" | stats count by sourcepage, uri_path",
    "cancelOnUnload": true,
    "earliest_time": "@w1",
    "app": utils.getCurrentApp(),
    "auto_cancel": 0,
    "preview": true
}, {tokens: true, tokenNamespace: "submitted"});

See also

  • The Adding a calendar heatmap of product purchases recipe
  • The Remotely querying Splunk's REST API for unique page views recipe
  • The Creating a Python application to return unique IP addresses recipe
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.172.68