|
1 | | -from dataclasses import dataclass |
2 | | - |
3 | | -from azure.cosmos import PartitionKey |
4 | | - |
5 | | -from commons.data_access_layer.cosmos_db import ( |
6 | | - CosmosDBModel, |
7 | | - CosmosDBDao, |
8 | | - CosmosDBRepository, |
9 | | -) |
10 | | -from time_tracker_api.database import CRUDDao, APICosmosDBDao |
11 | | -from typing import List, Callable |
12 | | -from commons.data_access_layer.database import EventContext |
13 | | -from utils.enums.status import Status |
14 | | -from utils.query_builder import CosmosDBQueryBuilder |
15 | | - |
16 | | - |
17 | | -class ActivityDao(CRUDDao): |
18 | | - pass |
19 | | - |
20 | | - |
21 | | -container_definition = { |
22 | | - 'id': 'activity', |
23 | | - 'partition_key': PartitionKey(path='/tenant_id'), |
24 | | - 'unique_key_policy': { |
25 | | - 'uniqueKeys': [ |
26 | | - {'paths': ['/name', '/deleted']}, |
27 | | - ] |
28 | | - }, |
29 | | -} |
30 | | - |
31 | | - |
32 | | -@dataclass() |
33 | | -class ActivityCosmosDBModel(CosmosDBModel): |
34 | | - id: str |
35 | | - name: str |
36 | | - description: str |
37 | | - deleted: str |
38 | | - status: str |
39 | | - tenant_id: str |
40 | | - |
41 | | - def __init__(self, data): |
42 | | - super(ActivityCosmosDBModel, self).__init__(data) # pragma: no cover |
43 | | - |
44 | | - def __repr__(self): |
45 | | - return '<Activity %r>' % self.name # pragma: no cover |
46 | | - |
47 | | - def __str___(self): |
48 | | - return "the activity \"%s\"" % self.name # pragma: no cover |
49 | | - |
50 | | - |
51 | | -class ActivityCosmosDBRepository(CosmosDBRepository): |
52 | | - def __init__(self): |
53 | | - CosmosDBRepository.__init__( |
54 | | - self, |
55 | | - container_id=container_definition['id'], |
56 | | - partition_key_attribute='tenant_id', |
57 | | - mapper=ActivityCosmosDBModel, |
58 | | - ) |
59 | | - |
60 | | - def find_all_with_id_in_list( |
61 | | - self, |
62 | | - event_context: EventContext, |
63 | | - activity_ids: List[str], |
64 | | - visible_only=True, |
65 | | - mapper: Callable = None, |
66 | | - ): |
67 | | - query_builder = ( |
68 | | - CosmosDBQueryBuilder() |
69 | | - .add_sql_in_condition('id', activity_ids) |
70 | | - .add_sql_visibility_condition(visible_only) |
71 | | - .build() |
72 | | - ) |
73 | | - query_str = query_builder.get_query() |
74 | | - |
75 | | - tenant_id_value = self.find_partition_key_value(event_context) |
76 | | - result = self.container.query_items( |
77 | | - query=query_str, |
78 | | - partition_key=tenant_id_value, |
79 | | - ) |
80 | | - |
81 | | - function_mapper = self.get_mapper_or_dict(mapper) |
82 | | - return list(map(function_mapper, result)) |
83 | | - |
84 | | - def find_all( |
85 | | - self, |
86 | | - event_context: EventContext, |
87 | | - conditions: dict = None, |
88 | | - activities_id: List = None, |
89 | | - visible_only=True, |
90 | | - max_count=None, |
91 | | - offset=0, |
92 | | - mapper: Callable = None, |
93 | | - ): |
94 | | - query_builder = ( |
95 | | - CosmosDBQueryBuilder() |
96 | | - .add_sql_in_condition('id', activities_id) |
97 | | - .add_sql_where_equal_condition(conditions) |
98 | | - .add_sql_visibility_condition(visible_only) |
99 | | - .add_sql_limit_condition(max_count) |
100 | | - .add_sql_offset_condition(offset) |
101 | | - .build() |
102 | | - ) |
103 | | - |
104 | | - query_str = query_builder.get_query() |
105 | | - tenant_id_value = self.find_partition_key_value(event_context) |
106 | | - params = query_builder.get_parameters() |
107 | | - |
108 | | - result = self.container.query_items( |
109 | | - query=query_str, |
110 | | - parameters=params, |
111 | | - partition_key=tenant_id_value, |
112 | | - ) |
113 | | - function_mapper = self.get_mapper_or_dict(mapper) |
114 | | - return list(map(function_mapper, result)) |
115 | | - |
116 | | - |
117 | | -class ActivityCosmosDBDao(APICosmosDBDao, ActivityDao): |
118 | | - def __init__(self, repository): |
119 | | - CosmosDBDao.__init__(self, repository) |
120 | | - |
121 | | - def get_all_with_id_in_list( |
122 | | - self, |
123 | | - activity_ids, |
124 | | - ): |
125 | | - event_ctx = self.create_event_context("read-many") |
126 | | - return self.repository.find_all_with_id_in_list( |
127 | | - event_ctx, |
128 | | - activity_ids, |
129 | | - ) |
130 | | - |
131 | | - def get_all( |
132 | | - self, |
133 | | - conditions: dict = None, |
134 | | - activities_id: List = None, |
135 | | - max_count=None, |
136 | | - visible_only=True, |
137 | | - ) -> list: |
138 | | - event_ctx = self.create_event_context("read-many") |
139 | | - max_count = self.repository.get_page_size_or(max_count) |
140 | | - |
141 | | - activities = self.repository.find_all( |
142 | | - event_context=event_ctx, |
143 | | - conditions=conditions, |
144 | | - activities_id=activities_id, |
145 | | - visible_only=visible_only, |
146 | | - max_count=max_count, |
147 | | - ) |
148 | | - return activities |
149 | | - |
150 | | - def create(self, activity_payload: dict): |
151 | | - event_ctx = self.create_event_context('create') |
152 | | - activity_payload['status'] = Status.ACTIVE.value |
153 | | - return self.repository.create( |
154 | | - data=activity_payload, event_context=event_ctx |
155 | | - ) |
156 | | - |
157 | | - |
158 | | -def create_dao() -> ActivityDao: |
159 | | - repository = ActivityCosmosDBRepository() |
160 | | - |
161 | | - return ActivityCosmosDBDao(repository) |
| 1 | +from dataclasses import dataclass |
| 2 | + |
| 3 | +from azure.cosmos import PartitionKey |
| 4 | + |
| 5 | +from commons.data_access_layer.cosmos_db import ( |
| 6 | + CosmosDBModel, |
| 7 | + CosmosDBDao, |
| 8 | + CosmosDBRepository, |
| 9 | +) |
| 10 | +from time_tracker_api.database import CRUDDao, APICosmosDBDao |
| 11 | +from typing import List, Callable |
| 12 | +from commons.data_access_layer.database import EventContext |
| 13 | +from utils.enums.status import Status |
| 14 | +from utils.query_builder import CosmosDBQueryBuilder |
| 15 | +from commons.data_access_layer.file_stream import FileStream |
| 16 | + |
| 17 | +class ActivityDao(CRUDDao): |
| 18 | + pass |
| 19 | + |
| 20 | + |
| 21 | +container_definition = { |
| 22 | + 'id': 'activity', |
| 23 | + 'partition_key': PartitionKey(path='/tenant_id'), |
| 24 | + 'unique_key_policy': { |
| 25 | + 'uniqueKeys': [ |
| 26 | + {'paths': ['/name', '/deleted']}, |
| 27 | + ] |
| 28 | + }, |
| 29 | +} |
| 30 | + |
| 31 | + |
| 32 | +@dataclass() |
| 33 | +class ActivityCosmosDBModel(CosmosDBModel): |
| 34 | + id: str |
| 35 | + name: str |
| 36 | + description: str |
| 37 | + deleted: str |
| 38 | + status: str |
| 39 | + tenant_id: str |
| 40 | + |
| 41 | + def __init__(self, data): |
| 42 | + super(ActivityCosmosDBModel, self).__init__(data) # pragma: no cover |
| 43 | + |
| 44 | + def __repr__(self): |
| 45 | + return '<Activity %r>' % self.name # pragma: no cover |
| 46 | + |
| 47 | + def __str___(self): |
| 48 | + return "the activity \"%s\"" % self.name # pragma: no cover |
| 49 | + |
| 50 | + |
| 51 | +class ActivityCosmosDBRepository(CosmosDBRepository): |
| 52 | + def __init__(self): |
| 53 | + CosmosDBRepository.__init__( |
| 54 | + self, |
| 55 | + container_id=container_definition['id'], |
| 56 | + partition_key_attribute='tenant_id', |
| 57 | + mapper=ActivityCosmosDBModel, |
| 58 | + ) |
| 59 | + |
| 60 | + def find_all_with_id_in_list( |
| 61 | + self, |
| 62 | + event_context: EventContext, |
| 63 | + activity_ids: List[str], |
| 64 | + visible_only=True, |
| 65 | + mapper: Callable = None, |
| 66 | + ): |
| 67 | + query_builder = ( |
| 68 | + CosmosDBQueryBuilder() |
| 69 | + .add_sql_in_condition('id', activity_ids) |
| 70 | + .add_sql_visibility_condition(visible_only) |
| 71 | + .build() |
| 72 | + ) |
| 73 | + query_str = query_builder.get_query() |
| 74 | + |
| 75 | + tenant_id_value = self.find_partition_key_value(event_context) |
| 76 | + result = self.container.query_items( |
| 77 | + query=query_str, |
| 78 | + partition_key=tenant_id_value, |
| 79 | + ) |
| 80 | + |
| 81 | + function_mapper = self.get_mapper_or_dict(mapper) |
| 82 | + return list(map(function_mapper, result)) |
| 83 | + |
| 84 | + def find_all( |
| 85 | + self, |
| 86 | + event_context: EventContext, |
| 87 | + conditions: dict = None, |
| 88 | + activities_id: List = None, |
| 89 | + visible_only=True, |
| 90 | + max_count=None, |
| 91 | + offset=0, |
| 92 | + mapper: Callable = None, |
| 93 | + ): |
| 94 | + query_builder = ( |
| 95 | + CosmosDBQueryBuilder() |
| 96 | + .add_sql_in_condition('id', activities_id) |
| 97 | + .add_sql_where_equal_condition(conditions) |
| 98 | + .add_sql_visibility_condition(visible_only) |
| 99 | + .add_sql_limit_condition(max_count) |
| 100 | + .add_sql_offset_condition(offset) |
| 101 | + .build() |
| 102 | + ) |
| 103 | + |
| 104 | + query_str = query_builder.get_query() |
| 105 | + tenant_id_value = self.find_partition_key_value(event_context) |
| 106 | + params = query_builder.get_parameters() |
| 107 | + |
| 108 | + result = self.container.query_items( |
| 109 | + query=query_str, |
| 110 | + parameters=params, |
| 111 | + partition_key=tenant_id_value, |
| 112 | + ) |
| 113 | + function_mapper = self.get_mapper_or_dict(mapper) |
| 114 | + return list(map(function_mapper, result)) |
| 115 | + |
| 116 | + def find_all_from_blob_storage(self,conditions: dict = None, mapper: Callable = None): |
| 117 | + import json |
| 118 | + fs = FileStream("storagefiles2","ioetfiles") |
| 119 | + result = fs.get_file_stream("activity.json") |
| 120 | + |
| 121 | + function_mapper = self.get_mapper_or_dict(mapper) |
| 122 | + return list(map(function_mapper, json.load(result))) |
| 123 | + |
| 124 | +class ActivityCosmosDBDao(APICosmosDBDao, ActivityDao): |
| 125 | + def __init__(self, repository): |
| 126 | + CosmosDBDao.__init__(self, repository) |
| 127 | + |
| 128 | + def get_all_with_id_in_list( |
| 129 | + self, |
| 130 | + activity_ids, |
| 131 | + ): |
| 132 | + event_ctx = self.create_event_context("read-many") |
| 133 | + return self.repository.find_all_with_id_in_list( |
| 134 | + event_ctx, |
| 135 | + activity_ids, |
| 136 | + ) |
| 137 | + |
| 138 | + def get_all_old( |
| 139 | + self, |
| 140 | + conditions: dict = None, |
| 141 | + activities_id: List = None, |
| 142 | + max_count=None, |
| 143 | + visible_only=True, |
| 144 | + ) -> list: |
| 145 | + event_ctx = self.create_event_context("read-many") |
| 146 | + max_count = self.repository.get_page_size_or(max_count) |
| 147 | + |
| 148 | + activities = self.repository.find_all( |
| 149 | + event_context=event_ctx, |
| 150 | + conditions=conditions, |
| 151 | + activities_id=activities_id, |
| 152 | + visible_only=visible_only, |
| 153 | + max_count=max_count, |
| 154 | + ) |
| 155 | + return activities |
| 156 | + |
| 157 | + def get_all(self,conditions: dict = None) -> list: |
| 158 | + activities = self.repository.find_all_from_blob_storage() |
| 159 | + return activities |
| 160 | + |
| 161 | + def create(self, activity_payload: dict): |
| 162 | + event_ctx = self.create_event_context('create') |
| 163 | + activity_payload['status'] = Status.ACTIVE.value |
| 164 | + return self.repository.create( |
| 165 | + data=activity_payload, event_context=event_ctx |
| 166 | + ) |
| 167 | + |
| 168 | + |
| 169 | +def create_dao() -> ActivityDao: |
| 170 | + repository = ActivityCosmosDBRepository() |
| 171 | + |
| 172 | + return ActivityCosmosDBDao(repository) |
0 commit comments