<commit_before># -*- coding: utf-8 -*-
from importlib import import_module

from django.db import models
from django.utils import timezone
from jsonbfield.fields import JSONField as JSONBField

from kpi.fields import KpiUidField


class Hook(models.Model):

    # Export types
    XML = "xml"
    JSON = "json"

    # Authentication levels
    NO_AUTH = "no_auth"
    BASIC_AUTH = "basic_auth"

    # Export types list
    EXPORT_TYPE_CHOICES = (
        (XML, XML),
        (JSON, JSON)
    )

    # Authentication levels list
    SECURITY_LEVEL_CHOICES = (
        (NO_AUTH, NO_AUTH),
        (BASIC_AUTH, BASIC_AUTH)
    )

    asset = models.ForeignKey("kpi.Asset", related_name="hooks", on_delete=models.CASCADE)
    uid = KpiUidField(uid_prefix="h")
    name = models.CharField(max_length=255, blank=False)
    endpoint = models.CharField(max_length=500, blank=False)
    active = models.BooleanField(default=True)
    export_type = models.CharField(choices=EXPORT_TYPE_CHOICES, default=JSON, max_length=10)
    security_level = models.CharField(choices=SECURITY_LEVEL_CHOICES, default=NO_AUTH, max_length=10)
    settings = JSONBField(default=dict)
    date_created = models.DateTimeField(auto_now_add=True)
    date_modified = models.DateTimeField(auto_now_add=True)

    class Meta:
        ordering = ["name"]

    def __init__(self, *args, **kwargs):
        self.__totals = {}
        return super(Hook, self).__init__(*args, **kwargs)

    def save(self, *args, **kwargs):
        # Update date_modified each time object is saved
        self.date_modified = timezone.now()
        super(Hook, self).save(*args, **kwargs)

    def __unicode__(self):
        return u"%s:%s - %s" % (self.asset, self.name, self.endpoint)

    def get_service_definition(self):
        mod = import_module("hook.services.service_{}".format(self.export_type))
        return getattr(mod, "ServiceDefinition")

    @property
    def success_count(self):
        if not self.__totals:
            self._get_totals()
        return self.__totals.get(True)

    @property
    def failed_count(self):
        if not self.__totals:
            self._get_totals()
        return self.__totals.get(False)

    def _get_totals(self):
        # TODO add some cache
        queryset = self.logs.values("success").annotate(values_count=models.Count("success"))
        queryset.query.clear_ordering(True)
        for record in queryset:
            self.__totals[record.get("success")] = record.get("values_count")

    def reset_totals(self):
        # TODO remove cache when it's enabled
        self.__totals = {}<commit_msg>Fixed hook logs success and fails totals when no requets have been made yet<commit_after># -*- coding: utf-8 -*-
from importlib import import_module

from django.db import models
from django.utils import timezone
from jsonbfield.fields import JSONField as JSONBField

from kpi.fields import KpiUidField


class Hook(models.Model):

    # Export types
    XML = "xml"
    JSON = "json"

    # Authentication levels
    NO_AUTH = "no_auth"
    BASIC_AUTH = "basic_auth"

    # Export types list
    EXPORT_TYPE_CHOICES = (
        (XML, XML),
        (JSON, JSON)
    )

    # Authentication levels list
    SECURITY_LEVEL_CHOICES = (
        (NO_AUTH, NO_AUTH),
        (BASIC_AUTH, BASIC_AUTH)
    )

    asset = models.ForeignKey("kpi.Asset", related_name="hooks", on_delete=models.CASCADE)
    uid = KpiUidField(uid_prefix="h")
    name = models.CharField(max_length=255, blank=False)
    endpoint = models.CharField(max_length=500, blank=False)
    active = models.BooleanField(default=True)
    export_type = models.CharField(choices=EXPORT_TYPE_CHOICES, default=JSON, max_length=10)
    security_level = models.CharField(choices=SECURITY_LEVEL_CHOICES, default=NO_AUTH, max_length=10)
    settings = JSONBField(default=dict)
    date_created = models.DateTimeField(auto_now_add=True)
    date_modified = models.DateTimeField(auto_now_add=True)

    class Meta:
        ordering = ["name"]

    def __init__(self, *args, **kwargs):
        self.__totals = {}
        return super(Hook, self).__init__(*args, **kwargs)

    def save(self, *args, **kwargs):
        # Update date_modified each time object is saved
        self.date_modified = timezone.now()
        super(Hook, self).save(*args, **kwargs)

    def __unicode__(self):
        return u"%s:%s - %s" % (self.asset, self.name, self.endpoint)

    def get_service_definition(self):
        mod = import_module("hook.services.service_{}".format(self.export_type))
        return getattr(mod, "ServiceDefinition")

    @property
    def success_count(self):
        if not self.__totals:
            self._get_totals()
        return self.__totals.get(True)

    @property
    def failed_count(self):
        if not self.__totals:
            self._get_totals()
        return self.__totals.get(False)

    def _get_totals(self):
        # TODO add some cache
        queryset = self.logs.values("success").annotate(values_count=models.Count("success"))
        queryset.query.clear_ordering(True)

        # Initialize totals
        self.__totals = {
            False: 0,
            True: 0
        }
        for record in queryset:
            self.__totals[record.get("success")] = record.get("values_count")

    def reset_totals(self):
        # TODO remove cache when it's enabled
        self.__totals = {}
{'fraction_non_alphanumeric': 0.10989418971598292, 'fraction_numerical': 0.004455169853350659, 'mean_word_length': 3.0420105026256565, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, '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': '7178074', 'n_tokens_mistral': 1719, 'n_tokens_neox': 1653, 'n_words': 358}