<commit_before>require!{mongodb}

# mongodb collection name
const ARTICLE = \article
const STATUTE = \statute

mongoUri = \mongodb://localhost:27017/laweasyread

exports.setMongoUri = ->
    console.log "Set mongoUri to #it"
    mongoUri := it

exports.getStatute = (params, cb) ->
    m = /^([^_]+)_(\d+)$/ ==  params.query
    if not m
        cb new Error "query string error", null
        return

    name = m.1
    article = m.2

    err, db <- mongodb.Db.connect mongoUri
    if err
        cb err, null
        return

    err, collection <- db.collection STATUTE
    if err
        cb err, null
        return

    err, data <- collection.find({name: $elemMatch:{name: name}}).toArray
    if err
        cb err, null
        return

    if data.length == 0
        cb null, {}
        return

    lyID = data[0].lyID

    err, collection <- db.collection ARTICLE
    if err
        cb err, null
        return

    err, data <- collection.find({lyID: lyID, article: article}).toArray
    if data.length == 0
        cb null, {}
        return

    res =<commit_msg>Call db.close! in getStatute<commit_after>require!{mongodb}

# mongodb collection name
const ARTICLE = \article
const STATUTE = \statute

mongoUri = \mongodb://localhost:27017/laweasyread

exports.setMongoUri = ->
    console.log "Set mongoUri to #it"
    mongoUri := it

chainCloseDB = (db, cb) ->
    db.close!
    (err, res) -> cb err, res

exports.getStatute = (params, cb) ->
    m = /^([^_]+)_(\d+)$/ ==  params.query
    if not m
        cb new Error "query string error", null
        return

    name = m.1
    article = m.2

    err, db <- mongodb.Db.connect mongoUri
    if err
        cb err, null
        return
    cb := chainCloseDB db, cb

    err, collection <- db.collection STATUTE
    if err
        cb err, null
        return

    err, data <- collection.find({name: $elemMatch:{name: name}}).toArray
    if err
        cb err, null
        return

    if data.length == 0
        cb null, {}
        return

    lyID = data[0].lyID

    err, collection <- db.collection ARTICLE
    if err
        cb err, null
        return

    err, data <- collection.find({lyID: lyID, article: article}).toArray
    if data.length == 0
        cb null, {}
        return

    res =
{'fraction_non_alphanumeric': 0.1152317880794702, 'fraction_numerical': 0.008830022075055188, 'mean_word_length': 1.981578947368421, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 9, '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': '4685882', 'n_tokens_mistral': 777, 'n_tokens_neox': 724, 'n_words': 222}