lib/cotcube-level/helpers.rb
# loading of swaps is also straight forward
# it takes few more efforts to normalize the values to their expected format
#
# it is not too nice that some actual interactive process is done here in the load section
loading of swaps is also straight forward
it takes few more efforts to normalize the values to their expected format

it is not too nice that some actual interactive process is done here in the load section
loading of swaps is also straight forward it takes few more efforts to normalize the values to their expected format it is not too nice that some actual interactive process is done here in the load section
def load_swaps(interval:, swap_type:, contract:, sym: nil, datetime: nil, recent: false, digest: nil, quiet: false, exceed: false, keep_ignored: false)
      file = get_jsonl_name(interval: interval, swap_type: swap_type, contract: contract, sym: sym)
      jsonl = File.read(file)
      data = jsonl.
        each_line.
        map do |x|
        JSON.parse(x).
          deep_transform_keys(&:to_sym).
          tap do |sw|
            sw[:datetime] = DateTime.parse(sw[:datetime]) rescue nil
            (sw[:exceeded] = DateTime.parse(sw[:exceeded]) rescue nil) if sw[:exceeded]
            (sw[:ignored] = DateTime.parse(sw[:ignored]) rescue nil) if sw[:ignored]
            sw[:interval] = interval
            sw[:swap_type] = swap_type
            sw[:contract] = contract
            %i[ side ].each {|key| sw[key] = sw[key].to_sym rescue false }
            unless sw[:empty] or sw[:exceeded] or sw[:ignored]
              sw[:color]    = sw[:color].to_sym 
              sw[:members].map{|mem| mem[:datetime] = DateTime.parse(mem[:datetime]) }
            end
        end
      end
      data.select{|swap| swap[:exceeded] }.each do |exc|
        swap = data.find{|ref| ref[:digest] == exc[:ref]}
        raise RuntimeError, "Inconsistent history for '#{exc}'. Origin not found." if swap.nil?
        swap[:exceeded] = exc[:exceeded]
      end
      data.select{|swap| swap[:ignored] }.each do |ign|
	swap = data.find{|ref| ref[:digest] == ign[:ref]}
        raise RuntimeError, "Inconsistent history for '#{ign}'. Origin not found." if swap.nil?
        swap[:ignored] = ign[:ignored]
      end
      data.reject!{|swap| (swap[:ignored] or swap[:exceeded]) and swap[:members].nil? }
      data.reject!{|swap| swap[:datetime] > datetime } unless datetime.nil?
      recent  = 7.days  if recent.is_a? TrueClass
      recent += 5.hours if recent
      data.reject!{|swap| swap[:ignored] } unless keep_ignored
      data.reject!{|swap| swap[:exceeded] and swap[:exceeded] < datetime - (recent ? recent : 0) } unless datetime.nil?
      data.map{|swap| swap.delete(:exceeded) if swap[:exceeded] and swap[:exceeded] > datetime} unless datetime.nil?
      unless digest.nil?
        data.select! do |z|
          (Cotcube::Helpers.sub(minimum: digest.length){ z[:digest] } === digest) and
          not z[:empty]
        end
        case data.size
        when 0
          puts "No swaps found for digest '#{digest}'." unless quiet
        when 1
          sym ||= Cotcube::Helpers.get_id_set(contract: contract)
          if not quiet or exceed
            puts "Found 1 digest: "
            data.each {|d| puts_swap( d, format: sym[:format], short: true, hash: digest.size + 2) }
            if exceed
              exceed = DateTime.now if exceed.is_a? TrueClass
              mark_exceeded(swap: data.first, datetime: exceed)
              puts "Swap marked exceeded."
            end
          end
        else
          sym ||= Cotcube::Helpers.get_id_set(contract: contract)
          unless quiet
            puts "Too many digests found for digest '#{digest}', please consider sending more figures: "
            data.each {|d| puts_swap( d, format: sym[:format], short: true, hash: digest.size + 3)}
          end
        end
      end
      data
    end
