<commit_before>Scheduler    = require '../processes/_scheduler'
Account      = require '../models/account'
Contact      = require '../models/contact'
Settings     = require '../models/settings'
CONSTANTS    = require '../utils/constants'
async        = require 'async'
cozydb       = require 'cozydb'
log          = require('../utils/logging')(prefix: 'controllers:index')
ramStore     = require '../models/store_account_and_boxes'


# render the application index
# with all necessary imports
module.exports.main = (req, res, next) ->

    if Scheduler.applicationStartupRunning()
        return res.render 'reindexing'

    async.series [
        (cb) -> Settings.getDefault cb
        (cb) -> cozydb.api.getCozyLocale cb
        (cb) -> cb null, ramStore.clientList()
        (cb) -> Contact.list cb
    ], (err, results) ->

        refreshes = Scheduler.clientSummary()

        if err
            log.error "err on index", err.stack

            imports = """
                console.log("#{err}");
                window.locale = "en"
                window.refreshes = []
                window.accounts  = []
                window.contacts  = []<commit_msg>dont show scary reindexing if the app start fast<commit_after>Scheduler    = require '../processes/_scheduler'
Account      = require '../models/account'
Contact      = require '../models/contact'
Settings     = require '../models/settings'
CONSTANTS    = require '../utils/constants'
async        = require 'async'
cozydb       = require 'cozydb'
log          = require('../utils/logging')(prefix: 'controllers:index')
ramStore     = require '../models/store_account_and_boxes'


# render the application index
# with all necessary imports
module.exports.main = (req, res, next) ->

    if Scheduler.applicationStartupRunning()
        if req.hasWaitedForStart
            res.render 'reindexing'
        else
            req.hasWaitedForStart = true
            tryAgain = -> module.exports.main req, res, next
             # if the app can start under 2s, we wont show the need reindexing
            setTimeout tryAgain, 2000
        return null

    async.series [
        (cb) -> Settings.getDefault cb
        (cb) -> cozydb.api.getCozyLocale cb
        (cb) -> cb null, ramStore.clientList()
        (cb) -> Contact.list cb
    ], (err, results) ->

        refreshes = Scheduler.clientSummary()

        if err
            log.error "err on index", err.stack

            imports = """
                console.log("#{err}");
                window.locale = "en"
                window.refreshes = []
                window.accounts  = []
                window.contacts  = []
{'fraction_non_alphanumeric': 0.12108638249717088, 'fraction_numerical': 0.001886080724254998, 'mean_word_length': 1.9797752808988764, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 16, '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': '1662588', 'n_tokens_mistral': 742, 'n_tokens_neox': 718, 'n_words': 187}