machineshop 0.0.4 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,35 @@
1
+ module MachineShop
2
+ class DataSources < APIResource
3
+ include MachineShop::APIOperations::List
4
+ include MachineShop::APIOperations::Create
5
+ include MachineShop::APIOperations::Update
6
+ # include MachineShop::APIOperations::Delete
7
+
8
+ # Specific API calls
9
+
10
+ def report_count(params)
11
+ MachineShop.get(report_count_url, @auth_token, params)
12
+ end
13
+
14
+ def reports(filters={})
15
+ filters.merge!(:device_instance_id => self.id)
16
+ MachineShop::Report.all(filters, @auth_token)
17
+ end
18
+
19
+ def meters(filters={})
20
+ filters.merge!(:device_instance_id => self.id)
21
+ MachineShop::Meter.all(filters, @auth_token)
22
+ end
23
+
24
+ def delete
25
+ MachineShop.delete("/platform/data_sources/#{self.id}?_type=#{self._type}", @auth_token,{})
26
+ end
27
+
28
+ private
29
+
30
+ def report_count_url
31
+ url + '/report_count'
32
+ end
33
+
34
+ end
35
+ end
@@ -1,12 +1,12 @@
1
1
  module MachineShop
2
- =begin
3
2
 
4
3
  require 'machineshop/errors/machineshop_error'
5
- require 'machineshop/errors/api_error'
4
+ # require 'machineshop/errors/api_error'
6
5
  require 'machineshop/errors/database_error'
6
+ require 'machineshop/errors/schema_error'
7
7
  require 'mysql'
8
8
  require 'active_record'
9
- require 'machineshop/configuration'
9
+ # require 'machineshop/configuration'
10
10
 
11
11
  class Database < Configuration
12
12
 
@@ -14,99 +14,46 @@ module MachineShop
14
14
 
15
15
  def initialize()
16
16
 
17
- @db_connected=true
18
-
19
- begin
20
- @@con = Mysql.new(MachineShop.configuration.db_host,MachineShop.configuration.db_username,MachineShop.configuration.db_password,MachineShop.configuration.db_name)
21
-
22
- @act = ActiveRecord::Base.establish_connection(
23
- adapter: 'mysql', # or 'postgresql' or 'sqlite3'
24
- host: MachineShop.configuration.db_host,
25
- # database: MachineShop.configuration.db_name,
26
- database: "machineshop",
27
- username: MachineShop.configuration.db_username,
28
- # password: MachineShop.configuration.db_password
29
- password: "root"
30
- )
31
-
32
- ActiveRecord::Migration.class_eval do
33
- create_table :devices do |t|
34
- t.string :_id
35
- t.string :active
36
- t.string :created_at
37
- t.string :deleted_at
38
- t.string :exe_path
39
- t.string :image_url
40
- t.string :init_cmd
41
- t.string :init_params
42
- t.string :last_known_translator_port
43
- t.string :long_description
44
- t.string :manual_url
45
- t.string :manufacturer
46
- t.string :model
47
- t.string :name
48
- t.string :rule_ids
49
- t.string :sample_data
50
- t.string :software
51
- t.string :tag_ids
52
- t.string :translator
53
- t.string :type
54
- t.string :unit_price
55
- t.string :updated_at
56
- t.string :user_id
57
- end
58
-
59
- create_table :people do |t|
60
- t.string :first_name
61
- t.string :last_name
62
- t.string :short_name
63
- end
64
-
65
- create_table :tags do |t|
66
- t.string :tags
67
- end
68
- end
69
- # puts "act is #{@act.inspect}"
70
-
71
-
72
- rescue Exception => e
73
- @db_connected=false
74
- return @db_connected
75
-
76
- raise DatabaseError.new("Connection to Database refused")
77
- end
17
+ # @db_connected=true
18
+ begin
19
+ ActiveRecord::Base.establish_connection(
20
+ adapter: 'mysql', # or 'postgresql' or 'sqlite3'
21
+ host: MachineShop.configuration.db_host,
22
+ database: MachineShop.configuration.db_name,
23
+ username: MachineShop.configuration.db_username,
24
+ password: MachineShop.configuration.db_password,
25
+ )
78
26
 
79
- @rs = @@con.query('select url from endpoints')
80
- @rs.each_hash { |h| puts h['url']}
81
- end
27
+ begin
28
+ load 'machineshop/models/schema.rb'
29
+ rescue Mysql::Error => e
30
+ # raise SchemaError.new(e.message)
31
+ rescue StandardError => e
32
+ # raise SchemaError.new(e.message)
33
+ end
82
34
 
83
- # def database_error(error)
84
- # DatabaseError.new(error)
85
- # end
86
35
 
