<commit_before>import re
import urllib

from django.db.models import Count, F, Q

from sal.plugin import ReportPlugin


REPORT_Q = Q(pluginscriptsubmission__plugin='MunkiInfo')
REPO_Q = Q(pluginscriptsubmission__pluginscriptrow__pluginscript_name='SoftwareRepoURL')
DATA_F = F('pluginscriptsubmission__pluginscriptrow__pluginscript_data')
DATA = 'pluginscript_data'


class MunkiInfo(ReportPlugin):

    class Meta(object):
        description = 'Information on Munki configuration.'
        plugin_type = 'report'
        width = 12

    def get_http_only(self, machines):
        return machines.filter(
            REPORT_Q, REPO_Q,
            pluginscriptsubmission__pluginscriptrow__pluginscript_data__startswith='http://')

    def get_https_only(self, machines):
        return machines.filter(
            REPORT_Q, REPO_Q,
            pluginscriptsubmission__pluginscriptrow__pluginscript_data__startswith='https://')

    def get_default_repo(self, machines):
        return machines.filter(
            REPORT_Q, REPO_Q,
            pluginscriptsubmission__pluginscriptrow__pluginscript_data__exact='http://munki')

    def get_client_certs(self, machines):
        return machines.filter(
            REPORT_Q,
            pluginscriptsubmission__pluginscriptrow__pluginscript_name='UseClientCertificate',
            pluginscriptsubmission__pluginscriptrow__pluginscript_data='True')

    def get_context(self, machines, group_type=None, group_id=None):
        http_only = self.get_http_only(machines).count()
        https_only = self.get_https_only(machines).count()
        http_munki = self.get_default_repo(machines).count()

        repo_urls = (
            machines
            .filter(REPORT_Q, REPO_Q)
            .annotate(pluginscript_data=DATA_F)
            .values(DATA)
            .annotate(count=Count(DATA))
            .order_by(DATA))

        for url in repo_urls:
            url['item_link'] = 'repo_urls?URL="%s"' % urllib.quote(url[DATA], safe='')

        package_urls = (
            machines
            .filter(
                REPORT_Q,
                pluginscriptsubmission__pluginscriptrow__pluginscript_name='PackageURL')
            .annotate(pluginscript_data=DATA_F)
            .values(DATA)
            .exclude(pluginscript_data='None')
            .annotate(count=Count(DATA))
            .order_by(DATA))

        package_url_prefix = 'package_urls?URL='
        for url in package_urls:
            url['item_link'] = package_url_prefix + urllib.quote(url[DATA])

        manifest_urls = (
            machines
            .filter(
                REPORT_Q,
                pluginscriptsubmission__pluginscriptrow__pluginscript_name='ManifestURL')
            .annotate(pluginscript_data=DATA_F)
            .values(DATA).exclude(pluginscript_data='None')
            .annotate(count=Count(DATA))
            .order_by(DATA))

        manifest_url_prefix = 'manifest_urls?URL='
        for url in manifest_urls:
            url['item_link'] = manifest_url_prefix + urllib.quote(url[DATA])

        catalog_urls = (
            machines
            .filter(
                REPORT_Q,
                pluginscriptsubmission__pluginscriptrow__pluginscript_name='CatalogURL')
            .annotate(pluginscript_data=DATA_F)
            .values(DATA)
            .exclude(pluginscript_data='None')
            .annotate(count=Count(DATA))
            .order_by(DATA))

        catalog_url_prefix = 'catalog_urls?URL='
        for url in catalog_urls:
            url['item_link'] = catalog_url_prefix + urllib.quote(url[DATA])

        client_certs = self.get_client_certs(machines).count()

        context = {
            'http_only': http_only,
            'https_only': https_only,
            'http_munki': http_munki,
            'repo_urls': repo_urls,
            'catalog_urls': catalog_urls,
            'manifest_urls': manifest_urls,
            'package_urls': package_urls,
            'client_certs': client_certs,
            'group_type': group_type,
            'group_id': group_id
        }
        return context

    def filter_machines(self, machines, data):
        if data.startswith('http_only?'):
            machines = self.get_http_only(machines)
            title = 'Machines using HTTP'
<commit_msg>Minor cleanup of munkiinfo report.<commit_after>import re
import urllib

from django.db.models import Count, F, Q

