def report_stats(call: APICall, company: str, request: ReportStatsOptionRequest): if not StatisticsReporter.supported: result = ReportStatsOptionResponse(supported=False) else: enabled = request.enabled with translate_errors_context(): query = Company.objects(id=company) if enabled is None: stats_option = query.first().defaults.stats_option else: stats_option = ReportStatsOption( enabled=enabled, enabled_time=datetime.utcnow(), enabled_version=get_version(), enabled_user=call.identity.user, ) updated = query.update(defaults__stats_option=stats_option) if not updated: raise errors.server_error.InternalError( f"Failed setting report_stats to {enabled}" ) data = stats_option.to_mongo() data["current_version"] = get_version() result = ReportStatsOptionResponse(**data) call.result.data_model = result
def _check_new_version_available(self) -> Optional[_VersionResponse]: url = config.get( "apiserver.check_for_updates.url", "https://updates.trains.allegro.ai/updates", ) uid = Settings.get_by_key("server.uuid") response = requests.get( url, json={ "versions": { self.component_name: str(get_version()) }, "uid": uid }, timeout=float( config.get("apiserver.check_for_updates.request_timeout_sec", 3.0)), ) if not response.ok: return response = response.json().get(self.component_name) if not response: return latest_version = response.get("version") if not latest_version: return cur_version = Version(get_version()) latest_version = Version(latest_version) if cur_version >= latest_version: return return self._VersionResponse( version=str(latest_version), patch_upgrade=(latest_version.major == cur_version.major and latest_version.minor == cur_version.minor), description=response.get("description").split("\r\n"), )
def get_token_for_user( user_id: str, company_id: str = None, expiration_sec: int = None, entities: dict = None, ) -> GetTokenResponse: with translate_errors_context(): query = dict(id=user_id) if company_id: query.update(company=company_id) user = User.objects(**query).first() if not user: raise errors.bad_request.InvalidUserId(**query) company_id = company_id or user.company company = Company.objects(id=company_id).only("id", "name").first() if not company: raise errors.bad_request.InvalidId( "invalid company associated with user", company=company) identity = Identity( user=user_id, company=company_id, role=user.role, user_name=user.name, company_name=company.name, ) token = Token.create_encoded_token( identity=identity, entities=entities, expiration_sec=expiration_sec, api_version=str(ServiceRepo.max_endpoint_version()), server_version=str(get_version()), server_build=str(get_build_number()), feature_set="basic", ) return GetTokenResponse(token=token.decode("ascii"))
def info(call: APICall): call.result.data = { "version": get_version(), "build": get_build_number(), "commit": get_commit_number(), }