view · edit · sidebar · attach · print · history

20110916-update-sort-function-migel

<< | Index | >>


  1. Update sort function
  2. Testcases
  3. Re-import method for missing data

Goal/Estimate/Evaluation:

  • Debug importer migel / 90% / 100%
Milestones
  1. Update sort
  2. reimport missing data method
  3. Importer job
Summary
ToDo
  • re-import missing data
  • require 'migel/util/m10l_document' problem in multilingual.rb
  • require 'oddb/util/m10l_document' problem in model_super.rb

Update sort function

Review

  • src/state/global.rb
      def migel_search
        if migel_code = @session.user_input(:migel_code) and product = @session.search_migel_products(migel_code).first
          sort_key = @session.user_input(:sortvalue) || @session.user_input(:reverse)
          reverse  = @session.user_input(:reverse)
          ODDB::State::Migel::Items.new(@session, ODDB::Migel::Items.new(product, sort_key, reverse))
  • src/model/migel/items.rb
class Items
  attr_reader :price, :qty, :unit, :pointer_descr
  def initialize(product, sortvalue = nil, reverse = nil)
    if product and items = product.items
      @sortvalue = sortvalue
      @list = items.values
      @reverse = reverse
    end
...
  def sort_by
    # This is called at the first time when a search result is shown
    @list.sort_by do |record|
      if @sortvalue
        if @sortvalue == 'ppub'
          record.ppub.to_f
        else
          record.send(@sortvalue.to_sym).to_s
        end
      else
        record.pharmacode
      end
    end
  end
  • src/view/migel/items.rb
