                             category: row[4]
                             productivity: row[5]
                         RescueTimeActivity.create data, (err) ->
+                            log.debug 'new activity imported'
+                            log.debug JSON.stringify data
+
                             if err then callback err
-                            else
-                                i++
-                                recSave i
+                            else recSave()
                     else
                         callback()
-                recSave 0
+                recSave()


module.exports =

    name: "rescuetime"
    fields:
        apikey: ""

    description: "Fetch all rescuetime data"

    models:
        activities: RescueTimeActivity

    # Define model requests.
    init: (callback) ->
        map = (doc) -> emit(doc.date, doc)
        RescueTimeActivity.defineRequest 'byDate', map, (err) ->
            callback err

    # Define parameters (start date and end date) then fetch data accordingly.
    fetch: (requiredFields, callback) ->
        params = limit: 1, descending: true
        RescueTimeActivity.request 'byDate', params, (err, activities) =>
            if err then callback err

            else
                apikey = requiredFields.apikey
                if activities.length > 0
                    start = moment(activities[0].date).format 'YYYY-MM-DD'
                else
                    start = moment().subtract('years', 10).format 'YYYY-MM-DD'
                end = moment().add('days', 1).format 'YYYY-MM-DD'

                @fetchData apikey, start, end, callback

    # Fetch activity list from rescuetime, then create an entry for each row.
    fetchData: (apikey, start, end, callback) ->
        client = request.newClient 'https://www.rescuetime.com/'
        path = 'anapi/data?'
        path += qs.stringify
            key: apikey
            format: "json"
            perspective: 'interval'

Something went wrong while fetching rescue time data.
"""
            else
                recSave = ->
                    if body.rows.length > 0
                        row = body.rows.pop()
                        data =
                            date: row[0]
                            duration: row[1]
