air18n 0.1.24 → 0.1.25

Sign up to get free protection for your applications and to get access to all the features.
@@ -56,10 +56,31 @@ module Air18n
56
56
  all_locales
57
57
  end
58
58
 
59
+ def self.translator_activity_data_master(user_id = 0)
60
+ d = Date.today.beginning_of_month
61
+ activities = []
62
+
63
+ while true
64
+ if d < Date.new(2012, 8, 02)
65
+ activity_for_month = translator_activity_data(user_id, :since => d, :to => (d >> 1), :enumerate_keys => false)
66
+ else
67
+ activity_for_month = translator_activity_data_new(user_id, :since => d, :to => (d >> 1), :enumerate_keys => false)
68
+ end
69
+
70
+ break if activity_for_month.blank? && d < (Date.today - 3.years)
71
+
72
+ activities += activity_for_month
73
+ d = d << 1
74
+ end
75
+
76
+ activities
77
+ end
78
+
59
79
  def self.translator_activity_data user_id=0, opts={}
60
80
  user_criterion = user_id > 0 ? "AND pt.user_id=#{user_id}" : "AND NOT pt.user_id=0"
61
- since_criterion = "AND pt.created_at >= '#{opts[:since].to_formatted_s(:db)}}'" if opts[:since]
62
- sql = "SELECT year(pt.created_at) year, month(pt.created_at) month, pt.user_id user_id, pt.locale locale, day(pt.created_at) day, p.id phrase_id, p.value phrase_value, pt.is_verification FROM phrase_translations pt, phrases p WHERE pt.phrase_id=p.id #{user_criterion} #{since_criterion} GROUP BY year(pt.created_at), month(pt.created_at), pt.user_id, pt.locale, pt.phrase_id"
81
+ since_criterion = "AND pt.created_at >= '#{opts[:since].to_formatted_s(:db)}'" if opts[:since]
82
+ to_criterion = "AND pt.created_at < '#{opts[:to].to_formatted_s(:db)}'" if opts[:to]
83
+ sql = "SELECT #{created_at_year_sql} year, #{created_at_month_sql} month, pt.user_id user_id, pt.locale locale, #{created_at_day_sql} day, p.id phrase_id, p.value phrase_value, pt.is_verification FROM phrase_translations pt, phrases p WHERE pt.phrase_id=p.id #{user_criterion} #{since_criterion} #{to_criterion} GROUP BY #{created_at_year_sql}, #{created_at_month_sql}, pt.user_id, pt.locale, pt.phrase_id"
63
84
  res = self.connection.select_rows(sql)
64
85
  phrases_per_user_locale_month_year =
