Module: Air18n::Metrics

Defined in:
lib/air18n/metrics.rb

Constant Summary collapse

TARGET_PRIORITY_PHRASE_TRANSLATION_PERCENT =
98

Class Method Summary collapse

Class Method Details

.metrics_suiteObject

Computes translation progress for all pages, then translator progress for all translators in each locale.



229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/air18n/metrics.rb', line 229

def self.metrics_suite
  p = translation_progress(nil)
  p.each do |locale, info|
    info[:translator_progress] = {}
    info[:translators].each do |user_id|
      info[:translator_progress][user_id] = translator_progress(locale, user_id)
    end
  end

  t = time_to_first_translation()
  t.each do |locale, info|
    p[locale].merge!(info)
  end

  p
end

.recent_work(locale, user_id) ⇒ Object

Returns scope for searching for most recent PhraseTranslations of a translator.



248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/air18n/metrics.rb', line 248

def self.recent_work(locale, user_id)
  scope = Air18n::PhraseTranslation
  scope = scope.order('`phrase_translations`.`created_at` DESC')
  scope.includes(:phrase_revision)
  if user_id.present?
    scope = scope.where(:user_id => user_id)
  end
  if locale.present?
    scope = scope.where(:locale => locale)
  end
  scope
end

.time_to_first_translationObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/air18n/metrics.rb', line 5

def self.time_to_first_translation
  # First we get all phrases created in the last 28 days and one of their
  # translations (leaving out verifications).
  #
  # Then we look at the difference between the phrase's created_at and the
  # phrase translation's created_at and return the average per language

  search_scope = Air18n::Phrase.still_used
  search_scope = search_scope.select(
    '`phrases`.`id`, `phrases`.`key`, `phrases`.`created_at`, ' +
    '`phrase_translations`.`locale` AS latest_translation_locale, ' +
    '`phrase_translations`.`user_id` AS latest_translation_user_id, ' +
    '`phrase_translations`.`created_at` AS latest_translation_created_at'
  )
  search_scope = search_scope.where('`phrases`.`created_at` >= ?', 28.days.ago)
  search_scope = search_scope.pull_in_all_historic_phrase_translations
  search_scope = search_scope.where('`phrase_translations`.`is_verification` = ?', false)
  search_scope = search_scope.group('`phrases`.`id`, `phrase_translations`.`phrase_id`, `latest_translation_locale`')

  phrases_and_translations = search_scope.all

  progress = {}

  locale_to_translations = phrases_and_translations.group_by do |phrase_and_translation|
    phrase_and_translation.latest_translation_locale
  end
  locale_to_translations.each do |locale, phrases_and_translations_for_locale|
    if locale.present?
      locale = locale.to_sym
      progress[locale] ||= {}
      elapsed_times = []
      phrases_and_translations_for_locale.each do |phrase_and_translation|
        phrase_created_at = phrase_and_translation.created_at
        translated_at = phrase_and_translation.latest_translation_created_at
        if translated_at.is_a?(String)
          translated_at = Time.parse(translated_at)
        end
        seconds_passed = (translated_at - phrase_created_at).to_i

        elapsed_times << seconds_passed
      end

      progress[locale][:average_seconds_to_first_translation] = (elapsed_times.inject(:+).to_f / elapsed_times.size).round
    end
  end

  progress
end

.translation_progress(page_ids) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/air18n/metrics.rb', line 54

