text:
define(['lodash', 'd3','log', './simple-statement-view', './../ast/action-invocation-expression', './point', 'd3utils', './../ast/ballerina-ast-factory'],
    function (_, d3, log, SimpleStatementView, ActionInvocationExpression, Point, D3Utils, BallerinaASTFactory) {

        /**
         * Action invocation statement view.
         * @param args {*} constructor arguments
         * @class ActionInvocationStatementView
         * @constructor
         * @extends SimpleStatementView
         */
        var ActionInvocationStatementView = function (args) {
            SimpleStatementView.call(this, args);
            this._connectorView = {};

            if (_.isNil(this._container)) {
                log.error("Container for action statement is undefined." + this._container);
                throw "Container for action statement is undefined." + this._container;
            }

            this.getBoundingBox().fromTopCenter(this._topCenter, this._viewOptions.width, this._viewOptions.height);
            this._processorConnector = undefined;
            this._processorConnector2 = undefined;
            this._forwardArrowHead = undefined;
            this._backArrowHead = undefined;
            this._arrowGroup = undefined;

        };

        ActionInvocationStatementView.prototype = Object.create(SimpleStatementView.prototype);
        ActionInvocationStatementView.prototype.constructor = ActionInvocationStatementView;

        ActionInvocationStatementView.prototype.init = function(){
            this.getModel().on("drawConnectionForAction", this.drawActionConnections, this);
            Object.getPrototypeOf(this.constructor.prototype).init.call(this);
        };
        ActionInvocationStatementView.prototype.setDiagramRenderingContext = function(context){
            this._diagramRenderingContext = context;
        };
        ActionInvocationStatementView.prototype.getDiagramRenderingContext = function(){
            return this._diagramRenderingContext;
        };

        ActionInvocationStatementView.prototype.drawActionConnections = function(startPoint){
            var actionInvocationModel = this.getActionInvocationExpressionModel();
            if(this.isAtValidDropTarget()){
                this.updateActivatedTarget(this.messageManager.getActivatedDropTarget(), actionInvocationModel);
            }

            if(!_.isNil(actionInvocationModel.getConnector())) {
                this.renderDisplayText(this.getModel().getStatementString());
                this.renderArrows(this.getDiagramRenderingContext());
            }
        };

        ActionInvocationStatementView.prototype.setConnectorView = function(view){
            this._connectorView = view;
        };
        ActionInvocationStatementView.prototype.getConnectorView = function(){
            return this._connectorView;
        };

        /**
         * Rendering the view for get-Action statement.
         * @returns {group} The svg group which contains the elements of the action statement view.
         */
        ActionInvocationStatementView.prototype.render = function (renderingContext) {
            var self = this;
            var model = this.getModel();
            // Calling super class's render function.
            (this.__proto__.__proto__).render.call(this, renderingContext);
            // Disable the event registered at the super class for the top-edge-moved, since we override the event action
            // TODO: we need to properly handle this at the super class level
            this.stopListening(this.getBoundingBox(), 'top-edge-moved');
            var actionInvocationExpressionModel = this.getActionInvocationExpressionModel();
            var connectorModel = actionInvocationExpressionModel.getConnector();
            model.accept(this);
            if (!_.isUndefined(connectorModel)) {
                this.connector = this.getDiagramRenderingContext().getViewOfModel(connectorModel);
            }
            else {
                var siblingConnectors = this._parent._model.children;
                _.some(siblingConnectors, function (key, i) {
                    if (BallerinaASTFactory.isConnectorDeclaration(siblingConnectors[i])) {
                        var connectorReference = siblingConnectors[i];
                        var actionInvocationExpressionModel = self.getActionInvocationExpressionModel();
                        actionInvocationExpressionModel.setConnector(connectorReference);
                        self.messageManager.setMessageSource(model);
                        self.updateActivatedTarget(connectorReference, actionInvocationExpressionModel);
                        return true;
                    }
                });
            }

            // Setting display text.
            this.renderDisplayText(model.getStatementString());
            this.renderProcessorConnectPoint(renderingContext);
            this.renderArrows(renderingContext);

            // Creating property pane
            var editableProperty = {
                    propertyType: "text",
                    key: "Action Invocation",
                    model: model,
                    getterMethod: model.getStatementString,
                    setterMethod: model.setStatementString
            };

            this._createPropertyPane({
                model: model,
                statementGroup: this.getStatementGroup(),
                editableProperties: editableProperty
            });

            this._createDebugIndicator({
                statementGroup: this.getStatementGroup()
            });

            this.listenTo(model, 'update-property-text', this.updateStatementText);

            // mouse events for 'processorConnectPoint'
            this.processorConnectPoint.on("mousedown", function () {
                d3.event.preventDefault();
                d3.event.stopPropagation();
                var x =  parseFloat(self.processorConnectPoint.attr('cx'));
                var y =  parseFloat(self.processorConnectPoint.attr('cy'));
                var sourcePoint = self.toGlobalCoordinates(new Point(x, y));

                self.messageManager.startDrawMessage(self, model, sourcePoint);
                self.messageManager.setTypeBeingDragged(true);
            });
            this.processorConnectPoint.on("mouseover", function () {
                self.processorConnectPoint
                    .style("fill", "#444")
                    .style("fill-opacity", 0.5)
                    .style("cursor", 'url(images/BlackHandwriting.cur), pointer');
            });
            this.processorConnectPoint.on("mouseout", function () {
                self.processorConnectPoint.style("fill-opacity", 0.01);
            });

            this.getBoundingBox().on('top-edge-moved', function(dy){
                self.processorConnectPoint.attr('cy',  parseFloat(self.processorConnectPoint.attr('cy')) + dy);
            });
        };

        ActionInvocationStatementView.prototype.renderArrows = function (renderingContext) {
            this.setDiagramRenderingContext(renderingContext);
            var actionInvocationExpressionModel = this.getActionInvocationExpressionModel();
            var connectorModel = actionInvocationExpressionModel.getConnector();

            if(!_.isUndefined(connectorModel)) {
                this.connector = this.getDiagramRenderingContext().getViewOfModel(connectorModel);
            }

            if(!_.isUndefined(this.connector)) {
                var parent = this.getStatementGroup();

                this._arrowGroup = D3Utils.group(parent).attr("transform", "translate(0,0)");
                var width = this.getBoundingBox().w();
                var height = this.getBoundingBox().h();
                var x = this.getBoundingBox().getLeft();
                var y = this.getBoundingBox().getTop();
                var sourcePointX = x + width;
                var sourcePointY = y + height / 2;

                var startPoint = new Point(sourcePointX, sourcePointY);
                var connectorCenterPointX = this.connector.getMiddleLineCenter().x();
                var connectorCenterPointY = this.connector.getMiddleLineCenter().y();
                var startX = Math.round(startPoint.x());
                this._processorConnector = D3Utils.line(Math.round(startPoint.x()), Math.round(startPoint.y()), Math.round(connectorCenterPointX),
                    Math.round(startPoint.y()), this._arrowGroup).classed("action-line", true);
                this._forwardArrowHead = D3Utils.inputTriangle(Math.round(connectorCenterPointX) - 5, Math.round(startPoint.y()), this._arrowGroup).classed("action-arrow", true);
                this._forwardArrowHead.attr("transform", "translate(0,0)");
                this._processorConnector2 = D3Utils.line(Math.round(startPoint.x()), Math.round(startPoint.y()) + 8, Math.round(connectorCenterPointX),
                    Math.round(startPoint.y()) + 8, this._arrowGroup).classed("action-dash-line", true);
                this._backArrowHead = D3Utils.outputTriangle(Math.round(startPoint.x()), Math.round(startPoint.y()) + 8, this._arrowGroup).classed("action-arrow", true);
                this._backArrowHead.attr("transform", "translate(0,0)");

                this.renderProcessorConnectEndPoint(renderingContext);

                this.listenTo(this.getBoundingBox(), 'top-edge-moved', function (offset) {
                    this.getSvgRect().attr('y', parseFloat(this.getSvgRect().attr('y')) + offset);
                    this.getSvgText().attr('y', parseFloat(this.getSvgText().attr('y')) + offset);
                    var currentY1ProcessorConnector = this._processorConnector.attr('y1');
                    var currentY1ProcessorConnector2 = this._processorConnector2.attr('y1');
                    var currentY2ProcessorConnector = this._processorConnector.attr('y2');
                    var currentY2ProcessorConnector2 = this._processorConnector2.attr('y2');
                    var forwardArrowTransformX = this._forwardArrowHead.node().transform.baseVal.consolidate().matrix.e;
                    var forwardArrowTransformY = this._forwardArrowHead.node().transform.baseVal.consolidate().matrix.f;
                    var backwardArrowTransformX = this._backArrowHead.node().transform.baseVal.consolidate().matrix.e;
                    var backwardArrowTransformY = this._backArrowHead.node().transform.baseVal.consolidate().matrix.f;

                    this._processorConnector.attr('y1', parseFloat(currentY1ProcessorConnector) + offset);
                    this._processorConnector2.attr('y1', parseFloat(currentY1ProcessorConnector2) + offset);
                    this._processorConnector.attr('y2', parseFloat(currentY2ProcessorConnector) + offset);
                    this._processorConnector2.attr('y2', parseFloat(currentY2ProcessorConnector2) + offset);
                    this._forwardArrowHead.node().transform.baseVal.getItem(0).setTranslate(forwardArrowTransformX + 0, forwardArrowTransformY + offset);
                    this._backArrowHead.node().transform.baseVal.getItem(0).setTranslate(backwardArrowTransformX + 0, backwardArrowTransformY + offset);

                    d3.select(this.processorConnectPoint.node()).attr("transform", "translate(" + backwardArrowTransformX + "," + (backwardArrowTransformY + offset) + ")");
                    d3.select(this.processorConnectEndPoint.node()).attr("transform", "translate(" + forwardArrowTransformX + "," + (forwardArrowTransformY + offset) + ")");
                }, this);

                this.getBoundingBox().on('right-edge-moved', function (offset) {
                    var currentX1ProcessorConnector = this._processorConnector.attr('x1');
                    var currentX1ProcessorConnector2 = this._processorConnector2.attr('x1');
                    var backwardArrowTransformX = this._backArrowHead.node().transform.baseVal.consolidate().matrix.e;
                    var backwardArrowTransformY = this._backArrowHead.node().transform.baseVal.consolidate().matrix.f;

                    this._processorConnector.attr('x1', parseFloat(currentX1ProcessorConnector) + offset);
                    this._processorConnector2.attr('x1', parseFloat(currentX1ProcessorConnector2) + offset);
                    this._backArrowHead.node().transform.baseVal.getItem(0).setTranslate(backwardArrowTransformX + offset, backwardArrowTransformY + 0);
                }, this);

                this.connector.getBoundingBox().on('moved', function (offset) {
                    var currentX2ProcessorConnector = this._processorConnector.attr('x2');
                    var currentX2ProcessorConnector2 = this._processorConnector2.attr('x2');
                    var forwardArrowTransformX = this._forwardArrowHead.node().transform.baseVal.consolidate().matrix.e;
                    var forwardArrowTransformY = this._forwardArrowHead.node().transform.baseVal.consolidate().matrix.f;

                    this._processorConnector.attr('x2', parseFloat(currentX2ProcessorConnector) + offset.dx);
                    this._processorConnector2.attr('x2', parseFloat(currentX2ProcessorConnector2) + offset.dx);
                    this._forwardArrowHead.node().transform.baseVal.getItem(0).setTranslate(forwardArrowTransformX + offset.dx, forwardArrowTransformY + offset.dy);
                    d3.select(this.processorConnectEndPoint.node()).attr("transform", "translate(" + (forwardArrowTransformX + offset.dx) + "," + (forwardArrowTransformY + offset.dy) + ")");
                }, this);

                this.processorConnectPoint.style("display", "none");

                if(!_.isUndefined(this.getParent()._contentGroup)) {
                    var thisNodeGroup = parent.node();
                    var thisNodeGroupParent = this.getParent()._contentGroup.node();

                    thisNodeGroupParent.appendChild(thisNodeGroup);
                }

                var self = this;

                connectorModel.addConnectorActionReference(this);

                this.processorConnectEndPoint.on("mousedown", function () {
                    d3.event.preventDefault();
                    d3.event.stopPropagation();

                    var x =  parseFloat(self.processorConnectEndPoint.attr('cx'));
                    var y =  parseFloat(self.processorConnectEndPoint.attr('cy'));
                    var x1 =  parseFloat(self._processorConnector.attr('x1'));
                    var y1 =  parseFloat(self._processorConnector.attr('y1'));

                    var sourcePoint = self.toGlobalCoordinates(new Point(x, y));
                    var connectorPoint = self.toGlobalCoordinates(new Point(x1, y1));

                    connectorModel.removeConnectorActionReference(self.getModel().id);

                    self.messageManager.startDrawMessage(self, actionInvocationExpressionModel, sourcePoint, connectorPoint);
                    self.messageManager.setTypeBeingDragged(true);

                    self.removeArrows();
                    self.processorConnectEndPoint.remove();
                });

                this.processorConnectEndPoint.on("mouseover", function () {
                    self.processorConnectEndPoint
                        .style("fill-opacity", 0.5)
                        .style("cursor", 'url(images/BlackHandwriting.cur), pointer');
                });

                this.processorConnectEndPoint.on("mouseout", function () {
                    self.processorConnectEndPoint.style("fill-opacity", 0.01);
                });
            }
        };

        ActionInvocationStatementView.prototype.renderProcessorConnectEndPoint = function (renderingContext) {
            var boundingBox = this.getBoundingBox();
            var width = boundingBox.w();
            var height = boundingBox.h();
            var x = boundingBox.getRight();
            var y = boundingBox.getTop();

            var processorConnectEndPoint = D3Utils.circle(parseFloat(this._processorConnector2.attr('x2')) - 3, ((y + height / 2)), 10, this.getStatementGroup());
            processorConnectEndPoint
                .attr("fill-opacity", 0.01)
                .style("fill", "#444");
            this.processorConnectEndPoint = processorConnectEndPoint;
        };

        ActionInvocationStatementView.prototype.renderProcessorConnectPoint = function (renderingContext) {
            var boundingBox = this.getBoundingBox();
            var width = boundingBox.w();
            var height = boundingBox.h();
            var x = boundingBox.getLeft();
            var y = boundingBox.getTop();

            var processorConnectPoint = D3Utils.circle((x + width), ((y + height / 2)), 10, this.getStatementGroup());
            processorConnectPoint.attr("fill-opacity", 0.01);
            this.processorConnectPoint = processorConnectPoint;
        };

        /**
         * Remove related arrow group
         */
        ActionInvocationStatementView.prototype.removeArrows = function () {
            if (!_.isNil(this._arrowGroup) && !_.isNil(this._arrowGroup.node())) {
                d3.select(this._arrowGroup).node().remove();
            }
        };

        /**
         * Covert a point in user space Coordinates to client viewport Coordinates.
         * @param {Point} point a point in current user coordinate system
         */
        ActionInvocationStatementView.prototype.toGlobalCoordinates = function (point) {
            var pt = this.processorConnectPoint.node().ownerSVGElement.createSVGPoint();
            pt.x = point.x();
            pt.y = point.y();
            pt = pt.matrixTransform(this.processorConnectPoint.node().getCTM());
            return new Point(pt.x, pt.y);
        };

        /**
         * Remove statement view callback
         */
        ActionInvocationStatementView.prototype.onBeforeModelRemove = function () {
            this.stopListening(this.getBoundingBox());
            d3.select("#_" +this._model.id).remove();
            this.removeArrows();
            // resize the bounding box in order to the other objects to resize
            this.getBoundingBox().h(0).w(0);
        };

        ActionInvocationStatementView.prototype.updateStatementText = function (newStatementText, propertyKey) {
            var displayText = this.getModel().getStatementString();
            var siblingConnectors = this._parent._model.children;
            var connectorName = newStatementText.match(/\((.*)\)/)[1];
            var self = this;

            connectorName = connectorName.split(",")[0].trim();

            this.getModel().setStatementString(newStatementText);
            this.renderDisplayText(newStatementText);

            self.removeArrows();
            self.processorConnectPoint.style("display", "block");
            self.processorConnectEndPoint.remove();

            _.some(siblingConnectors, function (key, i) {
                if ( (BallerinaASTFactory.isConnectorDeclaration(siblingConnectors[i]))
                         && (siblingConnectors[i]._connectorVariable == connectorName) ) {
                    self.getActionInvocationExpressionModel().setConnector(siblingConnectors[i]);
                    // Stop listening to the top edge moved event. Since this is re initialized at the render arrows
                    self.stopListening(self.getBoundingBox(), 'top-edge-moved');
                    // TODO: refactor this
                    self.renderArrows(self.getDiagramRenderingContext());
               }
            });
        };

        ActionInvocationStatementView.prototype.getActionInvocationExpressionModel = function () {
            if (BallerinaASTFactory.isActionInvocationStatement(this.getModel())) {
                var actionExpression = this.getModel().getChildren()[0];
                if(BallerinaASTFactory.isActionInvocationExpression(actionExpression)){
                    return actionExpression;
                }
            } else if (BallerinaASTFactory.isAssignmentStatement(this.getModel())) {
                var actionExpression = this.getModel().getChildren()[1].getChildren()[0];
                if (BallerinaASTFactory.isActionInvocationExpression(actionExpression)) {
                    return actionExpression;
                }
            } else if (BallerinaASTFactory.isVariableDefinitionStatement(this.getModel())) {
                var actionExpression = this.getModel().getChildren()[0];
                if (BallerinaASTFactory.isActionInvocationExpression(actionExpression)) {
                    return actionExpression;
                }
            }
        };

        ActionInvocationStatementView.prototype.updateActivatedTarget = function (target) {
            var actionInvocationExpressionModel = this.getActionInvocationExpressionModel();
            if (!_.isUndefined(target)) {
                actionInvocationExpressionModel.setConnector(target);
            } else {
                actionInvocationExpressionModel.setConnector(undefined);
            }
            this.updateStatementString();
        };

        ActionInvocationStatementView.prototype.updateStatementString = function () {
            var actionInvocationExpressionModel = this.getActionInvocationExpressionModel();
            if (BallerinaASTFactory.isAssignmentStatement(this.getModel())) {
                var rightOperandExp = this.getModel().getChildren()[1];
                if (!_.isUndefined(rightOperandExp) && BallerinaASTFactory.isRightOperandExpression(rightOperandExp)) {
                    rightOperandExp.setRightOperandExpressionString(actionInvocationExpressionModel.getExpression());
                }
            } else if (BallerinaASTFactory.isVariableDefinitionStatement(this.getModel())) {
                this.getModel().setRightExpression(actionInvocationExpressionModel.getExpression());
            }
        };

        ActionInvocationStatementView.prototype.isAtValidDropTarget = function(){
            return BallerinaASTFactory.isConnectorDeclaration(this.messageManager.getActivatedDropTarget());
        };

        return ActionInvocationStatementView;

    });


