Example #1
0
    def handle(self, *args, **options):

        purgatories = ImportPurgatory.objects.all()

        if not purgatories:
            self.stdout_writeln(("Purgatory is model-free! Congrats!"))
            return

        for purgatory in purgatories:
            self.stdout_writeln(
                "%s (%d %s, %s #%d)..." %
                (("Attempting to save models"), purgatory.model_count,
                 ("models"), ("attempt"), purgatory.retry_attempts))

            # Serialized version is ourselves (or an earlier version of ourselves),
            #   so say so explicitly to make sure errors get bubbled up to us properly.
            try:
                unsaved = model_sync.save_serialized_models(
                    data=purgatory,
                    src_version=version.VERSION)["unsaved_model_count"]
                if not unsaved:
                    self.stdout_writeln(
                        "\t%s :)" % (("All models were saved successfully!")))
                else:
                    self.stderr_writeln("\t%d %s" % (unsaved, (
                        "models still did not save.  Check 'exceptions' field in 'input purgatory' for failure details."
                    )))
            except Exception as e:
                self.stderr_writeln("\t%d %s  %s" %
                                    (unsaved,
                                     ("models still did not save."), e))
Example #2
0
    def sync_models(self):

        if self.counters_to_download is None or self.counters_to_upload is None:
            self.sync_device_records()

        # Download (but prepare for errors--both thrown and unthrown!)
        download_results = {
            "saved_model_count": 0,
            "unsaved_model_count": 0,
        }
        try:
            response = json.loads(
                self.post("models/download", {
                    "device_counters": self.counters_to_download
                }).content)
            # As usual, we're deserializing from the central server, so we assume that what we're getting
            #   is "smartly" dumbed down for us.  We don't need to specify the src_version, as it's
            #   pre-cleanaed for us.
            download_results = model_sync.save_serialized_models(
                response.get("models", "[]"))
            self.session.models_downloaded += download_results[
                "saved_model_count"]
            self.session.errors += download_results.has_key("error")
            self.session.errors += download_results.has_key("exceptions")
        except Exception as e:
            download_results["error"] = e
            self.session.errors += 1

        # Upload (but prepare for errors--both thrown and unthrown!)
        upload_results = {
            "saved_model_count": 0,
            "unsaved_model_count": 0,
        }
        try:
            # By not specifying a dest_version, we're sending everything.
            #   Again, this is OK because we're sending to the central server.
            response = self.post(
                "models/upload", {
                    "models":
                    model_sync.get_serialized_models(self.counters_to_upload)
                })
            upload_results = json.loads(response.content)
            self.session.models_uploaded += upload_results["saved_model_count"]
            self.session.errors += upload_results.has_key("error")
            self.session.errors += upload_results.has_key("exceptions")
        except Exception as e:
            upload_results["error"] = e
            self.session.errors += 1

        self.counters_to_download = None
        self.counters_to_upload = None

        return {
            "download_results": download_results,
            "upload_results": upload_results
        }
Example #3
0
    def sync_device_records(self):

        server_counters = self.get_server_device_counters()
        client_counters = self.get_client_device_counters()

        devices_to_download = []
        devices_to_upload = []

        self.counters_to_download = {}
        self.counters_to_upload = {}

        for device in client_counters:
            if device not in server_counters:
                devices_to_upload.append(device)
                self.counters_to_upload[device] = 0
            elif client_counters[device] > server_counters[device]:
                self.counters_to_upload[device] = server_counters[device]

        for device in server_counters:
            if device not in client_counters:
                devices_to_download.append(device)
                self.counters_to_download[device] = 0
            elif server_counters[device] > client_counters[device]:
                self.counters_to_download[device] = client_counters[device]

        response = json.loads(
            self.post("device/download", {
                "devices": devices_to_download
            }).content)
        # As usual, we're deserializing from the central server, so we assume that what we're getting
        #   is "smartly" dumbed down for us.  We don't need to specify the src_version, as it's
        #   pre-cleaned for us.
        download_results = model_sync.save_serialized_models(
            response.get("devices", "[]"), increment_counters=False)

        # BUGFIX(bcipolli) metadata only gets created if models are
        #   streamed; if a device is downloaded but no models are downloaded,
        #   metadata does not exist.  Let's just force it here.
        for device_id in devices_to_download:  # force
            try:
                d = Device.objects.get(id=device_id)
            except:
                continue
            dm = d.get_metadata()
            if not dm.counter_position:  # this would be nonzero if the device sync'd models
                dm.counter_position = self.counters_to_download[device_id]
            dm.save()

        self.session.models_downloaded += download_results["saved_model_count"]
        self.session.errors += download_results.has_key("error")
Example #4
0
def model_upload(data, session):
    """This device is getting data-related objects from another device."""
    if "models" not in data:
        return JsonResponse({"error": "Must provide models.", "saved_model_count": 0}, status=500)
    try:
        # Unserialize, knowing that the models were serialized by a client of its given version.
        #   dest_version assumed to be this device's version
        result = model_sync.save_serialized_models(data["models"], src_version=session.client_version)
    except Exception as e:
        result = { "error": e.message, "saved_model_count": 0 }

    session.models_uploaded += result["saved_model_count"]
    session.errors += result.has_key("error")
    return JsonResponse(result)
