Added all common modules in conductor directory
[optf/has.git] / conductor / conductor / common / music / voting.py
1 #
2 # -------------------------------------------------------------------------
3 #   Copyright (c) 2015-2017 AT&T Intellectual Property
4 #
5 #   Licensed under the Apache License, Version 2.0 (the "License");
6 #   you may not use this file except in compliance with the License.
7 #   You may obtain a copy of the License at
8 #
9 #       http://www.apache.org/licenses/LICENSE-2.0
10 #
11 #   Unless required by applicable law or agreed to in writing, software
12 #   distributed under the License is distributed on an "AS IS" BASIS,
13 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 #   See the License for the specific language governing permissions and
15 #   limitations under the License.
16 #
17 # -------------------------------------------------------------------------
18 #
19
20 import time
21
22 from oslo_config import cfg
23
24 from conductor.common.music import api
25 from conductor import service
26
27 CONF = cfg.CONF
28
29
30 def current_time_millis():
31     """Current time in milliseconds."""
32     return int(round(time.time() * 1000))
33
34
35 def main():
36     """Sample usage of Music."""
37     service.prepare_service()
38     CONF.set_override('debug', True, 'music_api')
39     CONF.set_override('mock', True, 'music_api')
40     CONF.set_override('hostnames', ['music2'], 'music_api')
41     music = api.API()
42     print("Music version %s" % music.version())
43
44     # Randomize the name so that we don't step on each other.
45     keyspace = 'NewVotingApp' + str(current_time_millis() / 100)
46     music.keyspace_create(keyspace)
47
48     # Create the table
49     kwargs = {
50         'keyspace': keyspace,
51         'table': 'votecount',
52         'schema': {
53             'name': 'text',
54             'count': 'varint',
55             'PRIMARY KEY': '(name)'
56         }
57     }
58     music.table_create(**kwargs)
59
60     # Candidate data
61     data = {
62         'Joe': 5,
63         'Shankar': 7,
64         'Gueyoung': 8,
65         'Matti': 2,
66         'Kaustubh': 0
67     }
68
69     # Create an entry in the voting table for each candidate
70     # and with a vote count of 0.
71     kwargs = {'keyspace': keyspace, 'table': 'votecount', 'pk_name': 'name'}
72     for name in data:  # We only want the keys
73         kwargs['pk_value'] = name
74         kwargs['values'] = {'name': name, 'count': 0}
75         music.row_create(**kwargs)
76
77     # Update each candidate's count atomically.
78     kwargs = {'keyspace': keyspace, 'table': 'votecount', 'pk_name': 'name'}
79     for name in data:
80         count = data[name]
81         kwargs['pk_value'] = name
82         kwargs['values'] = {'count': count}
83         kwargs['atomic'] = True
84         music.row_update(**kwargs)
85
86     # Read all rows
87     kwargs = {'keyspace': keyspace, 'table': 'votecount'}
88     print(music.row_read(**kwargs))  # Reads all rows
89
90     # Delete Joe, read Matti
91     kwargs = {'keyspace': keyspace, 'table': 'votecount', 'pk_name': 'name'}
92     kwargs['pk_value'] = 'Joe'
93     music.row_delete(**kwargs)
94     kwargs['pk_value'] = 'Matti'
95     print(music.row_read(**kwargs))
96
97     # Read all rows again
98     kwargs = {'keyspace': keyspace, 'table': 'votecount'}
99     print(music.row_read(**kwargs))  # Reads all rows
100
101     # Cleanup.
102     music.keyspace_delete(keyspace)
103
104
105 if __name__ == "__main__":
106     main()