36
+ rescue Mysql::Error =>e
37
+ raise DatabaseError.new("Mysql error ")
38
+ rescue StandardError =>e
39
+ raise DatabaseError.new("Database Error #1")
40
+ rescue Exception => e
41
+ # @db_connected=false
42
+ raise DatabaseError.new("Connection to Database refused")
43
+ # load schema file
44
+ end
87
45
 
88
46
 
89
- def self.insert(user_id, table_name, data={})
90
- @rs = @@con.query("INSERT INTO #{table_name} (`id`, `url`) VALUES (NULL, '#{data}')")
91
- puts "inserted #{@rs.inspect}"
92
- end
93
47
 
94
48
 
95
- def self.get(user_id,table_name,filter={})
96
- @rs = @@con.query("SELECT * FROM #{table_name}")
97
- puts "get #{@rs.inspect}"
98
- end
99
49
 
100
- def self.serialize_data(data)
101
- Marshal.dump(data)
102
- end
50
+ end
103
51
 
104
- end
52
+ def self.serialize_data(data)
53
+ Marshal.dump(data)
54
+ end
105
55
 
106
- end
107
- =end
56
+ end
108
57
 
109
- end
110
58
 
111
- # Database.new
112
- # Database.insert('endpoints',"/user/devices/id")
59
+ end
@@ -0,0 +1,5 @@
1
+ module MachineShop
2
+ class SchemaError < MachineShopError
3
+
4
+ end
5
+ end
@@ -20,7 +20,8 @@ module MachineShop
20
20
  @retrieve_options = {}
21
21
  end
22
22
 
23
- @auth_token = auth_token
23
+ @auth_token = auth_token
24
+
24
25
  @values = {}
25
26
  # This really belongs in APIResource, but not putting it there allows us
26
27
  # to have a unified inspect method