Example #5
0
def device_upload(data, session):
    """This device is getting device-related objects from another device"""
    # TODO(jamalex): check that the uploaded devices belong to the client device's zone and whatnot
    # (although it will only save zones from here if centrally signed, and devices if registered in a zone)
    try:
        # Unserialize, knowing that the models were serialized by a client of its given version.
        #   dest_version assumed to be this device's version
        result = model_sync.save_serialized_models(data.get("devices", "[]"), src_version=session.client_version)
    except Exception as e:
        result = { "error": e.message, "saved_model_count": 0 }

    session.models_uploaded += result["saved_model_count"]
    session.errors += result.has_key("error")
    return JsonResponse(result)
Example #6
0
    def sync_device_records(self):

        server_counters = self.get_server_device_counters()
        client_counters = self.get_client_device_counters()

        devices_to_download = []
        devices_to_upload = []

        self.counters_to_download = {}
        self.counters_to_upload = {}

        for device in client_counters:
            if device not in server_counters:
                devices_to_upload.append(device)
                self.counters_to_upload[device] = 0
            elif client_counters[device] > server_counters[device]:
                self.counters_to_upload[device] = server_counters[device]

        for device in server_counters:
            if device not in client_counters:
                devices_to_download.append(device)
                self.counters_to_download[device] = 0
            elif server_counters[device] > client_counters[device]:
                self.counters_to_download[device] = client_counters[device]

        response = json.loads(self.post("device/download", {"devices": devices_to_download}).content)
        # As usual, we're deserializing from the central server, so we assume that what we're getting
        #   is "smartly" dumbed down for us.  We don't need to specify the src_version, as it's
        #   pre-cleaned for us.
        download_results = model_sync.save_serialized_models(response.get("devices", "[]"), increment_counters=False)

        # BUGFIX(bcipolli) metadata only gets created if models are
        #   streamed; if a device is downloaded but no models are downloaded,
        #   metadata does not exist.  Let's just force it here.
        for device_id in devices_to_download: # force
            try:
                d = Device.objects.get(id=device_id)
            except:
                continue
            dm = d.get_metadata()
            if not dm.counter_position: # this would be nonzero if the device sync'd models
                dm.counter_position = self.counters_to_download[device_id]
            dm.save()

        self.session.models_downloaded += download_results["saved_model_count"]
        self.session.errors += download_results.has_key("error")
Example #7
0
    def sync_models(self):

        if self.counters_to_download is None or self.counters_to_upload is None:
            self.sync_device_records()

        # Download (but prepare for errors--both thrown and unthrown!)
        download_results = {
            "saved_model_count" : 0,
            "unsaved_model_count" : 0,
        }
        try:
            response = json.loads(self.post("models/download", {"device_counters": self.counters_to_download}).content)
            # As usual, we're deserializing from the central server, so we assume that what we're getting
            #   is "smartly" dumbed down for us.  We don't need to specify the src_version, as it's
            #   pre-cleanaed for us.
            download_results = model_sync.save_serialized_models(response.get("models", "[]"))
            self.session.models_downloaded += download_results["saved_model_count"]
            self.session.errors += download_results.has_key("error")
            self.session.errors += download_results.has_key("exceptions")
        except Exception as e:
            download_results["error"] = e
            self.session.errors += 1

        # Upload (but prepare for errors--both thrown and unthrown!)
        upload_results = {
            "saved_model_count" : 0,
            "unsaved_model_count" : 0,
        }
        try:
            # By not specifying a dest_version, we're sending everything.
            #   Again, this is OK because we're sending to the central server.
            response = self.post("models/upload", {"models": model_sync.get_serialized_models(self.counters_to_upload)})
            upload_results = json.loads(response.content)
            self.session.models_uploaded += upload_results["saved_model_count"]
            self.session.errors += upload_results.has_key("error")
            self.session.errors += upload_results.has_key("exceptions")
        except Exception as e:
            upload_results["error"] = e
            self.session.errors += 1

        self.counters_to_download = None
        self.counters_to_upload = None

        return {"download_results": download_results, "upload_results": upload_results}
Example #8
0
    def handle(self, *args, **options):
        
        purgatories = ImportPurgatory.objects.all()

        if not purgatories:
            self.stdout_writeln(("Purgatory is model-free! Congrats!"))
            return

        for purgatory in purgatories:
            self.stdout_writeln("%s (%d %s, %s #%d)..." %
                (("Attempting to save models"), 
                 purgatory.model_count, ("models"), 
                 ("attempt"), purgatory.retry_attempts))

            # Serialized version is ourselves (or an earlier version of ourselves),
            #   so say so explicitly to make sure errors get bubbled up to us properly.
            try:
                unsaved = model_sync.save_serialized_models(data=purgatory, src_version=version.VERSION)["unsaved_model_count"]
                if not unsaved:
                    self.stdout_writeln("\t%s :)"%(("All models were saved successfully!")))
                else:
                    self.stderr_writeln("\t%d %s" % (unsaved,("models still did not save.  Check 'exceptions' field in 'input purgatory' for failure details.")))
            except Exception as e:
                self.stderr_writeln("\t%d %s  %s" % (unsaved,("models still did not save."), e))