Module: Labimotion::DatasetHelpers

Extended by:
Grape::API::Helpers
Defined in:
lib/labimotion/helpers/dataset_helpers.rb

Overview

DatasetHelpers

Instance Method Summary collapse

Instance Method Details

#create_ai_dataset_klass(params, current_user) ⇒ Object

Create a new (inactive) dataset klass whose properties template is generated by an LLM from a CHMO ontology term plus optional description, reference links and uploaded files. The admin reviews/edits the generated template in the designer and activates it (human-in-the-loop).



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/labimotion/helpers/dataset_helpers.rb', line 65

def create_ai_dataset_klass(params, current_user)
  ols_term_id = params[:ols_term_id].to_s.split('|').first.to_s.strip
  raise 'An ontology term (CHMO) is required' if ols_term_id.blank?

  if Labimotion::DatasetKlass.find_by(ols_term_id: ols_term_id).present?
    return { status: 'error',
             message: "A dataset template already exists for #{ols_term_id}. Edit it in the designer instead." }
  end

  if Array(params[:files]).size > Labimotion::AiTemplate::MAX_FILES
    return { status: 'error', message: "Too many files (max #{Labimotion::AiTemplate::MAX_FILES})." }
  end

  overrides = ai_user_overrides(current_user)
  ai = Labimotion::AiTemplate.generate(
    ols_term_id: params[:ols_term_id],
    desc: params[:desc],
    cols: params[:cols],
    references: params[:references],
    files: params[:files],
    **overrides
  )

  uuid = SecureRandom.uuid
  label = ai['label'].presence ||
          params[:ols_term_id].to_s.split('|').last.to_s.strip.presence ||
          'AI dataset template'
  # property-base schema requires pkg, uuid, klass, layers, version, identifier.
  properties_template = {
    'uuid' => uuid,
    'klass' => 'DatasetKlass',
    'pkg' => Labimotion::Utils.pkg(nil),
    'version' => '1.0.0',
    'identifier' => uuid,
    'layers' => ai['layers'],
    'select_options' => ai['select_options'],
    'metadata' => ai['metadata']
  }
  attributes = {
    'uuid' => uuid,
    'label' => label,
    'desc' => params[:desc].presence || ai['label'].presence,
    'ols_term_id' => ols_term_id,
    'place' => ((Labimotion::DatasetKlass.all.length * 10) || 0) + 10,
    'is_active' => false,
    'released_at' => DateTime.now,
    'properties_template' => properties_template,
    'properties_release' => properties_template,
    'created_by' => current_user.id
  }

  ds = Labimotion::DatasetKlass.create!(attributes)
  ds.create_klasses_revision(current_user)
  { status: 'success',
    message: "The AI dataset template [#{label}] has been created as inactive. Review and activate it in the designer." }
rescue StandardError => e
  Labimotion.log_exception(e, current_user)
  { status: 'error', message: e.message }
end

#create_repo_klass(params, current_user, origin) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/labimotion/helpers/dataset_helpers.rb', line 18

def create_repo_klass(params, current_user, origin)
  response = Labimotion::TemplateHub.fetch_identifier('DatasetKlass', params[:identifier], origin)
  attributes = response.slice('ols_term_id', 'label', 'desc', 'uuid', 'identifier', 'properties_release', 'version', 'metadata') # .except(:id, :is_active, :place, :created_by, :created_at, :updated_at)
  attributes['properties_release']['identifier'] = attributes['identifier']
  attributes['properties_template'] = attributes['properties_release']
  attributes['place'] = ((Labimotion::DatasetKlass.all.length * 10) || 0) + 10
  attributes['is_active'] = false
  attributes['updated_by'] = current_user.id
  attributes['sync_by'] = current_user.id
  attributes['sync_time'] = DateTime.now
  dataset_klass = Labimotion::DatasetKlass.find_by(ols_term_id: attributes['ols_term_id'])
  if dataset_klass.present?
    if dataset_klass['uuid'] == attributes['uuid'] && dataset_klass['version'] == attributes['version']
      { status: 'success', message: "This dataset: #{attributes['label']} has the latest version!" }
    else
      ds = Labimotion::DatasetKlass.find_by(ols_term_id: attributes['ols_term_id'])
      # Upgrading bumps the version and cuts a revision — :release, owner-only; the gate
      # lives on the update branch because only the ols_term_id lookup decides it.
      authorize_klass!(ds, :release)
      ds.update!(attributes)
      ds.create_klasses_revision(current_user)
      { status: 'success',
        message: "This dataset: [#{attributes['label']}] has been upgraded to the version: #{attributes['version']}!" }
    end
  else
    attributes['created_by'] = current_user.id
    # The owner row shares the create's transaction: a klass committed without it is
    # owner-less, which falls open to the legacy designer-wide gate.
    ds = Labimotion::DatasetKlass.transaction do
      Labimotion::DatasetKlass.create!(attributes).tap do |klz|
        Labimotion::KlassShare.seed_owner!(klz, current_user.id)
      end
    end
    ds.create_klasses_revision(current_user)
    { status: 'success',
      message: "The dataset: #{attributes['label']} has been created using version: #{attributes['version']}!" }
  end
rescue StandardError => e
  Labimotion.log_exception(e, current_user)
  # { error: e.message }
  raise e
end

#find_best_match_template(ols_term_id) ⇒ Object



159
160
161
162
163
164
165
166
# File 'lib/labimotion/helpers/dataset_helpers.rb', line 159

def find_best_match_template(ols_term_id)
  result = Labimotion::TemplateMatcher.find_best_match(ols_term_id)
  if result[:template]
    build_template_response(result[:template], result[:match_type], result[:info_messages])
  else
    { error: '', data: {}, info: "No matching template found for term id [#{ols_term_id}]" }
  end
end

#klass_list(is_active, displayed_in_list = false) ⇒ Object



9
10
11
12
13
14
15
16
# File 'lib/labimotion/helpers/dataset_helpers.rb', line 9

def klass_list(is_active, displayed_in_list = false)
  scope = displayed_in_list ? Labimotion::DatasetKlass.for_list_display : Labimotion::DatasetKlass.all
  if is_active == true
    scope.where(is_active: true).order('place') || []
  else
    scope.order('place') || []
  end
end

#refine_ai_dataset_klass(params, current_user) ⇒ Object

Refine an existing dataset template with AI and return the revised template (label, layers, select_options) plus a one-line summary of the change. Nothing is persisted — the admin applies the result into the designer's working copy and saves it there (human-in-the-loop, mirrors the create flow).



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/labimotion/helpers/dataset_helpers.rb', line 129

def refine_ai_dataset_klass(params, current_user)
  instruction = params[:instruction].to_s.strip
  raise 'An instruction is required' if instruction.blank?

  current = {
    'label' => params[:label],
    'layers' => params[:layers] || {},
    'select_options' => params[:select_options] || {}
  }
  overrides = ai_user_overrides(current_user)
  ai = Labimotion::AiTemplate.refine(
    current: current,
    instruction: instruction,
    ols_term_id: ai_term_with_label(params[:ols_term_id]),
    history: params[:history],
    cols: params[:cols],
    **overrides
  )
  {
    status: 'success',
    label: ai['label'].presence || params[:label],
    layers: ai['layers'],
    select_options: ai['select_options'],
    summary: ai['summary']
  }
rescue StandardError => e
  Labimotion.log_exception(e, current_user)
  { status: 'error', message: e.message }
end