|  | 
| 1 | 1 | import logging | 
| 2 | 2 | from abc import abstractmethod, ABCMeta | 
| 3 | 3 | 
 | 
| 4 |  | -import time | 
| 5 |  | -import copy | 
| 6 | 4 | import base64 | 
| 7 | 5 | import random | 
| 8 | 6 | 
 | 
| 9 | 7 | from cbor2 import loads | 
| 10 | 8 | 
 | 
| 11 |  | -from . import utils | 
| 12 |  | -from .enums import PNStatusCategory, PNReconnectionPolicy, PNOperationType | 
| 13 |  | -from .models.consumer.common import PNStatus | 
| 14 |  | -from .models.server.subscribe import SubscribeEnvelope | 
| 15 |  | -from .dtos import SubscribeOperation, UnsubscribeOperation | 
| 16 |  | -from .callbacks import SubscribeCallback, ReconnectionCallback | 
| 17 |  | -from .models.subscription_item import SubscriptionItem | 
| 18 |  | -from .errors import PNERR_INVALID_ACCESS_TOKEN | 
| 19 |  | -from .exceptions import PubNubException | 
|  | 9 | +from pubnub import utils | 
|  | 10 | +from pubnub.enums import PNStatusCategory, PNReconnectionPolicy | 
|  | 11 | +from pubnub.models.consumer.common import PNStatus | 
|  | 12 | +from pubnub.models.server.subscribe import SubscribeEnvelope | 
|  | 13 | +from pubnub.dtos import SubscribeOperation, UnsubscribeOperation | 
|  | 14 | +from pubnub.callbacks import SubscribeCallback, ReconnectionCallback | 
|  | 15 | +from pubnub.models.subscription_item import SubscriptionItem | 
|  | 16 | +from pubnub.errors import PNERR_INVALID_ACCESS_TOKEN | 
|  | 17 | +from pubnub.exceptions import PubNubException | 
| 20 | 18 | 
 | 
| 21 | 19 | logger = logging.getLogger("pubnub") | 
| 22 | 20 | 
 | 
| @@ -398,171 +396,6 @@ def get_custom_params(self): | 
| 398 | 396 |         return {} | 
| 399 | 397 | 
 | 
| 400 | 398 | 
 | 
