Class: DiverDown::Web::Action

Inherits:
Object
  • Object
show all
Defined in:
lib/diver_down/web/action.rb

Constant Summary collapse

Pagination =
Data.define(
  :current_page,
  :total_pages,
  :total_count,
  :per
)
M =
Mutex.new

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(store:, metadata:, blob_prefix:) ⇒ Action

Returns a new instance of Action.

Parameters:



21
22
23
24
25
26
27
# File 'lib/diver_down/web/action.rb', line 21

def initialize(store:, metadata:, blob_prefix:)
  @store = store
  @metadata = 
  @blob_prefix = blob_prefix

  reload
end

Instance Attribute Details

#storeObject (readonly)

Returns the value of attribute store.



17
18
19
# File 'lib/diver_down/web/action.rb', line 17

def store
  @store
end

Instance Method Details

#combine_definitions(bit_id, compound, concentrate, only_module, remove_internal_sources, focus_modules, modules) ⇒ Object

GET /api/definitions/:bit_id.json

Parameters:

  • bit_id (Integer)
  • compound (Boolean)
  • concentrate (Boolean)
  • only_module (Boolean)


248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# File 'lib/diver_down/web/action.rb', line 248

def combine_definitions(bit_id, compound, concentrate, only_module, remove_internal_sources, focus_modules, modules)
  ids = DiverDown::Web::BitId.bit_id_to_ids(bit_id)

  valid_ids = ids.select do
    @store.key?(_1)
  end

  definition, titles = case valid_ids.length
                       when 0
                         return not_found
                       when 1
                         definition = @store.get(ids[0])
                         [definition, [definition.title]]
                       else
                         definitions = valid_ids.map { @store.get(_1) }
                         definition = DiverDown::Definition.combine(definition_group: nil, title: 'combined', definitions:)
                         [definition, definitions.map(&:title)]
                       end

  if definition
    # Resolve source aliases
    resolved_definition = @source_alias_resolver.resolve(definition)
    # Filter sources and dependencies by condition
    resolved_definition = DiverDown::Web::DefinitionFilter.new(@metadata, focus_modules:, modules:, remove_internal_sources:).filter(resolved_definition)

    render_combined_definition(
      valid_ids,
      resolved_definition,
      titles,
      compound:,
      concentrate:,
      only_module:
    )
  else
    not_found
  end
end

#configurationObject

GET /api/configuration.json



191
192
193
194
195
# File 'lib/diver_down/web/action.rb', line 191

def configuration
  json(
    blob_prefix: @blob_prefix
  )
end

#definitions(page:, per:, title:, source:, definition_group:) ⇒ Object

GET /api/definitions.json

Parameters:

  • page (Integer)
  • per (Integer)
  • title (String)
  • source (String)
  • definition_group (String)


164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/diver_down/web/action.rb', line 164

def definitions(page:, per:, title:, source:, definition_group:)
  definition_enumerator = DiverDown::Web::DefinitionEnumerator.new(@store, title:, source:, definition_group:)
  definitions, pagination = paginate(definition_enumerator, page, per)

  json(
    definitions: definitions.map do |definition|
      {
        id: definition.store_id,
        definition_group: definition.definition_group,
        title: definition.title,
        sources_count: definition.sources.size,
        unclassified_sources_count: definition.sources.reject { @metadata.source(_1.source_name).module? }.size,
      }
    end,
    pagination: pagination.to_h
  )
end

#initialization_status(total) ⇒ Object

GET /api/initialization_status.json



183
184
185
186
187
188
# File 'lib/diver_down/web/action.rb', line 183

def initialization_status(total)
  json(
    total:,
    loaded: @store.size
  )
end

#module(modulee) ⇒ Object

GET /api/modules/:modulee.json

Parameters:

  • modulee (String)


106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
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
# File 'lib/diver_down/web/action.rb', line 106

