<commit_before>import r2 from 'r2';
import sizeOf from 'image-size';
import request from 'request';
import { queryString, getCloudinaryUrl, svg2png } from '../lib/utils';
import { fetchMembersStats, fetchMembers } from '../lib/graphql';
import cachedRequestLib from 'cached-request';
import Promise from 'bluebird';
import url from 'url';

const cachedRequest = cachedRequestLib(request);
cachedRequest.setCacheDirectory('/tmp');
const requestPromise = Promise.promisify(cachedRequest, { multiArgs: true });

// Cache the list of members of a collective to avoid requesting it for every single /:collectiveSlug/backers/:position/avatar
const cache = require('lru-cache')({
  max: 100,
  maxAge: 1000 * 60 * 10
});

const WEBSITE_URL = process.env.WEBSITE_URL || "https://opencollective.com";

/**
 * Generates a github badge for a backerType (backers|sponsors) or for a tierSlug
 */
export async function badge(req, res) {
  try {
    const { style } = req.query;
    const color = req.query.color || 'brightgreen';
    
    const stats = await fetchMembersStats(req.params);

    const filename = `${stats.name}-${stats.count}-${color}.svg`;
    const imageUrl = `https://img.shields.io/badge/${filename}?style=${style}`;

    try {
      const imageRequest = await r2(imageUrl).text;
      res.setHeader('content-type','image/svg+xml;charset=utf-8');
      res.setHeader('cache-control','max-age=600');
      return res.send(imageRequest);
    } catch (e) {
      console.error(">>> error while fetching", imageUrl, e);
      res.setHeader('cache-control','max-age=30');
      return res.status(500).send(`Unable to fetch ${imageUrl}`);
    }
  } catch (e) {
    console.error("Catching an error in controllers.collectives.badge", e);
  }
}

export async function banner(req, res) {
  const { collectiveSlug, tierSlug, backerType } = req.params;
  const format = req.params.format || 'svg';
  const style = req.query.style || 'rounded';
  const limit = Number(req.query.limit) || Infinity;
  const imageWidth = Number(req.query.width) || 0;
  const imageHeight = Number(req.query.height) || 0;
  const showBtn = (req.query.button === 'false') ? false : true;
  
  let users = cache.get(queryString.stringify(req.params));<commit_msg>fix when badge shows 0 count<commit_after>import r2 from 'r2';
import sizeOf from 'image-size';
import request from 'request';
import { queryString, getCloudinaryUrl, svg2png } from '../lib/utils';
import { fetchMembersStats, fetchMembers } from '../lib/graphql';
import cachedRequestLib from 'cached-request';
import Promise from 'bluebird';
import url from 'url';

const cachedRequest = cachedRequestLib(request);
cachedRequest.setCacheDirectory('/tmp');
const requestPromise = Promise.promisify(cachedRequest, { multiArgs: true });

// Cache the list of members of a collective to avoid requesting it for every single /:collectiveSlug/backers/:position/avatar
const cache = require('lru-cache')({
  max: 100,
  maxAge: 1000 * 60 * 10
});

const WEBSITE_URL = process.env.WEBSITE_URL || "https://opencollective.com";

/**
 * Generates a github badge for a backerType (backers|sponsors) or for a tierSlug
 */
export async function badge(req, res) {
  try {
    const { style } = req.query;
    const color = req.query.color || 'brightgreen';
    
    const stats = await fetchMembersStats(req.params);

    const filename = `${stats.name}-${stats.count? stats.count : 0}-${color}.svg`;
    const imageUrl = `https://img.shields.io/badge/${filename}?style=${style}`;

    try {
      const imageRequest = await r2(imageUrl).text;
      res.setHeader('content-type','image/svg+xml;charset=utf-8');
      res.setHeader('cache-control','max-age=600');
      return res.send(imageRequest);
    } catch (e) {
      console.error(">>> error while fetching", imageUrl, e);
      res.setHeader('cache-control','max-age=30');
      return res.status(500).send(`Unable to fetch ${imageUrl}`);
    }
  } catch (e) {
    console.error("Catching an error in controllers.collectives.badge", e);
  }
}

export async function banner(req, res) {
  const { collectiveSlug, tierSlug, backerType } = req.params;
  const format = req.params.format || 'svg';
  const style = req.query.style || 'rounded';
  const limit = Number(req.query.limit) || Infinity;
  const imageWidth = Number(req.query.width) || 0;
  const imageHeight = Number(req.query.height) || 0;
  const showBtn = (req.query.button === 'false') ? false : true;
  
  let users = cache.get(queryString.stringify(req.params));
{'fraction_non_alphanumeric': 0.14904486894713462, 'fraction_numerical': 0.011994669035984007, 'mean_word_length': 4.175862068965517, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '3296388', 'n_tokens_mistral': 1455, 'n_tokens_neox': 1393, 'n_words': 362}