<commit_before>const express = require('express')
const AWS = require('aws-sdk')
const promisify = require('es6-promisify')
const path = require('path')
const errorhandler = require('errorhandler')
const { serializeKey, unserializeKey } = require('./util')

require('es7-object-polyfill')

const app = express()
app.set('json spaces', 2)
app.set('view engine', 'ejs')
app.set('views', path.resolve(__dirname, 'views'))

AWS.config.update({
  accessKeyId: process.env.AWS_ACCESS_KEY_ID || 'key',
  secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY || 'secret',
  endpoint: process.env.DYNAMO_ENDPOINT || 'http://localhost:8000',
  sslEnabled: process.env.DYNAMO_ENDPOINT && process.env.DYNAMO_ENDPOINT.indexOf('https://') === 0,
  region: process.env.AWS_REGION || 'us-east-1'
})

const dynamodb = new AWS.DynamoDB()
const documentClient = new AWS.DynamoDB.DocumentClient()

const listTables = promisify(dynamodb.listTables.bind(dynamodb))
const describeTable = promisify(dynamodb.describeTable.bind(dynamodb))
const scan = promisify(documentClient.scan.bind(documentClient))
const get = promisify(documentClient.get.bind(documentClient))

app.use(errorhandler())
app.use('/assets', express.static(path.join(__dirname, '/public')))

app.get('/', (req, res) => {
  dynamodb.listTables({}, (error, data) => {
    if (error) {
      res.json({error})
    } else {
      Promise.all(data.TableNames.map((TableName) => {
        return describeTable({TableName}).then((data) => data.Table)
      })).then((data) => {
        res.render('tables', {data})
      }).catch((error) => {
        res.json({error})
      })
    }
  })
})

app.get('/tables/:TableName', (req, res, next) => {
  const TableName = req.params.TableName
  Promise.all([
    describeTable({TableName}),
    scan({
      TableName,
      Limit: 25
    })
  ]).then(([description, result]) => {
    const data = Object.assign({},
      description,
      {
        Items: result.Items.map((item) => {
          return Object.assign({}, item, {
            __key: serializeKey(item, description.Table)
          })
        })
      }
    )
    res.render('scan', data)
  }).catch(next)
})

app.get('/tables/:TableName/meta', (req, res) => {
  const TableName = req.params.TableName
  Promise.all([
    describeTable({TableName}),
    scan({TableName})
  ]).then(([description, items]) => {
    const data = Object.assign({},
      description,
      items
    )
    res.render('meta', data)
  }).catch((error) => {
    res.json({error})
  })
})

app.get('/tables/:TableName/items/:key', (req, res, next) => {
  const TableName = req.params.TableName
  describeTable({TableName}).then((result) => {
    const params = {
      TableName,
      Key: unserializeKey(req.params.key, result.Table)
    }

    return get(params).then((response) => {
      if (!response) {
        return res.status(404).end()
      }
      res.render('item', {
        TableName: req.params.TableName,
        Item: response.Item
      })
    })
  }).catch(next)<commit_msg>send proper 404 when item not found<commit_after>const express = require('express')
const AWS = require('aws-sdk')
const promisify = require('es6-promisify')
const path = require('path')
const errorhandler = require('errorhandler')
const { serializeKey, unserializeKey } = require('./util')

require('es7-object-polyfill')

const app = express()
app.set('json spaces', 2)
app.set('view engine', 'ejs')
app.set('views', path.resolve(__dirname, 'views'))

AWS.config.update({
  accessKeyId: process.env.AWS_ACCESS_KEY_ID || 'key',
  secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY || 'secret',
  endpoint: process.env.DYNAMO_ENDPOINT || 'http://localhost:8000',
  sslEnabled: process.env.DYNAMO_ENDPOINT && process.env.DYNAMO_ENDPOINT.indexOf('https://') === 0,
  region: process.env.AWS_REGION || 'us-east-1'
})

const dynamodb = new AWS.DynamoDB()
const documentClient = new AWS.DynamoDB.DocumentClient()

const listTables = promisify(dynamodb.listTables.bind(dynamodb))
const describeTable = promisify(dynamodb.describeTable.bind(dynamodb))
const scan = promisify(documentClient.scan.bind(documentClient))
const get = promisify(documentClient.get.bind(documentClient))

app.use(errorhandler())
app.use('/assets', express.static(path.join(__dirname, '/public')))

app.get('/', (req, res) => {
  dynamodb.listTables({}, (error, data) => {
    if (error) {
      res.json({error})
    } else {
      Promise.all(data.TableNames.map((TableName) => {
        return describeTable({TableName}).then((data) => data.Table)
      })).then((data) => {
        res.render('tables', {data})
      }).catch((error) => {
        res.json({error})
      })
    }
  })
})

app.get('/tables/:TableName', (req, res, next) => {
  const TableName = req.params.TableName
  Promise.all([
    describeTable({TableName}),
    scan({
      TableName,
      Limit: 25
    })
  ]).then(([description, result]) => {
    const data = Object.assign({},
      description,
      {
        Items: result.Items.map((item) => {
          return Object.assign({}, item, {
            __key: serializeKey(item, description.Table)
          })
        })
      }
    )
    res.render('scan', data)
  }).catch(next)
})

app.get('/tables/:TableName/meta', (req, res) => {
  const TableName = req.params.TableName
  Promise.all([
    describeTable({TableName}),
    scan({TableName})
  ]).then(([description, items]) => {
    const data = Object.assign({},
      description,
      items
    )
    res.render('meta', data)
  }).catch((error) => {
    res.json({error})
  })
})

app.get('/tables/:TableName/items/:key', (req, res, next) => {
  const TableName = req.params.TableName
  describeTable({TableName}).then((result) => {
    const params = {
      TableName,
      Key: unserializeKey(req.params.key, result.Table)
    }

    return get(params).then((response) => {
      if (!response.Item) {
        return res.status(404).send('Not found')
      }
      res.render('item', {
        TableName: req.params.TableName,
        Item: response.Item
      })
    })
  }).catch(next)
{'fraction_non_alphanumeric': 0.17856553998351196, 'fraction_numerical': 0.005111294311624073, 'mean_word_length': 3.4052287581699345, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 33, 'https://': 2, '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': '5839372', 'n_tokens_mistral': 2021, 'n_tokens_neox': 1978, 'n_words': 342}