<commit_before>import { RequestHandler } from 'express'
import * as getHosts from '../hosts/db'
import * as stream from 'stream'
import docker from '../docker'

const handler: RequestHandler = async (req, res) => {
  const containerId = req.params.id
  const hostId = req.params.hostid
  const tail = req.query.tail || 50

  const host = await getHosts.getOne(hostId)
  if (!host) {
    res
      .status(400)
      .json({
        status: 400,
        message: 'Invalid Host ID'
      })
    return
  }

  const client = docker(host)
  try {
    const logStream = await client
      .getContainer(containerId)
      .logs({ tail, stdout: true, stderr: true })

    const logs: string[] = []
    const append = (data: Buffer) => {
      logs.push(data.toString())
    }

    const stdout = new stream.PassThrough()
    const stderr = new stream.PassThrough()
    client.modem.demuxStream(logStream, stdout, stderr)

    logStream.on('data', append)
    logStream.on('data', append)
    logStream.on('end', () => res.json(logs))
  } catch (ex) {
    res
      .status(500)
      .json(ex)
  }<commit_msg>Bugfix: Remove duplicate 'append' code in GET container/logs<commit_after>import { RequestHandler } from 'express'
import * as getHosts from '../hosts/db'
import * as stream from 'stream'
import docker from '../docker'

const handler: RequestHandler = async (req, res) => {
  const containerId = req.params.id
  const hostId = req.params.hostid
  const tail = req.query.tail || 100

  const host = await getHosts.getOne(hostId)
  if (!host) {
    res.status(400).json({
      status: 400,
      message: 'Invalid Host ID'
    })
    return
  }

  const client = docker(host)
  try {
    const logStream = await client
      .getContainer(containerId)
      .logs({ tail, stdout: true, stderr: true })

    const logs: string[] = []
    const append = (data: Buffer) => {
      logs.push(data.toString())
    }

    const stdout = new stream.PassThrough()
    const stderr = new stream.PassThrough()
    client.modem.demuxStream(logStream, stdout, stderr)

    logStream.on('data', append)
    logStream.on('end', () => res.json(logs))
  } catch (ex) {
    res.status(500).json(ex)
  }
{'fraction_non_alphanumeric': 0.13625971650663007, 'fraction_numerical': 0.010516689529035207, 'mean_word_length': 2.879432624113475, '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': '7629149', 'n_tokens_mistral': 737, 'n_tokens_neox': 693, 'n_words': 191}