import logging

import emission.core.wrapper.motionactivity as ecwa
import emission.net.usercache.formatters.common as fc
import attrdict as ad

def format(entry):
    formatted_entry = ad.AttrDict()
    formatted_entry["_id"] = entry["_id"]
    formatted_entry.user_id = entry.user_id

    metadata = entry.metadata
    if "time_zone" not in metadata:
        metadata.time_zone = "America/Los_Angeles" 
    fc.expand_metadata_times(metadata)
    formatted_entry.metadata = metadata

    data = ad.AttrDict()
    if 'agb' in entry.data:
        data.type = ecwa.MotionTypes(entry.data.agb).value
    else:
        data.type = ecwa.MotionTypes(entry.data.zzaEg).value

    if 'agc' in entry.data:
        data.confidence = entry.data.agc
    else:
        data.confidence = entry.data.zzaEh

    data.ts = formatted_entry.metadata.write_ts
    data.local_dt = formatted_entry.metadata.write_local_dt
    data.fmt_time = formatted_entry.metadata.write_fmt_time
    formatted_entry.data = data

    return formatted_entry

import logging

import emission.core.wrapper.motionactivity as ecwa
import emission.net.usercache.formatters.common as fc
import attrdict as ad

def format(entry):
    formatted_entry = ad.AttrDict()
    formatted_entry["_id"] = entry["_id"]
    formatted_entry.user_id = entry.user_id

    metadata = entry.metadata
    if "time_zone" not in metadata:
        metadata.time_zone = "America/Los_Angeles" 
    fc.expand_metadata_times(metadata)
    formatted_entry.metadata = metadata

    data = ad.AttrDict()
    if 'agb' in entry.data:
        data.type = ecwa.MotionTypes(entry.data.agb).value
    elif 'zzaEg' in entry.data:
        data.type = ecwa.MotionTypes(entry.data.zzaEg).value
    else:
        data.type = ecwa.MotionTypes(entry.data.zzaKM).value


    if 'agc' in entry.data:
        data.confidence = entry.data.agc
    elif 'zzaEh' in entry.data:
        data.confidence = entry.data.zzaEh
    else:
        data.confidence = entry.data.zzaKN

    data.ts = formatted_entry.metadata.write_ts
    data.local_dt = formatted_entry.metadata.write_local_dt
    data.fmt_time = formatted_entry.metadata.write_fmt_time
    formatted_entry.data = data

    return formatted_entry

Support the new google play motion activity format
Support the new google play motion activity format

As part of the change to slim down the apk for android,
https://github.com/e-mission/e-mission-phone/pull/46,
https://github.com/e-mission/e-mission-data-collection/pull/116
we switched from google play version from 8.1.0 to 8.3.0, which is the version
that has separate jar files, at least in my install.

But this changed the motion activity format - the field names are now `zzaKM`
and `zzaKN` instead of `zzaEg` and `zzaEh`. So we change the formatter on the
server to handle this use case as well.

Note that we should really fix
https://github.com/e-mission/e-mission-data-collection/issues/80
to stop running into this in the future

  import logging
  
  import emission.core.wrapper.motionactivity as ecwa
  import emission.net.usercache.formatters.common as fc
  import attrdict as ad
  
  def format(entry):
      formatted_entry = ad.AttrDict()
      formatted_entry["_id"] = entry["_id"]
      formatted_entry.user_id = entry.user_id
  
      metadata = entry.metadata
      if "time_zone" not in metadata:
          metadata.time_zone = "America/Los_Angeles" 
      fc.expand_metadata_times(metadata)
      formatted_entry.metadata = metadata
  
      data = ad.AttrDict()
      if 'agb' in entry.data:
          data.type = ecwa.MotionTypes(entry.data.agb).value
+     elif 'zzaEg' in entry.data:
+         data.type = ecwa.MotionTypes(entry.data.zzaEg).value
      else:
-         data.type = ecwa.MotionTypes(entry.data.zzaEg).value
+         data.type = ecwa.MotionTypes(entry.data.zzaKM).value
+ 
  
      if 'agc' in entry.data:
          data.confidence = entry.data.agc
+     elif 'zzaEh' in entry.data:
+         data.confidence = entry.data.zzaEh
      else:
-         data.confidence = entry.data.zzaEh
+         data.confidence = entry.data.zzaKN
  
      data.ts = formatted_entry.metadata.write_ts
      data.local_dt = formatted_entry.metadata.write_local_dt
      data.fmt_time = formatted_entry.metadata.write_fmt_time
      formatted_entry.data = data
  
      return formatted_entry
  
Support the new google play motion activity format
## Code Before:
import logging

import emission.core.wrapper.motionactivity as ecwa
import emission.net.usercache.formatters.common as fc
import attrdict as ad

def format(entry):
    formatted_entry = ad.AttrDict()
    formatted_entry["_id"] = entry["_id"]
    formatted_entry.user_id = entry.user_id

    metadata = entry.metadata
    if "time_zone" not in metadata:
        metadata.time_zone = "America/Los_Angeles" 
    fc.expand_metadata_times(metadata)
    formatted_entry.metadata = metadata

    data = ad.AttrDict()
    if 'agb' in entry.data:
        data.type = ecwa.MotionTypes(entry.data.agb).value
    else:
        data.type = ecwa.MotionTypes(entry.data.zzaEg).value

    if 'agc' in entry.data:
        data.confidence = entry.data.agc
    else:
        data.confidence = entry.data.zzaEh

    data.ts = formatted_entry.metadata.write_ts
    data.local_dt = formatted_entry.metadata.write_local_dt
    data.fmt_time = formatted_entry.metadata.write_fmt_time
    formatted_entry.data = data

    return formatted_entry

## Instruction:
Support the new google play motion activity format
## Code After:
import logging

import emission.core.wrapper.motionactivity as ecwa
import emission.net.usercache.formatters.common as fc
import attrdict as ad

def format(entry):
    formatted_entry = ad.AttrDict()
    formatted_entry["_id"] = entry["_id"]
    formatted_entry.user_id = entry.user_id

    metadata = entry.metadata
    if "time_zone" not in metadata:
        metadata.time_zone = "America/Los_Angeles" 
    fc.expand_metadata_times(metadata)
    formatted_entry.metadata = metadata

    data = ad.AttrDict()
    if 'agb' in entry.data:
        data.type = ecwa.MotionTypes(entry.data.agb).value
    elif 'zzaEg' in entry.data:
        data.type = ecwa.MotionTypes(entry.data.zzaEg).value
    else:
        data.type = ecwa.MotionTypes(entry.data.zzaKM).value


    if 'agc' in entry.data:
        data.confidence = entry.data.agc
    elif 'zzaEh' in entry.data:
        data.confidence = entry.data.zzaEh
    else:
        data.confidence = entry.data.zzaKN

    data.ts = formatted_entry.metadata.write_ts
    data.local_dt = formatted_entry.metadata.write_local_dt
    data.fmt_time = formatted_entry.metadata.write_fmt_time
    formatted_entry.data = data

    return formatted_entry