def module(modulee)
  module_dependency_map = fetch_module_dependency_map

  unless module_dependency_map.key?(modulee)
    return not_found
  end

  module_dependency = module_dependency_map.fetch(modulee)

  json(
    module: modulee,
    module_dependencies: module_dependency.module_dependencies.compact.sort,
    module_reverse_dependencies: module_dependency.module_reverse_dependencies.compact.sort,
    sources: module_dependency.sources.map do |source|
      {
        source_name: source.source_name,
        module: @metadata.source(source.source_name).module,
        memo: @metadata.source(source.source_name).memo,
        dependencies: source.dependencies.map do |dependency|
          {
            source_name: dependency.source_name,
            module: @metadata.source(dependency.source_name).module,
            dependency_type: @metadata.source(source.source_name).dependency_type(dependency.source_name),
            method_ids: dependency.method_ids.sort.map do |method_id|
              {
                context: method_id.context,
                name: method_id.name,
                paths: method_id.paths.sort,
              }
            end,
          }
        end,
      }
    end,
    source_reverse_dependencies: module_dependency.source_reverse_dependencies.map do |source|
      {
        source_name: source.source_name,
        module: @metadata.source(source.source_name).module,
        memo: @metadata.source(source.source_name).memo,
        dependencies: source.dependencies.map do |dependency|
          {
            source_name: dependency.source_name,
            module: @metadata.source(dependency.source_name).module,
            dependency_type: @metadata.source(source.source_name).dependency_type(dependency.source_name),
          }
        end,
      }
    end
  )
end

#modulesObject

GET /api/modules.json



90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/diver_down/web/action.rb', line 90

def modules
  # Hash{ DiverDown::Definition::Modulee => Set<Integer> }
  modules = Set.new

  @store.combined_definition.sources.each do |source|
    modulee = @metadata.source(source.source_name).module
    modules.add(modulee) unless modulee.nil?
  end

  json(
    modules: modules.sort
  )
end

#not_foundArray[Integer, Hash, Array]

Returns:

  • (Array[Integer, Hash, Array])


408
409
410
# File 'lib/diver_down/web/action.rb', line 408

def not_found
  [404, { 'content-type' => 'text/plain' }, ['not found']]
end

#pidObject

GET /api/pid.json



236
237
238
239
240
# File 'lib/diver_down/web/action.rb', line 236

def pid
  json(
    pid: Process.pid
  )
end

#reload_cacheArray[Integer, Hash, Array]

Returns:

  • (Array[Integer, Hash, Array])


401
402
403
404
405
# File 'lib/diver_down/web/action.rb', line 401

def reload_cache
  @metadata.reload
  reload
  json({})
end

#set_memo(source_name, memo) ⇒ Object

POST /api/sources/:source_name/memo.json

Parameters:

  • source_name (String)
  • memo (String)


382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
# File 'lib/diver_down/web/action.rb', line 382

def set_memo(source_name, memo)
  found_source = @store.any? do |_, definition|
    definition.sources.any? do |source|
      source.source_name == source_name
    end
  end

  if found_source
    @metadata.source(source_name).memo = memo
    @metadata.flush
    reload

    json({})
  else
    not_found
  end
end

#set_module(source_name, modulee) ⇒ Object

POST /api/sources/:source_name/module.json

Parameters:

  • source_name (String)
  • module (Array<String>)


360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
# File 'lib/diver_down/web/action.rb', line 360

def set_module(source_name, modulee)
  found_source = @store.any? do |_, definition|
    definition.sources.any? do |source|
      source.source_name == source_name
    end
  end

  if found_source
    @metadata.source(source_name).module = modulee
    @metadata.flush
    reload

    json({})
  else
    not_found
  end
end

#source(source_name) ⇒ Object

GET /api/sources/:source_name.json

Parameters:

  • source_name (String)


289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
# File 'lib/diver_down/web/action.rb', line 289

