Skip to content

Commit 4990617

Browse files
authored
Merge pull request #203 from poissoncorp/RDBC-780
RDBC-780 Python client - commands
2 parents 7316d03 + 6e7b87e commit 4990617

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+2866
-80
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
from __future__ import annotations
2+
3+
import json
4+
from typing import Dict, Any, TYPE_CHECKING, Optional, List
5+
6+
import requests
7+
from ravendb import RavenCommand, IndexQuery, ServerNode
8+
from ravendb.extensions.json_extensions import JsonExtensions
9+
10+
if TYPE_CHECKING:
11+
from ravendb.documents.conventions import DocumentConventions
12+
13+
14+
class ExplainQueryResult:
15+
def __init__(self, index: str, reason: str):
16+
self.index = index
17+
self.reason = reason
18+
19+
@classmethod
20+
def from_json(cls, json_dict: Dict[str, Any]) -> ExplainQueryResult:
21+
return cls(json_dict["Index"], json_dict["Reason"])
22+
23+
24+
class ExplainQueryCommand(RavenCommand[List[ExplainQueryResult]]):
25+
def __init__(self, conventions: DocumentConventions, index_query: IndexQuery):
26+
super().__init__(list)
27+
if conventions is None:
28+
raise ValueError("Conventions cannot be None")
29+
if index_query is None:
30+
raise ValueError("Index query cannot be None")
31+
32+
self._conventions = conventions
33+
self._index_query = index_query
34+
35+
def create_request(self, node: ServerNode) -> requests.Request:
36+
path = f"{node.url}/databases/{node.database}/queries?debug=explain"
37+
return requests.Request(
38+
"POST", path, data=JsonExtensions.write_index_query(self._conventions, self._index_query)
39+
)
40+
41+
def set_response(self, response: Optional[str], from_cache: bool) -> None:
42+
if response is None:
43+
return
44+
45+
json_node = json.loads(response)
46+
results = json_node.get("Results", None)
47+
if results is None:
48+
self._throw_invalid_response()
49+
return
50+
51+
self.result = [ExplainQueryResult.from_json(res) for res in results]
52+
53+
def is_read_request(self) -> bool:
54+
return True
Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
1-
from typing import Optional
1+
from typing import Optional, Any, Dict
22

33

44
class AnalyzerDefinition:
55
def __init__(self, name: Optional[str] = None, code: Optional[str] = None):
66
self.name = name
77
self.code = code
8+
9+
@classmethod
10+
def from_json(cls, json_dict: Dict[str, Any]) -> "AnalyzerDefinition":
11+
return cls(json_dict["Name"], json_dict["Code"])
12+
13+
def to_json(self) -> Dict[str, Any]:
14+
return {"Name": self.name, "Code": self.code}

ravendb/documents/indexes/definitions.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import annotations
22
import datetime
3+
import enum
34
import re
45
from enum import Enum
56
from abc import ABC
@@ -641,3 +642,10 @@ def _strip_comments(map_str: str) -> str:
641642
@staticmethod
642643
def _unify_white_space(map_str: str) -> str:
643644
return re.sub("\\s+", " ", map_str)
645+
646+
647+
class IndexRunningStatus(enum.Enum):
648+
RUNNING = "Running"
649+
PAUSED = "Paused"
650+
DISABLED = "Disabled"
651+
PENDING = "Pending"

