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

from django.db import models, migrations
from django.db.models import Count

# language=SQL
TRIGGER_SQL = """
---------------------------------------------------------------------------------
-- Increment or decrement a system label
---------------------------------------------------------------------------------
CREATE OR REPLACE FUNCTION
  temba_increment_system_label(_org_id INT, _label_type CHAR(1), _add BOOLEAN)
RETURNS VOID AS $$
DECLARE
  _label_id INT;
BEGIN
  IF _add THEN
    INSERT INTO msgs_systemlabel("org_id", "label_type", "count") VALUES(_org_id, _label_type, 1);
  ELSE
    INSERT INTO msgs_systemlabel("org_id", "label_type", "count") VALUES(_org_id, _label_type, -1);
  END IF;

  PERFORM temba_maybe_squash_systemlabel(_org_id, _label_type);
END;
$$ LANGUAGE plpgsql;

----------------------------------------------------------------------------------
-- Every 100 inserts or so this will squash the label by gathering the counts
----------------------------------------------------------------------------------
CREATE OR REPLACE FUNCTION temba_maybe_squash_systemlabel(_org_id INTEGER, _label_type CHAR(1))
RETURNS VOID AS $$
BEGIN
  IF RANDOM() < .01 THEN
    WITH deleted as (DELETE FROM msgs_systemlabel
      WHERE "org_id" = _org_id AND "label_type" = _label_type
      RETURNING "count")<commit_msg>Only squash every 1000 inserts<commit_after># -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations
from django.db.models import Count

# language=SQL
TRIGGER_SQL = """
---------------------------------------------------------------------------------
-- Increment or decrement a system label
---------------------------------------------------------------------------------
CREATE OR REPLACE FUNCTION
  temba_increment_system_label(_org_id INT, _label_type CHAR(1), _add BOOLEAN)
RETURNS VOID AS $$
DECLARE
  _label_id INT;
BEGIN
  IF _add THEN
    INSERT INTO msgs_systemlabel("org_id", "label_type", "count") VALUES(_org_id, _label_type, 1);
  ELSE
    INSERT INTO msgs_systemlabel("org_id", "label_type", "count") VALUES(_org_id, _label_type, -1);
  END IF;

  PERFORM temba_maybe_squash_systemlabel(_org_id, _label_type);
END;
$$ LANGUAGE plpgsql;

----------------------------------------------------------------------------------
-- Every 100 inserts or so this will squash the label by gathering the counts
----------------------------------------------------------------------------------
CREATE OR REPLACE FUNCTION temba_maybe_squash_systemlabel(_org_id INTEGER, _label_type CHAR(1))
RETURNS VOID AS $$
BEGIN
  IF RANDOM() < .001 THEN
    WITH deleted as (DELETE FROM msgs_systemlabel
      WHERE "org_id" = _org_id AND "label_type" = _label_type
      RETURNING "count")
{'fraction_non_alphanumeric': 0.29697396199859255, 'fraction_numerical': 0.008796622097114707, 'mean_word_length': 1.7682570593962998, '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': '4885242', 'n_tokens_mistral': 822, 'n_tokens_neox': 756, 'n_words': 229}