<commit_before>from django.db import connection
from django.db import models

class Base(models.Model):
    name = models.CharField(max_length=200)
    ip_address = models.GenericIPAddressField()
    user = models.ForeignKey('clowder_account.ClowderUser')
    create = models.DateTimeField(auto_now_add=True)

    class Meta:
        abstract = True

    def __str__(self):
        return self.name


class Alert(Base):
    notify_at = models.DateTimeField(null=True, blank=True)


class Ping(Base):
    value = models.FloatField()
    status_passing = models.BooleanField(default=True)

    @classmethod
    def num_passing(cls, user):
        cursor = connection.cursor()
        cursor.execute('''
        SELECT COUNT(*) FROM (
          SELECT DISTINCT ON (name)
          name, MAX(clowder_server_ping.create),
          clowder_server_ping.status_passing FROM
          clowder_server_ping WHERE user_id=73
          GROUP BY name, clowder_server_ping.status_passing
        ) AS q1 WHERE q1.status_passing=true;
        ''')
        result = cursor.fetchone()
        return result[0] if result else 0

    @classmethod
    def num_failing(cls, user):
        cursor = connection.cursor()
        cursor.execute('''
        SELECT COUNT(*) FROM (
          SELECT DISTINCT ON (name)
          name, MAX(clowder_server_ping.create),
          clowder_server_ping.status_passing FROM
          clowder_server_ping WHERE user_id=73
          GROUP BY name, clowder_server_ping.status_passing
        ) AS q1 WHERE q1.status_passing=false;
        ''')
        result = cursor.fetchone()
        return result[0] if result else 0
<commit_msg>Crazy SQL Window Function To Do Proper Query<commit_after>from django.db import connection
from django.db import models

class Base(models.Model):
    name = models.CharField(max_length=200)
    ip_address = models.GenericIPAddressField()
    user = models.ForeignKey('clowder_account.ClowderUser')
    create = models.DateTimeField(auto_now_add=True)

    class Meta:
        abstract = True

    def __str__(self):
        return self.name


class Alert(Base):
    notify_at = models.DateTimeField(null=True, blank=True)


class Ping(Base):
    value = models.FloatField()
    status_passing = models.BooleanField(default=True)

    @classmethod
    def num_passing(cls, user):
        cursor = connection.cursor()
        cursor.execute('''
        SELECT COUNT(*) FROM (
          SELECT DISTINCT ON (name)
            name, last_value(clowder_server_ping.create) OVER wnd,
            user_id, status_passing FROM clowder_server_ping
          WHERE user_id = 73 WINDOW wnd AS (
            PARTITION BY user_id ORDER BY clowder_server_ping.create
            ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
          )
        ) AS q1 WHERE q1.status_passing=true;
        ''')
        result = cursor.fetchone()
        return result[0] if result else 0

    @classmethod
    def num_failing(cls, user):
        cursor = connection.cursor()
        cursor.execute('''
        SELECT COUNT(*) FROM (
          SELECT DISTINCT ON (name)
            name, last_value(clowder_server_ping.create) OVER wnd,
            user_id, status_passing FROM clowder_server_ping
          WHERE user_id = 73 WINDOW wnd AS (
            PARTITION BY user_id ORDER BY clowder_server_ping.create
            ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
          )
        ) AS q1 WHERE q1.status_passing=false;
        ''')
        result = cursor.fetchone()
        return result[0] if result else 0

{'fraction_non_alphanumeric': 0.08152937868990723, 'fraction_numerical': 0.00843407365757661, 'mean_word_length': 2.6012145748987856, 'pattern_counts': {'":': 0, '<': 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': '3591632', 'n_tokens_mistral': 1116, 'n_tokens_neox': 1059, 'n_words': 261}