<commit_before>'use strict';

angular.module('app')
    .directive('splash', ['$window', '$q', '$rester', '$error', function ($window, $q, $rester, $error) {

        function openOldDatabase() {
            const request = $window.indexedDB.open('rester'),
                  dfd = $q.defer();

            request.onerror = function () {
                dfd.reject();
            };

            request.onsuccess = function (event) {
                const db = event.target.result;
                if (db.objectStoreNames.length > 0) {
                    dfd.resolve(db);
                } else {
                    dfd.reject();
                }
            };

            return dfd.promise;
        }

        function deleteOldDatabase() {
            $window.indexedDB.deleteDatabase('rester');
        }

        function getAllEntitiesFromDatabase(db) {
            let dfd = $q.defer(),
                transaction = db.transaction(db.objectStoreNames, 'readonly'),
                entities = {};

            transaction.oncomplete = function () {
                dfd.resolve(entities);
            };

            transaction.onerror = function (event) {
                dfd.reject('Error executing transaction: ' + event.target.errorCode);
            };

            for (let name of db.objectStoreNames) {
                const objectStore = transaction.objectStore(name);
                entities[name] = getAllEntitiesFromObjectStore(objectStore);
            }

            return dfd.promise;
        }

        function getAllEntitiesFromObjectStore(objectStore) {
            let cursorRequest = objectStore.openCursor(),
                result = [];

            cursorRequest.onsuccess = function (event) {
                let cursor = event.target.result;
                if (cursor) {
                    result.push(cursor.value);
                    cursor.continue();
                }
            };

            return result;
        }

        function saveEntitiesInNewDatabase(entities) {
            // Clean entries from IDs and angular keys, that have been added
            // by accident.
            Object.keys(entities).forEach(objectStore => {
                for (let entity of entities[objectStore]) {
                    delete entity.id;

                    const angularKeys = Object.keys(entity).filter(key => key.startsWith('$$'));
                    for (let angularKey of angularKeys) {
                        delete entity[angularKey];
                    }
                }
            });

            // Import data in new database.
            return $rester.importData(entities);
        }

        function migrateDatabase(db) {
            return getAllEntitiesFromDatabase(db)
                .then(saveEntitiesInNewDatabase)
                .then(deleteOldDatabase);
        }

        function hideSplash(element) {
            $window.requestAnimationFrame(() => {
                element.style.opacity = 0;
                element.addEventListener('transitionend', () => {
                    element.remove();
                });
            });
        }

        return {
            restrict: 'E',
            scope: {},
            templateUrl: 'views/directives/splash.html',
            link: function ($scope, $element) {
                const element = $element[0];

                $scope.isMigrating = false;

                openOldDatabase()
                    .then(
                        db => {
                            $scope.isMigrating = true;
                            return migrateDatabase(db);
                        },
                        // If database doesn't exist of cannot be opened,
                        // no migration is needed.
                        angular.noop
                    )
                    .then(() => {
                        hideSplash(element);
                    })<commit_msg>Add data migration from old settings in website to new ones in background.<commit_after>'use strict';

angular.module('app')
    .directive('splash', ['$window', '$q', '$rester', '$error', function ($window, $q, $rester, $error) {

        function openOldDatabase() {
            const request = $window.indexedDB.open('rester'),
                  dfd = $q.defer();

            request.onerror = function () {
                dfd.reject();
            };

            request.onsuccess = function (event) {
                const db = event.target.result;
                if (db.objectStoreNames.length > 0) {
                    dfd.resolve(db);
                } else {
                    dfd.reject();
                }
            };

            return dfd.promise;
        }

        function deleteOldDatabase() {
            $window.indexedDB.deleteDatabase('rester');
        }

        function getAllEntitiesFromDatabase(db) {
            let dfd = $q.defer(),
                transaction = db.transaction(db.objectStoreNames, 'readonly'),
                entities = {};

            transaction.oncomplete = function () {
                dfd.resolve(entities);
            };

            transaction.onerror = function (event) {
                dfd.reject('Error executing transaction: ' + event.target.errorCode);
            };

            for (let name of db.objectStoreNames) {
                const objectStore = transaction.objectStore(name);
                entities[name] = getAllEntitiesFromObjectStore(objectStore);
            }

            return dfd.promise;
        }

        function getAllEntitiesFromObjectStore(objectStore) {
            let cursorRequest = objectStore.openCursor(),
                result = [];

            cursorRequest.onsuccess = function (event) {
                let cursor = event.target.result;
                if (cursor) {
                    result.push(cursor.value);
                    cursor.continue();
                }
            };

            return result;
        }

        function saveEntitiesInNewDatabase(entities) {
            // Clean entries from IDs and angular keys, that have been added
            // by accident.
            Object.keys(entities).forEach(objectStore => {
                for (let entity of entities[objectStore]) {
                    delete entity.id;

                    const angularKeys = Object.keys(entity).filter(key => key.startsWith('$$'));
                    for (let angularKey of angularKeys) {
                        delete entity[angularKey];
                    }
                }
            });

            // Import data in new database.
            return $rester.importData(entities);
        }

        function migrateDatabase(db) {
            return getAllEntitiesFromDatabase(db)
                .then(saveEntitiesInNewDatabase)
                .then(deleteOldDatabase);
        }

        function getAndRemoveOldSetting(key) {
            const value = $window.localStorage.getItem(key);
            $window.localStorage.removeItem(key);
            if (value) {
                try {
                    return JSON.parse(value);
                } catch (e) {
                }
            }
        }

        function migrateSettings() {
            const oldSettingsKeys = {
                activeEnvironment: 'rester.active_environment',
                stripDefaultHeaders: 'rester.strip_default_headers',
                enableRequestLintInspections: 'rester.enable_request_lint_inspections',
                pinSidenav: 'rester.pin_sidenav',
                exeprimentalResponseHighlighting: 'rester.experimental_response_highlighting'
            };

            return $rester.settingsLoaded.then(() => {
                Object.keys(oldSettingsKeys).forEach(key => {
                    const value = getAndRemoveOldSetting(oldSettingsKeys[key]);
                    if (typeof value !== 'undefined') {
                        $rester.settings[key] = value;
                    }
                });
            });
        }

        function hideSplash(element) {
            $window.requestAnimationFrame(() => {
                element.style.opacity = 0;
                element.addEventListener('transitionend', () => {
                    element.remove();
                });
            });
        }

        return {
            restrict: 'E',
            scope: {},
            templateUrl: 'views/directives/splash.html',
            link: function ($scope, $element) {
                const element = $element[0];

                $scope.isMigrating = false;

                openOldDatabase()
                    .then(
                        db => {
                            $scope.isMigrating = true;
                            return $q.all([
                                migrateDatabase(db),
                                migrateSettings()
                            ]);
                        },
                        // If database doesn't exist of cannot be opened,
                        // no migration is needed.
                        angular.noop
                    )
                    .then(() => {
                        hideSplash(element);
                    })
{'fraction_non_alphanumeric': 0.10005443658138269, 'fraction_numerical': 0.00065323897659227, 'mean_word_length': 1.2242130750605327, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 19, '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': '4213530', 'n_tokens_mistral': 2216, 'n_tokens_neox': 2066, 'n_words': 415}