Ejemplo n.º 1
0
    def thumbnail(self, pk: int, digest: str,
                  **kwargs: Any) -> WerkzeugResponse:
        """Get Dashboard thumbnail
        ---
        get:
          description: >-
            Compute async or get already computed dashboard thumbnail from cache.
          parameters:
          - in: path
            schema:
              type: integer
            name: pk
          - in: path
            name: digest
            description: A hex digest that makes this dashboard unique
            schema:
              type: string
          - in: query
            name: q
            content:
              application/json:
                schema:
                  $ref: '#/components/schemas/thumbnail_query_schema'
          responses:
            200:
              description: Dashboard thumbnail image
              content:
               image/*:
                 schema:
                   type: string
                   format: binary
            202:
              description: Thumbnail does not exist on cache, fired async to compute
              content:
                application/json:
                  schema:
                    type: object
                    properties:
                      message:
                        type: string
            302:
              description: Redirects to the current digest
            401:
              $ref: '#/components/responses/401'
            404:
              $ref: '#/components/responses/404'
            422:
              $ref: '#/components/responses/422'
            500:
              $ref: '#/components/responses/500'
        """
        dashboard = self.datamodel.get(pk, self._base_filters)
        if not dashboard:
            return self.response_404()

        dashboard_url = get_url_path("Superset.dashboard",
                                     dashboard_id_or_slug=dashboard.id)
        # If force, request a screenshot from the workers
        if kwargs["rison"].get("force", False):
            cache_dashboard_thumbnail.delay(dashboard_url,
                                            dashboard.digest,
                                            force=True)
            return self.response(202, message="OK Async")
        # fetch the dashboard screenshot using the current user and cache if set
        screenshot = DashboardScreenshot(
            dashboard_url,
            dashboard.digest).get_from_cache(cache=thumbnail_cache)
        # If the screenshot does not exist, request one from the workers
        if not screenshot:
            self.incr_stats("async", self.thumbnail.__name__)
            cache_dashboard_thumbnail.delay(dashboard_url,
                                            dashboard.digest,
                                            force=True)
            return self.response(202, message="OK Async")
        # If digests
        if dashboard.digest != digest:
            self.incr_stats("redirect", self.thumbnail.__name__)
            return redirect(
                url_for(
                    f"{self.__class__.__name__}.thumbnail",
                    pk=pk,
                    digest=dashboard.digest,
                ))
        self.incr_stats("from_cache", self.thumbnail.__name__)
        return Response(FileWrapper(screenshot),
                        mimetype="image/png",
                        direct_passthrough=True)
Ejemplo n.º 2
0
def event_after_dashboard_changed(  # pylint: disable=unused-argument
    mapper: Mapper, connection: Connection, target: Dashboard
) -> None:
    cache_dashboard_thumbnail.delay(target.id, force=True)
Ejemplo n.º 3
0
def event_after_dashboard_changed(
    _mapper: Mapper, _connection: Connection, target: Dashboard
) -> None:
    url = get_url_path("Superset.dashboard", dashboard_id_or_slug=target.id)
    cache_dashboard_thumbnail.delay(url, target.digest, force=True)
Ejemplo n.º 4
0
 def update_thumbnail(self) -> None:
     url = get_url_path("Superset.dashboard", dashboard_id_or_slug=self.id)
     cache_dashboard_thumbnail.delay(url, self.digest, force=True)
Ejemplo n.º 5
0
def event_after_dashboard_changed(  # pylint: disable=unused-argument
    mapper: Mapper, connection: Connection, target: Dashboard
) -> None:
    url = get_url_path("Superset.dashboard", dashboard_id=target.id)
    cache_dashboard_thumbnail.delay(url, target.digest, force=True)