lineageController.js 7.67 KB
Newer Older
1 2 3 4 5 6
/*
 * 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
7
 * 'License'); you may not use this file except in compliance
8 9 10 11 12
 * 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
13
 * distributed under the License is distributed on an 'AS IS' BASIS,
14 15 16 17 18
 * 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 28
        function getLineageData(tableData, callRender) {
            LineageResource.get({
                tableName: tableData.tableName,
                type: tableData.type
            }, function lineageSuccess(response) {
29 30 31 32 33
                if (!_.isEmpty(response.results.values.vertices)) {
                    $scope.lineageData = transformData(response.results);
                    if (callRender) {
                        render();
                    }
34
                }
35
                $scope.requested = false;
36 37
            });
        }
38

39
        $scope.type = $element.parent().attr('data-table-type');
40
        $scope.requested = false;
41

42 43 44 45 46 47 48 49
        function render() {
            renderGraph($scope.lineageData, {
                element: $element[0],
                height: $element[0].offsetHeight,
                width: $element[0].offsetWidth
            });
            $scope.rendered = true;
        }
50

51 52 53
        $scope.$on('render-lineage', function(event, lineageData) {
            if (lineageData.type === $scope.type) {
                if (!$scope.lineageData) {
54
                    if (!$scope.requested) {
55
                        getLineageData(lineageData, true);
56
                        $scope.requested = true;
57 58 59 60 61
                    }
                } else {
                    render();
                }
            }
62
        });
63 64

        function transformData(metaData) {
65 66 67 68 69 70
            var edges = metaData.values.edges,
                vertices = metaData.values.vertices,
                nodes = {};

            function getNode(guid) {
                var vertex = {
71
                    guid: guid,
DarshanKumar committed
72 73
                    name: vertices.hasOwnProperty(guid) ? vertices[guid].values.name : 'Load Process',
                    type: vertices.hasOwnProperty(guid) ? vertices[guid].values.vertexId.values.typeName : 'LoadProcess'
74 75 76 77 78 79 80 81 82 83 84 85 86
                };
                if (!nodes.hasOwnProperty(guid)) {
                    nodes[guid] = vertex;
                }
                return nodes[guid];
            }

            function attachParent(edge, node) {
                edge.forEach(function eachPoint(childGuid) {
                    var childNode = getNode(childGuid);
                    node.children = node.children || [];
                    node.children.push(childNode);
                    childNode.parent = node.guid;
87
                });
88 89
            }

90 91 92 93 94 95 96
            /* Loop through all edges and attach them to correct parent */
            for (var guid in edges) {
                var edge = edges[guid],
                    node = getNode(guid);

                /* Attach parent to each endpoint of edge */
                attachParent(edge, node);
97
            }
98 99 100 101 102

            /* Return the first node w/o parent, this is root node*/
            return _.find(nodes, function(node) {
                return !node.hasOwnProperty('parent');
            });
103 104
        }

105
        function renderGraph(data, container) {
106 107 108 109 110 111
            // ************** Generate the tree diagram	 *****************
            var element = d3.select(container.element),
                width = Math.max(container.width, 960),
                height = Math.max(container.height, 350);

            var margin = {
DarshanKumar committed
112
                top: 100,
113
                right: 70,
DarshanKumar committed
114
                bottom: 30,
115
                left: 70
116 117 118 119 120
            };
            width = width - margin.right - margin.left;
            height = height - margin.top - margin.bottom;

            var i = 0;
121

122
            var tree = d3.layout.tree()
Vishal Kadam committed
123
                .size([height, width]);
124

125 126 127
            var diagonal = d3.svg.diagonal()
                .projection(function(d) {
                    return [d.y, d.x];
128
                });
Vishal Kadam committed
129

130 131 132 133
            var svg = element.select('svg')
                .attr('width', width + margin.right + margin.left)
                .attr('height', height + margin.top + margin.bottom)
                .select('g')
DarshanKumar committed
134 135 136 137 138

            .attr('transform',
                'translate(' + margin.left + ',' + margin.right + ')');
            //arrow
            svg.append("svg:defs").append("svg:marker").attr("id", "arrow").attr("viewBox", "0 0 10 10").attr("refX", 26).attr("refY", 5).attr("markerUnits", "strokeWidth").attr("markerWidth", 6).attr("markerHeight", 9).attr("orient", "auto").append("svg:path").attr("d", "M 0 0 L 10 5 L 0 10 z");
139 140 141 142 143 144 145 146 147 148 149 150

            var root = data;

            function update(source) {

                // Compute the new tree layout.
                var nodes = tree.nodes(source).reverse(),
                    links = tree.links(nodes);

                // Normalize for fixed-depth.
                nodes.forEach(function(d) {
                    d.y = d.depth * 180;
151
                });
Vishal Kadam committed
152

153 154 155 156 157
                // Declare the nodes…
                var node = svg.selectAll('g.node')
                    .data(nodes, function(d) {
                        return d.id || (d.id = ++i);
                    });
Vishal Kadam committed
158

159 160 161 162 163 164 165
                // Enter the nodes.
                var nodeEnter = node.enter().append('g')
                    .attr('class', 'node')
                    .attr('transform', function(d) {
                        return 'translate(' + d.y + ',' + d.x + ')';
                    });

DarshanKumar committed
166 167 168 169
                nodeEnter.append("image")
                    .attr("xlink:href", function(d) {
                        //return d.icon;
                        return d.type === 'Table' ? '../img/tableicon.png' : '../img/process.png';
170
                    })
171 172
                    .attr("x", "-18px")
                    .attr("y", "-18px")
173 174
                    .attr("width", "34px")
                    .attr("height", "34px");
175 176 177 178

                nodeEnter.append('text')
                    .attr('x', function(d) {
                        return d.children || d._children ?
DarshanKumar committed
179
                            (5) * -1 : +15;
180
                    })
181
                    .attr('dy', '-1.75em')
182
                    .attr('text-anchor', function(d) {
DarshanKumar committed
183
                        return d.children || d._children ? 'middle' : 'middle';
184 185 186 187
                    })
                    .text(function(d) {
                        return d.name;
                    })
DarshanKumar committed
188 189

                .style('fill-opacity', 1);
190 191 192 193 194 195 196 197 198 199

                // Declare the links…
                var link = svg.selectAll('path.link')
                    .data(links, function(d) {
                        return d.target.id;
                    });

                link.enter().insert('path', 'g')
                    .attr('class', 'link')
                    //.style('stroke', function(d) { return d.target.level; })
DarshanKumar committed
200
                    .style('stroke', 'green')
201
                    .attr('d', diagonal);
202
                link.attr("marker-end", "url(#arrow)");
203 204 205 206

            }

            update(root);
207
        }
Vishal Kadam committed
208 209

    }
Vishal Kadam committed
210
]);