text:
#include "predicate_merge_rule.hpp"

#include "expression/expression_functional.hpp"
#include "expression/logical_expression.hpp"
#include "logical_query_plan/lqp_utils.hpp"
#include "logical_query_plan/union_node.hpp"

using namespace hyrise::expression_functional;  // NOLINT

namespace hyrise {

std::string PredicateMergeRule::name() const {
  static const auto name = std::string{"PredicateMergeRule"};
  return name;
}

/**
 * Merge subplans that only consists of PredicateNodes and UnionNodes (with SetOperationMode::Positions) into a single
 * PredicateNode. A subplan consists of linear "chain" and forked "diamond" parts.
 *
 * EXAMPLE:
 *
 *           |                                       |                                       |
 *      ___Union___                           Predicate(a OR b)            Predicate((d AND c) AND (a OR b))
 *    /            \                                 |                                       |
 * Predicate(a)    |                                 |                                       |
 *    |            |                                 |                                       |
 *    |       Predicate(b)                           |                                       |
 *    \           /               ----->             |                ----->                 |
 *     Predicate(c)                             Predicate(c)                                 |
 *           |                                       |                                       |
 *     Predicate(d)                             Predicate(d)                                 |
 *           |                                       |                                       |
 *         Table                                   Table                                   Table
 *
 * Note that predicates c and d are now also included in the new predicate and will be evaluated by the
 * ExpressionEvaluator. The reason for this is that the current detection algorithm does not yet identify predicates
 * that are inputs to a merged subplan but do not necessarily belong to that subplan. When it becomes necessary, this
 * rule might be adapted to make more sophisticated decisions on which predicates to include.
 */
void PredicateMergeRule::_apply_to_plan_without_subqueries(const std::shared_ptr<AbstractLQPNode>& lqp_root) const {
  Assert(lqp_root->type == LQPNodeType::Root, "PredicateMergeRule needs root to hold onto");

  // (Potentially mergeable) subplans are identified by their topmost UnionNode. node_to_topmost holds a mapping from
  // PredicateNodes and UnionNodes within such subplans to the respective topmost UnionNode.
  std::map<const std::shared_ptr<AbstractLQPNode>, const std::shared_ptr<UnionNode>> node_to_topmost;

  // Store the same nodes as node_to_topmost but preserve their order
  std::queue<std::shared_ptr<AbstractLQPNode>> node_queue;

  // For every topmost UnionNode, store the total number of UnionNodes in its subplan below
  std::map<const std::shared_ptr<UnionNode>, size_t> topmost_to_union_count;

  // Step 1: Collect mergeable nodes
  visit_lqp(lqp_root, [&](const auto& node) {
    const auto& union_node = std::dynamic_pointer_cast<UnionNode>(node);
    // We need to check for SetOperationMode::Positions as other SetOperationModes are
    // not guaranteed to represent a disjunction
    if (node->type == LQPNodeType::Predicate ||
        (union_node && union_node->set_operation_mode == SetOperationMode::Positions)) {
      const auto& outputs = node->outputs();
      const auto parent = std::find_if(outputs.begin(), outputs.end(),
                                       [&](const auto& output) { return node_to_topmost.count(output); });

      if (parent == outputs.end() && union_node) {
        // New subplan found
        node_to_topmost.emplace(union_node, union_node);
        node_queue.push(union_node);
        topmost_to_union_count.emplace(union_node, 0);
      } else if (parent != outputs.end()) {
        // New node for a known subplan found
        node_to_topmost.emplace(node, node_to_topmost[*parent]);
        node_queue.push(node);
      }

      if (union_node) {
        topmost_to_union_count[node_to_topmost[node]]++;
      }
    }
    return LQPVisitation::VisitInputs;
  });

  // Step 2: Try to merge each node
  while (!node_queue.empty()) {
    const auto& node = node_queue.front();
    node_queue.pop();

    if (node->output_count() == 0 || topmost_to_union_count[node_to_topmost[node]] < minimum_union_count) {
      // The node was already removed due to a previous merge or its subplan is too small to be merged.
      continue;
    }

    switch (node->type) {
      // _merge_disjunction() and _merge_conjunction() merge UnionNodes and PredicateNodes by
      //   a) being called on any node inside a mergable subplan
      //   b) calling each other recursively after a merge to check whether they can merge right again
      case LQPNodeType::Union: {
        const auto& union_node = std::static_pointer_cast<UnionNode>(node);
        _merge_disjunction(union_node);
        break;
      }

      case LQPNodeType::Predicate: {
        _merge_conjunction(std::static_pointer_cast<PredicateNode>(node));
        break;
      }

      default: {
      }
    }
  }
}

/**
 * Merge "simple" diamonds, which only consist of one UnionNode, having two PredicateNodes as inputs
 */
void PredicateMergeRule::_merge_disjunction(const std::shared_ptr<UnionNode>& union_node) const {
  Assert(union_node->set_operation_mode == SetOperationMode::Positions, "Cannot merge union_node into disjunction");
  const auto left_predicate_node = std::dynamic_pointer_cast<PredicateNode>(union_node->left_input());
  const auto right_predicate_node = std::dynamic_pointer_cast<PredicateNode>(union_node->right_input());

  // Step 1: Check if the diamond is simple
  if (!left_predicate_node || !right_predicate_node ||
      left_predicate_node->left_input() != right_predicate_node->left_input() ||
      left_predicate_node->output_count() != 1 || right_predicate_node->output_count() != 1) {
    return;
  }

  // Step 2: Merge diamond
  const auto merged_predicate = or_(left_predicate_node->predicate(), right_predicate_node->predicate());
  const auto merged_predicate_node = PredicateNode::make(merged_predicate);
  lqp_remove_node(left_predicate_node);
  lqp_remove_node(right_predicate_node);
  union_node->set_right_input(nullptr);
  lqp_replace_node(union_node, merged_predicate_node);

  // Step 3: Try to merge other nodes
  // There could be another diamond that just became simple so that it can be merged.
  const auto parent_union_node = std::dynamic_pointer_cast<UnionNode>(merged_predicate_node->outputs().front());
  if (parent_union_node && parent_union_node->set_operation_mode == SetOperationMode::Positions) {
    _merge_disjunction(parent_union_node);
  }

  if (merged_predicate_node->output_count()) {
    // There was no disjunction above that could be merged. But there could be a predicate chain that just became simple
    // so that it can be merged.
    const auto parent_predicate_node =
        std::dynamic_pointer_cast<PredicateNode>(merged_predicate_node->outputs().front());
    if (parent_predicate_node) {
      _merge_conjunction(parent_predicate_node);
    } else {
      _merge_conjunction(merged_predicate_node);
    }
  }
}

/**
 * Merge "simple" predicate chains, which only consist of PredicateNodes
 */
void PredicateMergeRule::_merge_conjunction(const std::shared_ptr<PredicateNode>& predicate_node) const {
  std::vector<std::shared_ptr<PredicateNode>> predicate_nodes;

  // Step 1: Collect other PredicateNodes in predicate chain
  std::shared_ptr<AbstractLQPNode> current_node = predicate_node->left_input();
  while (current_node->type == LQPNodeType::Predicate) {
    // Once a node has multiple outputs, we're not talking about a predicate chain anymore.
    if (current_node->outputs().size() > 1) {
      break;
    }
    predicate_nodes.emplace_back(std::static_pointer_cast<PredicateNode>(current_node));
    current_node = current_node->left_input();
  }

  if (predicate_nodes.empty()) {
    return;
  }

  // Step 2: Merge predicate chain
  auto merged_predicate = predicate_node->predicate();
  for (const auto& current_predicate_node : predicate_nodes) {
    merged_predicate = and_(current_predicate_node->predicate(), merged_predicate);
    lqp_remove_node(current_predicate_node);
  }
  const auto merged_predicate_node = PredicateNode::make(merged_predicate);
  lqp_replace_node(predicate_node, merged_predicate_node);

  // Step 3: Try to merge other nodes
  // There could be a diamond that just became simple so that it can be merged.
  const auto parent_union_node = std::dynamic_pointer_cast<UnionNode>(merged_predicate_node->outputs().front());
  if (parent_union_node) {
    _merge_disjunction(parent_union_node);
  }
}

}  // namespace hyrise