from sal.plugin import ReportPlugin


REPORT_Q = Q(pluginscriptsubmission__plugin='MunkiInfo')
REPO_Q = Q(pluginscriptsubmission__pluginscriptrow__pluginscript_name='SoftwareRepoURL')
DATA_F = F('pluginscriptsubmission__pluginscriptrow__pluginscript_data')
DATA = 'pluginscript_data'


class MunkiInfo(ReportPlugin):

    class Meta(object):
        description = 'Information on Munki configuration.'

    def get_http_only(self, machines):
        return machines.filter(
            REPORT_Q, REPO_Q,
            pluginscriptsubmission__pluginscriptrow__pluginscript_data__startswith='http://')

    def get_https_only(self, machines):
        return machines.filter(
            REPORT_Q, REPO_Q,
            pluginscriptsubmission__pluginscriptrow__pluginscript_data__startswith='https://')

    def get_default_repo(self, machines):
        return machines.filter(
            REPORT_Q, REPO_Q,
            pluginscriptsubmission__pluginscriptrow__pluginscript_data__exact='http://munki')

    def get_client_certs(self, machines):
        return machines.filter(
            REPORT_Q,
            pluginscriptsubmission__pluginscriptrow__pluginscript_name='UseClientCertificate',
            pluginscriptsubmission__pluginscriptrow__pluginscript_data='True')

    def get_context(self, machines, group_type=None, group_id=None):
        http_only = self.get_http_only(machines).count()
        https_only = self.get_https_only(machines).count()
        http_munki = self.get_default_repo(machines).count()

        repo_urls = (
            machines
            .filter(REPORT_Q, REPO_Q)
            .annotate(pluginscript_data=DATA_F)
            .values(DATA)
            .annotate(count=Count(DATA))
            .order_by(DATA))

        for url in repo_urls:
            url['item_link'] = 'repo_urls?URL="%s"' % urllib.quote(url[DATA], safe='')

        package_urls = (
            machines
            .filter(
                REPORT_Q,
                pluginscriptsubmission__pluginscriptrow__pluginscript_name='PackageURL')
            .annotate(pluginscript_data=DATA_F)
            .values(DATA)
            .exclude(pluginscript_data='None')
            .annotate(count=Count(DATA))
            .order_by(DATA))

        package_url_prefix = 'package_urls?URL='
        for url in package_urls:
            url['item_link'] = package_url_prefix + urllib.quote(url[DATA])

        manifest_urls = (
            machines
            .filter(
                REPORT_Q,
                pluginscriptsubmission__pluginscriptrow__pluginscript_name='ManifestURL')
            .annotate(pluginscript_data=DATA_F)
            .values(DATA).exclude(pluginscript_data='None')
            .annotate(count=Count(DATA))
            .order_by(DATA))

        manifest_url_prefix = 'manifest_urls?URL='
        for url in manifest_urls:
            url['item_link'] = manifest_url_prefix + urllib.quote(url[DATA])

        catalog_urls = (
            machines
            .filter(
                REPORT_Q,
                pluginscriptsubmission__pluginscriptrow__pluginscript_name='CatalogURL')
            .annotate(pluginscript_data=DATA_F)
            .values(DATA)
            .exclude(pluginscript_data='None')
            .annotate(count=Count(DATA))
            .order_by(DATA))

        catalog_url_prefix = 'catalog_urls?URL='
        for url in catalog_urls:
            url['item_link'] = catalog_url_prefix + urllib.quote(url[DATA])

        client_certs = self.get_client_certs(machines).count()

        context = {
            'http_only': http_only,
            'https_only': https_only,
            'http_munki': http_munki,
            'repo_urls': repo_urls,
            'catalog_urls': catalog_urls,
            'manifest_urls': manifest_urls,
            'package_urls': package_urls,
            'client_certs': client_certs,
            'group_type': group_type,
            'group_id': group_id
        }
        return context

    def filter(self, machines, data):
        if data.startswith('http_only?'):
            machines = self.get_http_only(machines)
            title = 'Machines using HTTP'

{'fraction_non_alphanumeric': 0.09155920281359906, 'fraction_numerical': 0.00023446658851113716, 'mean_word_length': 2.4622564935064934, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '7304468', 'n_tokens_mistral': 2507, 'n_tokens_neox': 2475, 'n_words': 374}