<commit_before>from django.views import View
from django.shortcuts import render
from apps.rss_feeds.models import MStory, MStarredStory
from apps.rss_feeds.models import MStory, MStarredStory
    
class Stories(View):

    def get(self, request):
        data = {
            'stories': MStory.objects.count(),
            'starred_stories': MStarredStory.objects.count(),
        }
        chart_name = "stories"
        chart_type = "counter"

        formatted_data = {}
        for k, v in data.items():
            formatted_data[k] = f'{chart_name}{{category="{k}"}} {v}'
        context = {
            "data": formatted_data,
            "chart_name": chart_name,
            "chart_type": chart_type,
        }
        return render(request, 'monitor/prometheus_data.html', context, content_type="text/plain")

<commit_msg>Caching MStory counts.<commit_after>from django.views import View
from django.shortcuts import render
from apps.rss_feeds.models import MStory, MStarredStory
from apps.rss_feeds.models import MStory, MStarredStory
    
class Stories(View):

    def get(self, request):
        stories_count = MStatistics.get('munin:stories_count')
        if not stories_count:
            stories_count = MStory.objects.all().count()
            MStatistics.set('munin:stories_count', stories_count, 60*60*12)

        starred_stories_count = MStatistics.get('munin:starred_stories_count')
        if not starred_stories_count:
            starred_stories_count = MStarredStory.objects.all().count()
            MStatistics.set('munin:starred_stories_count', starred_stories_count, 60*60*12)

        data = {
            'stories': stories_count,
            'starred_stories': starred_stories_count,
        }
        chart_name = "stories"
        chart_type = "counter"

        formatted_data = {}
        for k, v in data.items():
            formatted_data[k] = f'{chart_name}{{category="{k}"}} {v}'
        context = {
            "data": formatted_data,
            "chart_name": chart_name,
            "chart_type": chart_type,
        }
        return render(request, 'monitor/prometheus_data.html', context, content_type="text/plain")


{'fraction_non_alphanumeric': 0.11029072450392248, 'fraction_numerical': 0.0055376095985233045, 'mean_word_length': 2.712328767123288, 'pattern_counts': {'":': 6, '<': 3, '<?xml version=': 0, '>': 3, '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': '510212', 'n_tokens_mistral': 686, 'n_tokens_neox': 648, 'n_words': 128}