redpajama_set_name: RedPajamaGithub


text:
package org.apache.cassandra.stress.report;

import org.apache.cassandra.stress.StressAction.MeasurementSink;

// a timer - this timer must be used by a single thread, and co-ordinates with other timers by
public final class Timer
{
    private final String opType;
    private final MeasurementSink sink;

    // event timing info
    private long intendedTimeNs;
    private long startTimeNs;

    public Timer(String opType, MeasurementSink sink)
    {
        this.opType = opType;
        this.sink = sink;
    }

    public void stop(long partitionCount, long rowCount, boolean error)
    {
        sink.record(opType, intendedTimeNs, startTimeNs, System.nanoTime(), rowCount, partitionCount, error);
        resetTimes();
    }

    private void resetTimes()
    {
        intendedTimeNs = startTimeNs = 0;
    }

    public void intendedTimeNs(long v)
    {
        intendedTimeNs = v;
    }

    public void start()
    {
        startTimeNs = System.nanoTime();
    }
}


text:
<?php

namespace Phpforce\SoapClient\Result;

class GetUpdatedResult
{
    protected $ids = array();

    protected $latestDateCovered;

    /**
     * @return array
     */
    public function getIds()
    {
        return $this->ids;
    }

    /**
     * @return \DateTime
     */
    public function getLatestDateCovered()
    {
        return $this->latestDateCovered;
    }
}


