<commit_before>const log = require('@sepal/log')
const job = require('@sepal/job')
const eeAuth = require('@sepal/ee/auth')

const worker$ = ({select, from, where = [], orderBy = []}) => {
    const ee = require('@google/earthengine')
    const {getInfo$} = require('@sepal/ee/utils')
    const {map} = require('rxjs/operators')
    const _ = require('lodash')

    log.debug('Query EE table:', {select, from, where, orderBy})

    const collection = ee.FeatureCollection(from)
    const filtered = where.reduce((c, f) => c.filterMetadata(f[0], f[1], f[2]), collection)
    const sorted = orderBy.reduce((c, sort) => c.sort(sort), filtered)
    
    return getInfo$(sorted
        .reduceColumns(ee.Reducer.toList(select.length), select)
        .get('list')
    ).pipe(
        map(
            rows => rows.map(row => {
                const l = row.map((value, i) =>
                    ({[select[i]]: value})
                )
                return _.assign({}, ...l)
            }
            )
        )
    )
}

module.exports = job({
    jobName: 'Query EE Table',
    jobPath: __filename,
    before: [eeAuth],
    worker$
})<commit_msg>Refactoring.<commit_after>const log = require('@sepal/log')
const job = require('@sepal/job')
const eeAuth = require('@sepal/ee/auth')

const worker$ = ({select, from, where = [], orderBy = []}) => {
    const ee = require('@google/earthengine')
    const {getInfo$} = require('@sepal/ee/utils')
    const {map} = require('rxjs/operators')
    const _ = require('lodash')

    log.debug('Query EE table:', {select, from, where, orderBy})

    const collection = ee.FeatureCollection(from)
    const filtered = where.reduce((c, f) => c.filterMetadata(f[0], f[1], f[2]), collection)
    const sorted = orderBy.reduce((c, sort) => c.sort(sort), filtered)

    return getInfo$(sorted
        .reduceColumns(ee.Reducer.toList(select.length), select)
        .get('list')
    ).pipe(
        map(rows =>
            rows.map(row =>
                row.reduce(
                    (acc, value, i) => ({...acc, [select[i]]: value}),
                    {}
                )
            )
        )
    )
}

module.exports = job({
    jobName: 'Query EE Table',
    jobPath: __filename,
    before: [eeAuth],
    worker$
})
{'fraction_non_alphanumeric': 0.17558602388323752, 'fraction_numerical': 0.0026536930561698365, 'mean_word_length': 2.3761194029850747, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, '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': '6635212', 'n_tokens_mistral': 746, 'n_tokens_neox': 724, 'n_words': 155}