Passed
Push — master ( 087ae8...083155 )
by Daniel
01:41
created

tableau_hyper_management.TableauServerCommunicator   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 14
eloc 91
dl 0
loc 103
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A TableauServerCommunicator.connect_to_tableau_server() 0 15 1
A TableauServerCommunicator.disconnect_from_tableau_server() 0 5 1
A TableauServerCommunicator.get_project_relevancy() 0 10 5
A TableauServerCommunicator.publish_data_source_to_tableau_server() 0 13 1
A TableauServerCommunicator.is_publishing_possible() 0 20 3
A TableauServerCommunicator.load_tableau_project_ids() 0 24 3
1
# package tto ensure communication with Tableau Server
2
import tableauserverclient as tsc
3
4
from pathlib import Path
5
6
7
class TableauServerCommunicator():
8
    req_opts = tsc.RequestOptions(pagesize=1000)
9
    tableau_server = None
10
11
    def connect_to_tableau_server(self, local_logger, timmer, connection_details):
12
        timmer.start()
13
        local_logger.debug('I am about to connect to the Tableau Server with URL: '
14
                           + connection_details['Tableau Server'] + ' and the Site '
15
                           + connection_details['Tableau Site'] + ' using the Username '
16
                           + connection_details['Username'])
17
        self.tableau_server = tsc.Server(server_address     = connection_details['Tableau Server'],
18
                                         use_server_version = True)
19
        tableau_auth = tsc.TableauAuth(username               = connection_details['Username'],
20
                                       password               = connection_details['Password'],
21
                                       site_id                = connection_details['Tableau Site'],
22
                                       user_id_to_impersonate = None)
23
        self.tableau_server.auth.sign_in(tableau_auth)
24
        local_logger.debug('Connection to the Tableau Server has been established successfully!')
25
        timmer.stop()
26
27
    def disconnect_from_tableau_server(self, local_logger, timmer):
28
        timmer.start()
29
        self.tableau_server.auth.sign_out()
30
        local_logger.debug('The connection to the Tableau Server has been terminated!')
31
        timmer.stop()
32
33
    def get_project_relevancy(self, relevant_projects_to_filter, filtering_type, current_project):
34
        relevant = 0
35
        if filtering_type == 'JustOnesMentioned':
36
            if current_project.name.replace(chr(8211), chr(45)) in relevant_projects_to_filter:
37
                relevant = 1
38
        elif filtering_type == 'OnesMentionedMarked':
39
            relevant = 2
40
        elif filtering_type == 'All':
41
            relevant = 99
42
        return relevant
43
44
    @staticmethod
45
    def is_publishing_possible(self, local_logger, relevant_project_name, relevant_project_ids):
46
        publish_possible = False
47
        int_found_projects = len(relevant_project_ids)
48
        if int_found_projects == 0:
49
            local_logger.error('No project with provided name "' + relevant_project_name
50
                               + '" has been found')
51
            local_logger.debug('No publishing action will take place!')
52
            local_logger.debug('Check your input parameter values for accuracy and try again!')
53
        elif int_found_projects > 1:
54
            local_logger.error(f'There are {str(int_found_projects)} projects with provided name "'
55
                               + relevant_project_name + ' but a unique identifier is expected')
56
            local_logger.debug('No publishing action will take place!')
57
            local_logger.info('Check your input parameter values for accuracy and try again!')
58
        else:
59
            publish_possible = True
60
            local_logger.info(f'A single project identifier was found "' + relevant_project_ids[0]
61
                              + '" so I will proceed with publishing provided file there')
62
            local_logger.info('Stay tuned for the confirmation')
63
        return publish_possible
64
65
    def load_tableau_project_ids(self, local_logger, timmer, relevant_projects_to_filter,
66
                                 str_filtering_type):
67
        timmer.start()
68
        project_items, pagination_item = self.tableau_server.projects.get(req_options=self.req_opts)
69
        local_logger.info('Reading list of all projects available has been completed')
70
        local_logger.info(str(len(project_items)) + ' projects were found')
71
        timmer.stop()
72
        timmer.start()
73
        dictionary_project_ids = []
74
        project_counter = 0
75
        for project_current in project_items:
76
            relevant = self.get_project_relevancy(self,
77
                                                  relevant_projects_to_filter,
78
                                                  str_filtering_type,
79
                                                  project_current)
80
            if relevant >= 1:
81
                dictionary_project_ids.append(project_counter)
82
                dictionary_project_ids[project_counter] = project_current.id
83
                project_counter = project_counter + 1
84
        local_logger.info('Retaining the projects according to filtering type provided ("'
85
                          + str_filtering_type + '") has been completed')
86
        local_logger.info(str(len(dictionary_project_ids)) + ' projects were retained')
87
        timmer.stop()
88
        return dictionary_project_ids
89
90
    def publish_data_source_to_tableau_server(self, local_logger, timmer, publish_details):
91
        timmer.start()
92
        local_logger.info('About to start publishing')
93
        data_source_name = Path(publish_details['Tableau Extract File']).name\
94
                               .replace('.hyper', '') + " Extract"
95
        project_data_source = tsc.DatasourceItem(project_id = publish_details['Project ID'],
96
                                                 name       = data_source_name)
97
        self.tableau_server.datasources.publish(project_data_source,
98
                                                publish_details['Tableau Extract File'],
99
                                                publish_details['Publishing Mode'],
100
                                                )
101
        local_logger.info('Publishing has finished with success!')
102
        timmer.stop()
103