text:
/**
 * @overview
 *
 * @since 0.4.0
 * @author Stefan Rimaila <stefan@rimaila.fi>
 * @module src/main/schema/rdb
 */
import invariant from 'invariant';

export default function (thinky, type) {
  invariant(thinky && type, 'Thinky instance required');
  const r = thinky.r;

  const MaterialState = thinky.createModel('MaterialStateRecord', {
    timestamp: type.date().default(r.now()),
    fuel: type.number().integer(),
    ammo: type.number().integer(),
    steel: type.number().integer(),
    bauxite: type.number().integer(),
    instantConstruction: type.number().integer().optional(),
    instantRepair: type.number().integer().optional(),
    developmentMaterials: type.number().integer().optional(),
    improvementMaterials: type.number().integer().optional()
  });
  MaterialState.ensureIndex('timestamp');

  const CraftingLog = thinky.createModel('CraftingLog', {
    timestamp: type.date().default(r.now()),
    fuel: type.number().integer(),
    ammo: type.number().integer(),
    steel: type.number().integer(),
    bauxite: type.number().integer(),
    flags: {
      successful: type.boolean().optional(),
      usedDevelopmentMaterials: type.boolean().optional(),
      lsc: type.boolean().optional(),
      instant: type.boolean().optional()
    },
    id: type.number().optional(),
    entityId: type.number().optional()
  });
  CraftingLog.ensureIndex('timestamp');

  // @todo(@stuf): this will need migrating
  const Opponent = thinky.createModel('Opponent', {
    timestamp: type.date().default(r.now()),
    id: type.number().required(),
    level: type.number().required(),
    nickname: type.string(),
    comment: type.string(),
    rank: type.number(),
    counts: {
      ships: [type.number()],
      slotItems: [type.number()]
    },
    fleet: {
      name: type.string(),
      ships: [type.object()]
    }
  });
  Opponent.ensureIndex('timestamp');

  const GameEvent = thinky.createModel('GameEvent', {
    timestamp: type.date().default(r.now()),
    type: type.string()
  });
  GameEvent.ensureIndex('timestamp');

  const SortieResult = thinky.createModel('SortieResult', {
    timestamp: type.date().default(r.now()),
    rank: type.string()
  });
  SortieResult.ensureIndex('timestamp');

  const Drops = thinky.createModel('Drops', {
    timestamp: type.date().default(r.now()),
    id: type.number().required()
  });
  Drops.ensureIndex('timestamp');

  return { MaterialState, CraftingLog, Opponent, GameEvent, Drops, SortieResult };
}


text:
layout: page
title: Callahan Ready Foundation Dinner
date: 2016-05-24
author: Judith Singleton
tags: weekly links, java
status: published
summary: Sed consectetur felis leo, eget feugiat.
banner: images/banner/leisure-03.jpg
booking:
  startDate: 07/02/2017
  endDate: 07/04/2017
  ctyhocn: LBFNEHX
  groupCode: CRFD
published: true
---
In leo magna, sagittis feugiat condimentum sed, tristique vel risus. Nunc eget nisl congue, sagittis mi vitae, lobortis tortor. Aliquam vehicula sollicitudin elit sit amet condimentum. Ut ac arcu pulvinar, varius mauris quis, sodales quam. Quisque fringilla eget tortor nec tincidunt. Ut vel sapien nec neque vestibulum gravida a at velit. Pellentesque commodo interdum purus, ac molestie tortor cursus sit amet. Fusce imperdiet ex vitae maximus gravida. Curabitur eleifend nisl sit amet urna dignissim molestie. Mauris vitae velit at sem mollis tincidunt vel id est. Cras consequat molestie nunc, quis tempus neque condimentum vel. Aenean sed lorem fringilla, blandit sem mollis, placerat ex. Nulla fringilla a nisi id pellentesque. Morbi finibus dui ipsum, eu mollis nunc imperdiet a. Vestibulum scelerisque pretium nisi sed dapibus.

