-
Notifications
You must be signed in to change notification settings - Fork 29
/
activities.py
129 lines (101 loc) · 4.61 KB
/
activities.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# Copyright Swiss Data Science Center (SDSC). A partnership between
# École Polytechnique Fédérale de Lausanne (EPFL) and
# Eidgenössische Technische Hochschule Zürich (ETHZ).
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Checks needed to determine integrity of datasets."""
import itertools
import click
from renku.command.command_builder import inject
from renku.command.util import WARNING
from renku.core.interface.activity_gateway import IActivityGateway
from renku.core.util import communication
from renku.domain_model.project_context import project_context
@inject.autoparams("activity_gateway")
def check_migrated_activity_ids(fix, activity_gateway: IActivityGateway, **_):
"""Check that activity ids were correctly migrated in the past."""
activities = activity_gateway.get_all_activities(include_deleted=True)
wrong_activities = [a for a in activities if not a.id.startswith("/activities/")]
if fix:
current_database = project_context.database
for activity in wrong_activities:
communication.info(f"Fixing activity '{activity.id}'")
activity_gateway.remove(activity, keep_reference=False)
# NOTE: Modify id on activity and children
activity.unfreeze()
activity.id = f"/activities/{activity.id}"
activity._p_oid = current_database.hash_id(activity.id)
activity.freeze()
for attribute in itertools.chain(
activity.usages, activity.hidden_usages, activity.generations, activity.parameters
):
object.__setattr__(attribute, "id", f"/activities/{attribute.id}") # type: ignore
activity.association.id = f"/activities/{activity.association.id}"
activity_gateway.add(activity)
wrong_activities = []
if not wrong_activities:
return True, None
problems = (
WARNING
+ "There are invalid activity ids in the project (use 'renku doctor --fix' to fix them):"
+ "\n\n\t"
+ "\n\t".join(click.style(a.id, fg="yellow") for a in wrong_activities)
+ "\n"
)
return False, problems
@inject.autoparams("activity_gateway")
def check_activity_dates(fix, activity_gateway: IActivityGateway, **_):
"""Check activities have correct start/end/delete dates.
Args:
fix(bool): Whether to fix found issues.
activity_gateway(IActivityGateway): Injected ActivityGateway.
_: keyword arguments.
Returns:
Tuple[bool, Optional[str]]: Tuple of whether there are activities with invalid dates a string of the problem.
"""
invalid_activities = []
for activity in activity_gateway.get_all_activities(include_deleted=True):
plan = activity.association.plan
if (
activity.started_at_time < plan.date_created
or activity.ended_at_time < activity.started_at_time
or (activity.invalidated_at and activity.invalidated_at < activity.ended_at_time)
):
invalid_activities.append(activity)
if not invalid_activities:
return True, None
if not fix:
ids = [a.id for a in invalid_activities]
message = (
WARNING
+ "The following activity have incorrect start, end, or delete date (use 'renku doctor --fix' to fix them):"
+ "\n\t"
+ "\n\t".join(ids)
)
return False, message
fix_activity_dates(activities=invalid_activities)
project_context.database.commit()
communication.info("Activity dates were fixed")
return True, None
def fix_activity_dates(activities):
"""Fix activities' start/end/delete dates."""
for activity in activities:
plan = activity.association.plan
activity.unfreeze()
if activity.started_at_time < plan.date_created:
activity.started_at_time = plan.date_created
if activity.ended_at_time < activity.started_at_time:
activity.ended_at_time = activity.started_at_time
if activity.invalidated_at and activity.invalidated_at < activity.ended_at_time:
activity.invalidated_at = activity.ended_at_time
activity.freeze()