lineageController.js 6.78 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

Vishal Kadam committed
19 20
'use strict';

21 22
angular.module('dgc.lineage').controller('LineageController', ['$element', '$scope', '$state', '$stateParams', 'lodash', 'LineageResource', 'd3',
    function($element, $scope, $state, $stateParams, _, LineageResource, d3) {
Vishal Kadam committed
23

24 25 26 27
        $scope.lineageData = LineageResource.get({
            id: $stateParams.id
        }, function(data) {
            var nodes = {};
Vishal Kadam committed
28

29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
            function getNode(nodeId) {
                if (!nodes[nodeId]) {
                    var node;
                    if (data.vertices[nodeId]) {
                        node = angular.copy(data.vertices[nodeId]);
                        node.__key = nodeId;
                        node.__name = node['hive_table.name'] || node.__key;
                        node.__tooltip = node['hive_table.description'] || node['HiveLineage.query'];
                    } else {
                        node = {};
                        node.__key = nodeId;
                        node.__tooltip = node.__name = nodeId + ', Node Missing';
                    }
                    nodes[nodeId] = node;
                }
                return nodes[nodeId];
            }
Vishal Kadam committed
46

47 48
            var edges = [],
                edgeTypes = [];
Vishal Kadam committed
49

50 51 52 53 54 55 56 57
            angular.forEach(data.edges, function(edge) {
                /* Put the head (edge) inside tail (edge)
                 * Tail is parent
                 * Head is child
                 * */
                var parentNode = getNode(edge.tail);
                edge.source = parentNode;
                edge.target = getNode(edge.head);
Vishal Kadam committed
58

59
                parentNode.__hasChild = true;
60

61 62 63
                edge.__type = edge.label;
                edgeTypes.push(edge.label);
                edges.push(edge);
Vishal Kadam committed
64
            });
65 66 67
            edgeTypes = _.uniq(edgeTypes);
            render(nodes, edges, edgeTypes);
        });
Vishal Kadam committed
68

69 70 71 72 73 74 75 76 77 78 79
        function render(nodes, links, linkTypes) {
            // Use elliptical arc path segments to doubly-encode directionality.
            function click(node) {
                if (node.guid) {
                    $state.go('details', {
                        id: node.guid
                    }, {
                        location: 'replace'
                    });
                }
            }
Vishal Kadam committed
80

81 82 83 84 85
            function tick() {
                path.attr("d", linkArc);
                circle.attr("transform", transform);
                text.attr("transform", transform);
            }
Vishal Kadam committed
86

87 88 89 90 91 92
            function linkArc(d) {
                var dx = d.target.x - d.source.x,
                    dy = d.target.y - d.source.y,
                    dr = Math.sqrt(dx * dx + dy * dy);
                return "M" + d.source.x + "," + d.source.y + "A" + dr + "," + dr + " 0 0,1 " + d.target.x + "," + d.target.y;
            }
Vishal Kadam committed
93

94 95 96
            function transform(d) {
                return "translate(" + d.x + "," + d.y + ")";
            }
Vishal Kadam committed
97

98 99
            var width = Math.max($element[0].offsetWidth, 960),
                height = Math.max($element[0].offsetHeight, 350);
Vishal Kadam committed
100

101 102 103 104 105 106 107 108 109
            var force = d3.layout.force()
                .nodes(d3.values(nodes))
                .links(links)
                .size([width, height])
                .linkDistance(200)
                .charge(-120)
                .gravity(0.05)
                .on("tick", tick)
                .start();
Vishal Kadam committed
110

111 112 113
            var svg = d3.select($element[0]).select('svg')
                .attr("width", width)
                .attr("height", height);
Vishal Kadam committed
114

115 116 117 118 119 120
            /* Initialize tooltip */
            var tooltip = d3.tip()
                .attr('class', 'd3-tip')
                .html(function(d) {
                    return '<pre class="alert alert-success">' + d.__tooltip + '</pre>';
                });
Vishal Kadam committed
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
            /* Invoke the tip in the context of your visualization */
            svg.call(tooltip);

            // Per-type markers, as they don't inherit styles.
            var defs = svg.append("defs");

            var imageDim = 10;
            defs.append('svg:pattern')
                .attr('id', 'process-image')
                .attr('patternUnits', 'userSpaceOnUse')
                .attr('width', imageDim)
                .attr('height', imageDim)
                .append('svg:image')
                .attr('xlink:href', '/img/process.png')
                .attr('x', 0)
                .attr('y', 0)
                .attr('width', imageDim)
                .attr('height', imageDim);

            defs.selectAll("marker")
                .data(linkTypes)
                .enter().append("marker")
                .attr("id", function(d) {
                    return d;
                })
                .attr("viewBox", "0 -5 10 10")
                .attr("refX", 15)
                .attr("refY", -1.5)
                .attr("markerWidth", 6)
                .attr("markerHeight", 6)
                .attr("orient", "auto")
                .append("path")
                .attr("d", "M0,-5L10,0L0,5");

            var path = svg.append("g").selectAll("path")
                .data(force.links())
                .enter().append("path")
                .attr("class", function(d) {
                    return "link " + d.__type;
                })
                .attr("marker-end", function(d) {
                    return "url(#" + d.__type + ")";
                });
Vishal Kadam committed
165

166 167 168 169 170 171 172 173 174 175 176 177 178
            var circle = svg.append("g").selectAll("circle")
                .data(force.nodes())
                .enter().append("circle")
                .on('click', click)
                .on('mouseover', tooltip.show)
                .on('mouseout', tooltip.hide)
                .attr('class', function(d) {
                    return d.__hasChild ? '' : 'empty';
                })
                .attr("r", function(d) {
                    return d.__hasChild ? 15 : 10;
                })
                .call(force.drag);
Vishal Kadam committed
179

180 181 182 183 184 185 186 187
            var text = svg.append("g").selectAll("text")
                .data(force.nodes())
                .enter().append("text")
                .attr('dy', '2em')
                .text(function(d) {
                    return d.__name;
                });
        }
Vishal Kadam committed
188 189 190

    }
]);