• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

SwissDataScienceCenter / renku-python / 5529030370

pending completion
5529030370

push

github-actions

Ralf Grubenmann
fix docker build, pin versions

24252 of 28479 relevant lines covered (85.16%)

2.95 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

35.06
/renku/ui/service/controllers/graph_export.py
1
# -*- coding: utf-8 -*-
2
#
3
# Copyright 2020 - Swiss Data Science Center (SDSC)
4
# A partnership between École Polytechnique Fédérale de Lausanne (EPFL) and
5
# Eidgenössische Technische Hochschule Zürich (ETHZ).
6
#
7
# Licensed under the Apache License, Version 2.0 (the "License");
8
# you may not use this file except in compliance with the License.
9
# You may obtain a copy of the License at
10
#
11
#     http://www.apache.org/licenses/LICENSE-2.0
12
#
13
# Unless required by applicable law or agreed to in writing, software
14
# distributed under the License is distributed on an "AS IS" BASIS,
15
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
# See the License for the specific language governing permissions and
17
# limitations under the License.
18
"""Renku graph export controller."""
4✔
19

20
from requests import RequestException
4✔
21
from sentry_sdk import capture_exception
4✔
22

23
from renku.command.graph import export_graph_command
4✔
24
from renku.command.migrate import migrations_check
4✔
25
from renku.command.view_model.graph import DotFormat
4✔
26
from renku.core.errors import RenkuException
4✔
27
from renku.ui.service.config import PROJECT_CLONE_NO_DEPTH
4✔
28
from renku.ui.service.controllers.api.abstract import ServiceCtrl
4✔
29
from renku.ui.service.controllers.api.mixins import RenkuOperationMixin
4✔
30
from renku.ui.service.serializers.graph import (
4✔
31
    GraphExportCallbackError,
32
    GraphExportCallbackSuccess,
33
    GraphExportRequest,
34
    GraphExportResponseRPC,
35
)
36
from renku.ui.service.views import result_response
4✔
37

38

39
class GraphExportCtrl(ServiceCtrl, RenkuOperationMixin):
4✔
40
    """Controller for export graph endpoint."""
41

42
    REQUEST_SERIALIZER = GraphExportRequest()
4✔
43
    RESPONSE_SERIALIZER = GraphExportResponseRPC()
4✔
44

45
    def __init__(self, cache, user_data, request_data):
4✔
46
        """Construct a datasets list controller."""
47
        self.ctx = GraphExportCtrl.REQUEST_SERIALIZER.load(request_data)
1✔
48
        super(GraphExportCtrl, self).__init__(cache, user_data, request_data, clone_depth=PROJECT_CLONE_NO_DEPTH)
1✔
49

50
    @property
4✔
51
    def context(self):
4✔
52
        """Controller operation context."""
53
        return self.ctx
1✔
54

55
    def renku_op(self):
4✔
56
        """Renku operation for the controller."""
57
        result = migrations_check().build().execute().output
×
58

59
        if not result["project_supported"]:
×
60
            raise RenkuException("project not supported")
×
61

62
        callback_payload = {
×
63
            "project_url": self.context["git_url"],
64
            "commit_id": self.context["revision"] or "master",
65
        }
66

67
        try:
×
68
            result = export_graph_command().build().execute(revision_or_range=self.context["revision"])
×
69

70
            format = self.context["format"]
×
71

72
            if format == "json-ld":
×
73
                result = result.output.as_jsonld_string(indentation=None)
×
74
            elif format == "rdf":
×
75
                result = result.output.as_rdf_string()
×
76
            elif format == "nt":
×
77
                result = result.output.as_nt_string()
×
78
            elif format == "dot":
×
79
                result = result.output.as_dot_string(format=DotFormat.FULL)
×
80
            elif format == "dot-landscape":
×
81
                result = result.output.as_dot_string(format=DotFormat.FULL_LANDSCAPE)
×
82
            else:
83
                raise NotImplementedError(f"Format {format} is not supported on this endpoint.")
×
84

85
            if self.context.get("callback_url"):
×
86
                self.report_success(callback_payload, {"payload": result}, self.context["callback_url"])
×
87

88
            return result
×
89
        except (RequestException, RenkuException, MemoryError) as e:
×
90
            if self.context.get("callback_url"):
×
91
                self.report_recoverable(callback_payload, e, self.context["callback_url"])
×
92
            raise
×
93
        except BaseException as e:
×
94
            if self.context.get("callback_url"):
×
95
                self.report_unrecoverable(callback_payload, e, self.context["callback_url"])
×
96
            raise
×
97

98
    def to_response(self):
4✔
99
        """Execute controller flow and serialize to service response."""
100
        self.ctx["graph"] = self.execute_op()
1✔
101
        return result_response(GraphExportCtrl.RESPONSE_SERIALIZER, self.ctx)
×
102

103
    def report_recoverable(self, payload, exception, callback_url):
4✔
104
        """Report to callback URL recoverable state."""
105
        from renku.core.util import requests
×
106

107
        capture_exception(exception)
×
108

109
        if not callback_url:
×
110
            return
×
111

112
        payload["failure"] = {"type": "RECOVERABLE_FAILURE", "message": str(exception)}
×
113

114
        data = GraphExportCallbackError().load(payload)
×
115
        requests.post(callback_url, data=data)
×
116

117
    def report_unrecoverable(self, payload, exception, callback_url):
4✔
118
        """Report to callback URL unrecoverable state."""
119
        from renku.core.util import requests
×
120

121
        capture_exception(exception)
×
122

123
        if not callback_url:
×
124
            return
×
125

126
        payload["failure"] = {"type": "UNRECOVERABLE_FAILURE", "message": str(exception)}
×
127

128
        data = GraphExportCallbackError().load(payload)
×
129
        requests.post(callback_url, data=data)
×
130

131
    def report_success(self, request_payload, graph_payload, callback_url):
4✔
132
        """Report to callback URL success state."""
133
        from renku.core.util import requests
×
134

135
        data = GraphExportCallbackSuccess().load({**request_payload, **graph_payload})
×
136

137
        if not callback_url:
×
138
            return data
×
139

140
        requests.post(callback_url, data=data)
×
141

142
        return data
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc