Skip to content
This repository has been archived by the owner on Jan 31, 2018. It is now read-only.

Commit

Permalink
[bug 1026131] Rework translations system logging code
Browse files Browse the repository at this point in the history
  • Loading branch information
willkg committed Jun 19, 2014
1 parent 98d30fb commit 38d8584
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 57 deletions.
Expand Up @@ -18,9 +18,9 @@ def forwards(self, orm):
('src_lang', self.gf('django.db.models.fields.CharField')(default=u'', max_length=10, blank=True)),
('dst_lang', self.gf('django.db.models.fields.CharField')(default=u'', max_length=10, blank=True)),
('status', self.gf('django.db.models.fields.CharField')(default='created', max_length=12)),
('order', self.gf('django.db.models.fields.related.ForeignKey')(to=orm['translations.GengoJob'], null=True)),
('order', self.gf('django.db.models.fields.related.ForeignKey')(to=orm['translations.GengoOrder'], null=True)),
('custom_data', self.gf('django.db.models.fields.CharField')(default=u'', max_length=100, blank=True)),
('created', self.gf('django.db.models.fields.DateTimeField')(default=datetime.datetime(2014, 6, 17, 0, 0))),
('created', self.gf('django.db.models.fields.DateTimeField')(default=datetime.datetime(2014, 6, 18, 0, 0))),
))
db.send_create_signal(u'translations', ['GengoJob'])

Expand Down Expand Up @@ -53,13 +53,13 @@ def backwards(self, orm):
u'translations.gengojob': {
'Meta': {'object_name': 'GengoJob'},
'content_type': ('django.db.models.fields.related.ForeignKey', [], {'to': u"orm['contenttypes.ContentType']"}),
'created': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime(2014, 6, 17, 0, 0)'}),
'created': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime(2014, 6, 18, 0, 0)'}),
'custom_data': ('django.db.models.fields.CharField', [], {'default': "u''", 'max_length': '100', 'blank': 'True'}),
'dst_field': ('django.db.models.fields.CharField', [], {'max_length': '50'}),
'dst_lang': ('django.db.models.fields.CharField', [], {'default': "u''", 'max_length': '10', 'blank': 'True'}),
u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'object_id': ('django.db.models.fields.PositiveIntegerField', [], {}),
'order': ('django.db.models.fields.related.ForeignKey', [], {'to': u"orm['translations.GengoJob']", 'null': 'True'}),
'order': ('django.db.models.fields.related.ForeignKey', [], {'to': u"orm['translations.GengoOrder']", 'null': 'True'}),
'src_field': ('django.db.models.fields.CharField', [], {'max_length': '50'}),
'src_lang': ('django.db.models.fields.CharField', [], {'default': "u''", 'max_length': '10', 'blank': 'True'}),
'status': ('django.db.models.fields.CharField', [], {'default': "'created'", 'max_length': '12'})
Expand Down
115 changes: 62 additions & 53 deletions fjord/translations/models.py
Expand Up @@ -116,6 +116,30 @@ def pull_translations(self):
"""
raise NotImplementedError()

def log_info(self, instance, action='translate', msg=u'', metadata=None):
metadata = metadata or {}

record = j_info(
app='translations',
src=self.name,
action=action,
msg=msg,
instance=instance,
metadata=metadata
)

def log_error(self, instance, action='translate', msg=u'', metadata=None):
metadata = metadata or {}

j_error(
app='translations',
src=self.name,
action=action,
msg=msg,
instance=instance,
metadata=metadata
)


# ---------------------------------------------------------
# Fake translation system
Expand All @@ -128,12 +152,7 @@ class FakeTranslator(TranslationSystem):
def translate(self, instance, src_lang, src_field, dst_lang, dst_field):
setattr(instance, dst_field, getattr(instance, src_field).upper())
instance.save()
j_info(
app='translations',
src=self.name,
action='translate',
msg='success',
instance=instance)
self.log_info(instance=instance, action='translate', msg='success')

def pull_translations(self):
# This is a no-op for testing purposes.
Expand All @@ -159,12 +178,7 @@ def translate(self, instance, src_lang, src_field, dst_lang, dst_field):
translated = Translator([], pipeline).translate_string(text)
setattr(instance, dst_field, translated)
instance.save()
j_info(
app='translations',
src=self.name,
action='translate',
msg='success',
instance=instance)
self.log_info(instance=instance, action='translate', msg='success')


# ---------------------------------------------------------
Expand All @@ -175,36 +189,6 @@ class GengoMachineTranslator(TranslationSystem):
"""Translates using Gengo machine translation"""
name = 'gengo_machine'

def info(self, instance, action='translate', msg=u'', text=u''):
text = text or u''

j_info(
app='translations',
src=self.name,
action=action,
msg=msg,
instance=instance,
metadata={
'locale': instance.locale,
'length': len(text),
'body': text[:50].encode('utf-8'),
})

def error(self, instance, action='translate', msg=u'', text=u''):
text = text or u''

j_error(
app='translations',
src=self.name,
action=action,
msg=msg,
instance=instance,
metadata={
'locale': instance.locale,
'length': len(text),
'body': text[:50].encode('utf-8'),
})

def translate(self, instance, src_lang, src_field, dst_lang, dst_field):
text = getattr(instance, src_field)

Expand All @@ -225,36 +209,61 @@ def translate(self, instance, src_lang, src_field, dst_lang, dst_field):
if translated:
setattr(instance, dst_field, translated)
instance.save()
self.info(instance, action='translate', msg='success',
text=text)
metadata = {
'locale': instance.locale,
'length': len(text),
'body': text[:50].encode('utf-8')
}
self.log_info(instance, action='translate', msg='success',
metadata=metadata)
statsd.incr('translation.gengo_machine.success')

else:
self.error(instance, action='translate',
msg='did not translate', text=text)
metadata = {
'locale': instance.locale,
'length': len(text),
'body': text[:50].encode('utf-8')
}
self.log_error(instance, action='translate',
msg='did not translate', metadata=metadata)
statsd.incr('translation.gengo_machine.failure')

except GengoUnknownLanguage as exc:
# FIXME: This might be an indicator that this response is
# spam. At some point p, we can write code to account for
# that.
self.error(instance, action='guess-language', msg=unicode(exc),
text=text)
metadata = {
'locale': instance.locale,
'length': len(text),
'body': text[:50].encode('utf-8')
}
self.log_error(instance, action='guess-language', msg=unicode(exc),
metadata=metadata)
statsd.incr('translation.gengo_machine.unknown')

except GengoUnsupportedLanguage as exc:
# FIXME: This is a similar boat to GengoUnknownLanguage
# where for now, we're just going to ignore it because I'm
# not sure what to do about it and I'd like more data.
self.error(instance, action='translate', msg=unicode(exc),
text=text)
metadata = {
'locale': instance.locale,
'length': len(text),
'body': text[:50].encode('utf-8')
}
self.log_error(instance, action='translate', msg=unicode(exc),
metadata=metadata)
statsd.incr('translation.gengo_machine.unsupported')

except GengoMachineTranslationFailure:
# FIXME: For now, if we have a machine translation
# failure, we're just going to ignore it and move on.
self.error(instance, action='translate', msg=unicode(exc),
text=text)
metadata = {
'locale': instance.locale,
'length': len(text),
'body': text[:50].encode('utf-8')
}
self.log_error(instance, action='translate', msg=unicode(exc),
metadata=metadata)
statsd.incr('translation.gengo_machine.failure')


Expand Down Expand Up @@ -291,7 +300,7 @@ class GengoJob(models.Model):
# Status of the job and the order it's tied to
status = models.CharField(
choices=STATUS_CHOICES, default=STATUS_CREATED, max_length=12)
order = models.ForeignKey('translations.GengoJob', null=True)
order = models.ForeignKey('translations.GengoOrder', null=True)

# When the Gengo job is submitted, we generate an "id" that ties
# it back to our system. This is that id.
Expand Down

0 comments on commit 38d8584

Please sign in to comment.