<commit_before>/* eslint-disable prefer-arrow-callback, import/no-extraneous-dependencies */
const AWS = require('aws-sdk')

const dynamoDb = new AWS.DynamoDB.DocumentClient()

const projectPlatformConstant = {
    GitHub: 1,
    PyPI: 2,
    npm: 3,
}
const subscriberPlatformConstant = {
    telegram: 1,
    skype: 2,
    slack: 3,
}

function getKeyByValue(obj, val) {
    let k = null
    Object.keys(obj).forEach((key) => {
        if (obj[key] === val) k = key
    })
    return k
}

function subscriberPlatform(constant) {
    return getKeyByValue(subscriberPlatformConstant, constant)
}

exports.projectPlatform = function (constant) {
    return getKeyByValue(projectPlatformConstant, constant)
}

exports.getAllProject = function (cb) {
    const scanParams = {
        TableName: 'ProjectDetail',
        ConsistentRead: true
    }

    function onScan(err, data) {
        if (err)
            return cb(err)

        // continue scanning if previous scanning reach 1MB limit
        if (typeof data.LastEvaluatedKey !== 'undefined') {
            scanParams.ExclusiveStartKey = data.LastEvaluatedKey
            dynamoDb.scan(scanParams, onScan)
        }
        cb(null, data.Items)
    }

    dynamoDb.scan(scanParams, onScan)
}

exports.getAllSubscriber = function (project_name, cb) {
    const queryParams = {
        TableName: 'SubscribedProject',
        IndexName: 'ProjectSubscriber',
        KeyConditionExpression: 'project_name = :x',
        ExpressionAttributeValues: {
            ':x': project_name
        },
        ProjectionExpression: 'subscriber_id, subscriber_platform'
    }

    function onQuery(err, data) {
        if (err)
            return cb(err)

        const list = []
        data.Items.forEach((item) => {
            list.push({
                platform: subscriberPlatform(item.subscriber_platform),
                id: item.subscriber_id
            })
        })

        cb(null, list)
    }

    dynamoDb.query(queryParams, onQuery)
}

exports.storeSlackTokenPromise = function (channel_id, token) {
    const putSlackTokenParams = {
        TableName: 'SlackToken',
        Item: {
            channel_id,
            token
        }
    }

    return dynamoDb.put(putSlackTokenParams).promise()
}

exports.getSlackTokenPromise = function (channel_id) {
    const getSlackTokenParams = {
        TableName: 'SlackToken',
        ConsistentRead: true,
        KeyConditionExpression: 'channel_id = :x',
        ExpressionAttributeValues: {
            ':x': channel_id
        }
    }

    return dynamoDb.query(getSlackTokenParams).promise()
        .then(data => data.Items[0].token)
}<commit_msg>clean unnecessary function<commit_after>/* eslint-disable prefer-arrow-callback, import/no-extraneous-dependencies */
const AWS = require('aws-sdk')

const dynamoDb = new AWS.DynamoDB.DocumentClient()

const projectPlatformConstant = {
    GitHub: 1,
    PyPI: 2,
    npm: 3,
}
const subscriberPlatformConstant = {
    telegram: 1,
    skype: 2,
    slack: 3,
}

function getKeyByValue(obj, val) {
    let k = null
    Object.keys(obj).forEach((key) => {
        if (obj[key] === val) k = key
    })
    return k
}

function subscriberPlatform(constant) {
    return getKeyByValue(subscriberPlatformConstant, constant)
}

exports.projectPlatform = function (constant) {
    return getKeyByValue(projectPlatformConstant, constant)
}

exports.getAllProject = function (cb) {
    const scanParams = {
        TableName: 'ProjectDetail',
        ConsistentRead: true
    }

    function onScan(err, data) {
        if (err)
            return cb(err)

        // continue scanning if previous scanning reach 1MB limit
        if (typeof data.LastEvaluatedKey !== 'undefined') {
            scanParams.ExclusiveStartKey = data.LastEvaluatedKey
            dynamoDb.scan(scanParams, onScan)
        }
        cb(null, data.Items)
    }

    dynamoDb.scan(scanParams, onScan)
}

exports.getAllSubscriber = function (project_name, cb) {
    const queryParams = {
        TableName: 'SubscribedProject',
        IndexName: 'ProjectSubscriber',
        KeyConditionExpression: 'project_name = :x',
        ExpressionAttributeValues: {
            ':x': project_name
        },
        ProjectionExpression: 'subscriber_id, subscriber_platform'
    }

    function onQuery(err, data) {
        if (err)
            return cb(err)

        const list = []
        data.Items.forEach((item) => {
            list.push({
                platform: subscriberPlatform(item.subscriber_platform),
                id: item.subscriber_id
            })
        })

        cb(null, list)
    }

    dynamoDb.query(queryParams, onQuery)
}

exports.getSlackTokenPromise = function (channel_id) {
    const getSlackTokenParams = {
        TableName: 'SlackToken',
        ConsistentRead: true,
        KeyConditionExpression: 'channel_id = :x',
        ExpressionAttributeValues: {
            ':x': channel_id
        }
    }

    return dynamoDb.query(getSlackTokenParams).promise()
        .then(data => data.Items[0].token)
}
{'fraction_non_alphanumeric': 0.1054710645862137, 'fraction_numerical': 0.003160181710448351, 'mean_word_length': 2.46611909650924, 'pattern_counts': {'":': 0, '<': 3, '<?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': '5464678', 'n_tokens_mistral': 1527, 'n_tokens_neox': 1478, 'n_words': 305}