<commit_before># -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations


# language=SQL
TRIGGER_SQL = """
--------------------------------------------------------------------------------------
-- Increment or decrement the results count on counter of given type and ruleset uuid
--------------------------------------------------------------------------------------
CREATE OR REPLACE FUNCTION
  ureport_insert_results_counter(_org_id INT, _ruleset CHAR(36), _type VARCHAR, _count INT)
RETURNS VOID AS $$
BEGIN
  INSERT INTO polls_pollresultscounter("org_id", "ruleset", "type", "count") VALUES(_org_id, _ruleset, _type, _count);
  PERFORM ureport_may_be_squash_resultscounters(_org_id, _ruleset, _type);
END;
$$ LANGUAGE plpgsql;
-----------------------------------------------------------------------------
-- Every 100 inserts or so this will squash the counters by gathering
-----------------------------------------------------------------------------
CREATE OR REPLACE FUNCTION
  ureport_may_be_squash_resultscounters(_org_id INT, _ruleset CHAR(36), _type VARCHAR)
RETURNS VOID AS $$
BEGIN
  IF RANDOM() < .01 THEN
    WITH deleted as (DELETE FROM polls_pollresultscounter
      WHERE "org_id" = _org_id AND "type" = _type AND "ruleset" = _ruleset
      RETURNING "count")
      INSERT INTO polls_pollresultscounter("org_id", "ruleset", "type", "count")
      VALUES (_org_id, _ruleset, _type ,GREATEST(0, (SELECT SUM("count") FROM deleted)));
  END IF;
END;
$$ LANGUAGE plpgsql;
-----------------------------------------------------------------------------
-- Increment counters for a poll result
-----------------------------------------------------------------------------
CREATE OR REPLACE FUNCTION ureport_increment_counter_for_poll_result(_poll_result polls_pollresult, _add BOOLEAN)
RETURNS VOID AS $$
DECLARE
  _count INT;
BEGIN
  IF _add THEN
    _count = 1;
  ELSE<commit_msg>maybe instead of may be<commit_after># -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations


# language=SQL
TRIGGER_SQL = """
--------------------------------------------------------------------------------------
-- Increment or decrement the results count on counter of given type and ruleset uuid
--------------------------------------------------------------------------------------
CREATE OR REPLACE FUNCTION
  ureport_insert_results_counter(_org_id INT, _ruleset CHAR(36), _type VARCHAR, _count INT)
RETURNS VOID AS $$
BEGIN
  INSERT INTO polls_pollresultscounter("org_id", "ruleset", "type", "count") VALUES(_org_id, _ruleset, _type, _count);
  PERFORM ureport_maybe_squash_resultscounters(_org_id, _ruleset, _type);
END;
$$ LANGUAGE plpgsql;
-----------------------------------------------------------------------------
-- Every 100 inserts or so this will squash the counters by gathering
-----------------------------------------------------------------------------
CREATE OR REPLACE FUNCTION
  ureport_maybe_squash_resultscounters(_org_id INT, _ruleset CHAR(36), _type VARCHAR)
RETURNS VOID AS $$
BEGIN
  IF RANDOM() < .01 THEN
    WITH deleted as (DELETE FROM polls_pollresultscounter
      WHERE "org_id" = _org_id AND "type" = _type AND "ruleset" = _ruleset
      RETURNING "count")
      INSERT INTO polls_pollresultscounter("org_id", "ruleset", "type", "count")
      VALUES (_org_id, _ruleset, _type ,GREATEST(0, (SELECT SUM("count") FROM deleted)));
  END IF;
END;
$$ LANGUAGE plpgsql;
-----------------------------------------------------------------------------
-- Increment counters for a poll result
-----------------------------------------------------------------------------
CREATE OR REPLACE FUNCTION ureport_increment_counter_for_poll_result(_poll_result polls_pollresult, _add BOOLEAN)
RETURNS VOID AS $$
DECLARE
  _count INT;
BEGIN
  IF _add THEN
    _count = 1;
  ELSE
{'fraction_non_alphanumeric': 0.3131105398457584, 'fraction_numerical': 0.006169665809768638, 'mean_word_length': 1.6779077770130764, 'pattern_counts': {'":': 0, '<': 5, '<?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': '6000894', 'n_tokens_mistral': 1073, 'n_tokens_neox': 998, 'n_words': 313}