Conditions | 10 |
Total Lines | 72 |
Code Lines | 47 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like asyncua.server.history_mongo.HistoryMongoDb.read_event_history() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | from ..common.events import Event |
||
57 | async def read_event_history(self, source_id, start, end, nb_values, evfilter, session: InternalSession): |
||
58 | # create the variables |
||
59 | _session_id = str(session.session_id) |
||
60 | events_query = list() |
||
61 | query = {'$and': [{"time_stamp": {"$gte": start}}, {"time_stamp": {"$lte": end}}]} |
||
62 | |||
63 | # limits the number of requests per query |
||
64 | if (not nb_values) or nb_values > self.limits_for_request: |
||
65 | _max_value = self.limits_for_request + 1 |
||
66 | else: |
||
67 | _max_value = nb_values + 1 |
||
68 | |||
69 | # check if the session exists, otherwise create a data key for it |
||
70 | if _session_id not in self.clients_session_packages: |
||
71 | self.clients_session_packages[_session_id] = dict() |
||
72 | self.clients_session_packages[_session_id]["isFirstRequest"] = True |
||
73 | self.clients_session_packages[_session_id]["length"] = 0 |
||
74 | self.clients_session_packages[_session_id]["count_pages"] = 0 |
||
75 | self.clients_session_packages[_session_id]["pages"] = 0 |
||
76 | |||
77 | # if the session already exists it means it's a next appointment |
||
78 | else: |
||
79 | self.clients_session_packages[_session_id]["count_pages"] += 1 |
||
80 | |||
81 | # create initial query data, return number of documents, calculate number of required pages. |
||
82 | if self.clients_session_packages[_session_id]["isFirstRequest"]: |
||
83 | length_request = await self.collection.count_documents(query) |
||
84 | pages = round(length_request / (_max_value - 1)) |
||
85 | missing = length_request % (_max_value - 1) |
||
86 | |||
87 | if missing > 0: |
||
88 | pages += 1 |
||
89 | |||
90 | self.clients_session_packages[_session_id]["length"] = length_request |
||
91 | self.clients_session_packages[_session_id]["pages"] = pages |
||
92 | self.clients_session_packages[_session_id]["isFirstRequest"] = False |
||
93 | |||
94 | # execute the query by sorting by date and limiting the cursor to only the number requested by the client |
||
95 | cursor = self.collection.find(query, {"_id": 0}).sort([('sample_datetime', 1)]).limit(_max_value) |
||
96 | async for document in cursor: |
||
97 | events_query.append(document) |
||
98 | |||
99 | events = list(map(self.format_event, events_query)) |
||
100 | length = len(events) |
||
101 | |||
102 | # if the query returns one more format document and event to display pagination data |
||
103 | if length > (_max_value - 1): |
||
104 | last_event = events_query[length - 1] |
||
105 | last_time_stamp = last_event["time_stamp"] |
||
106 | |||
107 | last_sample = dict() |
||
108 | last_sample["pages"] = self.clients_session_packages[_session_id]["pages"] |
||
109 | last_sample["count"] = self.clients_session_packages[_session_id]["count_pages"] |
||
110 | last_sample["length"] = self.clients_session_packages[_session_id]["length"] |
||
111 | last_sample["last_sample"] = last_time_stamp.isoformat() |
||
112 | |||
113 | event = Event() |
||
114 | content = LocalizedText(text=json.dumps(last_sample)) |
||
115 | event.add_property("Message", content, VariantType(21)) |
||
116 | |||
117 | events.pop(length - 1) |
||
118 | events.append(event) |
||
119 | |||
120 | pages = self.clients_session_packages[_session_id]["pages"] |
||
121 | count_pages = self.clients_session_packages[_session_id]["count_pages"] |
||
122 | |||
123 | # check if the query number is equal to the number of pages, if yes delete the session |
||
124 | if count_pages >= (pages - 1): |
||
125 | if _session_id in self.clients_session_packages: |
||
126 | del self.clients_session_packages[_session_id] |
||
127 | |||
128 | return events, None |
||
129 | |||
139 |