* Etiam maximus turpis ac massa interdum, vitae posuere sapien cursus
* Aliquam vel sem vel ante malesuada feugiat
* Fusce ut ligula interdum, congue nunc in, feugiat elit
* Suspendisse gravida nibh sed sapien sagittis laoreet
* Nam quis purus sed dui elementum molestie
* Vivamus porta libero eu enim semper rhoncus.

Suspendisse neque eros, auctor eget malesuada sed, feugiat nec mauris. Sed hendrerit diam iaculis accumsan consequat. Fusce consectetur elit at lacus vulputate commodo. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Duis vel tellus ut sapien hendrerit interdum. Aenean pretium lorem ut justo fringilla consequat.


text: export { UMAP, UMAPParameters } from './umap';


text:
/**
 * This code is part of the Fungus library (http://fungusgames.com) maintained by Chris Gregan (http://twitter.com/gofungus).
 * It is released for free under the MIT open source license (https://github.com/snozbot/fungus/blob/master/LICENSE)
 */

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Events;
using System;
using System.Collections;
using System.Collections.Generic;

namespace Fungus
{

    [CommandInfo("Narrative",
                 "Portrait",
                 "Controls a character portrait. ")]
    public class Portrait : ControlWithDisplay<DisplayType>
    {
        [Tooltip("Stage to display portrait on")]
        public Stage stage;

        [Tooltip("Character to display")]
        public Character character;

        [Tooltip("Character to swap with")]
        public Character replacedCharacter;

        [Tooltip("Portrait to display")]
        public Sprite portrait;

        [Tooltip("Move the portrait from/to this offset position")]
        public PositionOffset offset;

        [Tooltip("Move the portrait from this position")]
        public RectTransform fromPosition;

        [Tooltip("Move the portrait to this positoin")]
        public RectTransform toPosition;

        [Tooltip("Direction character is facing")]
        public FacingDirection facing;

        [Tooltip("Use Default Settings")]
        public bool useDefaultSettings = true;

        [Tooltip("Fade Duration")]
        public float fadeDuration = 0.5f;

        [Tooltip("Movement Duration")]
        public float moveDuration = 1f;

        [Tooltip("Shift Offset")]
        public Vector2 shiftOffset;

        [Tooltip("Move")]
        public bool move;

        [Tooltip("Start from offset")]
        public bool shiftIntoPlace;

        [Tooltip("Wait until the tween has finished before executing the next command")]
        public bool waitUntilFinished = false;

        public override void OnEnter()
        {
            // Selected "use default Portrait Stage"
            if (stage == null)
            {
                // If no default specified, try to get any portrait stage in the scene
                stage = FindObjectOfType<Stage>();
                // If portrait stage does not exist, do nothing
                if (stage == null)
                {
                    Continue();
                    return;
                }

            }

            // If no display specified, do nothing
            if (IsDisplayNone(display))
            {
                Continue();
                return;
            }

            PortraitOptions options = new PortraitOptions();

            options.character = character;
            options.replacedCharacter = replacedCharacter;
            options.portrait = portrait;
            options.display = display;
            options.offset = offset;
            options.fromPosition = fromPosition;
            options.toPosition = toPosition;
            options.facing = facing;
            options.useDefaultSettings = useDefaultSettings;
            options.fadeDuration = fadeDuration;
            options.moveDuration = moveDuration;
            options.shiftOffset = shiftOffset;
            options.move = move;
            options.shiftIntoPlace = shiftIntoPlace;
            options.waitUntilFinished = waitUntilFinished;

            stage.RunPortraitCommand(options, Continue);

        }

        public override string GetSummary()
        {
            if (display == DisplayType.None && character == null)
            {
                return "Error: No character or display selected";
            }
            else if (display == DisplayType.None)
            {
                return "Error: No display selected";
            }
            else if (character == null)
            {
                return "Error: No character selected";
            }

            string displaySummary = "";
            string characterSummary = "";
            string fromPositionSummary = "";
            string toPositionSummary = "";
            string stageSummary = "";
            string portraitSummary = "";
            string facingSummary = "";

            displaySummary = StringFormatter.SplitCamelCase(display.ToString());

            if (display == DisplayType.Replace)
            {
                if (replacedCharacter != null)
                {
                    displaySummary += " \"" + replacedCharacter.name + "\" with";
                }
            }

            characterSummary = character.name;
            if (stage != null)
            {
                stageSummary = " on \"" + stage.name + "\"";
            }

            if (portrait != null)
            {
                portraitSummary = " " + portrait.name;
            }

            if (shiftIntoPlace)
            {
                if (offset != 0)
                {
                    fromPositionSummary = offset.ToString();
                    fromPositionSummary = " from " + "\"" + fromPositionSummary + "\"";
                }
            }
            else if (fromPosition != null)
            {
                fromPositionSummary = " from " + "\"" + fromPosition.name + "\"";
            }

            if (toPosition != null)
            {
                string toPositionPrefixSummary = "";
                if (move)
                {
                    toPositionPrefixSummary = " to ";
                }
                else
                {
                    toPositionPrefixSummary = " at ";
                }

                toPositionSummary = toPositionPrefixSummary + "\"" + toPosition.name + "\"";
            }

            if (facing != FacingDirection.None)
            {
                if (facing == FacingDirection.Left)
                {
                    facingSummary = "<--";
                }
                if (facing == FacingDirection.Right)
                {
                    facingSummary = "-->";
                }

                facingSummary = " facing \"" + facingSummary + "\"";
            }

            return displaySummary + " \"" + characterSummary + portraitSummary + "\"" + stageSummary + facingSummary + fromPositionSummary + toPositionSummary;
        }

        public override Color GetButtonColor()
        {
            return new Color32(230, 200, 250, 255);
        }

        public override void OnCommandAdded(Block parentBlock)
        {
            //Default to display type: show
            display = DisplayType.Show;
        }
    }
}


text:
package main

import (
	"encoding/json"
	"flag"
	"fmt"
	"net"
	"net/http"
	"net/url"
	"strings"
	"sync"
	"time"

	"github.com/antonlindstrom/mesos_stats"
	"github.com/prometheus/client_golang/prometheus"
	"github.com/prometheus/log"
)

const concurrentFetch = 100

// Commandline flags.
var (
	addr                 = flag.String("web.listen-address", ":9105", "Address to listen on for web interface and telemetry")
	autoDiscoverInterval = flag.Duration("exporter.discover-interval", 10*time.Minute, "Interval at which to update available slaves from a Mesos Master. Only used if exporter.scrape-mode=discover.")
	queryURL             = flag.String("exporter.url", "", "The URL of a Mesos Slave, if mode=slave. The URL of a Mesos Master, if mode=discover or mode=master.")
	metricsPath          = flag.String("web.telemetry-path", "/metrics", "Path under which to expose metrics")
	scrapeInterval       = flag.Duration("exporter.interval", 60*time.Second, "Scrape interval duration")
	scrapeMode           = flag.String("exporter.scrape-mode", "", "The mode in which to run the exporter: 'discover', 'master' or 'slave'.")
)