class SearchedList < HtmlGrid::List
  def sort_link(header_key, matrix, component)
    link = HtmlGrid::Link.new(header_key, @model, @session, self)
    sort = @session.user_input(:sortvalue) ? :reverse : :sortvalue
    link.href = @lookandfeel._event_url(:migel_search, [:migel_code, @model.first.migel_code.gsub('.',''), sort, component.to_s])
    link
  end
  • src/util/validator.rb
   STRINGS = [
...
    :reverse,
...

Access

Result

  • Sort works but Reverse does not work

Experiment

  • src/model/migel/items.rb
  def sort_by
    # This is called at the first time when a search result is shown
    if @reverse
      @list.sort_by do |record|
        if @sortvalue
          if @sortvalue == 'ppub'
            record.ppub.to_f
          else
            record.send(@sortvalue.to_sym).to_s
          end
        else
          record.pharmacode
        end
      end
    else
      @list.sort_by do |record|
        if @sortvalue
          if @sortvalue == 'ppub'
            record.ppub.to_f
          else
            record.send(@sortvalue.to_sym).to_s
          end
        else
          record.pharmacode
        end
      end.reverse
    end
  end

Access

Note

  • The code is redundant though.
  • The other errors have been resolved.

Next

  • the case of searching products by search box

Problem

Idea

  • Switch the sort_link depending on the URL
  • -> update the View class, sort_link method

Experiment

  • src/view/migel/items.rb
  def sort_link(header_key, matrix, component)
    sortvalue = @session.user_input(:sortvalue) || @session.user_input(:reverse)
    sort_way = @session.user_input(:sortvalue) ? :sortvalue : :reverse
    sort_way = if sort_way == :sortvalue and component.to_s == sortvalue
                 :reverse
               else
                 :sortvalue
               end

    link = HtmlGrid::Link.new(header_key, @model, @session, self)
    if search_query = @session.user_input(:search_query)
      args = [:zone, @session.zone, :search_query, @session.user_input(:search_query), sort_way, component.to_s]
      #args = [:search_query, @session.user_input(:search_query), sort_way, component.to_s]
      link.href = @lookandfeel._event_url(@session.event, args)
    else
      sortvalue = @session.user_input(:sortvalue) || @session.user_input(:reverse)
      sort_way = @session.user_input(:sortvalue) ? :sortvalue : :reverse
      sort_way = if sort_way == :sortvalue and component.to_s == sortvalue
                   :reverse
                 else
                   :sortvalue
                 end
      link.href = @lookandfeel._event_url(:migel_search, [:migel_code, @model.first.migel_code.gsub('.',''), sort_way, component.to_s])
    end
    link
  end
  • src/state/global.rb
module ODDB
  module State
    class Global < SBSM::State
      class StubProduct
        attr_reader :items
        def initialize(items)
          @items = items
        end
      end
...
      def search
...
          when :migel
            if result = @session.search_migel_products(query) and !result.empty?
              State::Migel::Result.new(@session, result)
            elsif result = @session.app.search_migel_items(query, @session.language) and !result.empty?
              product = StubProduct.new(result)
              sort_key = @session.user_input(:sortvalue) || @session.user_input(:reverse)
              reverse  = @session.user_input(:reverse)
              ODDB::State::Migel::Items.new(@session, ODDB::Migel::Items.new(product, sort_key, reverse))
              #State::Migel::Items.new(@session, result)

Access

Note

  • The order of migel-code is not sorted

Next

  • How to sort products with migel_code

Experiment

  • /usr/lib64/ruby/site_ruby/1.8/htmlgrid/list.rb
    def sort_model
      block = self::class::SORT_DEFAULT
      if(block && (@session.event != :sort))
        begin
          if(block == :self)
            @model = @model.sort
          else
            unless(block.is_a?(Proc))
              block = Proc.new { |item|
                begin
                  item.send(self::class::SORT_DEFAULT)
                rescue RuntimeError => e
                  item.to_s
                end
              }
            end
            @model = @model.sort_by(&block)
p @model.map{|m| m.pharmacode}
            @model
          end

Access (sort and reverse)

Result

  • Log
["4091488", "3390852", "3390823", "3390786", "3390740", "3390734", "3389234", "3191259", "3191213", "3184319", "3184294", "3184207", "1821147", "1821130"]
["4091488", "3390852", "3390823", "3390786", "3390740", "3390734", "3389234", "3191259", "3191213", "3184319", "3184294", "3184207", "1821147", "1821130"]
["1821130", "1821147", "3184207", "3184294", "3184319", "3191213", "3191259", "3389234", "3390734", "3390740", "3390786", "3390823", "3390852", "4091488"]
  • Screen

Note

  • In the sort_model method, the pharmacode is sorted but on the screen it is not sorted
  • I guess this is re-sorted somewhere (probably by HtmlGrid) because it is grouped by subheader (migel code)

Experiment

  • src/view/migel/items.rb def compose_list(model = @model, offset=[0,0])
    # Grouping products with migel_code
    migel_code_group = {}
    model.each do |product|
      (migel_code_group[product.migel_code] ||= []) <<  product
    end

    # list up items
    #migel_code_group.keys.each do |migel_code|
    migel_code_group.keys.sort.each do |migel_code|
      offset_length = migel_code_group[migel_code].length
      compose_subheader(migel_code_group[migel_code][0], offset)
      super(migel_code_group[migel_code], offset)
      offset[1] += offset_length
    end
end

Access

Note

  • The list is sorted by migel_code in both 'sort' and 'reverse'
  • In the same migel_code, the list is sorted (reversed) by pharmacode

Testcases

  • test/test_view/migel/items.rb (coverage: 100%)
  • test/test_state/global.rb (coverage: 99.75%)
  • src/view/migel/limitationtext.rb (coverage: 100%)
  • src/view/notify.rb (coverage: 100%)
  • src/test_model/migel/items.rb (coverage: 97.45%)

Total coverage

  • oddb.org: 94.98%
  • migel: 91.83%

Re-import method for missing data

Experiment

  • lib/migel/util/importer.rb
  def reimport_missing_data(langauge = 'de', estimate = false, output_filename = nil)
    migel_codes = migel_code_list.select do |migel_code|
      migelid = Migel::Model::Migelid.find_by_migel_code(migel_code) and !migelid.products.empty? and migelid.products.first.article_name.send(langauge).to_s.empty?
    end
    total = migel_codes.length
    start_time = Time.now
    migel_codes.each_with_index do |migel_code, count|
      update_products_by_migel_code(migel_code, language)
      puts estimate_time(start_time, total, count+1) if estimate
    end
    if output_filename
      File.open(output_filename, 'w') do |out|
        out.print migel_codes.join("\n"), "\n"
      end
    end
    migel_codes
  end

Log

1 / 52  Estimate total: 11.74 [m] It will be done in: 11.52 [m]
2 / 52  Estimate total: 8.41 [m] It will be done in: 8.09 [m]
3 / 52  Estimate total: 27.40 [m] It will be done in: 25.82 [m]
4 / 52  Estimate total: 20.94 [m] It will be done in: 19.33 [m]
5 / 52  Estimate total: 17.10 [m] It will be done in: 15.46 [m]
...

Note

  • The number of missing data is 52 migelids

Result

  • Save the PostgreSQL dump data next Monday
view · edit · sidebar · attach · print · history
Page last modified on September 26, 2011, at 08:32 AM