Beispiel #1
0
 def _get_children(self, assets: AssetList) -> AssetList:
     ids = [a.id for a in assets]
     tasks = []
     chunk_size = 100
     for i in range(0, len(ids), chunk_size):
         tasks.append({"parent_ids": ids[i:i + chunk_size], "limit": -1})
     tasks_summary = utils._concurrency.execute_tasks_concurrently(
         self.list, tasks=tasks, max_workers=self._config.max_workers)
     tasks_summary.raise_compound_exception_if_failed_tasks()
     res_list = tasks_summary.results
     children = AssetList([])
     for res in res_list:
         children.extend(res)
     return children
Beispiel #2
0
 def run(self):
     unblocked_assets_lists = self._get_unblocked_assets()
     for unblocked_assets in unblocked_assets_lists:
         self.request_queue.put(list(unblocked_assets))
     while self.assets_remaining():
         res = self.response_queue.get()
         if isinstance(res, _AssetsFailedToPost):
             if isinstance(res.exc, CogniteAPIError):
                 self.exception = res.exc
                 for asset in res.assets:
                     if res.exc.code >= 500:
                         self.may_have_been_posted_assets.add(asset)
                     elif res.exc.code >= 400:
                         self.not_posted_assets.add(asset)
                     for descendant in self._get_descendants(asset):
                         self.not_posted_assets.add(descendant)
             else:
                 raise res.exc
         else:
             for asset in res:
                 self.posted_assets.add(asset)
                 self.successfully_posted_external_ids.add(
                     asset.external_id)
             unblocked_assets_lists = self._get_unblocked_assets()
             for unblocked_assets in unblocked_assets_lists:
                 self.request_queue.put(list(unblocked_assets))
     if len(self.may_have_been_posted_assets) > 0 or len(
             self.not_posted_assets) > 0:
         if isinstance(self.exception, CogniteAPIError):
             raise CogniteAPIError(
                 message=self.exception.message,
                 code=self.exception.code,
                 x_request_id=self.exception.x_request_id,
                 missing=self.exception.missing,
                 duplicated=self.exception.duplicated,
                 successful=AssetList(list(self.posted_assets)),
                 unknown=AssetList(list(self.may_have_been_posted_assets)),
                 failed=AssetList(list(self.not_posted_assets)),
                 unwrap_fn=lambda a: a.external_id,
             )
         raise self.exception
Beispiel #3
0
    def post(self):
        workers = []
        for _ in range(self.client._config.max_workers):
            worker = _AssetPosterWorker(self.client, self.request_queue,
                                        self.response_queue)
            workers.append(worker)
            worker.start()

        self.run()

        for worker in workers:
            worker.stop = True

        return AssetList(self.posted_assets)
Beispiel #4
0
    def retrieve_subtree(self,
                         id: int = None,
                         external_id: str = None,
                         depth: int = None) -> AssetList:
        """Retrieve the subtree for this asset up to a specified depth.

        Args:
            id (int): Id of the root asset in the subtree.
            external_id (str): External id of the root asset in the subtree.
            depth (int): Retrieve assets up to this depth below the root asset in the subtree. Omit to get the entire
                subtree.

        Returns:
            AssetList: The requested assets.
        """
        utils._auxiliary.assert_exactly_one_of_id_or_external_id(
            id, external_id)
        asset = self.retrieve(id=id, external_id=external_id)
        subtree = self._get_asset_subtree(AssetList([asset]),
                                          current_depth=0,
                                          depth=depth)
        subtree._cognite_client = self._cognite_client
        return subtree