<commit_before>os = require 'os'
fs = require 'fs'
exec = require('child_process').exec

ControllerClient = require("cozy-clients").ControllerClient

freeMemCmd = "LC_ALL=C free | grep /cache | cut -d':' -f2 | sed -e 's/^ *[0-9]* *//'"


# Utilities to get memory consumption and disk usage.
class exports.MemoryManager

    constructor: ->
        @controllerClient = new ControllerClient
            token: @_getAuthController()

    # Get token from token file if in production mode.
    # TODO: this method should be integrated to the controller client.
    _getAuthController: ->
        if process.env.NODE_ENV is 'production'
            try
                token = process.env.TOKEN
                return token
            catch err
                console.log err.message
                console.log err.stack
                return null
        else
            return ""


    # Crappy method to extract result from a df command, should be rewritter
    # with the one located in the controller.
    _extractDataFromDfResult: (resp) ->
        data = {}
        lines = resp.split('\n')
        for line in lines
            line = line.replace(/[\s]+/g, ' ')
            lineData = line.split(' ')

            if lineData.length > 5 and lineData[5] is '/'
                freeSpace = lineData[3].substring(0, lineData[3].length - 1)
                totalSpace = lineData[1].substring(0, lineData[1].length - 1)
                usedSpace = lineData[2].substring(0, lineData[2].length - 1)
                unit = lineData[1].slice(-1)

                data.totalDiskSpace = totalSpace
                data.freeDiskSpace = freeSpace
                data.usedDiskSpace = usedSpace
                data.unit = unit
        data

    # Return memory information from a complex free command (remove useless
    # information)
    getMemoryInfos: (callback) ->
        data = totalMem: os.totalmem() / (1024)
        # check if free command is available.
        # Otherwise, use Node native method, which is less accurate
        exec 'which free', (err, res) ->
            if err
                data.freeMem = Math.floor os.freemem() / 1000
                callback null, data
            else
                exec freeMemCmd, (err, resp) ->
                    if err
                        callback err
                    else
                        lines = resp.split('\n')
                        line = lines[0]
                        # This is to prevent a misbehaviour which happens if
                        # the syntax of free command is different than expected
                        if isNaN parseInt(line)
                            data.freeMem = Math.floor os.freemem() / 1000
                        else
                            data.freeMem = line
                        callback null, data
<commit_msg>Bad comment<commit_after>os = require 'os'
fs = require 'fs'
exec = require('child_process').exec

ControllerClient = require("cozy-clients").ControllerClient

freeMemCmd = "LC_ALL=C free | grep /cache | cut -d':' -f2 | sed -e 's/^ *[0-9]* *//'"


# Utilities to get memory consumption and disk usage.
class exports.MemoryManager

    constructor: ->
        @controllerClient = new ControllerClient
            token: @_getAuthController()

    # Get token from token file if in production mode.
    # TODO: this method should be integrated to the controller client.
    _getAuthController: ->
        if process.env.NODE_ENV is 'production'
            try
                token = process.env.TOKEN
                return token
            catch err
                console.log err.message
                console.log err.stack
                return null
        else
            return ""


    # Crappy method to extract result from a df command, should be rewritter
    # with the one located in the controller.
    _extractDataFromDfResult: (resp) ->
        data = {}
        lines = resp.split('\n')
        for line in lines
            line = line.replace(/[\s]+/g, ' ')
            lineData = line.split(' ')

            if lineData.length > 5 and lineData[5] is '/'
                freeSpace = lineData[3].substring(0, lineData[3].length - 1)
                totalSpace = lineData[1].substring(0, lineData[1].length - 1)
                usedSpace = lineData[2].substring(0, lineData[2].length - 1)
                unit = lineData[1].slice(-1)

                data.totalDiskSpace = totalSpace
                data.freeDiskSpace = freeSpace
                data.usedDiskSpace = usedSpace
                data.unit = unit
        data

    # Return memory information from a complex free command (remove useless
    # information)
    getMemoryInfos: (callback) ->
        data = totalMem: os.totalmem() / (1024)
        # check if free command is available.
        # Otherwise, use Node native method, which is less accurate
        exec 'which free', (err, res) ->
            if err
                data.freeMem = Math.floor os.freemem() / 1000
                callback null, data
            else
                exec freeMemCmd, (err, resp) ->
                    if err
                        callback err
                    else
                        lines = resp.split('\n')
                        line = lines[0]
                        # This is to prevent a misbehaviour which happens if
                        # the syntax of free command is different than
                        # expected
                        if isNaN parseInt(line)
                            data.freeMem = Math.floor os.freemem() / 1000
                        else
                            data.freeMem = line
                        callback null, data

{'fraction_non_alphanumeric': 0.08496846531184303, 'fraction_numerical': 0.011212333566923615, 'mean_word_length': 1.4597156398104265, 'pattern_counts': {'":': 0, '<': 3, '<?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': '5685770', 'n_tokens_mistral': 1486, 'n_tokens_neox': 1386, 'n_words': 459}