def self.translation_progress(page_ids)
  search_scope = Air18n::Phrase.still_used
  search_scope = search_scope.select(
    '`phrases`.`id`, `phrases`.`key`, `phrases`.`created_at`, ' +
    '`phrase_translations`.`locale` AS latest_translation_locale, ' +
    '`phrase_translations`.`user_id` AS latest_translation_user_id, ' +
    '`phrase_translations`.`created_at` AS latest_translation_created_at, ' +
    '`phrase_translations`.`is_stale` AS latest_translation_is_stale, ' +
    '`phrase_translations`.`is_verification` AS latest_translation_is_verification'
  )
  search_scope = search_scope.pull_in_all_up_to_date_phrase_translations
  search_scope = search_scope.group('`phrases`.`id`, `phrase_translations`.`phrase_id`, `latest_translation_locale`')

  if page_ids.present?
    search_scope = search_scope.joins(:phrase_screenshots)
    conditions = page_ids.map do |page_id|
      controller_action = Air18n::PhraseScreenshot.deserialize_page_id(page_id)
      "(phrase_screenshots.controller = '#{controller_action.controller}' AND phrase_screenshots.action = '#{controller_action.action}')"
    end
    search_scope = search_scope.where(conditions.join(' OR '))
  end

  progress = {}

  phrases_and_translations = search_scope.all

  # This block is useful for debugging.
  if false
    LoggingHelper.info "Phrases and translations:"
    phrases_and_translations.each do |thing|
      LoggingHelper.info "key #{thing.key} locale #{thing.latest_translation_locale} user #{thing.latest_translation_user_id} at #{thing.latest_translation_created_at} stale? #{thing.latest_translation_is_stale} verified? #{thing.latest_translation_is_verification}"
    end
  end

  priority_keys = phrases_and_translations.map { |phrase_and_translation| phrase_and_translation.key }.uniq
  number_of_priority_phrases = priority_keys.size
  number_of_priority_words = priority_keys.map { |k| word_counts[k] }.sum
  locale_to_translations = phrases_and_translations.group_by do |phrase_and_translation|
    phrase_and_translation.latest_translation_locale
  end
  locale_to_translations.each do |locale, phrases_and_translations_for_locale|
    if locale.present?
      locale = locale.to_sym
      progress[locale] ||= {}
      phrases_with_translations_for_locale = phrases_and_translations_for_locale.find_all do |phrase_and_translation|
        phrase_and_translation.latest_translation_user_id
      end
      phrases_with_verifications_for_locale = phrases_with_translations_for_locale.find_all do |phrase_and_translation|
        true_value?(phrase_and_translation.latest_translation_is_verification)
      end

      words_with_translations_for_locale = phrases_with_translations_for_locale.map { |p| word_counts[p.key] }.sum
      words_with_verifications_for_locale = phrases_with_verifications_for_locale.map { |p| word_counts[p.key] }.sum
      progress[locale][:phrases] = number_of_priority_phrases
      progress[locale][:words] = number_of_priority_words

      progress[locale][:phrases_translated] = phrases_with_translations_for_locale.size
      progress[locale][:words_translated] = words_with_translations_for_locale

      progress[locale][:phrases_verified] = phrases_with_verifications_for_locale.size
      progress[locale][:words_verified] = words_with_verifications_for_locale

      phrases_translated_in_last_day = phrases_with_translations_for_locale.find_all { |phrase_and_translation| since?(phrase_and_translation.latest_translation_created_at, 1.day.ago) }
      phrases_translated_in_last_week = phrases_with_translations_for_locale.find_all { |phrase_and_translation| since?(phrase_and_translation.latest_translation_created_at, 1.week.ago) }
      progress[locale][:phrases_translated_in_last_day] = phrases_translated_in_last_day.size
      progress[locale][:words_translated_in_last_day] = phrases_translated_in_last_day.map { |p| word_counts[p.key] }.sum
      progress[locale][:phrases_translated_in_last_week] = phrases_translated_in_last_week.size
      progress[locale][:words_translated_in_last_week] = phrases_translated_in_last_week.map { |p| word_counts[p.key] }.sum

      progress[locale][:phrases_translated_percent] = (100 * progress[locale][:phrases_translated].to_f / progress[locale][:phrases]).round
      progress[locale][:words_translated_percent] = (100 * progress[locale][:words_translated].to_f / progress[locale][:words]).round
      progress[locale][:phrases_verified_percent] = (100 * progress[locale][:phrases_verified].to_f / progress[locale][:phrases]).round
      progress[locale][:words_verified_percent] = (100 * progress[locale][:words_verified].to_f / progress[locale][:words]).round
      if progress[locale][:phrases_translated_percent] >= TARGET_PRIORITY_PHRASE_TRANSLATION_PERCENT
        progress[locale][:phrases_progress] = :good
      elsif progress[locale][:phrases_translated_in_last_day] < 50
        progress[locale][:phrases_progress] = :bad
      else
        progress[locale][:phrases_progress] = :okay
      end

      progress[locale][:phrases_remaining_to_translate] = progress[locale][:phrases] - progress[locale][:phrases_translated]
      progress[locale][:words_remaining_to_translate] = progress[locale][:words] - progress[locale][:words_translated]

      progress[locale][:phrases_remaining_to_verify] = progress[locale][:phrases] - progress[locale][:phrases_verified]
      progress[locale][:words_remaining_to_verify] = progress[locale][:words] - progress[locale][:words_verified]

      progress[locale][:translators] =
        phrases_with_translations_for_locale.inject(Set.new) do |carry, value|
          if since?(value.latest_translation_created_at, 28.days.ago)
            carry.add(value.latest_translation_user_id)
          end
          carry
        end
      progress[locale][:number_of_nonautomatic_translators] =
        if progress[locale][:translators].include?(0)
          progress[locale][:translators].size - 1
        else
          progress[locale][:translators].size
        end
    end
  end

  progress
