From 4d72dce327750e4a934e008b40505ad2bf150f82 Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Wed, 28 Nov 2012 00:01:33 +0100 Subject: [PATCH] Moved to ticket counter log in database. --- app/models/ticket.rb | 2 +- app/models/ticket/counter.rb | 2 + app/models/ticket/number/date.rb | 53 +++++++++------------ app/models/ticket/number/increment.rb | 50 +++++++++---------- db/migrate/20121127193555_ticket_counter.rb | 12 +++++ db/seeds.rb | 14 ------ 6 files changed, 60 insertions(+), 73 deletions(-) create mode 100644 app/models/ticket/counter.rb create mode 100644 db/migrate/20121127193555_ticket_counter.rb diff --git a/app/models/ticket.rb b/app/models/ticket.rb index c51c79aa2..e9e47242b 100644 --- a/app/models/ticket.rb +++ b/app/models/ticket.rb @@ -386,7 +386,7 @@ class Ticket < ApplicationModel private def number_generate Ticket.new.number_adapter = Setting.get('ticket_number') - (1..15_000).each do |i| + (1..25_000).each do |i| number = @@number_adapter.number_generate_item() ticket = Ticket.where( :number => number ).first if ticket != nil diff --git a/app/models/ticket/counter.rb b/app/models/ticket/counter.rb new file mode 100644 index 000000000..a5ae6bca6 --- /dev/null +++ b/app/models/ticket/counter.rb @@ -0,0 +1,2 @@ +class Ticket::Counter < ApplicationModel +end \ No newline at end of file diff --git a/app/models/ticket/number/date.rb b/app/models/ticket/number/date.rb index f1cd3997a..2d3328e46 100644 --- a/app/models/ticket/number/date.rb +++ b/app/models/ticket/number/date.rb @@ -2,44 +2,37 @@ module Ticket::Number::Date extend self def number_generate_item - + # get config config = Setting.get('ticket_number_date') - + t = Time.now date = t.strftime("%Y-%m-%d") # read counter - file_name = Rails.root.to_s + '/' + config[:file] - contents = "" - begin - file = File.open(file_name) - file.each {|line| - contents << line - } - file.close - rescue - contents = '0' - end - - # increase counter - counter, date_file = contents.to_s.split(';') + counter_increment = nil + Ticket::Counter.transaction do + counter = Ticket::Counter.where( :generator => 'Date' ).lock(true).first + if !counter + counter = Ticket::Counter.new( :generator => 'Date', :content => '0' ) + end - if date_file == date - counter = counter.to_i + 1 - else - counter = 1 - end - contents = counter.to_s + ';' + date + # increase counter + counter_increment, date_file = counter.content.to_s.split(';') + if date_file == date + counter_increment = counter_increment.to_i + 1 + else + counter_increment = 1 + end - # write counter - file = File.open(file_name, 'w') - file.write(contents) - file.close + # store new counter value + counter.content = counter_increment.to_s + ';' + date + counter.save + end system_id = Setting.get('system_id') || '' - number = t.strftime("%Y%m%d") + system_id.to_s + sprintf( "%04d", counter) - + number = t.strftime("%Y%m%d") + system_id.to_s + sprintf( "%04d", counter_increment) + # calculate a checksum # The algorithm to calculate the checksum is derived from the one # Deutsche Bundesbahn (german railway company) uses for calculation @@ -55,9 +48,9 @@ module Ticket::Number::Date (1..number.length).each do |i| digit = number.to_s[i, 1] chksum = chksum + ( mult * digit.to_i ) - mult += 1; + mult += 1 if mult == 3 - mult = 1; + mult = 1 end end chksum %= 10 diff --git a/app/models/ticket/number/increment.rb b/app/models/ticket/number/increment.rb index e7a420394..e0ea1d6b7 100644 --- a/app/models/ticket/number/increment.rb +++ b/app/models/ticket/number/increment.rb @@ -8,40 +8,34 @@ module Ticket::Number::Increment # read counter min_digs = config[:min_size] || 4; - file_name = Rails.root.to_s + '/' + config[:file] - contents = "" - begin - file = File.open( file_name ) - file.each {|line| - contents << line - } - file.close - rescue - contents = '0' + counter_increment = nil + Ticket::Counter.transaction do + counter = Ticket::Counter.where( :generator => 'Increment' ).lock(true).first + if !counter + counter = Ticket::Counter.new( :generator => 'Increment', :content => '0' ) + end + counter_increment = counter.content.to_i + + # increase counter + counter_increment += 1 + + # store new counter value + counter.content = counter_increment.to_s + counter.save end - # increase counter - counter, date_file = contents.to_s.split(';') - counter = counter.to_i + 1 - contents = counter.to_s + ';' - - # write counter - file = File.open( file_name, 'w' ) - file.write(contents) - file.close - # fill up number counter if config[:checksum] min_digs = min_digs.to_i - 1 end fillup = Setting.get('system_id') || '1' ( 1..100 ).each do |i| - if ( fillup.length.to_i + counter.to_s.length.to_i ) < min_digs.to_i + if ( fillup.length.to_i + counter_increment.to_s.length.to_i ) < min_digs.to_i fillup = fillup + '0' end end - number = fillup.to_s + counter.to_s - + number = fillup.to_s + counter_increment.to_s + # calculate a checksum # The algorithm to calculate the checksum is derived from the one # Deutsche Bundesbahn (german railway company) uses for calculation @@ -57,9 +51,9 @@ module Ticket::Number::Increment (1..number.length).each do |i| digit = number.to_s[i, 1] chksum = chksum + ( mult * digit.to_i ) - mult += 1; + mult += 1 if mult == 3 - mult = 1; + mult = 1 end end chksum %= 10 @@ -79,11 +73,11 @@ module Ticket::Number::Increment ticket_hook = Setting.get('ticket_hook') ticket_hook_divider = Setting.get('ticket_hook_divider') || '' ticket = nil - + # probe format - if string =~ /#{ticket_hook}#{ticket_hook_divider}(#{system_id}\d{2,50})/i then + if string =~ /#{ticket_hook}#{ticket_hook_divider}(#{system_id}\d{2,48})/i then ticket = Ticket.where( :number => $1 ).first - elsif string =~ /#{ticket_hook}\s{0,2}(#{system_id}\d{2,50})/i then + elsif string =~ /#{ticket_hook}\s{0,2}(#{system_id}\d{2,48})/i then ticket = Ticket.where( :number => $1 ).first end return ticket diff --git a/db/migrate/20121127193555_ticket_counter.rb b/db/migrate/20121127193555_ticket_counter.rb new file mode 100644 index 000000000..cd0dc26b4 --- /dev/null +++ b/db/migrate/20121127193555_ticket_counter.rb @@ -0,0 +1,12 @@ +class TicketCounter < ActiveRecord::Migration + def up + create_table :ticket_counters do |t| + t.column :content, :string, :limit => 100, :null => false + t.column :generator, :string, :limit => 100, :null => false + end + add_index :ticket_counters, [:generator], :unique => true + end + + def down + end +end diff --git a/db/seeds.rb b/db/seeds.rb index a131e817f..4e6662d97 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -748,18 +748,11 @@ Setting.create( 20 => 20, }, }, - { - :display => 'Logfile', - :null => false, - :name => 'file', - :tag => 'input', - }, ], }, :state => { :value => { :checksum => false, - :file => 'tmp/counter.log', :min_size => 5, }, }, @@ -782,18 +775,11 @@ Setting.create( false => 'no', }, }, - { - :display => 'Logfile', - :null => false, - :name => 'file', - :tag => 'input', - }, ], }, :state => { :value => { :checksum => false, - :file => 'tmp/counter.log', } }, :frontend => false