var (
	variableLabels = []string{"task", "slave", "framework_id", "framework_name", "task_name"}

	cpuLimitDesc = prometheus.NewDesc(
		"mesos_task_cpu_limit",
		"Fractional CPU limit.",
		variableLabels, nil,
	)
	cpuSysDesc = prometheus.NewDesc(
		"mesos_task_cpu_system_seconds_total",
		"Cumulative system CPU time in seconds.",
		variableLabels, nil,
	)
	cpuUsrDesc = prometheus.NewDesc(
		"mesos_task_cpu_user_seconds_total",
		"Cumulative user CPU time in seconds.",
		variableLabels, nil,
	)
	memLimitDesc = prometheus.NewDesc(
		"mesos_task_memory_limit_bytes",
		"Task memory limit in bytes.",
		variableLabels, nil,
	)
	memRssDesc = prometheus.NewDesc(
		"mesos_task_memory_rss_bytes",
		"Task memory RSS usage in bytes.",
		variableLabels, nil,
	)

	frameworkLabels = []string{"id", "name"}

	frameworkResourcesUsedCPUs = prometheus.NewDesc(
		"mesos_framework_resources_used_cpus",
		"Fractional CPUs used by a framework.",
		frameworkLabels, nil,
	)

	frameworkResourcesUsedDisk = prometheus.NewDesc(
		"mesos_framework_resources_used_disk_bytes",
		"Disk space used by a framework.",
		frameworkLabels, nil,
	)

	frameworkResourcesUsedMemory = prometheus.NewDesc(
		"mesos_framework_resources_used_memory_bytes",
		"Memory used by a framework.",
		frameworkLabels, nil,
	)

	masterMetricsLabels = []string{"host"}

	masterMetrics = []snapshotMetric{
		snapshotMetric{
			desc: prometheus.NewDesc(
				"mesos_master_tasks_error_total",
				"Number of tasks that have errored.",
				masterMetricsLabels, nil,
			),
			snapshotKey: "master/tasks_error",
			valueType:   prometheus.CounterValue,
		},
		snapshotMetric{
			desc: prometheus.NewDesc(
				"mesos_master_tasks_failed_total",
				"Number of tasks that have failed.",
				masterMetricsLabels, nil,
			),
			snapshotKey: "master/tasks_failed",
			valueType:   prometheus.CounterValue,
		},
		snapshotMetric{
			desc: prometheus.NewDesc(
				"mesos_master_tasks_finished_total",
				"Number of tasks that have finished.",
				masterMetricsLabels, nil,
			),
			snapshotKey: "master/tasks_finished",
			valueType:   prometheus.CounterValue,
		},
		snapshotMetric{
			desc: prometheus.NewDesc(
				"mesos_master_tasks_killed_total",
				"Number of tasks that got killed.",
				masterMetricsLabels, nil,
			),
			snapshotKey: "master/tasks_killed",
			valueType:   prometheus.CounterValue,
		},
		snapshotMetric{
			desc: prometheus.NewDesc(
				"mesos_master_tasks_lost_total",
				"Number of tasks that got lost.",
				masterMetricsLabels, nil,
			),
			snapshotKey: "master/tasks_lost",
			valueType:   prometheus.CounterValue,
		},
		snapshotMetric{
			desc: prometheus.NewDesc(
				"mesos_master_tasks_running",
				"Number of tasks that are running.",
				masterMetricsLabels, nil,
			),
			snapshotKey: "master/tasks_running",
			valueType:   prometheus.GaugeValue,
		},
		snapshotMetric{
			desc: prometheus.NewDesc(
				"mesos_master_tasks_staging",
				"Number of tasks that are staging.",
				masterMetricsLabels, nil,
			),
			snapshotKey: "master/tasks_staging",
			valueType:   prometheus.GaugeValue,
		},
		snapshotMetric{
			desc: prometheus.NewDesc(
				"mesos_master_tasks_starting",
				"Number of tasks that are starting.",
				masterMetricsLabels, nil,
			),
			snapshotKey: "master/tasks_starting",
			valueType:   prometheus.GaugeValue,
		},
	}

	slaveMetricsLabels = []string{"host"}

	slaveMetrics = []snapshotMetric{
		snapshotMetric{
			desc: prometheus.NewDesc(
				"mesos_slave_cpus",
				"CPUs advertised by a Mesos Slave.",
				slaveMetricsLabels, nil,
			),
			snapshotKey: "slave/cpus_total",
			valueType:   prometheus.GaugeValue,
		},
		snapshotMetric{
			convertFn: megabytesToBytes,
			desc: prometheus.NewDesc(
				"mesos_slave_disk_bytes",
				"Disk space advertised by a Mesos Slave.",
				slaveMetricsLabels, nil,
			),
			snapshotKey: "slave/disk_total",
			valueType:   prometheus.GaugeValue,
		},
		snapshotMetric{
			convertFn: megabytesToBytes,
			desc: prometheus.NewDesc(
				"mesos_slave_memory_bytes",
				"Memory advertised by a Mesos Slave.",
				slaveMetricsLabels, nil,
			),
			snapshotKey: "slave/mem_total",
			valueType:   prometheus.GaugeValue,
		},
	}
)

var httpClient = http.Client{
	Timeout: 5 * time.Second,
}

type exporterOpts struct {
	autoDiscoverInterval time.Duration
	interval             time.Duration
	mode                 string
	queryURL             string
}

type exporterTaskInfo struct {
	FrameworkName string
	TaskName      string
}

type framework struct {
	ID            string
	Name          string
	UsedResources *resources `json:"used_resources"`
}

type masterState struct {
	Frameworks []*framework
	Hostname   string
	Slaves     []*slave
}

type snapshotMetric struct {
	convertFn   func(float64) float64
	desc        *prometheus.Desc
	snapshotKey string
	valueType   prometheus.ValueType
}

type metricsSnapshot map[string]float64

type periodicExporter struct {
	sync.RWMutex
	errors   *prometheus.CounterVec
	metrics  []prometheus.Metric
	opts     *exporterOpts
	queryURL *url.URL
	slaves   struct {
		sync.Mutex
		urls []string
	}
}

type resources struct {
	CPUs float64
	Disk float64
	Mem  float64
}

type slave struct {
	Active   bool
	Hostname string
	PID      string
}

type slaveState struct {
	Frameworks []*slaveStateFramework
}

type slaveStateExecutor struct {
	Tasks []*slaveStateTask
}

type slaveStateFramework struct {
	Name      string
	Executors []*slaveStateExecutor
}

type slaveStateTask struct {
	FrameworkID string `json:"framework_id"`
	ID          string
	Name        string
}

func newMesosExporter(opts *exporterOpts) *periodicExporter {
	e := &periodicExporter{
		errors: prometheus.NewCounterVec(
			prometheus.CounterOpts{
				Namespace: "mesos_exporter",
				Name:      "slave_scrape_errors_total",
				Help:      "Current total scrape errors",
			},
			[]string{"slave"},
		),
		opts: opts,
	}

	if opts.queryURL == "" {
		log.Fatal("Flag '-exporter.url' not set")
	}

	switch opts.mode {
	case "discover":
		log.Info("starting mesos_exporter in scrape mode 'discover'")

		e.queryURL = parseMasterURL(opts.queryURL)

		// Update nr. of mesos slaves.
		e.updateSlaves()
		go runEvery(e.updateSlaves, e.opts.autoDiscoverInterval)

		// Fetch slave metrics every interval.
		go runEvery(e.scrapeSlaves, e.opts.interval)
	case "master":
		log.Info("starting mesos_exporter in scrape mode 'master'")
		e.queryURL = parseMasterURL(opts.queryURL)
	case "slave":
		log.Info("starting mesos_exporter in scrape mode 'slave'")
		e.slaves.urls = []string{opts.queryURL}
	default:
		log.Fatalf("Invalid value '%s' of flag '-exporter.mode' - must be one of 'discover', 'master' or 'slave'", opts.mode)
	}

	return e
}

func (e *periodicExporter) Describe(ch chan<- *prometheus.Desc) {
	switch e.opts.mode {
	case "master":
		e.scrapeMaster()
	case "slave":
		e.scrapeSlaves()
	}

	e.rLockMetrics(func() {
		for _, m := range e.metrics {
			ch <- m.Desc()
		}
	})
	e.errors.MetricVec.Describe(ch)
}

func (e *periodicExporter) Collect(ch chan<- prometheus.Metric) {
	switch e.opts.mode {
	case "master":
		e.scrapeMaster()
	case "slave":
		e.scrapeSlaves()
	}

	e.rLockMetrics(func() {
		for _, m := range e.metrics {
			ch <- m
		}
	})
	e.errors.MetricVec.Collect(ch)
}

