<commit_before>(function () {
    var DashboardCtrl = function ($scope, $routeParams, $http, $timeout, Dashboard) {
        $scope.refreshEnabled = false;
        $scope.refreshRate = 60;

        var refreshDashboard = function() {
            $scope.dashboard = Dashboard.get({slug: $routeParams.dashboardSlug}, function(dashboard) {
                $scope.$parent.pageTitle = dashboard.name;
            });

            autoRefresh();
        };

        var autoRefresh = function() {
            if ($scope.refreshEnabled) {
                $timeout(refreshDashboard, $scope.refreshRate);
            };
        }

        $scope.triggerRefresh = function(){
            $scope.refreshEnabled = !$scope.refreshEnabled;

            if ($scope.refreshEnabled) {
                var refreshRate = _.min(_.flatten($scope.dashboard.widgets), function(widget) {
                    return widget.visualization.query.ttl;
                }).visualization.query.ttl;

                $scope.refreshRate = _.max([120, refreshRate * 2])*1000;

                autoRefresh();
            }
        };

        refreshDashboard();
    };

    var WidgetCtrl = function ($scope, $http, $location, Query) {
        $scope.deleteWidget = function() {
            if (!confirm('Are you sure you want to remove "' + $scope.widget.visualization.name + '" from the dashboard?')) {
                return;
            }

            $http.delete('/api/widgets/' + $scope.widget.id).success(function() {
                $scope.dashboard.widgets = _.map($scope.dashboard.widgets, function(row) {
                    return _.filter(row, function(widget) {
                        return widget.id != $scope.widget.id;
                    })
                });
            });
        };

        $scope.open = function(query, visualization) {
            $location.path('/queries/' + query.id);
            $location.hash(visualization.id);
        }

        $scope.query = new Query($scope.widget.visualization.query);
        $scope.queryResult = $scope.query.getQueryResult();

        $scope.updateTime = (new Date($scope.queryResult.getUpdatedAt())).toISOString();
        $scope.nextUpdateTime = moment(new Date(($scope.query.updated_at + $scope.query.ttl + $scope.query.runtime + 300) * 1000)).fromNow();

        $scope.updateTime = '';
    }

    var QueryFiddleCtrl = function ($scope, $window, $location, $routeParams, $http, $location, growl, notifications, Query, Visualization) {
        var DEFAULT_TAB = 'table';
        var pristineHash = null;
        var leavingPageText = "You will lose your changes if you leave";

        $scope.dirty = undefined;
        $scope.newVisualization = undefined;

        $window.onbeforeunload = function(){
            if (currentUser.canEdit($scope.query) && $scope.dirty) {
                return leavingPageText;
            }
        }

        Mousetrap.bindGlobal("meta+s", function(e) {
            e.preventDefault();

            if (currentUser.canEdit($scope.query)) {
                $scope.saveQuery();
            }
        });

        $scope.$on('$locationChangeStart', function(event, next, current) {
            if (next.split("#")[0] == current.split("#")[0]) {
                return;
            }

            if (!currentUser.canEdit($scope.query)) {
                return;
            }

            if($scope.dirty &&
                !confirm(leavingPageText + "\n\nAre you sure you want to leave this page?")) {
                event.preventDefault();
            } else {
                Mousetrap.unbind("meta+s");
            }
        });

        $scope.$parent.pageTitle = "Query Fiddle";

        $scope.$watch(function() {return $location.hash()}, function(hash) {
            $scope.selectedTab = hash || DEFAULT_TAB;
        });

        $scope.lockButton = function (lock) {
            $scope.queryExecuting = lock;
        };

        $scope.formatQuery = function() {
            $scope.editorOptions.readOnly = 'nocursor';

            $http.post('/api/queries/format', {'query': $scope.query.query}).success(function(response) {
                $scope.query.query = response;
                $scope.editorOptions.readOnly = false;
            })
        }

        $scope.saveQuery = function (duplicate, oldId) {
            if (!oldId) {
                oldId = $scope.query.id;
            }
            delete $scope.query.latest_query_data;
            $scope.query.$save(function (q) {
                pristineHash = q.getHash();
                $scope.dirty = false;

                if (duplicate) {
                    growl.addInfoMessage("Query duplicated.", {ttl: 2000});
                } else{
                    growl.addSuccessMessage("Query saved.", {ttl: 2000});
                }

                if (oldId != q.id) {
                    if (oldId == undefined) {
                        $location.path($location.path().replace('new', q.id)).replace();
                    } else {
                        // TODO: replace this with a safer method
                        $location.path($location.path().replace(oldId, q.id)).replace();

                        // Reset visualizations tab to table after duplicating a query:
                        $location.hash('table');
                    }
                }
            }, function(httpResponse) {
                growl.addErrorMessage("Query could not be saved");
            });
        };

        $scope.duplicateQuery = function () {
            var oldId = $scope.query.id;
            $scope.query.id = null;
            $scope.query.ttl = -1;

            $scope.saveQuery(true, oldId);
        };

        // Query Editor:
        $scope.editorOptions = {
            mode: 'text/x-sql',
            lineWrapping: true,
            lineNumbers: true,
            readOnly: false,
            matchBrackets: true,
            autoCloseBrackets: true
        };

        $scope.refreshOptions = [
            {value: -1, name: 'No Refresh'},
        ]

        _.each(_.range(1, 13), function(i) {
            $scope.refreshOptions.push({value: i*3600, name: 'Every ' + i + 'h'});
        })<commit_msg>Option to set every minute refresh rate.<commit_after>(function () {
    var DashboardCtrl = function ($scope, $routeParams, $http, $timeout, Dashboard) {
        $scope.refreshEnabled = false;
        $scope.refreshRate = 60;

        var refreshDashboard = function() {
            $scope.dashboard = Dashboard.get({slug: $routeParams.dashboardSlug}, function(dashboard) {
                $scope.$parent.pageTitle = dashboard.name;
            });

            autoRefresh();
        };

        var autoRefresh = function() {
            if ($scope.refreshEnabled) {
                $timeout(refreshDashboard, $scope.refreshRate);
            };
        }

        $scope.triggerRefresh = function(){
            $scope.refreshEnabled = !$scope.refreshEnabled;

            if ($scope.refreshEnabled) {
                var refreshRate = _.min(_.flatten($scope.dashboard.widgets), function(widget) {
                    return widget.visualization.query.ttl;
                }).visualization.query.ttl;

                $scope.refreshRate = _.max([120, refreshRate * 2])*1000;

                autoRefresh();
            }
        };

        refreshDashboard();
    };

    var WidgetCtrl = function ($scope, $http, $location, Query) {
        $scope.deleteWidget = function() {
            if (!confirm('Are you sure you want to remove "' + $scope.widget.visualization.name + '" from the dashboard?')) {
                return;
            }

            $http.delete('/api/widgets/' + $scope.widget.id).success(function() {
                $scope.dashboard.widgets = _.map($scope.dashboard.widgets, function(row) {
                    return _.filter(row, function(widget) {
                        return widget.id != $scope.widget.id;
                    })
                });
            });
        };

        $scope.open = function(query, visualization) {
            $location.path('/queries/' + query.id);
            $location.hash(visualization.id);
        }

        $scope.query = new Query($scope.widget.visualization.query);
        $scope.queryResult = $scope.query.getQueryResult();

        $scope.updateTime = (new Date($scope.queryResult.getUpdatedAt())).toISOString();
        $scope.nextUpdateTime = moment(new Date(($scope.query.updated_at + $scope.query.ttl + $scope.query.runtime + 300) * 1000)).fromNow();

        $scope.updateTime = '';
    }

    var QueryFiddleCtrl = function ($scope, $window, $location, $routeParams, $http, $location, growl, notifications, Query, Visualization) {
        var DEFAULT_TAB = 'table';
        var pristineHash = null;
        var leavingPageText = "You will lose your changes if you leave";

        $scope.dirty = undefined;
        $scope.newVisualization = undefined;

        $window.onbeforeunload = function(){
            if (currentUser.canEdit($scope.query) && $scope.dirty) {
                return leavingPageText;
            }
        }

        Mousetrap.bindGlobal("meta+s", function(e) {
            e.preventDefault();

            if (currentUser.canEdit($scope.query)) {
                $scope.saveQuery();
            }
        });

        $scope.$on('$locationChangeStart', function(event, next, current) {
            if (next.split("#")[0] == current.split("#")[0]) {
                return;
            }

            if (!currentUser.canEdit($scope.query)) {
                return;
            }

            if($scope.dirty &&
                !confirm(leavingPageText + "\n\nAre you sure you want to leave this page?")) {
                event.preventDefault();
            } else {
                Mousetrap.unbind("meta+s");
            }
        });

        $scope.$parent.pageTitle = "Query Fiddle";

        $scope.$watch(function() {return $location.hash()}, function(hash) {
            $scope.selectedTab = hash || DEFAULT_TAB;
        });

        $scope.lockButton = function (lock) {
            $scope.queryExecuting = lock;
        };

        $scope.formatQuery = function() {
            $scope.editorOptions.readOnly = 'nocursor';

            $http.post('/api/queries/format', {'query': $scope.query.query}).success(function(response) {
                $scope.query.query = response;
                $scope.editorOptions.readOnly = false;
            })
        }

        $scope.saveQuery = function (duplicate, oldId) {
            if (!oldId) {
                oldId = $scope.query.id;
            }
            delete $scope.query.latest_query_data;
            $scope.query.$save(function (q) {
                pristineHash = q.getHash();
                $scope.dirty = false;

                if (duplicate) {
                    growl.addInfoMessage("Query duplicated.", {ttl: 2000});
                } else{
                    growl.addSuccessMessage("Query saved.", {ttl: 2000});
                }

                if (oldId != q.id) {
                    if (oldId == undefined) {
                        $location.path($location.path().replace('new', q.id)).replace();
                    } else {
                        // TODO: replace this with a safer method
                        $location.path($location.path().replace(oldId, q.id)).replace();

                        // Reset visualizations tab to table after duplicating a query:
                        $location.hash('table');
                    }
                }
            }, function(httpResponse) {
                growl.addErrorMessage("Query could not be saved");
            });
        };

        $scope.duplicateQuery = function () {
            var oldId = $scope.query.id;
            $scope.query.id = null;
            $scope.query.ttl = -1;

            $scope.saveQuery(true, oldId);
        };

        // Query Editor:
        $scope.editorOptions = {
            mode: 'text/x-sql',
            lineWrapping: true,
            lineNumbers: true,
            readOnly: false,
            matchBrackets: true,
            autoCloseBrackets: true
        };

        $scope.refreshOptions = [
            {value: -1, name: 'No Refresh'},
            {value: 60, name: 'Every minute'},
        ]

        _.each(_.range(1, 13), function(i) {
            $scope.refreshOptions.push({value: i*3600, name: 'Every ' + i + 'h'});
        })
{'fraction_non_alphanumeric': 0.13488484458044775, 'fraction_numerical': 0.00595909164116605, 'mean_word_length': 1.5997487963156793, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2992707', 'n_tokens_mistral': 3454, 'n_tokens_neox': 3227, 'n_words': 617}