<commit_before>var Docker = require('dockerode');
var url = require('url');
var dockerOpts = require('dockerode-options');
var options = dockerOpts(process.env.DOCKER_HOST);
var docker = new Docker(options);
var express = require('express');
var app = express();

// Cache
app.locals.tasks = [];

app.get('/api/services', (req, res) => {
  docker.listServices((err, services) => {
    if(err) {
      return res.status(500).send(err);
    } else {
      services.map((service) => {
        const replicasRunning = app.locals.tasks.filter((task) => {
          return (task.ServiceID === service.ID) && (task.Status.State === 'running');
        }).length;

        service.ReplicasRunning = replicasRunning;
        return service;
      })

      return res.json(services);
    }
  });
});

app.get('/api/tasks', (req, res) => {
  docker.listTasks((err, tasks) => {
    if(err) {
      return res.status(500).send(err);
    } else {
      app.locals.tasks = tasks;
      return res.json(tasks);
    }
  });
});

app.listen(3001, function () {
  console.info('DOCKER_HOST parsed as: ', dockerOpts);<commit_msg>refactor: remove intermediary locals caching for tasks<commit_after>var Docker = require('dockerode');
var url = require('url');
var dockerOpts = require('dockerode-options');
var options = dockerOpts(process.env.DOCKER_HOST);
var docker = new Docker(options);
var express = require('express');
var app = express();

app.get('/api/services', (req, res) => {
  docker.listTasks((err, tasks) => {
    if(err) {
      console.error(err);
      return res.status(500).send(err);
    } else {
      docker.listServices((err, services) => {
        if(err) {
          console.error(err);
          return res.status(500).send(err);
        } else {
          services.map((service) => {
            const replicasRunning = tasks.filter((task) => {
              return (task.ServiceID === service.ID) && (task.Status.State === 'running');
            }).length;

            service.ReplicasRunning = replicasRunning;
            return service;
          })

          return res.json(services);
        }
      });
    }
  });
});

app.get('/api/tasks', (req, res) => {
  docker.listTasks((err, tasks) => {
    if(err) {
      return res.status(500).send(err);
    } else {
      return res.json(tasks);
    }
  });
});

app.listen(3001, function () {
  console.info('DOCKER_HOST parsed as: ', dockerOpts);
{'fraction_non_alphanumeric': 0.17530045586406962, 'fraction_numerical': 0.009531703273932863, 'mean_word_length': 2.7836990595611284, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 16, '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': '179636', 'n_tokens_mistral': 794, 'n_tokens_neox': 754, 'n_words': 145}