def source(source_name)
  found_sources = []
  related_definitions = []
  reverse_dependencies = Hash.new { |h, dependency_source_name| h[dependency_source_name] = DiverDown::Definition::Dependency.new(source_name: dependency_source_name) }

  @store.each do |id, definition|
    found_source = nil

    definition.sources.each do |definition_source|
      found_source = definition_source if definition_source.source_name == source_name

      found_reverse_dependencies = definition_source.dependencies.select do |dependency|
        dependency.source_name == source_name
      end

      found_reverse_dependencies.each do |dependency|
        reverse_dependency = reverse_dependencies[dependency.source_name]

        dependency.method_ids.each do |method_id|
          reverse_method_id = reverse_dependency.find_or_build_method_id(name: method_id.name, context: method_id.context)
          reverse_method_id.paths.merge(method_id.paths)
        end
      end
    end

    if found_source
      found_sources << found_source
      related_definitions << [id, definition]
    end
  end

  return not_found if related_definitions.empty?

  modulee = if found_sources.empty?
              nil
            else
              source = DiverDown::Definition::Source.combine(*found_sources)
              @metadata.source(source.source_name).module
            end

  json(
    source_name:,
    resolved_alias: @metadata.source_alias.resolve_alias(source_name),
    memo: @metadata.source(source_name).memo,
    module: modulee,
    related_definitions: related_definitions.map do |id, definition|
      {
        id:,
        title: definition.title,
      }
    end,
    reverse_dependencies: reverse_dependencies.values.map { |reverse_dependency|
      {
        source_name: reverse_dependency.source_name,
        module: @metadata.source(reverse_dependency.source_name).module,
        method_ids: reverse_dependency.method_ids.sort.map do |method_id|
          {
            context: method_id.context,
            name: method_id.name,
            paths: method_id.paths.sort,
          }
        end,
      }
    }
  )
end

#source_aliasesObject

GET /api/source_aliases.json



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/diver_down/web/action.rb', line 30

def source_aliases
  source_aliases = @metadata.source_alias.to_h.map do |alias_name, source_names|
    {
      alias_name:,
      source_names:,
    }
  end

  json(
    source_aliases:
  )
end

#sourcesObject

GET /api/sources.json



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/diver_down/web/action.rb', line 65

def sources
  source_names = Set.new

  @store.combined_definition.sources.each do |source|
    source_names.add(source.source_name)
  end

  classified_sources_count = source_names.count { @metadata.source(_1).module? }

  json(
    sources: source_names.sort.map do |source_name|
       = @metadata.source(source_name)

      {
        source_name:,
        resolved_alias: @metadata.source_alias.resolve_alias(source_name),
        memo: .memo,
        module: .module,
      }
    end,
    classified_sources_count:
  )
end

#update_module_dependency_type(from_module, to_module, dependency_type) ⇒ Object

POST /api/modules/:from_module/dependency_types/:to_module.json



198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/diver_down/web/action.rb', line 198

def update_module_dependency_type(from_module, to_module, dependency_type)
  module_dependency_map = fetch_module_dependency_map

  unless module_dependency_map.key?(from_module) && module_dependency_map.key?(to_module)
    return not_found
  end

  module_dependency = module_dependency_map.fetch(from_module)
  module_dependency.sources.each do |source|
     = @metadata.source(source.source_name)

    source.dependencies.each do |dependency|
      next unless @metadata.source(dependency.source_name).module == to_module

      .update_dependency_type(dependency.source_name, dependency_type)
    end
  end

  @metadata.flush

  json({})
end

#update_source_alias(alias_name, old_alias_name, source_names) ⇒ Object

POST /api/source_aliases.json

Parameters:

  • alias_name (String)
  • old_alias_name (String)
  • source_names (Array<String>)


47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/diver_down/web/action.rb', line 47

def update_source_alias(alias_name, old_alias_name, source_names)
  @metadata.source_alias.transaction do
    unless old_alias_name.to_s.empty?
      # Delete old alias
      @metadata.source_alias.update_alias(old_alias_name, [])
    end

    @metadata.source_alias.update_alias(alias_name, source_names)
  end

  @metadata.flush

  json({})
rescue DiverDown::Web::Metadata::SourceAlias::ConflictError => e
  json_error(e.message)
end

#update_source_dependency_type(from_source_name, to_source_name, dependency_type) ⇒ Object

POST /api/sources/:from_source_name/dependency_types/:to_source_name.json



222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/diver_down/web/action.rb', line 222

def update_source_dependency_type(from_source_name, to_source_name, dependency_type)
  source = @metadata.source(from_source_name)

  begin
    source.update_dependency_type(to_source_name, dependency_type)
  rescue ArgumentError => e
    return json_error(e.message)
  end

  @metadata.flush
  json({})
end