Conditions | 6 |
Total Lines | 55 |
Code Lines | 32 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | # -*- coding: utf-8 -*- |
||
55 | def init(self): |
||
56 | """Init the connection to the ES server.""" |
||
57 | if not self.export_enable: |
||
58 | return None |
||
59 | |||
60 | self.index='{}-{}'.format(self.index, datetime.utcnow().strftime("%Y.%m.%d")) |
||
61 | template_body = { |
||
62 | "mappings": { |
||
63 | "glances": { |
||
64 | "dynamic_templates": [ |
||
65 | { |
||
66 | "integers": { |
||
67 | "match_mapping_type": "long", |
||
68 | "mapping": { |
||
69 | "type": "integer" |
||
70 | } |
||
71 | } |
||
72 | }, |
||
73 | { |
||
74 | "strings": { |
||
75 | "match_mapping_type": "string", |
||
76 | "mapping": { |
||
77 | "type": "text", |
||
78 | "fields": { |
||
79 | "raw": { |
||
80 | "type": "keyword", |
||
81 | "ignore_above": 256 |
||
82 | } |
||
83 | } |
||
84 | } |
||
85 | } |
||
86 | } |
||
87 | ] |
||
88 | } |
||
89 | } |
||
90 | } |
||
91 | |||
92 | try: |
||
93 | es = Elasticsearch(hosts=['{}:{}'.format(self.host, self.port)]) |
||
94 | except Exception as e: |
||
95 | logger.critical("Cannot connect to ElasticSearch server %s:%s (%s)" % (self.host, self.port, e)) |
||
96 | sys.exit(2) |
||
97 | else: |
||
98 | logger.info("Connected to the ElasticSearch server %s:%s" % (self.host, self.port)) |
||
99 | |||
100 | try: |
||
101 | index_count = es.count(index=self.index)['count'] |
||
102 | except Exception as e: |
||
103 | # Index did not exist, it will be created at the first write |
||
104 | # Create it... |
||
105 | es.indices.create(index=self.index,body=template_body) |
||
106 | else: |
||
107 | logger.info("The index %s exists and holds %s entries." % (self.index, index_count)) |
||
108 | |||
109 | return es |
||
110 | |||
139 |