Skip to content

Commit

Permalink
divertdown-web will store data other than source, so change the format
Browse files Browse the repository at this point in the history
  • Loading branch information
alpaca-tc committed Jun 5, 2024
1 parent 3108b72 commit 2c59b94
Show file tree
Hide file tree
Showing 14 changed files with 446 additions and 347 deletions.
6 changes: 3 additions & 3 deletions exe/diver_down_web
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ option_parser = OptionParser.new do |opts|
Usage: diver_down_web [options]
Example:
diver_down_web --definition-dir /path/to/definitions --module-store /path/to/module_store.yml
diver_down_web --definition-dir /path/to/definitions --module-store /path/to/metadata.yml
Options:
BANNER
Expand All @@ -24,7 +24,7 @@ option_parser = OptionParser.new do |opts|
end

opts.on('--module-store PATH', 'Path to the module store') do |path|
options[:module_store] = path
options[:metadata] = path
end
end
option_parser.parse!(ARGV)
Expand All @@ -39,7 +39,7 @@ end
app = Rack::JSONBodyParser.new(
DiverDown::Web.new(
definition_dir: options.fetch(:definition_dir),
module_store: DiverDown::Web::ModuleStore.new(options[:module_store] || Tempfile.new(['module_store', '.yaml']))
metadata: DiverDown::Web::Metadata.new(options[:metadata] || Tempfile.new(['metadata', '.yaml']).path)
)
)

Expand Down
11 changes: 6 additions & 5 deletions lib/diver_down/web.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ class Web
WEB_DIR = File.expand_path('../../web', __dir__)

require 'diver_down/web/action'
require 'diver_down/web/alias_store'
require 'diver_down/web/definition_to_dot'
require 'diver_down/web/definition_enumerator'
require 'diver_down/web/bit_id'
require 'diver_down/web/module_store'
require 'diver_down/web/metadata'
require 'diver_down/web/indented_string_io'
require 'diver_down/web/definition_store'
require 'diver_down/web/definition_loader'
Expand All @@ -20,11 +21,11 @@ class Web
autoload :DevServerMiddleware, 'diver_down/web/dev_server_middleware'

# @param definition_dir [String]
# @param module_store [DiverDown::ModuleStore]
# @param metadata [DiverDown::Web::Metadata]
# @param store [DiverDown::Web::DefinitionStore]
def initialize(definition_dir:, module_store:, store: DiverDown::Web::DefinitionStore.new)
def initialize(definition_dir:, metadata:, store: DiverDown::Web::DefinitionStore.new)
@store = store
@module_store = module_store
@metadata = metadata
@files_server = Rack::Files.new(File.join(WEB_DIR))

definition_files = ::Dir["#{definition_dir}/**/*.{yml,yaml,msgpack,json}"].sort
Expand All @@ -37,7 +38,7 @@ def initialize(definition_dir:, module_store:, store: DiverDown::Web::Definition
# @return [Array[Integer, Hash, Array]]
def call(env)
request = Rack::Request.new(env)
action = DiverDown::Web::Action.new(store: @store, module_store: @module_store, request:)
action = DiverDown::Web::Action.new(store: @store, metadata: @metadata, request:)

case [request.request_method, request.path]
in ['GET', %r{\A/api/definitions\.json\z}]
Expand Down
42 changes: 22 additions & 20 deletions lib/diver_down/web/action.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ class Action
)

# @param store [DiverDown::Definition::Store]
# @param module_store [DiverDown::Web::ModuleStore]
# @param metadata [DiverDown::Web::Metadata]
# @param request [Rack::Request]
def initialize(store:, module_store:, request:)
def initialize(store:, metadata:, request:)
@store = store
@module_store = module_store
@metadata = metadata
@request = request
end

Expand All @@ -33,14 +33,16 @@ def sources
end
# rubocop:enable Style/HashEachMethods

classified_sources_count = source_names.count { @module_store.classified?(_1) }
classified_sources_count = source_names.count { @metadata.source(_1).modules? }

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

{
source_name:,
memo: @module_store.get_memo(source_name),
modules: @module_store.get_modules(source_name).map do |module_name|
memo: source.memo,
modules: source.modules.map do |module_name|
{ module_name: }
end,
}
Expand All @@ -57,7 +59,7 @@ def modules
# rubocop:disable Style/HashEachMethods
@store.each do |_, definition|
definition.sources.each do |source|
modules = @module_store.get_modules(source.source_name)
modules = @metadata.source(source.source_name).modules
module_set.add(modules) unless modules.empty?
end
end
Expand All @@ -84,7 +86,7 @@ def module(module_names)
# rubocop:disable Style/HashEachMethods
@store.each do |_, definition|
definition.sources.each do |source|
source_module_names = @module_store.get_modules(source.source_name)
source_module_names = @metadata.source(source.source_name).modules

next unless source_module_names[0..module_names.size - 1] == module_names

Expand All @@ -109,7 +111,7 @@ def module(module_names)
sources: source_names.sort.map do |source_name|
{
source_name:,
memo: @module_store.get_memo(source_name),
memo: @metadata.source(source_name).memo,
}
end,
related_definitions: related_definitions.map do |definition|
Expand Down Expand Up @@ -139,7 +141,7 @@ def definitions(page:, per:, title:, source:, definition_group:)
definition_group: definition.definition_group,
title: definition.title,
sources_count: definition.sources.size,
unclassified_sources_count: definition.sources.reject { @module_store.classified?(_1.source_name) }.size,
unclassified_sources_count: definition.sources.reject { @metadata.source(_1.source_name).modules? }.size,
}
end,
pagination: pagination.to_h
Expand Down Expand Up @@ -187,18 +189,18 @@ def combine_definitions(bit_id, compound, concentrate, only_module)
end

if definition
definition_to_dot = DiverDown::Web::DefinitionToDot.new(definition, @module_store, compound:, concentrate:, only_module:)
definition_to_dot = DiverDown::Web::DefinitionToDot.new(definition, @metadata, compound:, concentrate:, only_module:)

json(
titles:,
bit_id: DiverDown::Web::BitId.ids_to_bit_id(valid_ids).to_s,
dot: definition_to_dot.to_s,
dot_metadata: definition_to_dot.metadata,
dot_metadata: definition_to_dot.dot_metadata,
sources: definition.sources.map do
{
source_name: _1.source_name,
memo: @module_store.get_memo(_1.source_name),
modules: @module_store.get_modules(_1.source_name).map do |module_name|
memo: @metadata.source(_1.source_name).memo,
modules: @metadata.source(_1.source_name).modules.map do |module_name|
{ module_name: }
end,
}
Expand Down Expand Up @@ -249,12 +251,12 @@ def source(source_name)
[]
else
source = DiverDown::Definition::Source.combine(*found_sources)
@module_store.get_modules(source.source_name)
@metadata.source(source.source_name).modules
end

json(
source_name:,
memo: @module_store.get_memo(source_name),
memo: @metadata.source(source_name).memo,
modules: module_names.map do
{ module_name: _1 }
end,
Expand Down Expand Up @@ -291,8 +293,8 @@ def set_modules(source_name, modules)
end

if found_source
@module_store.set_modules(source_name, modules)
@module_store.flush
@metadata.source(source_name).modules = modules
@metadata.flush

json({})
else
Expand All @@ -312,8 +314,8 @@ def set_memo(source_name, memo)
end

if found_source
@module_store.set_memo(source_name, memo)
@module_store.flush
@metadata.source(source_name).memo = memo
@metadata.flush

json({})
else
Expand Down
40 changes: 40 additions & 0 deletions lib/diver_down/web/alias_store.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# frozen_string_literal: true

module DiverDown
class Web
class AliasStore
def initialize
# Hash{ alias_name => Set<source_name, ...> }
@alias_to_source_names = Hash.new { |h, k| h[k] = Set.new }
@source_name_to_alias = {}
end

# @param alias_name [String]
# @param source_names [Array<String>]
# @return [void]
def add_alias(alias_name, source_names)
@alias_to_source_names[alias_name].merge(source_names)

source_names.each do |source_name|
@source_name_to_alias[source_name] = alias_name
end
end

# @param alias_name [String]
# @return [String]
def resolve_alias(source_name)
if @source_name_to_alias.key?(source_name)
@source_name_to_alias[source_name]
else
source_name
end
end

# @param alias_name [String]
# @return [Array<String>]
def aliased_source_names(alias_name)
@alias_to_source_names[alias_name].to_a if @alias_to_source_names.key?(alias_name)
end
end
end
end
Loading

0 comments on commit 2c59b94

Please sign in to comment.