65
86
  Hash.new {|h, year| h[year] =
@@ -73,8 +94,8 @@ module Air18n
73
94
 
74
95
  res.each do |row|
75
96
  year, month, user_id, locale, day, phrase_id, phrase_value, is_verification = row
76
- is_verification = (is_verification == 1)
77
- phrases_per_user_locale_month_year[year][month][user_id][locale][day][is_verification].merge!({phrase_id => phrase_value})
97
+ is_verification = (is_verification == 1 || is_verification == 't')
98
+ phrases_per_user_locale_month_year[year.to_i][month.to_i][user_id][locale][day.to_i][is_verification].merge!({phrase_id => phrase_value})
78
99
  end
79
100
 
80
101
  activities = []
@@ -133,8 +154,9 @@ module Air18n
133
154
 
134
155
  def self.translator_activity_data_new user_id=0, opts={}
135
156
  user_criterion = user_id > 0 ? "AND pt.user_id=#{user_id}" : "AND NOT pt.user_id=0"
136
- since_criterion = "AND pt.created_at >= '#{opts[:since].to_formatted_s(:db)}}'" if opts[:since]
137
- sql = "SELECT year(pt.created_at) year, month(pt.created_at) month, pt.user_id user_id, pt.locale locale, day(pt.created_at) day, p.id phrase_id, pt.source_word_count source_word_count, pt.key, pt.source_hash source_hash, pt.is_verification FROM phrase_translations pt, phrases p WHERE pt.phrase_id=p.id #{user_criterion} #{since_criterion} GROUP BY year(pt.created_at), month(pt.created_at), pt.user_id, pt.locale, pt.phrase_id, pt.source_hash, pt.is_verification"
157
+ since_criterion = "AND pt.created_at >= '#{opts[:since].to_formatted_s(:db)}'" if opts[:since]
158
+ to_criterion = "AND pt.created_at < '#{opts[:to].to_formatted_s(:db)}'" if opts[:to]
159
+ sql = "SELECT #{created_at_year_sql} year, #{created_at_month_sql} month, pt.user_id user_id, pt.locale locale, #{created_at_day_sql} day, p.id phrase_id, pt.source_word_count source_word_count, pt.key, pt.source_hash source_hash, pt.is_verification FROM phrase_translations pt, phrases p WHERE pt.phrase_id=p.id #{user_criterion} #{since_criterion} #{to_criterion} GROUP BY #{created_at_year_sql}, #{created_at_month_sql}, pt.user_id, pt.locale, pt.phrase_id, pt.source_hash, pt.is_verification"
138
160
  res = self.connection.select_rows(sql)
139
161
  phrases_per_user_locale_month_year =
140
162
  Hash.new {|h, year| h[year] =
@@ -148,8 +170,8 @@ module Air18n
148
170
 
149
171
  res.each do |row|
150
172
  year, month, user_id, locale, day, phrase_id, source_word_count, key, source_hash, is_verification = row
151
- is_verification = (is_verification == 1)
152
- phrases_per_user_locale_month_year[year][month][user_id][locale][day][is_verification].merge!({[key, source_hash] => source_word_count})
173
+ is_verification = (is_verification == 1 || is_verification == 't')
174
+ phrases_per_user_locale_month_year[year.to_i][month.to_i][user_id][locale][day.to_i][is_verification].merge!({[key, source_hash] => source_word_count})
153
175
  end
154
176
 
155
177
  activities = []
@@ -388,7 +410,6 @@ module Air18n
388
410
 
389
411
  locales.each do |locale|
390
412
  # Output progress to console in case of running this on console.
391
- puts "Resetting flags for #{locale}..."
392
413
  latest_translations = PhraseTranslation.select("max(id) as max_id").where("locale='#{locale}'").group(:phrase_id).collect{|e| e.max_id}.to_set
393
414
 
394
415
  PhraseTranslation.where(:locale => locale).find_each do |translation|
@@ -424,5 +445,17 @@ module Air18n
424
445
  def quote_vars(vars)
425
446
  vars.map { |var| "%{#{var}}" }.join(", ")
426
447
  end
448
+
449
+ def self.created_at_day_sql
450
+ Air18n::PhraseTranslation.connection.adapter_name.starts_with?('Mysql') ? 'DAY(pt.created_at)' : 'STRFTIME("%d", pt.created_at)'
451
+ end
452
+
453
+ def self.created_at_month_sql
454
+ Air18n::PhraseTranslation.connection.adapter_name.starts_with?('Mysql') ? 'MONTH(pt.created_at)' : 'STRFTIME("%m", pt.created_at)'
455
+ end
456
+
457
+ def self.created_at_year_sql
458
+ Air18n::PhraseTranslation.connection.adapter_name.starts_with?('Mysql') ? 'YEAR(pt.created_at)' : 'STRFTIME("%Y", pt.created_at)'
459
+ end
427
460
  end
428
461
  end
@@ -1,3 +1,3 @@
1
1
  module Air18n
2
- VERSION = "0.1.24"
2
+ VERSION = "0.1.25"
3
3
  end
@@ -132,4 +132,113 @@ describe Air18n::PhraseTranslation do
132
132
  end
133
133
  end
134
134
 
135
+ context 'word counts' do
136
+ it 'should keep track of translated and verified word count in new regime' do
137
+ @user1 = 1
138
+ @user2 = 2
139
+ @phrase1 = FactoryGirl.create(:phrase, :key => 'old regime key 1', :value => 'two words')
140
+ @phrase2 = FactoryGirl.create(:phrase, :key => 'old regime key 2', :value => 'three long words')
141
+
142
+ without_timestamping_of Air18n::PhraseTranslation do
143
+ translation = FactoryGirl.create(:phrase_translation, :phrase => @phrase1, :user_id => @user1, :created_at => Date.new(2012, 05, 01))
144
+ other_phrase_translation = FactoryGirl.create(:phrase_translation, :phrase => @phrase2, :user_id => @user1, :created_at => Date.new(2012, 05, 02))
145
+ other_user_translation = FactoryGirl.create(:phrase_translation, :phrase => @phrase1, :user_id => @user2, :created_at => Date.new(2012, 05, 03))
146
+ phrase_verification = FactoryGirl.create(:phrase_translation, :phrase => @phrase2, :user_id => @user2, :is_verification => true, :created_at => Date.new(2012, 05, 04))
147
+ end
148
+
149
+ data = Air18n::PhraseTranslation.translator_activity_data_master
150
+
151
+ data.should include(
152
+ {:year=>2012,
153
+ :month=>5,
154
+ :locale=>"es",
155
+ :user_id=>1,
156
+ :activity=>
157
+ {:num_translations=>2,
158
+ :word_count_translations=>5,
159
+ :num_verifications=>0,
160
+ :word_count_verifications=>0,
161
+ :num_phrases_prev_translated=>2}}
162
+ )
163
+
164
+ data.should include(
165
+ {:year=>2012,
166
+ :month=>5,
167
+ :locale=>"es",
168
+ :user_id=>2,
169
+ :activity=>
170
+ {:num_translations=>1,
171
+ :word_count_translations=>2,
172
+ :num_verifications=>1,
173
+ :word_count_verifications=>3,
174
+ :num_phrases_prev_translated=>1}}
175
+ )
176
+ end
177
+
178
+
179
+ it 'should keep track of translated and verified word count in new regime' do
180
+ @user1 = 3
181
+ @user2 = 4
182
+ @phrase1 = FactoryGirl.create(:phrase, :value => 'two words')
183
+ @phrase2 = FactoryGirl.create(:phrase, :value => 'three long words')
184
+
185
+ without_timestamping_of Air18n::PhraseTranslation do
186
+ @phrase1.value = "one two three four five six seven eight"
187
+ @phrase1.save!
188
+ translation = FactoryGirl.create(:phrase_translation, :value => 'urgle burgle', :phrase => @phrase1, :user_id => @user1, :created_at => Date.new(2012, 10, 1))
189
+ translation_tweak = FactoryGirl.create(:phrase_translation, :value => 'urgle burgle boo', :phrase => @phrase1, :user_id => @user1, :created_at => 1.month.ago)
190
+ verification_by_other_user = FactoryGirl.create(:phrase_translation, :value => 'urgle burgle boo', :phrase => @phrase1, :user_id => @user2, :is_verification => true, :created_at => Date.new(2012, 10, 2))
191
+ @phrase1.value = "one two three four five six seven eight nine ten eleven twelve thirteen"
192
+ @phrase1.save!
193
+ retranslation = FactoryGirl.create(:phrase_translation, :value => 'oogie boogie boo', :phrase => @phrase1, :user_id => @user1, :source_hash => 'new default text', :source_word_count => 13, :created_at => Date.new(2012, 10, 3))
194
+ other_user_translation = FactoryGirl.create(:phrase_translation, :value => 'oogie boogie boo', :phrase => @phrase1, :user_id => @user2, :source_hash => 'new default text', :source_word_count => 13, :created_at => Date.new(2012, 10, 4))
195
+ verification = FactoryGirl.create(:phrase_translation, :value => 'oogie boogie boo', :phrase => @phrase1, :user_id => @user1, :source_hash => 'new default text', :source_word_count => 13, :is_verification => true, :created_at => Date.new(2012, 10, 5))
196
+ end
197
+
198
+ data = Air18n::PhraseTranslation.translator_activity_data_master
199
+
200
+ # Example Scenario:
201
+ # Translator A translates phrase Y when it is 8 English words
202
+ # Translator A tweaks translation of phrase Y
203
+ # Translator B verifies translation of phrase Y
204
+ # <English text of phrase Y changes to be 13 English words and translation becomes stale>
205
+ # Translator A retranslates phrase Y
206
+ # Translator B tweaks translation of phrase Y
207
+ # Translator A verifies translation of phrase Y
208
+ # Payment:
209
+ # Translator A is paid for translating phrase Y -- 8 words + 13 words = 21 words
210
+ # Translator B is paid for verifying phrase Y -- 8 words
211
+ # Translator B is paid for translating phrase Y -- 13 words
212
+
213
+ data.should include(
214
+ {:year=>2012,
215
+ :month=>10,
216
+ :locale=>"es",
217
+ :user_id=>3,
218
+ :activity=>
219
+ {:num_translations=>2,
220
+ :num_verifications=>0,
221
+ :word_count_translations=>21,
222
+ :word_count_verifications=>0,
223
+ :translated_keys=>
224
+ [["example.key.4", "38a72c02b5febe4975fdcf19d32e412d"],
225
+ ["example.key.4", "bef8ae7ec7a9d0145615a1dd8e902e43"]],
226
+ :verified_keys=>[]}}
227
+ )
228
+ data.should include(
229
+ {:year=>2012,
230
+ :month=>10,
231
+ :locale=>"es",
232
+ :user_id=>4,
233
+ :activity=>
234
+ {:num_translations=>1,
235
+ :num_verifications=>1,
236
+ :word_count_translations=>13,
237
+ :word_count_verifications=>8,
238
+ :translated_keys=>[["example.key.4", "bef8ae7ec7a9d0145615a1dd8e902e43"]],
239
+ :verified_keys=>[["example.key.4", "38a72c02b5febe4975fdcf19d32e412d"]]}}
240
+ )
241
+ end
242
+ end
243
+
135
244
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: air18n
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.24
4
+ version: 0.1.25
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ authors:
13
13
  autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
- date: 2012-11-01 00:00:00.000000000 Z
16
+ date: 2012-11-06 00:00:00.000000000 Z
17
17
  dependencies:
18
18
  - !ruby/object:Gem::Dependency
19
19
  name: i18n