ravendb/documents/indexes/stats.py

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
from __future__ import annotations
2+
from datetime import datetime
3+
from typing import Dict, Any
4+
5+
from ravendb.documents.indexes.definitions import (
6+
IndexState,
7+
IndexPriority,
8+
IndexLockMode,
9+
IndexType,
10+
IndexSourceType,
11+
IndexRunningStatus,
12+
)
13+
from ravendb.tools.utils import Utils
14+
15+
16+
class IndexStats:
17+
def __init__(
18+
self,
19+
name: str = None,
20+
map_attempts: int = None,
21+
map_successes: int = None,
22+
map_errors: int = None,
23+
map_reference_attempts: int = None,
24+
map_reference_successes: int = None,
25+
map_reference_errors: int = None,
26+
reduce_attempts: int = None,
27+
reduce_successes: int = None,
28+
reduce_errors: int = None,
29+
reduce_output_collection: str = None,
30+
reduce_output_reference_pattern: str = None,
31+
pattern_references_collection_name: str = None,
32+
mapped_per_second_rate: float = None,
33+
reduced_per_second_rate: float = None,
34+
max_number_of_outputs_per_document: int = None,
35+
collections: Dict[str, CollectionStats] = None,
36+
last_querying_time: datetime = None,
37+
state: IndexState = None,
38+
priority: IndexPriority = None,
39+
created_timestamp: datetime = None,
40+
last_indexing_time: datetime = None,
41+
stale: bool = None,
42+
lock_mode: IndexLockMode = None,
43+
type: IndexType = None,
44+
status: IndexRunningStatus = None,
45+
entries_count: int = None,
46+
errors_count: int = None,
47+
source_type: IndexSourceType = None,
48+
is_test_index: bool = None,
49+
):
50+
self.name = name
51+
self.map_attempts = map_attempts
52+
self.map_successes = map_successes
53+
self.map_errors = map_errors
54+
self.map_reference_attempts = map_reference_attempts
55+
self.map_reference_successes = map_reference_successes
56+
self.map_reference_errors = map_reference_errors
57+
self.reduce_attempts = reduce_attempts
58+
self.reduce_successes = reduce_successes
59+
self.reduce_errors = reduce_errors
60+
self.reduce_output_collection = reduce_output_collection
61+
self.reduce_output_reference_pattern = reduce_output_reference_pattern
62+
self.pattern_references_collection_name = pattern_references_collection_name
63+
self.mapped_per_second_rate = mapped_per_second_rate
64+
self.reduced_per_second_rate = reduced_per_second_rate
65+
self.max_number_of_outputs_per_document = max_number_of_outputs_per_document
66+
self.collections = collections
67+
self.last_querying_time = last_querying_time
68+
self.state = state
69+
self.priority = priority
70+
self.created_timestamp = created_timestamp
71+
self.last_indexing_time = last_indexing_time
72+
self.stale = stale
73+
self.lock_mode = lock_mode
74+
self.type = type
75+
self.status = status
76+
self.entries_count = entries_count
77+
self.errors_count = errors_count
78+
self.source_type = source_type
79+
self.is_test_index = is_test_index
80+
81+
class CollectionStats:
82+
def __init__(
83+
self,
84+
last_processed_document_etag: int = None,
85+
last_processed_tombstone_etag: int = None,
86+
document_lag: int = None,
87+
tombstone_lag: int = None,
88+
):
89+
self.last_processed_document_etag = last_processed_document_etag
90+
self.last_processed_tombstone_etag = last_processed_tombstone_etag
91+
self.document_lag = document_lag
92+
self.tombstone_lag = tombstone_lag
93+
94+
@classmethod
95+
def from_json(cls, json_dict: Dict[str, Any]) -> "IndexStats.CollectionStats":
96+
return cls(
97+
json_dict["LastProcessedDocumentEtag"],
98+
json_dict["LastProcessedTombstoneEtag"],
99+
json_dict["DocumentLag"],
100+
json_dict["TombstoneLag"],
101+
)
102+
103+
@classmethod
104+
def from_json(cls, json_dict: Dict[str, Any]) -> IndexStats:
105+
return cls(
106+
json_dict["Name"],
107+
json_dict["MapAttempts"],
108+
json_dict["MapSuccesses"],
109+
json_dict["MapErrors"],
110+
json_dict["MapReferenceAttempts"],
111+
json_dict["MapReferenceSuccesses"],
112+
json_dict["MapReferenceErrors"],
113+
json_dict["ReduceAttempts"],
114+
json_dict["ReduceSuccesses"],
115+
json_dict["ReduceErrors"],
116+
json_dict["ReduceOutputCollection"],
117+
json_dict["ReduceOutputReferencePattern"],
118+
json_dict["PatternReferencesCollectionName"],
119+
json_dict["MappedPerSecondRate"],
120+
json_dict["ReducedPerSecondRate"],
121+
json_dict["MaxNumberOfOutputsPerDocument"],
122+
{key: cls.CollectionStats.from_json(value) for key, value in json_dict["Collections"].items()},
123+
Utils.string_to_datetime(json_dict["LastQueryingTime"]),
124+
IndexState(json_dict["State"]),
125+
IndexPriority(json_dict["Priority"]),
126+
Utils.string_to_datetime(json_dict["CreatedTimestamp"]),
127+
Utils.string_to_datetime(json_dict["LastIndexingTime"]),
128+
json_dict["IsStale"],
129+
IndexLockMode(json_dict["LockMode"]),
130+
IndexType(json_dict["Type"]),
131+
IndexRunningStatus(json_dict["Status"]),
132+
json_dict["EntriesCount"],
133+
json_dict["ErrorsCount"],
134+
IndexSourceType(json_dict["SourceType"]),
135+
json_dict.get("IsTestIndex", None),
136+
)

0 commit comments

Comments
 (0)