Exemplo n.º 1
0
    def Transform(self, client, activity, callback):
        from activity import Activity
        from asset_id import AssetIdUniquifier
        from device import Device

        def _OnUpdate(new_activity):
            """Delete the old activity."""
            if Version._mutate_items:
                activity.Delete(client, partial(callback, new_activity))
            else:
                callback(new_activity)

        timestamp, device_id, uniquifier = Activity.DeconstructActivityId(
            activity.activity_id)

        if device_id == Device.SYSTEM:
            if activity.activity_id in DisambiguateActivityIds2._unique_activity_ids:
                # Already saw this activity id, so append viewpoint id to it.
                assert uniquifier.server_id is None, (activity, uniquifier)
                new_uniquifier = AssetIdUniquifier(uniquifier.client_id,
                                                   activity.viewpoint_id)
                new_activity_id = Activity.ConstructActivityId(
                    timestamp, device_id, new_uniquifier)

                new_activity_dict = activity._asdict()
                new_activity_dict['activity_id'] = new_activity_id

                new_activity = Activity.CreateFromKeywords(**new_activity_dict)

                logging.info('%s\n%s (%s/%s/%s) => %s (%s/%s/%s/%s)' %
                             (activity, activity.activity_id, timestamp,
                              device_id, uniquifier.client_id, new_activity_id,
                              timestamp, device_id, new_uniquifier.client_id,
                              new_uniquifier.server_id))

                if Version._mutate_items:
                    new_activity.Update(client,
                                        partial(_OnUpdate, new_activity))
                else:
                    _OnUpdate(new_activity)
            else:
                DisambiguateActivityIds2._unique_activity_ids.add(
                    activity.activity_id)
                callback(activity)
        else:
            assert activity.activity_id not in DisambiguateActivityIds2._unique_activity_ids, activity
            callback(activity)
Exemplo n.º 2
0
        def _OnQueryActivity(activity):
            if activity is None:
                # Also migrate any notifications which are using the older activity id.
                timestamp, device_id, uniquifier = Activity.DeconstructActivityId(
                    notification.activity_id)
                new_uniquifier = AssetIdUniquifier(uniquifier.client_id,
                                                   notification.viewpoint_id)
                new_activity_id = Activity.ConstructActivityId(
                    timestamp, device_id, new_uniquifier)
                Activity.Query(client,
                               notification.viewpoint_id,
                               new_activity_id,
                               None,
                               _OnQueryNewActivity,
                               must_exist=False)
                return

            notification.activity_id = activity.activity_id
            notification.update_seq = activity.update_seq
            _DoUpdate()