def load_swaps ( interval : , swap_type : , contract : , sym : nil , datetime : nil , recent : false , digest : nil , quiet : false , exceed : false , keep_ignored : false ) file = get_jsonl_name ( interval : interval , swap_type : swap_type , contract : contract , sym : sym ) jsonl = File . read ( file ) data = jsonl . each_line . map do | x | JSON . parse ( x ) . deep_transform_keys ( & :to_sym ) . tap do | sw | sw [ :datetime ] = DateTime . parse ( sw [ :datetime ] ) rescue nil ( sw [ :exceeded ] = DateTime . parse ( sw [ :exceeded ] ) rescue nil ) if sw [ :exceeded ] ( sw [ :ignored ] = DateTime . parse ( sw [ :ignored ] ) rescue nil ) if sw [ :ignored ] sw [ :interval ] = interval sw [ :swap_type ] = swap_type sw [ :contract ] = contract %i[ side ] . each { | key | sw [ key ] = sw [ key ] . to_sym rescue false } unless sw [ :empty ] or sw [ :exceeded ] or sw [ :ignored ] sw [ :color ] = sw [ :color ] . to_sym sw [ :members ] . map { | mem | mem [ :datetime ] = DateTime . parse ( mem [ :datetime ] ) } end end end data . select { | swap | swap [ :exceeded ] } . each do | exc | swap = data . find { | ref | ref [ :digest ] == exc [ :ref ] } raise RuntimeError , "Inconsistent history for '#{exc}'. Origin not found." if swap . nil? swap [ :exceeded ] = exc [ :exceeded ] end data . select { | swap | swap [ :ignored ] } . each do | ign | swap = data . find { | ref | ref [ :digest ] == ign [ :ref ] } raise RuntimeError , "Inconsistent history for '#{ign}'. Origin not found." if swap . nil? swap [ :ignored ] = ign [ :ignored ] end data . reject! { | swap | ( swap [ :ignored ] or swap [ :exceeded ] ) and swap [ :members ] . nil? } data . reject! { | swap | swap [ :datetime ] > datetime } unless datetime . nil? recent = 7 . days if recent . is_a? TrueClass recent += 5 . hours if recent data . reject! { | swap | swap [ :ignored ] } unless keep_ignored data . reject! { | swap | swap [ :exceeded ] and swap [ :exceeded ] < datetime - ( recent ? recent : 0 ) } unless datetime . nil? data . map { | swap | swap . delete ( :exceeded ) if swap [ :exceeded ] and swap [ :exceeded ] > datetime } unless datetime . nil? unless digest . nil? data . select! do | z | ( Cotcube :: Helpers . sub ( minimum : digest . length ) { z [ :digest ] } === digest ) and not z [ :empty ] end case data . size when 0 puts "No swaps found for digest '#{digest}'." unless quiet when 1 sym ||= Cotcube :: Helpers . get_id_set ( contract : contract ) if not quiet or exceed puts "Found 1 digest: " data . each { | d | puts_swap ( d , format : sym [ :format ] , short : true , hash : digest . size + 2 ) } if exceed exceed = DateTime . now if exceed . is_a? TrueClass mark_exceeded ( swap : data . first , datetime : exceed ) puts "Swap marked exceeded." end end else sym ||= Cotcube :: Helpers . get_id_set ( contract : contract ) unless quiet puts "Too many digests found for digest '#{digest}', please consider sending more figures: " data . each { | d | puts_swap ( d , format : sym [ :format ] , short : true , hash : digest . size + 3 ) } end end end data end
loading of swaps is also straight forward
it takes few more efforts to normalize the values to their expected format
loading of swaps is also straight forward it takes few more efforts to normalize the values to their expected format
# assign exceedance data to actual swaps # assign ignorance data to actual swaps # do not return bare exceedance information # do not return swaps that are found 'later' # do not return exceeded swaps, that are exceeded in the past # remove exceedance information that is found 'later'
interval
<not_specific>
swap_type
<not_specific>
contract
<not_specific>
sym
<not_specific>
datetime
<not_specific>
recent
<not_specific>
digest
<not_specific>
quiet
<not_specific>
exceed
<not_specific>
keep_ignored
<not_specific>