end

.translator_progress(locale, user_id) ⇒ Object



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/air18n/metrics.rb', line 160

def self.translator_progress(locale, user_id)
  scope = PhraseTranslation.where(:locale => locale).where(:user_id => user_id)
  scope = scope.where('created_at >= ?', 28.days.ago)
  scope = scope.group(:phrase_id, :source_hash)
  scope.select("`id`, `key`, `source_word_count`")

  words_translated_last_month = 0
  words_translated_last_week = 0
  words_verified_last_month = 0
  words_verified_last_week = 0
  phrases_translated_last_month = 0
  number_of_later_modified_phrases_last_month = 0
  phrases_translated = {}
  scope.find_each do |pt|
    if pt.created_at >= 1.week.ago
      if pt.is_verification?
        words_verified_last_week += pt.source_word_count
      else
        words_translated_last_week += pt.source_word_count
      end
    end

    if pt.is_verification?
      words_verified_last_month += pt.source_word_count
    else
      words_translated_last_month += pt.source_word_count
    end

    if !pt.is_verification?
      later_modifications = PhraseTranslation.
        where(:locale => pt.locale).
        where(:phrase_id => pt.phrase_id).
        where(:source_hash => pt.source_hash).
        where('`phrase_translations`.id > ?', pt.id).
        where('user_id != ?', pt.user_id).
        where('payment_details NOT LIKE ?', '{"v3":{"t":0,%').
        count
      if later_modifications > 0
        number_of_later_modified_phrases_last_month += 1
      end
      phrases_translated_last_month += 1
    end
  end

  daily_average = (words_translated_last_month + words_verified_last_month) / 28
  later_modified_percent =
    if phrases_translated_last_month == 0
      0
    else
      (100.0 * number_of_later_modified_phrases_last_month.to_f / phrases_translated_last_month).round
    end
  classification = words_translated_last_month > words_verified_last_month ? :translator : :verifier

  {
    :user_id => user_id,
    :classification => classification,
    :phrases_translated_last_month => phrases_translated_last_month,
    :words_translated_last_month => words_translated_last_month,
    :words_translated_last_week => words_translated_last_week,
    :words_verified_last_month => words_verified_last_month,
    :words_verified_last_week => words_verified_last_week,
    :daily_average => daily_average,
    :number_of_later_modified_phrases_last_month => number_of_later_modified_phrases_last_month,
    :later_modified_percent => later_modified_percent
  }
end