func (e *periodicExporter) fetch(urlChan <-chan string, metricsChan chan<- prometheus.Metric, wg *sync.WaitGroup) {
	defer wg.Done()

	for u := range urlChan {
		u, err := url.Parse(u)
		if err != nil {
			log.Warn("could not parse slave URL: ", err)
			continue
		}

		host, _, err := net.SplitHostPort(u.Host)
		if err != nil {
			log.Warn("could not parse network address: ", err)
			continue
		}

		taskInfo := map[string]exporterTaskInfo{}
		var state slaveState
		stateURL := fmt.Sprintf("%s/state.json", u)

		err = getJSON(&state, stateURL)
		if err != nil {
			log.Warn(err)
			e.errors.WithLabelValues(host).Inc()
			continue
		}

		for _, fw := range state.Frameworks {
			for _, ex := range fw.Executors {
				for _, t := range ex.Tasks {
					taskInfo[t.ID] = exporterTaskInfo{fw.Name, t.Name}
				}
			}
		}

		monitorURL := fmt.Sprintf("%s/monitor/statistics.json", u)
		var stats []mesos_stats.Monitor

		err = getJSON(&stats, monitorURL)
		if err != nil {
			log.Warn(err)
			e.errors.WithLabelValues(host).Inc()
			continue
		}

		for _, stat := range stats {
			tinfo, ok := taskInfo[stat.Source]
			if !ok {
				continue
			}

			metricsChan <- prometheus.MustNewConstMetric(
				cpuLimitDesc,
				prometheus.GaugeValue,
				float64(stat.Statistics.CpusLimit),
				stat.Source, host, stat.FrameworkId, tinfo.FrameworkName, tinfo.TaskName,
			)
			metricsChan <- prometheus.MustNewConstMetric(
				cpuSysDesc,
				prometheus.CounterValue,
				float64(stat.Statistics.CpusSystemTimeSecs),
				stat.Source, host, stat.FrameworkId, tinfo.FrameworkName, tinfo.TaskName,
			)
			metricsChan <- prometheus.MustNewConstMetric(
				cpuUsrDesc,
				prometheus.CounterValue,
				float64(stat.Statistics.CpusUserTimeSecs),
				stat.Source, host, stat.FrameworkId, tinfo.FrameworkName, tinfo.TaskName,
			)
			metricsChan <- prometheus.MustNewConstMetric(
				memLimitDesc,
				prometheus.GaugeValue,
				float64(stat.Statistics.MemLimitBytes),
				stat.Source, host, stat.FrameworkId, tinfo.FrameworkName, tinfo.TaskName,
			)
			metricsChan <- prometheus.MustNewConstMetric(
				memRssDesc,
				prometheus.GaugeValue,
				float64(stat.Statistics.MemRssBytes),
				stat.Source, host, stat.FrameworkId, tinfo.FrameworkName, tinfo.TaskName,
			)
		}

		metricsSnapshotURL := fmt.Sprintf("%s/metrics/snapshot", u)
		var ms metricsSnapshot

		err = getJSON(&ms, metricsSnapshotURL)
		if err != nil {
			log.Warn(err)
			e.errors.WithLabelValues(host).Inc()
			continue
		}

		for _, mm := range slaveMetrics {
			metricValue, ok := ms[mm.snapshotKey]
			if !ok {
				continue
			}

			if mm.convertFn != nil {
				metricValue = mm.convertFn(metricValue)
			}

			metricsChan <- prometheus.MustNewConstMetric(
				mm.desc, mm.valueType, metricValue, host,
			)
		}
	}
}

func (e *periodicExporter) rLockMetrics(f func()) {
	e.RLock()
	defer e.RUnlock()
	f()
}

func (e *periodicExporter) setMetrics(ch chan prometheus.Metric) {
	metrics := []prometheus.Metric{}
	for metric := range ch {
		metrics = append(metrics, metric)
	}

	e.Lock()
	e.metrics = metrics
	e.Unlock()
}

func (e *periodicExporter) scrapeMaster() {
	stateURL := fmt.Sprintf("%s://%s/master/state.json", e.queryURL.Scheme, e.queryURL.Host)

	log.Debugf("Scraping master at %s", stateURL)

	var state masterState

	err := getJSON(&state, stateURL)
	if err != nil {
		log.Warn(err)
		return
	}

	metrics := []prometheus.Metric{}

	for _, fw := range state.Frameworks {
		metrics = append(metrics, prometheus.MustNewConstMetric(
			frameworkResourcesUsedCPUs,
			prometheus.GaugeValue,
			fw.UsedResources.CPUs,
			fw.ID, fw.Name,
		))
		metrics = append(metrics, prometheus.MustNewConstMetric(
			frameworkResourcesUsedDisk,
			prometheus.GaugeValue,
			megabytesToBytes(fw.UsedResources.Disk),
			fw.ID, fw.Name,
		))
		metrics = append(metrics, prometheus.MustNewConstMetric(
			frameworkResourcesUsedMemory,
			prometheus.GaugeValue,
			megabytesToBytes(fw.UsedResources.Mem),
			fw.ID, fw.Name,
		))
	}

	snapshotURL := fmt.Sprintf("%s://%s/metrics/snapshot", e.queryURL.Scheme, e.queryURL.Host)

	var ms metricsSnapshot

	err = getJSON(&ms, snapshotURL)
	if err != nil {
		log.Warn(err)
		return
	}

	for _, mm := range masterMetrics {
		metricValue, ok := ms[mm.snapshotKey]
		if !ok {
			continue
		}

		if mm.convertFn != nil {
			metricValue = mm.convertFn(metricValue)
		}

		metrics = append(metrics, prometheus.MustNewConstMetric(
			mm.desc, mm.valueType, metricValue, state.Hostname,
		))
	}

	e.Lock()
	e.metrics = metrics
	e.Unlock()
}

func (e *periodicExporter) scrapeSlaves() {
	e.slaves.Lock()
	urls := make([]string, len(e.slaves.urls))
	copy(urls, e.slaves.urls)
	e.slaves.Unlock()

	urlCount := len(urls)
	log.Debugf("active slaves: %d", urlCount)

	urlChan := make(chan string)
	metricsChan := make(chan prometheus.Metric)
	go e.setMetrics(metricsChan)

	poolSize := concurrentFetch
	if urlCount < concurrentFetch {
		poolSize = urlCount
	}

	log.Debugf("creating fetch pool of size %d", poolSize)

	var wg sync.WaitGroup
	wg.Add(poolSize)
	for i := 0; i < poolSize; i++ {
		go e.fetch(urlChan, metricsChan, &wg)
	}

	for _, url := range urls {
		urlChan <- url
	}
	close(urlChan)

	wg.Wait()
	close(metricsChan)
}

func (e *periodicExporter) updateSlaves() {
	log.Debug("discovering slaves...")

	// This will redirect us to the elected mesos master
	redirectURL := fmt.Sprintf("%s://%s/master/redirect", e.queryURL.Scheme, e.queryURL.Host)
	rReq, err := http.NewRequest("GET", redirectURL, nil)
	if err != nil {
		panic(err)
	}

	tr := http.Transport{
		DisableKeepAlives: true,
	}
	rresp, err := tr.RoundTrip(rReq)
	if err != nil {
		log.Warn(err)
		return
	}
	defer rresp.Body.Close()

	// This will/should return http://master.ip:5050
	masterLoc := rresp.Header.Get("Location")
	if masterLoc == "" {
		log.Warnf("%d response missing Location header", rresp.StatusCode)
		return
	}

	log.Debugf("current elected master at: %s", masterLoc)

	// Starting from 0.23.0, a Mesos Master does not set the scheme in the "Location" header.
	// Use the scheme from the master URL in this case.
	var stateURL string
	if strings.HasPrefix(masterLoc, "http") {
		stateURL = fmt.Sprintf("%s/master/state.json", masterLoc)
	} else {
		stateURL = fmt.Sprintf("%s:%s/master/state.json", e.queryURL.Scheme, masterLoc)
	}

	var state masterState

	// Find all active slaves
	err = getJSON(&state, stateURL)
	if err != nil {
		log.Warn(err)
		return
	}

	var slaveURLs []string
	for _, slave := range state.Slaves {
		if slave.Active {
			// Extract slave port from pid
			_, port, err := net.SplitHostPort(slave.PID)
			if err != nil {
				port = "5051"
			}
			url := fmt.Sprintf("http://%s:%s", slave.Hostname, port)

			slaveURLs = append(slaveURLs, url)
		}
	}

	log.Debugf("%d slaves discovered", len(slaveURLs))

	e.slaves.Lock()
	e.slaves.urls = slaveURLs
	e.slaves.Unlock()
}

