<commit_before>module Cellect
  module Server
    module Adapters
      module PanoptesAssociation
        class Workflow < ActiveRecord::Base
          has_many :subject_sets_workflows
          has_many :subject_sets, through: :subject_sets_workflows
        end

        class SubjectSetsWorkflow < ActiveRecord::Base
          belongs_to :workflow
          belongs_to :subject_set
        end

        class SubjectSet < ActiveRecord::Base
          has_many :subject_sets_workflows
          has_many :workflows, through: :subject_sets_workflows
          has_many :set_member_subjects
          has_many :subjects, through: :set_member_subjects
        end

        class SetMemberSubject < ActiveRecord::Base
          belongs_to :subject_set
          belongs_to :subject
          has_many :workflows, through: :subject_set
        end

        class UserSeenSubject < ActiveRecord::Base
        end
      end

      class Panoptes < Default
        def status
          { connected: ActiveRecord::Base.connected? }
        end

        def workflow_list(*names)
          workflow_data = with_connection do
            PanoptesAssociation::Workflow
              .select(:id, :prioritized, :pairwise, :grouped)
              .where(id: names)
          end

          workflow_data.map do |row|
            {
              'id' => row.id,
              'name' => "#{row.id}",
              'prioritized' => row.prioritized,
              'pairwise' => row.pairwise,
              'grouped' => row.grouped
            }
          end
        end

        def load_data_for(workflow_id)
          subject_data = with_connection do
            PanoptesAssociation::SetMemberSubject
              .joins(:workflows)
              .where(workflows: {id: workflow_id})
              .joins("LEFT OUTER JOIN subject_workflow_counts ON subject_workflow_counts.subject_id = set_member_subjects.subject_id")
              .where('subject_workflow_counts.retired_at IS NULL')
              .select(:subject_id, :priority, :subject_set_id)
              .distinct
          end

          subject_data.map do |row|
            {
              'id' => row.subject_id,
              'priority' => 1 / (row.priority || 1).to_f,
              'group_id' => row.subject_set_id
            }
          end<commit_msg>load workflow data by find each<commit_after>module Cellect
  module Server
    module Adapters
      module PanoptesAssociation
        class Workflow < ActiveRecord::Base
          has_many :subject_sets_workflows
          has_many :subject_sets, through: :subject_sets_workflows
        end

        class SubjectSetsWorkflow < ActiveRecord::Base
          belongs_to :workflow
          belongs_to :subject_set
        end

        class SubjectSet < ActiveRecord::Base
          has_many :subject_sets_workflows
          has_many :workflows, through: :subject_sets_workflows
          has_many :set_member_subjects
          has_many :subjects, through: :set_member_subjects
        end

        class SetMemberSubject < ActiveRecord::Base
          belongs_to :subject_set
          belongs_to :subject
          has_many :workflows, through: :subject_set
        end

        class UserSeenSubject < ActiveRecord::Base
        end
      end

      class Panoptes < Default
        def status
          { connected: ActiveRecord::Base.connected? }
        end

        def workflow_list(*names)
          workflow_data = with_connection do
            PanoptesAssociation::Workflow
              .select(:id, :prioritized, :pairwise, :grouped)
              .where(id: names)
          end

          [].tap do |rows|
            workflow_data.find_each do |w|
              rows << {
                'id' => w.id,
                'name' => "#{w.id}",
                'prioritized' => w.prioritized,
                'pairwise' => w.pairwise,
                'grouped' => w.grouped
              }
            end
          end
        end

        def load_data_for(workflow_id)
          subject_data = with_connection do
            PanoptesAssociation::SetMemberSubject
              .joins(:workflows)
              .where(workflows: {id: workflow_id})
              .joins("LEFT OUTER JOIN subject_workflow_counts ON subject_workflow_counts.subject_id = set_member_subjects.subject_id")
              .where('subject_workflow_counts.retired_at IS NULL')
              .select(:subject_id, :priority, :subject_set_id)
              .distinct
          end

          subject_data.map do |row|
            {
              'id' => row.subject_id,
              'priority' => 1 / (row.priority || 1).to_f,
              'group_id' => row.subject_set_id
            }
          end
{'fraction_non_alphanumeric': 0.07597361140668227, 'fraction_numerical': 0.0008512449457331347, 'mean_word_length': 1.7501462843768285, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 19, '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': '397502', 'n_tokens_mistral': 1333, 'n_tokens_neox': 1291, 'n_words': 273}