<commit_before>async = require "async"
moment = require "moment"
log = require('printit')
    prefix: null
    date: true
importer = require "./importer"
Konnector = require '../models/konnector'

hour = 60 * 60 * 1000
day = 24 * hour
week = 7 * day
month = 30 * day
format = "DD/MM/YYYY [at] HH:mm:ss"
periods = {hour: hour, day: day, week: week, month: month}
timeouts = {}

class KonnectorPoller

    start: ->
        log.debug "Launching Konnector Poller..."
        @init()

    init: ->
        Konnector.all (err, konnectors) =>

            async.eachSeries konnectors, (konnector, callback) =>

                # if both importInterval and lastAutoImport are valid
                if konnector.importInterval? \
                and konnector.importInterval isnt 'none' \
                and konnector.lastAutoImport?

                    importInterval = periods[konnector.importInterval]
                    now = moment()
                    lastImport = moment(konnector.lastImport)
                    lastAutoImport = moment(konnector.lastAutoImport)

                    # if we missed an import interval
                    if (now.valueOf() - lastAutoImport.valueOf()) > importInterval
                        log.debug "#{konnector.slug} missed an import interval"

                        # calculate the supposed last Auto-import
                        importTime = lastAutoImport.valueOf() + importInterval

                        # calculate the time elapsed
                        while importTime < now.valueOf()
                            importTime += importInterval
                        interval = importTime - now.valueOf()

                        # We import now
                        importer konnector

                        nextUpdate = now.clone()
                        nextUpdate = nextUpdate.add interval, 'ms'
                        log.debug "#{konnector.slug} | Next update : " +
                        "#{nextUpdate.format(format)}"

                        @prepareNextCheck konnector, interval
                        callback()

                    # if we didn't missed an import interval
                    else
                        #interval = (lastAutoImport + importInterval) - now
                        interval = (lastAutoImport.valueOf() + importInterval)
                        interval -= now.valueOf()
                        log.debug "#{konnector.slug} didnt miss an interval"

                        nextUpdate = now.clone()
                        nextUpdate = nextUpdate.add interval, 'ms'
                        log.debug "#{konnector.slug} | Next update : " +
                        "#{nextUpdate.format(format)}"

                        @prepareNextCheck konnector, interval
                        callback()
                else
                    callback()

    handleTimeout: (konnector) ->

        # Retrive current Autoimport value in database
        Konnector.find konnector.id, (err, savedKonnector) =>

            currentInterval = savedKonnector.importInterval

            # If the importInterval has changed
            if konnector.importInterval isnt currentInterval

                # if there is already a timeout for this konnector, destroy it
                if timeouts[konnector.slug]?
                    clearTimeout timeouts[konnector.slug]
                    delete timeouts[konnector.slug]

                if konnector.importInterval isnt 'none'

                    diff = 0
                    # if date is present in fieldValues
                    if konnector.fieldValues.date?
                        now = moment()
                        firstImportDate = moment(konnector.fieldValues.date, "DD-MM-YYYY")

                        diff = firstImportDate.valueOf() - now.valueOf()

                        console.log moment(firstImportDate).isValid()

                        # We set the date of the first import
                        data =
                            lastAutoImport: firstImportDate<commit_msg>remove console.log<commit_after>async = require "async"
moment = require "moment"
log = require('printit')
    prefix: null
    date: true
importer = require "./importer"
Konnector = require '../models/konnector'

hour = 60 * 60 * 1000
day = 24 * hour
week = 7 * day
month = 30 * day
format = "DD/MM/YYYY [at] HH:mm:ss"
periods = {hour: hour, day: day, week: week, month: month}
timeouts = {}

class KonnectorPoller

    start: ->
        log.debug "Launching Konnector Poller..."
        @init()

    init: ->
        Konnector.all (err, konnectors) =>

            async.eachSeries konnectors, (konnector, callback) =>

                # if both importInterval and lastAutoImport are valid
                if konnector.importInterval? \
                and konnector.importInterval isnt 'none' \
                and konnector.lastAutoImport?

                    importInterval = periods[konnector.importInterval]
                    now = moment()
                    lastImport = moment(konnector.lastImport)
                    lastAutoImport = moment(konnector.lastAutoImport)

                    # if we missed an import interval
                    if (now.valueOf() - lastAutoImport.valueOf()) > importInterval
                        log.debug "#{konnector.slug} missed an import interval"

                        # calculate the supposed last Auto-import
                        importTime = lastAutoImport.valueOf() + importInterval

                        # calculate the time elapsed
                        while importTime < now.valueOf()
                            importTime += importInterval
                        interval = importTime - now.valueOf()

                        # We import now
                        importer konnector

                        nextUpdate = now.clone()
                        nextUpdate = nextUpdate.add interval, 'ms'
                        log.debug "#{konnector.slug} | Next update : " +
                        "#{nextUpdate.format(format)}"

                        @prepareNextCheck konnector, interval
                        callback()

                    # if we didn't missed an import interval
                    else
                        #interval = (lastAutoImport + importInterval) - now
                        interval = (lastAutoImport.valueOf() + importInterval)
                        interval -= now.valueOf()
                        log.debug "#{konnector.slug} didnt miss an interval"

                        nextUpdate = now.clone()
                        nextUpdate = nextUpdate.add interval, 'ms'
                        log.debug "#{konnector.slug} | Next update : " +
                        "#{nextUpdate.format(format)}"

                        @prepareNextCheck konnector, interval
                        callback()
                else
                    callback()

    handleTimeout: (konnector) ->

        # Retrive current Autoimport value in database
        Konnector.find konnector.id, (err, savedKonnector) =>

            currentInterval = savedKonnector.importInterval

            # If the importInterval has changed
            if konnector.importInterval isnt currentInterval

                # if there is already a timeout for this konnector, destroy it
                if timeouts[konnector.slug]?
                    clearTimeout timeouts[konnector.slug]
                    delete timeouts[konnector.slug]

                if konnector.importInterval isnt 'none'

                    diff = 0
                    # if date is present in fieldValues
                    if konnector.fieldValues.date?
                        now = moment()
                        firstImportDate = moment(konnector.fieldValues.date, "DD-MM-YYYY")

                        diff = firstImportDate.valueOf() - now.valueOf()

                        # We set the date of the first import
                        data =
                            lastAutoImport: firstImportDate
{'fraction_non_alphanumeric': 0.07641485913737223, 'fraction_numerical': 0.0034904013961605585, 'mean_word_length': 1.3555490311215501, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 17, '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': '6626996', 'n_tokens_mistral': 1967, 'n_tokens_neox': 1797, 'n_words': 508}