<commit_before>namespace :notifications do
  desc 'Creates new system notification for all active users'
  task :new_system, [:title, :message] => :environment do |_, args|
    include NotificationsHelper

    if args.blank? ||
       args.empty? ||
       args[:title].blank? ||
       args[:message].blank?
      puts 'One or both of arguments are missing'
      return
    end

    title = args[:title]
    message = args[:message]

    puts 'Creating following system notification:'
    puts "    ***  #{title} ***"
    puts "    #{I18n.l(Time.now, format: :full)} | #{message}"

    create_system_notification(title, message)
  end
end
<commit_msg>added copy and delete tasks, remove obsolete new_system task<commit_after># frozen_string_literal: true

namespace :notifications do
  desc 'Copies system notifications to newly created data structure. IT SHOULD BE RUN ONE TIME ONLY'
  task copy_system_notifications: :environment do
    t0 = Time.now
    system_notifications = Notification
                           .where(type_of: :system_message)
                           .where(generator_user_id: nil)
                           .where.not('title like ?', 'Congratulations%')

    system_notifications.each do |system_notification|
      new_notification = SystemNotification.create!(
        source_id: -1,
        title: system_notification.title,
        description: system_notification.message,
        modal_title: system_notification.title,
        modal_body: system_notification.message,
        show_on_login: false,
        source_created_at: system_notification.created_at
      )

      created_at = system_notification.created_at

      sql = ' INSERT INTO user_system_notifications
          (
            user_id,
            created_at,
            updated_at,
            system_notification_id,
            seen_at,
            read_at
          )
          VALUES
        '
      user_notifications = UserNotification
                           .where(notification_id: system_notification.id)
      values_array = user_notifications.map do |user_notification|
        user_notification
          .slice(:user_id, :created_at, :updated_at)
          .merge(system_notification_id: new_notification.id)
          .merge(seen_at: created_at, read_at: created_at)
          .values
          .map { |v| "'#{v}'" }
          .join(',')
      end

      values_sql = values_array
                   .map { |v| "(#{v})" }
                   .join(',')

      sql += values_sql
      ActiveRecord::Base.connection.execute(sql)
    end

    t1 = Time.now
    puts "Task took #{t1 - t0}"
  end

  desc 'Removes obsolete system notifications from notifications table.'
  task delete_obsolete_system_notifications: :environment do
    system_notifications = Notification
                           .where(type_of: :system_message)
                           .where(generator_user_id: nil)
                           .where.not('title like ?', 'Congratulations%')
    UserNotification
      .where(notification_id: system_notifications.pluck(:id))
      .delete_all

    system_notifications.delete_all
  end
end

{'fraction_non_alphanumeric': 0.08783136378153944, 'fraction_numerical': 0.002235707441711913, 'mean_word_length': 2.259105098855359, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, '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': '7221104', 'n_tokens_mistral': 873, 'n_tokens_neox': 847, 'n_words': 203}