| 401 |  | -class TelemetryManager: | 
| 402 |  | -    TIMESTAMP_DIVIDER = 1000 | 
| 403 |  | -    MAXIMUM_LATENCY_DATA_AGE = 60 | 
| 404 |  | -    CLEAN_UP_INTERVAL = 1 | 
| 405 |  | -    CLEAN_UP_INTERVAL_MULTIPLIER = 1000 | 
| 406 |  | - | 
| 407 |  | -    def __init__(self): | 
| 408 |  | -        self.latencies = {} | 
| 409 |  | - | 
| 410 |  | -    @abstractmethod | 
| 411 |  | -    def _start_clean_up_timer(self): | 
| 412 |  | -        pass | 
| 413 |  | - | 
| 414 |  | -    @abstractmethod | 
| 415 |  | -    def _stop_clean_up_timer(self): | 
| 416 |  | -        pass | 
| 417 |  | - | 
| 418 |  | -    def operation_latencies(self): | 
| 419 |  | -        operation_latencies = {} | 
| 420 |  | - | 
| 421 |  | -        for endpoint_name, endpoint_latencies in self.latencies.items(): | 
| 422 |  | -            latency_key = 'l_' + endpoint_name | 
| 423 |  | - | 
| 424 |  | -            endpoint_average_latency = self.average_latency_from_data(endpoint_latencies) | 
| 425 |  | - | 
| 426 |  | -            if endpoint_average_latency > 0: | 
| 427 |  | -                operation_latencies[latency_key] = endpoint_average_latency | 
| 428 |  | - | 
| 429 |  | -        return operation_latencies | 
| 430 |  | - | 
| 431 |  | -    def clean_up_telemetry_data(self): | 
| 432 |  | -        current_timestamp = time.time() | 
| 433 |  | -        copy_latencies = copy.deepcopy(self.latencies) | 
| 434 |  | - | 
| 435 |  | -        for endpoint_name, endpoint_latencies in copy_latencies.items(): | 
| 436 |  | -            for latency_information in endpoint_latencies: | 
| 437 |  | -                if current_timestamp - latency_information["timestamp"] > self.MAXIMUM_LATENCY_DATA_AGE: | 
| 438 |  | -                    self.latencies[endpoint_name].remove(latency_information) | 
| 439 |  | - | 
| 440 |  | -            if len(self.latencies[endpoint_name]) == 0: | 
| 441 |  | -                del self.latencies[endpoint_name] | 
| 442 |  | - | 
| 443 |  | -    def store_latency(self, latency, operation_type): | 
| 444 |  | -        if operation_type != PNOperationType.PNSubscribeOperation and latency > 0: | 
| 445 |  | -            endpoint_name = self.endpoint_name_for_operation(operation_type) | 
| 446 |  | - | 
| 447 |  | -            store_timestamp = time.time() | 
| 448 |  | - | 
| 449 |  | -            if endpoint_name not in self.latencies: | 
| 450 |  | -                self.latencies[endpoint_name] = [] | 
| 451 |  | - | 
| 452 |  | -            latency_entry = { | 
| 453 |  | -                "timestamp": store_timestamp, | 
| 454 |  | -                "latency": latency, | 
| 455 |  | -            } | 
| 456 |  | - | 
| 457 |  | -            self.latencies[endpoint_name].append(latency_entry) | 
| 458 |  | - | 
| 459 |  | -    @staticmethod | 
| 460 |  | -    def average_latency_from_data(endpoint_latencies): | 
| 461 |  | -        total_latency = 0 | 
| 462 |  | - | 
| 463 |  | -        for latency_data in endpoint_latencies: | 
| 464 |  | -            total_latency += latency_data['latency'] | 
| 465 |  | - | 
| 466 |  | -        return total_latency / len(endpoint_latencies) | 
| 467 |  | - | 
| 468 |  | -    @staticmethod | 
| 469 |  | -    def endpoint_name_for_operation(operation_type): | 
| 470 |  | -        endpoint = { | 
| 471 |  | -            PNOperationType.PNPublishOperation: 'pub', | 
| 472 |  | -            PNOperationType.PNFireOperation: 'pub', | 
| 473 |  | -            PNOperationType.PNSendFileNotification: "pub", | 
| 474 |  | - | 
| 475 |  | -            PNOperationType.PNHistoryOperation: 'hist', | 
| 476 |  | -            PNOperationType.PNHistoryDeleteOperation: 'hist', | 
| 477 |  | -            PNOperationType.PNMessageCountOperation: 'mc', | 
| 478 |  | - | 
| 479 |  | -            PNOperationType.PNUnsubscribeOperation: 'pres', | 
| 480 |  | -            PNOperationType.PNWhereNowOperation: 'pres', | 
| 481 |  | -            PNOperationType.PNHereNowOperation: 'pres', | 
| 482 |  | -            PNOperationType.PNGetState: 'pres', | 
| 483 |  | -            PNOperationType.PNSetStateOperation: 'pres', | 
| 484 |  | -            PNOperationType.PNHeartbeatOperation: 'pres', | 
| 485 |  | - | 
| 486 |  | -            PNOperationType.PNAddChannelsToGroupOperation: 'cg', | 
| 487 |  | -            PNOperationType.PNRemoveChannelsFromGroupOperation: 'cg', | 
| 488 |  | -            PNOperationType.PNChannelGroupsOperation: 'cg', | 
| 489 |  | -            PNOperationType.PNChannelsForGroupOperation: 'cg', | 
| 490 |  | -            PNOperationType.PNRemoveGroupOperation: 'cg', | 
| 491 |  | - | 
| 492 |  | -            PNOperationType.PNAddPushNotificationsOnChannelsOperation: 'push', | 
| 493 |  | -            PNOperationType.PNPushNotificationEnabledChannelsOperation: 'push', | 
| 494 |  | -            PNOperationType.PNRemoveAllPushNotificationsOperation: 'push', | 
| 495 |  | -            PNOperationType.PNRemovePushNotificationsFromChannelsOperation: 'push', | 
| 496 |  | - | 
| 497 |  | -            PNOperationType.PNAccessManagerAudit: 'pam', | 
| 498 |  | -            PNOperationType.PNAccessManagerGrant: 'pam', | 
| 499 |  | -            PNOperationType.PNAccessManagerRevoke: 'pam', | 
| 500 |  | -            PNOperationType.PNTimeOperation: 'pam', | 
| 501 |  | - | 
| 502 |  | -            PNOperationType.PNAccessManagerGrantToken: 'pamv3', | 
| 503 |  | -            PNOperationType.PNAccessManagerRevokeToken: 'pamv3', | 
| 504 |  | - | 
| 505 |  | -            PNOperationType.PNSignalOperation: 'sig', | 
| 506 |  | - | 
| 507 |  | -            PNOperationType.PNSetUuidMetadataOperation: 'obj', | 
| 508 |  | -            PNOperationType.PNGetUuidMetadataOperation: 'obj', | 
| 509 |  | -            PNOperationType.PNRemoveUuidMetadataOperation: 'obj', | 
| 510 |  | -            PNOperationType.PNGetAllUuidMetadataOperation: 'obj', | 
| 511 |  | - | 
| 512 |  | -            PNOperationType.PNSetChannelMetadataOperation: 'obj', | 
| 513 |  | -            PNOperationType.PNGetChannelMetadataOperation: 'obj', | 
| 514 |  | -            PNOperationType.PNRemoveChannelMetadataOperation: 'obj', | 
| 515 |  | -            PNOperationType.PNGetAllChannelMetadataOperation: 'obj', | 
| 516 |  | - | 
| 517 |  | -            PNOperationType.PNSetChannelMembersOperation: 'obj', | 
| 518 |  | -            PNOperationType.PNGetChannelMembersOperation: 'obj', | 
| 519 |  | -            PNOperationType.PNRemoveChannelMembersOperation: 'obj', | 
| 520 |  | -            PNOperationType.PNManageChannelMembersOperation: 'obj', | 
| 521 |  | - | 
| 522 |  | -            PNOperationType.PNSetMembershipsOperation: 'obj', | 
| 523 |  | -            PNOperationType.PNGetMembershipsOperation: 'obj', | 
| 524 |  | -            PNOperationType.PNRemoveMembershipsOperation: 'obj', | 
| 525 |  | -            PNOperationType.PNManageMembershipsOperation: 'obj', | 
| 526 |  | - | 
| 527 |  | -            PNOperationType.PNAddMessageAction: 'msga', | 
| 528 |  | -            PNOperationType.PNGetMessageActions: 'msga', | 
| 529 |  | -            PNOperationType.PNDeleteMessageAction: 'msga', | 
| 530 |  | - | 
| 531 |  | -            PNOperationType.PNGetFilesAction: 'file', | 
| 532 |  | -            PNOperationType.PNDeleteFileOperation: 'file', | 
| 533 |  | -            PNOperationType.PNGetFileDownloadURLAction: 'file', | 
| 534 |  | -            PNOperationType.PNFetchFileUploadS3DataAction: 'file', | 
| 535 |  | -            PNOperationType.PNDownloadFileAction: 'file', | 
| 536 |  | -            PNOperationType.PNSendFileAction: 'file', | 
| 537 |  | - | 
| 538 |  | - | 
| 539 |  | -            PNOperationType.PNFetchMessagesOperation: "hist", | 
| 540 |  | - | 
| 541 |  | -            PNOperationType.PNCreateSpaceOperation: "obj", | 
| 542 |  | -            PNOperationType.PNUpdateSpaceOperation: "obj", | 
| 543 |  | -            PNOperationType.PNFetchSpaceOperation: "obj", | 
| 544 |  | -            PNOperationType.PNFetchSpacesOperation: "obj", | 
| 545 |  | -            PNOperationType.PNRemoveSpaceOperation: "obj", | 
| 546 |  | - | 
| 547 |  | -            PNOperationType.PNCreateUserOperation: "obj", | 
| 548 |  | -            PNOperationType.PNUpdateUserOperation: "obj", | 
| 549 |  | -            PNOperationType.PNFetchUserOperation: "obj", | 
| 550 |  | -            PNOperationType.PNFetchUsersOperation: "obj", | 
| 551 |  | -            PNOperationType.PNRemoveUserOperation: "obj", | 
| 552 |  | - | 
| 553 |  | -            PNOperationType.PNAddUserSpacesOperation: "obj", | 
| 554 |  | -            PNOperationType.PNAddSpaceUsersOperation: "obj", | 
| 555 |  | -            PNOperationType.PNUpdateUserSpacesOperation: "obj", | 
| 556 |  | - | 
| 557 |  | -            PNOperationType.PNUpdateSpaceUsersOperation: "obj", | 
| 558 |  | -            PNOperationType.PNFetchUserMembershipsOperation: "obj", | 
| 559 |  | -            PNOperationType.PNFetchSpaceMembershipsOperation: "obj", | 
| 560 |  | - | 
| 561 |  | -        }[operation_type] | 
| 562 |  | - | 
| 563 |  | -        return endpoint | 
| 564 |  | - | 
| 565 |  | - | 
| 566 | 399 | class TokenManager: | 
| 567 | 400 |     def __init__(self): | 
| 568 | 401 |         self.token = None | 
|  | 
0 commit comments