func getJSON(data interface{}, url string) error {
	resp, err := http.Get(url)
	if err != nil {
		return err
	}
	defer resp.Body.Close()

	if err := json.NewDecoder(resp.Body).Decode(data); err != nil {
		return err
	}

	return nil
}

func megabytesToBytes(v float64) float64 { return v * 1024 * 1024 }

func parseMasterURL(masterURL string) *url.URL {
	parsedMasterURL, err := url.Parse(masterURL)
	if err != nil {
		log.Fatalf("unable to parse master URL '%s': ", masterURL, err)
	}
	if strings.HasPrefix(parsedMasterURL.Scheme, "http") == false {
		log.Fatalf("invalid scheme '%s' in master url - use 'http' or 'https'", parsedMasterURL.Scheme)
	}

	return parsedMasterURL
}

func runEvery(f func(), interval time.Duration) {
	for _ = range time.NewTicker(interval).C {
		f()
	}
}

func main() {
	flag.Parse()

	opts := &exporterOpts{
		autoDiscoverInterval: *autoDiscoverInterval,
		interval:             *scrapeInterval,
		mode:                 *scrapeMode,
		queryURL:             strings.TrimRight(*queryURL, "/"),
	}
	exporter := newMesosExporter(opts)
	prometheus.MustRegister(exporter)

	http.Handle(*metricsPath, prometheus.Handler())
	http.HandleFunc("/status", func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprintf(w, "OK")
	})
	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		http.Redirect(w, r, *metricsPath, http.StatusMovedPermanently)
	})

	log.Info("starting mesos_exporter on ", *addr)

	log.Fatal(http.ListenAndServe(*addr, nil))
}


text:
package cloud;

import cloud.resources.HardwareInLocation;
import cloud.resources.ImageInLocation;
import cloud.resources.LocationInCloud;
import cloud.resources.VirtualMachineInLocation;
import com.google.common.base.Function;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.common.collect.Sets;
import de.uniulm.omi.cloudiator.sword.api.service.ComputeService;
import de.uniulm.omi.cloudiator.sword.api.service.DiscoveryService;
import models.CloudCredential;
import models.service.ModelService;

import javax.annotation.Nullable;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import static com.google.common.base.Preconditions.checkState;

/**
 * Created by daniel on 19.06.15.
 */
public class BaseComputeServiceRegistry implements ComputeServiceRegistry {

    private final ComputeServiceFactory computeServiceFactory;
    private final ModelService<CloudCredential> cloudCredentialModelService;

    BaseComputeServiceRegistry(ComputeServiceFactory computeServiceFactory,
        ModelService<CloudCredential> cloudCredentialModelService) {
        this.computeServiceFactory = computeServiceFactory;
        this.cloudCredentialModelService = cloudCredentialModelService;
    }

    @Override
    public Set<ComputeService<HardwareInLocation, ImageInLocation, LocationInCloud, VirtualMachineInLocation>> getComputeServices() {
        return getComputeServices(cloudCredentialModelService.getAll());
    }

    @Override
    public Iterable<DiscoveryService<HardwareInLocation, ImageInLocation, LocationInCloud, VirtualMachineInLocation>> getDiscoveryServices() {
        Set<DiscoveryService<HardwareInLocation, ImageInLocation, LocationInCloud, VirtualMachineInLocation>>
            discoveryServices = Sets.newHashSet();
        for (ComputeService<HardwareInLocation, ImageInLocation, LocationInCloud, VirtualMachineInLocation> computeService : getComputeServices()) {
            discoveryServices.add(computeService.discoveryService());
        }
        return discoveryServices;
    }

    @Override
    public ComputeService<HardwareInLocation, ImageInLocation, LocationInCloud, VirtualMachineInLocation> getComputeService(
        String cloudCredentialUuid) {
        Set<ComputeService<HardwareInLocation, ImageInLocation, LocationInCloud, VirtualMachineInLocation>>
            computeServices = getComputeServices(
            Collections.singletonList(cloudCredentialModelService.getByUuid(cloudCredentialUuid)));
        checkState(computeServices.size() == 1);
        return computeServices.iterator().next();
    }

    @Override
    public Set<ComputeService<HardwareInLocation, ImageInLocation, LocationInCloud, VirtualMachineInLocation>> getComputeServices(
        List<CloudCredential> cloudCredentials) {
        return ImmutableSet.copyOf(Iterables.transform(cloudCredentials,
            new Function<CloudCredential, ComputeService<HardwareInLocation, ImageInLocation, LocationInCloud, VirtualMachineInLocation>>() {
                @Nullable @Override
                public ComputeService<HardwareInLocation, ImageInLocation, LocationInCloud, VirtualMachineInLocation> apply(
                    CloudCredential cloudCredential) {
                    return computeServiceFactory.computeService(cloudCredential);
                }
            }));
    }

    @Override
    public Iterator<ComputeService<HardwareInLocation, ImageInLocation, LocationInCloud, VirtualMachineInLocation>> iterator() {
        return ImmutableSet.copyOf(getComputeServices()).iterator();
    }
}


text: Qlik Sense Basic Extension Example


text:
require.ensure([], function(require) {
require("./66.async.js");
require("./132.async.js");
require("./264.async.js");
require("./528.async.js");
});
module.exports = 529;


text:
bin=`dirname "${BASH_SOURCE-$0}"`
bin=`cd "$bin"; pwd`

DEFAULT_LIBEXEC_DIR="$bin"/../libexec
HADOOP_LIBEXEC_DIR=${HADOOP_LIBEXEC_DIR:-$DEFAULT_LIBEXEC_DIR}
. $HADOOP_LIBEXEC_DIR/hdfs-config.sh

# Start balancer daemon.

"$HADOOP_PREFIX"/bin/hadoop-daemon.sh --config $HADOOP_CONF_DIR --script "$bin"/hdfs start balancer $@


text:
import { request } from '../utils'
import config from '../config'

export function findSpaceKey({ ownerName, projectName }) {
  return request.get(`/ws/find/coding/${ownerName}/${projectName}`, null,
    { headers: { Accept: '*/*' } }
  ).then(res => res.data);
}

export function getDefaultWorkspace() {
  return request.get('/workspaces?default');
}

export function setupWorkspace() {
  return request.post(`/workspaces/${config.spaceKey}`).catch(err => err.response.data);
}

export function setupWorkspace2() {
  return request.post(`/workspaces/${config.spaceKey}`).catch(err => err.response.data);
}

export function getUserProfile() {
  // @fixme: initialize2 requires removing .then(res => res.data)
  return request.get('/user/current', null,
    { headers: { Accept: '*/*' } }
  ).then(res => res);
}

export function fetchRequestState() {
  return request.get(`/workspaces/${config.spaceKey}/collaborator/request`);
}

// Switch back to old version
export function switchVersion() {
  return request.put('/versions')
  .then((res) => {
    window.location.reload();
  });
}

export function fetchProjectType() {
  return request.get(`/ws/${config.spaceKey}/type`)
  .then((res) => {
    config.estimatedMap.replace(res);
  });
}

export function getWorkspaceList() {
  return request.get('/workspaces?page=0&size=5&sort=lastModifiedDate,desc');
}

export function getHardwares() {
  return request.get('/workspaces/hardwares');
}

export function postAdjust(options) {
  return request.post(`/workspaces/${config.spaceKey}/adjust`, options);
}

export function getStaticServingToken() {
  return request.get(`/workspaces/${config.spaceKey}/static_serving_token`);
}