|
| 1 | +import logging.config |
| 2 | +import datetime as dt |
| 3 | +import logging |
| 4 | + |
| 5 | +import httpx |
| 6 | + |
| 7 | +from cs_tools.models.ts_dataservice import TSDataService |
| 8 | +from cs_tools.models.dependency import _Dependency |
| 9 | +from cs_tools.models.periscope import _Periscope |
| 10 | +from cs_tools.models.metadata import Metadata, _Metadata |
| 11 | +from cs_tools.models.security import _Security |
| 12 | +from cs_tools.models.session import _Session |
| 13 | +from cs_tools.models.auth import Session |
| 14 | +from cs_tools.models.user import User |
| 15 | +from cs_tools.schema.user import User as UserSchema |
| 16 | +from cs_tools.errors import CertificateVerifyFailure |
| 17 | +from cs_tools.const import APP_DIR |
| 18 | + |
| 19 | + |
| 20 | +log = logging.getLogger(__name__) |
| 21 | + |
| 22 | + |
| 23 | +class ThoughtSpot: |
| 24 | + """ |
| 25 | + """ |
| 26 | + def __init__(self, ts_config): |
| 27 | + self.config = ts_config |
| 28 | + self._setup_logging() |
| 29 | + |
| 30 | + # set up our session |
| 31 | + # NOTE: base_url is a valid parameter for httpx.Client |
| 32 | + self.http = httpx.Client(timeout=180.0, verify=not ts_config.thoughtspot.disable_ssl) |
| 33 | + self.http.headers.update({'X-Requested-By': 'ThoughtSpot'}) |
| 34 | + |
| 35 | + # set in __enter__() |
| 36 | + self.logged_in_user = None |
| 37 | + self.thoughtspot_version = None |
| 38 | + |
| 39 | + # add remote TQL & tsload services |
| 40 | + self.ts_dataservice = TSDataService(self) |
| 41 | + |
| 42 | + # add public API endpoints |
| 43 | + self.metadata = Metadata(self) |
| 44 | + self.auth = Session(self) |
| 45 | + self.user = User(self) |
| 46 | + |
| 47 | + # add private API endpoints |
| 48 | + self._dependency = _Dependency(self) |
| 49 | + self._metadata = _Metadata(self) |
| 50 | + self._periscope = _Periscope(self) |
| 51 | + self._security = _Security(self) |
| 52 | + self._session = _Session(self) |
| 53 | + |
| 54 | + def _clean_logs(self, now): |
| 55 | + logs_dir = APP_DIR / 'logs' |
| 56 | + logs_dir.mkdir(parents=True, exist_ok=True) |
| 57 | + |
| 58 | + # keep only the last 25 logfiles |
| 59 | + lifo = sorted(logs_dir.iterdir(), reverse=True) |
| 60 | + |
| 61 | + for idx, log in enumerate(lifo): |
| 62 | + if idx > 25: |
| 63 | + log.unlink() |
| 64 | + |
| 65 | + def _setup_logging(self): |
| 66 | + logging.getLogger('urllib3').setLevel(logging.ERROR) |
| 67 | + |
| 68 | + now = dt.datetime.now().strftime('%Y-%m-%dT%H_%M_%S') |
| 69 | + self._clean_logs(now) |
| 70 | + |
| 71 | + logging.basicConfig( |
| 72 | + filename=f'{APP_DIR}/logs/{now}.log', |
| 73 | + format='[%(levelname)s - %(asctime)s] ' |
| 74 | + '[%(name)s - %(module)s.%(funcName)s %(lineno)d] ' |
| 75 | + '%(message)s', |
| 76 | + level=self.config.logging.level |
| 77 | + ) |
| 78 | + |
| 79 | + @property |
| 80 | + def host(self): |
| 81 | + """ |
| 82 | + URL of ThoughtSpot. |
| 83 | + """ |
| 84 | + return self.config.thoughtspot.host |
| 85 | + |
| 86 | + def __enter__(self): |
| 87 | + try: |
| 88 | + r = self.auth.login() |
| 89 | + except httpx.ConnectError as e: |
| 90 | + if 'CERTIFICATE_VERIFY_FAILED' in str(e): |
| 91 | + raise CertificateVerifyFailure() |
| 92 | + |
| 93 | + log.exception('something went wrong.. :(') |
| 94 | + raise SystemExit(1) |
| 95 | + |
| 96 | + rj = r.json() |
| 97 | + |
| 98 | + self.logged_in_user = UserSchema( |
| 99 | + guid=rj['userGUID'], name=rj['userName'], display_name=rj['userDisplayName'], |
| 100 | + email=rj['userEmail'], privileges=rj.get('privileges', ['AUTHORING']) |
| 101 | + ) |
| 102 | + |
| 103 | + self.thoughtspot_version = rj['releaseVersion'] |
| 104 | + return self |
| 105 | + |
| 106 | + def __exit__(self, exc_type, exc_value, exc_tb): |
| 107 | + self.auth.logout() |
0 commit comments