@@ -0,0 +1,27 @@
1
+
2
+ class ApiRequest < ActiveRecord::Base
3
+ validates :url, presence: true, uniqueness: true
4
+ # validates: url, presence: true, uniqueness: true
5
+
6
+ def self.cache(url,auth_token, cache_policy)
7
+ find_or_initialize_by(url: url, auth_token:auth_token).cache(cache_policy) do
8
+ if block_given?
9
+ yield
10
+ end
11
+ end
12
+ end
13
+
14
+
15
+ def cache(cache_policy)
16
+ if new_record?
17
+ update_attributes(updated_at: Time.now.utc)
18
+ end
19
+
20
+ if updated_at < cache_policy.call.utc
21
+ update_attributes(updated_at: Time.now.utc)
22
+ else
23
+ yield
24
+ end
25
+ end
26
+
27
+ end
@@ -0,0 +1,13 @@
1
+ # require 'active_record'
2
+
3
+ # require 'machineshop/database'
4
+
5
+ # Database.new
6
+ # module MachineShop
7
+
8
+ class People < ActiveRecord::Base
9
+ # end
10
+
11
+ end
12
+
13
+ # end
@@ -0,0 +1,169 @@
1
+ ActiveRecord::Schema.define do
2
+ self.verbose = false
3
+
4
+ create_table :data_source_types_caches do |t|
5
+ t.string :_id
6
+ t.string :active
7
+ t.string :type
8
+ t.string :exe_path
9
+ t.string :name
10
+ t.string :image_url
11
+ t.string :init_cmd
12
+ t.string :init_params
13
+ t.string :last_known_translator_port
14
+ t.string :long_description
15
+ t.string :user_id
16
+ t.string :manual_url
17
+ t.string :manufacturer
18
+ t.string :updated_at
19
+ t.string :created_at
20
+ t.string :deleted_at
21
+ t.string :model
22
+ t.string :payload
23
+ t.string :rule_ids
24
+ t.string :sample_data
25
+ t.string :software
26
+ t.string :translator
27
+ t.string :unit_price
28
+ t.string :auth_token
29
+
30
+ end
31
+
32
+ create_table :data_sources_caches do |t|
33
+ t.string :_id
34
+ t.string :_type
35
+ t.string :type
36
+ t.string :last_checked_datetime
37
+ t.string :name
38
+ t.string :user_name
39
+ t.string :sender
40
+ t.string :encrypted_password
41
+ t.string :pop_server
42
+ t.string :port
43
+ t.string :user_id
44
+ t.string :auth_token
45
+ t.string :data_source_type_id
46
+ t.string :updated_at
47
+ t.string :created_at
48
+ t.text :data_source_type
49
+ t.string :last_report
50
+ t.string :report_count
51
+ t.string :active
52
+
53
+ end
54
+
55
+
56
+ create_table :device_instance_caches do |t|
57
+ t.string :_id
58
+ t.string :alert_count
59
+ t.string :name
60
+ t.string :active
61
+ t.string :device_id
62
+ t.string :user_id
63
+ t.string :auth_token
64
+ t.string :updated_at
65
+ t.string :created_at
66
+ t.string :_type
67
+ t.text :device
68
+ t.text :last_report
69
+ t.string :report_count
70
+ t.string :tag_ids
71
+ t.string :rule_ids
72
+ t.string :user_type
73
+ t.string :deleted_at
74
+
75
+ end
76
+
77
+ create_table :api_requests do |t|
78
+ t.timestamps null: false
79
+ t.text :url, null: false
80
+ t.string :auth_token, null:false
81
+ end
82
+
83
+ create_table :report_caches do |t|
84
+ t.string :_id
85
+ t.string :created_at
86
+ t.string :deleted_at
87
+ t.string :device_datetime
88
+ t.string :device_instance_id
89
+ t.string :duplicate
90
+ t.text :payload
91
+ t.string :profile_timestamps
92
+ t.string :raw_data
93
+ t.string :report_processor
94
+ t.string :stale
95
+ t.string :updated_at
96
+ t.string :auth_token
97
+ end
98
+
99
+ create_table :comparison_rule_conditions_caches do |t|
100
+ t.string :auth_token
101
+ t.string :rule_condition
102
+ t.string :rule_description
103
+ end
104
+
105
+
106
+ create_table :join_rule_conditions_caches do |t|
107
+ t.string :rule_condition
108
+ t.string :rule_description
109
+ t.timestamp
110
+ end
111
+
112
+ create_table :rule_caches do |t|
113
+ t.string :_id
114
+ t.string :active
115
+ t.string :created_at
116
+ t.string :deleted_at
117
+ t.string :description
118
+ t.string :device_ids
119
+ t.string :device_instance_ids
120
+ t.string :downstream_rule_id
121
+ t.string :last_run_status
122
+ t.string :plain_english
123
+ t.string :tag_ids
124
+ t.string :then_actions
125
+ t.string :updated_at
126
+ t.string :user_id
127
+ t.string :auth_token
128
+ t.string :actions
129
+ t.string :comparison_value
130
+ t.string :deleted
131
+ t.string :device_attribute
132
+ t.string :last_run
133
+ t.string :modified_date
134
+ t.string :operator
135
+ t.string :rule_histories
136
+ t.string :else_actions
137
+ end
138
+
139
+
140
+ create_table :device_caches do |t|
141
+ t.string :_id
142
+ t.string :active
143
+ t.string :created_at
144
+ t.string :deleted_at
145
+ t.string :exe_path
146
+ t.string :image_url
147
+ t.string :init_cmd
148
+ t.string :init_params
149
+ t.string :last_known_translator_port
150
+ t.string :long_description
151
+ t.string :manual_url
152
+ t.string :manufacturer
153
+ t.string :model
154
+ t.string :name
155
+ t.string :rule_ids
156
+ t.string :sample_data
157
+ t.string :software
158
+ t.string :tag_ids
159
+ t.string :translator
160
+ t.string :type
161
+ t.string :unit_price
162
+ t.string :updated_at
163
+ t.string :user_id
164
+ t.string :auth_token
165
+ end
166
+
167
+
168
+
169
+ end
@@ -0,0 +1,43 @@
1
+ module MachineShop
2
+ class Users < APIResource
3
+
4
+ # Specific API calls
5
+ def self.authenticate(user_hash)
6
+ #user_hash is => { email: email, password: password }
7
+ response = MachineShop.post(authenticate_url, nil, user_hash)
8
+ auth_token = response[:authentication_token]
9
+ id = response[:_id]
10
+
11
+ return auth_token, self.retrieve(id, auth_token)
12
+ end
13
+
14
+ def self.all_roles(auth_token)
15
+ MachineShop.get(self.role_url, auth_token)
16
+ end
17
+
18
+ def all_roles
19
+ MachineShop.get(self.class.role_url, @auth_token)
20
+ end
21
+
22
+ def device_instances(filters={})
23
+ filters.merge!(:user_id => self.id)
24
+ MachineShop::DeviceInstance.all(filters, @auth_token)
25
+ end
26
+
27
+ def meters(filters={})
28
+ filters.merge!(:user_id => self.id)
29
+ MachineShop::Meter.all(filters, @auth_token)
30
+ end
31
+
32
+ private
33
+
34
+ def self.authenticate_url
35
+ '/user_session/user/authenticate'
36
+ end
37
+
38
+ def self.role_url
39
+ '/platform/role'
40
+ end
41
+
42
+ end
43
+ end