cjapy 0.2.2.post3__tar.gz → 0.2.3__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {cjapy-0.2.2.post3 → cjapy-0.2.3}/PKG-INFO +1 -1
- cjapy-0.2.3/cjapy/__version__.py +1 -0
- {cjapy-0.2.2.post3 → cjapy-0.2.3}/cjapy/cjapy.py +106 -0
- {cjapy-0.2.2.post3 → cjapy-0.2.3}/cjapy/requestCreator.py +73 -7
- {cjapy-0.2.2.post3 → cjapy-0.2.3}/cjapy.egg-info/PKG-INFO +1 -1
- {cjapy-0.2.2.post3 → cjapy-0.2.3}/docs/releases.md +1 -0
- cjapy-0.2.2.post3/cjapy/__version__.py +0 -1
- {cjapy-0.2.2.post3 → cjapy-0.2.3}/LICENSE +0 -0
- {cjapy-0.2.2.post3 → cjapy-0.2.3}/README.md +0 -0
- {cjapy-0.2.2.post3 → cjapy-0.2.3}/cjapy/__init__.py +0 -0
- {cjapy-0.2.2.post3 → cjapy-0.2.3}/cjapy/config.py +0 -0
- {cjapy-0.2.2.post3 → cjapy-0.2.3}/cjapy/configs.py +0 -0
- {cjapy-0.2.2.post3 → cjapy-0.2.3}/cjapy/connector.py +0 -0
- {cjapy-0.2.2.post3 → cjapy-0.2.3}/cjapy/projects.py +0 -0
- {cjapy-0.2.2.post3 → cjapy-0.2.3}/cjapy/token_provider.py +0 -0
- {cjapy-0.2.2.post3 → cjapy-0.2.3}/cjapy/workspace.py +0 -0
- {cjapy-0.2.2.post3 → cjapy-0.2.3}/cjapy.egg-info/SOURCES.txt +0 -0
- {cjapy-0.2.2.post3 → cjapy-0.2.3}/cjapy.egg-info/dependency_links.txt +0 -0
- {cjapy-0.2.2.post3 → cjapy-0.2.3}/cjapy.egg-info/requires.txt +0 -0
- {cjapy-0.2.2.post3 → cjapy-0.2.3}/cjapy.egg-info/top_level.txt +0 -0
- {cjapy-0.2.2.post3 → cjapy-0.2.3}/docs/authenticating_without_config_json.md +0 -0
- {cjapy-0.2.2.post3 → cjapy-0.2.3}/docs/cja.md +0 -0
- {cjapy-0.2.2.post3 → cjapy-0.2.3}/docs/getting_started.md +0 -0
- {cjapy-0.2.2.post3 → cjapy-0.2.3}/docs/logging.md +0 -0
- {cjapy-0.2.2.post3 → cjapy-0.2.3}/docs/main.md +0 -0
- {cjapy-0.2.2.post3 → cjapy-0.2.3}/docs/projects.md +0 -0
- {cjapy-0.2.2.post3 → cjapy-0.2.3}/docs/requestCreator.md +0 -0
- {cjapy-0.2.2.post3 → cjapy-0.2.3}/docs/workspace.md +0 -0
- {cjapy-0.2.2.post3 → cjapy-0.2.3}/pyproject.toml +0 -0
- {cjapy-0.2.2.post3 → cjapy-0.2.3}/requirements.txt +0 -0
- {cjapy-0.2.2.post3 → cjapy-0.2.3}/setup.cfg +0 -0
- {cjapy-0.2.2.post3 → cjapy-0.2.3}/setup.py +0 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
__version__ = "0.2.3"
|
|
@@ -7,6 +7,8 @@ from typing import IO, Union, List
|
|
|
7
7
|
from collections import defaultdict, deque
|
|
8
8
|
import time, logging, re
|
|
9
9
|
from itertools import tee
|
|
10
|
+
from datetime import datetime, timedelta
|
|
11
|
+
|
|
10
12
|
|
|
11
13
|
# Non standard libraries
|
|
12
14
|
import pandas as pd
|
|
@@ -2454,3 +2456,107 @@ class CJA:
|
|
|
2454
2456
|
metricFilters="notApplicable",
|
|
2455
2457
|
)
|
|
2456
2458
|
return workspace
|
|
2459
|
+
|
|
2460
|
+
def getFreeformTable(
|
|
2461
|
+
self,
|
|
2462
|
+
dimension: str = None,
|
|
2463
|
+
metrics: List[str] = None,
|
|
2464
|
+
dataviewId: str = None,
|
|
2465
|
+
start_date: Union[str, datetime] = None,
|
|
2466
|
+
end_date: Union[str, datetime] = None,
|
|
2467
|
+
top_n: int = 400,
|
|
2468
|
+
filterId: str = None,
|
|
2469
|
+
search: Union[str, List[str]] = None,
|
|
2470
|
+
search_operator: str = "OR",
|
|
2471
|
+
) -> pd.DataFrame:
|
|
2472
|
+
"""
|
|
2473
|
+
Retrieves a freeform table report with the specified parameters.
|
|
2474
|
+
|
|
2475
|
+
Arguments:
|
|
2476
|
+
dimension : REQUIRED : The dimension to include in the report.
|
|
2477
|
+
metrics : REQUIRED : List of metrics to include in the report.
|
|
2478
|
+
dataviewId : REQUIRED : The dataView ID to use for your report.
|
|
2479
|
+
start_date : OPTIONAL : The start date for the report in 'YYYY-MM-DD' format or datetime object (defaults to 30 days ago).
|
|
2480
|
+
end_date : OPTIONAL : The end date for the report in 'YYYY-MM-DD' format or datetime object (defaults to today).
|
|
2481
|
+
top_n : OPTIONAL : Number of top results to return (default 400).
|
|
2482
|
+
filterId : OPTIONAL : The segment ID to filter the report.
|
|
2483
|
+
search : OPTIONAL : A search parameter (string or list of strings) to filter the dimension values.
|
|
2484
|
+
search_operator : OPTIONAL : The logical operator to combine search terms (default is OR).
|
|
2485
|
+
|
|
2486
|
+
Returns:
|
|
2487
|
+
A pandas DataFrame containing the report data.
|
|
2488
|
+
"""
|
|
2489
|
+
if dimension is None:
|
|
2490
|
+
raise ValueError("Require at least a dimension")
|
|
2491
|
+
if metrics is None:
|
|
2492
|
+
raise ValueError("Require at least a metric")
|
|
2493
|
+
if dataviewId is None:
|
|
2494
|
+
raise ValueError("Require a Data View ID")
|
|
2495
|
+
def ensure_dimension_prefix(dimension: str) -> str:
|
|
2496
|
+
if not dimension.startswith("variables/"):
|
|
2497
|
+
return f"variables/{dimension}"
|
|
2498
|
+
return dimension
|
|
2499
|
+
def ensure_metric_prefix(metric: str) -> str:
|
|
2500
|
+
if not metric.startswith("metrics/"):
|
|
2501
|
+
return f"metrics/{metric}"
|
|
2502
|
+
return metric
|
|
2503
|
+
def ensure_datetime_format(date: Union[str, datetime], is_start: bool) -> str:
|
|
2504
|
+
if isinstance(date, str):
|
|
2505
|
+
date = datetime.fromisoformat(date)
|
|
2506
|
+
if date.time() == datetime.min.time():
|
|
2507
|
+
if is_start:
|
|
2508
|
+
return date.strftime('%Y-%m-%dT00:00:00.000')
|
|
2509
|
+
else:
|
|
2510
|
+
return date.strftime('%Y-%m-%dT23:59:59.999')
|
|
2511
|
+
return date.strftime('%Y-%m-%dT%H:%M:%S.000')
|
|
2512
|
+
if search_operator not in ["OR", "AND"]:
|
|
2513
|
+
raise ValueError("search_operator must be 'OR' or 'AND'")
|
|
2514
|
+
if start_date is None:
|
|
2515
|
+
start_date = datetime.today() - timedelta(days=30)
|
|
2516
|
+
if end_date is None:
|
|
2517
|
+
end_date = datetime.today()
|
|
2518
|
+
start_date_str = ensure_datetime_format(start_date, is_start=True)
|
|
2519
|
+
end_date_str = ensure_datetime_format(end_date, is_start=False)
|
|
2520
|
+
date_range = f"{start_date_str}/{end_date_str}"
|
|
2521
|
+
dimension = ensure_dimension_prefix(dimension)
|
|
2522
|
+
metrics = [ensure_metric_prefix(metric) for metric in metrics]
|
|
2523
|
+
template = RequestCreator()
|
|
2524
|
+
template.setDataViewId(dataviewId)
|
|
2525
|
+
template.setDimension(dimension)
|
|
2526
|
+
for metric in metrics:
|
|
2527
|
+
template.addMetric(metric)
|
|
2528
|
+
if filterId:
|
|
2529
|
+
template.addGlobalFilter(filterId)
|
|
2530
|
+
template.addGlobalFilter(date_range)
|
|
2531
|
+
if search:
|
|
2532
|
+
if isinstance(search, str):
|
|
2533
|
+
search = [search]
|
|
2534
|
+
search_clause = f" {search_operator} ".join([f"CONTAINS '{term}'" for term in search])
|
|
2535
|
+
template.setSearch(clause=search_clause)
|
|
2536
|
+
if top_n < 50000:
|
|
2537
|
+
limit = top_n
|
|
2538
|
+
n_results = top_n
|
|
2539
|
+
else:
|
|
2540
|
+
limit = 50000
|
|
2541
|
+
n_results = 20
|
|
2542
|
+
request = template.to_dict()
|
|
2543
|
+
response = self.getReport(request=request, limit=limit, n_results=n_results)
|
|
2544
|
+
df = response.dataframe
|
|
2545
|
+
# Drop the itemId column if it exists
|
|
2546
|
+
if 'itemId' in df.columns:
|
|
2547
|
+
df = df.drop(columns=['itemId'])
|
|
2548
|
+
|
|
2549
|
+
# Remove any rows that exceed the requested top_n
|
|
2550
|
+
if len(df) > top_n:
|
|
2551
|
+
df = df.head(top_n)
|
|
2552
|
+
|
|
2553
|
+
# Clean the column names to remove prefixes
|
|
2554
|
+
def clean_column_name(col):
|
|
2555
|
+
if col.startswith("variables/"):
|
|
2556
|
+
return col.replace("variables/", "")
|
|
2557
|
+
elif col.startswith("metrics/"):
|
|
2558
|
+
return col.replace("metrics/", "")
|
|
2559
|
+
return col
|
|
2560
|
+
|
|
2561
|
+
df.columns = [clean_column_name(col) for col in df.columns]
|
|
2562
|
+
return df
|
|
@@ -21,9 +21,20 @@ class RequestCreator:
|
|
|
21
21
|
"limit": 20000,
|
|
22
22
|
"page": 0,
|
|
23
23
|
"nonesBehavior": "exclude-nones",
|
|
24
|
+
"sampling": None,
|
|
25
|
+
"samplingUpSample": None
|
|
24
26
|
},
|
|
25
27
|
"statistics": {"functions": ["col-max", "col-min"]},
|
|
26
28
|
"dataId": "",
|
|
29
|
+
"identityOverrides": [],
|
|
30
|
+
"capacityMetadata":{
|
|
31
|
+
"associations": [
|
|
32
|
+
{
|
|
33
|
+
"name": "applicationName",
|
|
34
|
+
"value": "cjapy Python Library"
|
|
35
|
+
}
|
|
36
|
+
]
|
|
37
|
+
}
|
|
27
38
|
}
|
|
28
39
|
|
|
29
40
|
def __init__(self, request: dict = None) -> None:
|
|
@@ -212,17 +223,43 @@ class RequestCreator:
|
|
|
212
223
|
"""
|
|
213
224
|
self.__request["settings"]["limit"] = limit
|
|
214
225
|
|
|
215
|
-
def setSearch(self,itemIds:list=None)-> None:
|
|
226
|
+
def setSearch(self,itemIds:list=None,clause:str=None,reset:bool=True)-> None:
|
|
216
227
|
"""
|
|
217
228
|
Set the items ID search in the specified dimension
|
|
218
229
|
Arguments :
|
|
219
|
-
itemIds :
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
230
|
+
itemIds : OPTIONAL : The list of itemId to be searched in the dimension
|
|
231
|
+
clause : OPTIONAL : If you want to set a specific clause such as:
|
|
232
|
+
"( CONTAINS 'Python' ) AND ( CONTAINS 'Platform' )"
|
|
233
|
+
"( CONTAINS 'Python' ) OR ( CONTAINS 'Platform' )"
|
|
234
|
+
"( MATCH 'Python' )" : Equals
|
|
235
|
+
"( BEGINS-WITH 'Python' )"
|
|
236
|
+
"( NOT CONTAINS 'Python' )"
|
|
237
|
+
reset : OPTIONAL : automatically reset the search from your request
|
|
238
|
+
"""
|
|
239
|
+
if reset:
|
|
240
|
+
self.__request['search']={}
|
|
241
|
+
if itemIds is not None:
|
|
242
|
+
self.__request["search"]["itemIds"] = itemIds
|
|
243
|
+
elif clause is not None:
|
|
244
|
+
self.__request["search"]["clause"] = clause
|
|
224
245
|
self.search = True
|
|
225
|
-
|
|
246
|
+
|
|
247
|
+
# def addDimensions(self,dimensionIds:list=None)->None:
|
|
248
|
+
# """
|
|
249
|
+
# In case you want to pass a list of dimensions to the report API.
|
|
250
|
+
# Arguments:
|
|
251
|
+
# dimensionIds : REQUIRED : The list of dimensions IDs you want to use
|
|
252
|
+
# """
|
|
253
|
+
# if dimensionIds is None:
|
|
254
|
+
# raise ValueError("dimensionIds parameters should be used")
|
|
255
|
+
# if type(dimensionIds) != list:
|
|
256
|
+
# raise TypeError("dimensionIds should be a list")
|
|
257
|
+
# if len(dimensionIds)>5:
|
|
258
|
+
# raise ValueError("dimensionIds should not countain more than 5 elements")
|
|
259
|
+
# if 'dimension' in self.__request.keys():
|
|
260
|
+
# del self.__request["dimension"]
|
|
261
|
+
# self.__request["dimensions"] = dimensionIds
|
|
262
|
+
|
|
226
263
|
def removeSearch(self)->None:
|
|
227
264
|
"""
|
|
228
265
|
Remove the search capability in the request.
|
|
@@ -230,6 +267,20 @@ class RequestCreator:
|
|
|
230
267
|
del self.__request["search"]
|
|
231
268
|
self.search = False
|
|
232
269
|
|
|
270
|
+
def setSampling(self, sample: float = None, upsample: bool = False) -> None:
|
|
271
|
+
"""
|
|
272
|
+
Set the sampling parameters. Sampling happens at the person level,
|
|
273
|
+
not at the event level to ensure all events for a person are included.
|
|
274
|
+
Upsampling will multiply the sample results to the full scale of the
|
|
275
|
+
original dataset (assuming the sample is representative).
|
|
276
|
+
Arguments:
|
|
277
|
+
sample : OPTIONAL : A float between 0 and 1 indicating the sample rate.
|
|
278
|
+
upsample : OPTIONAL : A boolean indicating whether to upsample (default is False).
|
|
279
|
+
"""
|
|
280
|
+
if sample is not None and (sample < 0 or sample > 1):
|
|
281
|
+
raise ValueError("Sample rate must be between 0 and 1.")
|
|
282
|
+
self.__request["settings"]["sampling"] = sample
|
|
283
|
+
self.__request["settings"]["samplingUpSample"] = upsample
|
|
233
284
|
|
|
234
285
|
def setRepeatInstance(self, repeat: bool = True) -> None:
|
|
235
286
|
"""
|
|
@@ -258,8 +309,23 @@ class RequestCreator:
|
|
|
258
309
|
"""
|
|
259
310
|
if dimension is None:
|
|
260
311
|
raise ValueError("A dimension must be passed")
|
|
312
|
+
if 'dimensions' in self.__request.keys():
|
|
313
|
+
del self.__request["dimensions"]
|
|
261
314
|
self.__request["dimension"] = dimension
|
|
262
315
|
|
|
316
|
+
def setIdentityOverrides(self, identity_overrides: list) -> None:
|
|
317
|
+
"""
|
|
318
|
+
Set the identityOverrides for the request.
|
|
319
|
+
Arguments:
|
|
320
|
+
identity_overrides : REQUIRED : the identityOverrides definition as a list of dictionaries
|
|
321
|
+
"""
|
|
322
|
+
if not isinstance(identity_overrides, list):
|
|
323
|
+
raise ValueError("identity_overrides must be a list of dictionaries")
|
|
324
|
+
if not all(isinstance(item, dict) for item in identity_overrides):
|
|
325
|
+
raise ValueError("All items in identity_overrides must be dictionaries")
|
|
326
|
+
|
|
327
|
+
self.__request["identityOverrides"] = identity_overrides
|
|
328
|
+
|
|
263
329
|
def setDataViewId(self, dataViewId: str = None) -> None:
|
|
264
330
|
"""
|
|
265
331
|
Set the dataView ID to be used for the reporting.
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
__version__ = "0.2.2-3"
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|