smartkiosk-client 0.1.14 → 0.1.15

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ class Application
2
+ get '/session_records' do
3
+ json SessionRecord.recent.as_json(
4
+ :only => [ :started_at, :upstream, :downstream, :time ]
5
+ )
6
+ end
7
+ end
@@ -1,3 +1,5 @@
1
1
  class SessionRecord < ActiveRecord::Base
2
-
2
+ scope :recent, where(
3
+ arel_table[:created_at].gt(Date.today-1.month)
4
+ ).order(arel_table[:id].desc)
3
5
  end
@@ -5,7 +5,6 @@ class PingWorker
5
5
 
6
6
  sidekiq_options :retry => false, :queue => :pings
7
7
 
8
- DAY = 24 * 3600
9
8
  SEMAPHORE = ConnectionPool.new(:size => 1, :timeout => 5) { true }
10
9
 
11
10
  def perform
@@ -15,34 +14,6 @@ class PingWorker
15
14
 
16
15
  Sidekiq::Logging.logger.info "Requesting with updated_at: #{Terminal.providers_updated_at}"
17
16
 
18
- sessions_per_day = {}
19
- sessions = SessionRecord.all
20
-
21
- sessions.each do |session|
22
- next if session.time == 0
23
-
24
- session_start = session.started_at
25
- session_start -= session_start % DAY
26
- session_end = session.started_at + session.time
27
-
28
- session_start.step(session_end - 1, DAY) do |day|
29
- day_time = [ day + DAY - 1, session.started_at + session.time ].min -
30
- [ day, session.started_at ].max
31
-
32
- fraction = day_time / session.time
33
-
34
- sessions_per_day[day] = {
35
- upstream: session.upstream * fraction,
36
- downstream: session.downstream * fraction,
37
- time: session.time * fraction
38
- }
39
- end
40
- end
41
-
42
- sessions.each do |day, charges|
43
- Sidekiq::Logging.logger.info "reporting #{charges.inspect} on #{day}"
44
- end
45
-
46
17
  begin
47
18
  response = RestClient::Request.execute(
48
19
  :method => :post,
@@ -55,8 +26,7 @@ class PingWorker
55
26
  },
56
27
  :payload => JSON.dump(
57
28
  :terminal => Terminal.keyword,
58
- :terminal_ping => condition,
59
- :sessions => sessions_per_day
29
+ :terminal_ping => condition
60
30
  )
61
31
  )
62
32
  rescue Exception => e
@@ -73,10 +43,6 @@ class PingWorker
73
43
 
74
44
  Sidekiq::Logging.logger.info "Response: #{response.inspect}"
75
45
 
76
- ActiveRecord::Base.transaction do
77
- sessions.each &:destroy
78
- end
79
-
80
46
  #
81
47
  # PROFILE
82
48
  #
@@ -110,6 +76,12 @@ class PingWorker
110
76
  end
111
77
 
112
78
  Sync::ProvidersWorker.perform_async if response[:update_providers]
79
+
80
+ #
81
+ # SESSIONS
82
+ #
83
+ last_session = response[:last_session_started_at] || 0
84
+ Sync::SessionsWorker.perform_async(last_session )if SessionRecord.exists?([ 'started_at > ?', last_session ])
113
85
  end
114
86
  rescue Timeout::Error => e
115
87
  Sidekiq::Logging.logger.warn "Semaphore timeout. #{e.to_s}"
@@ -0,0 +1,37 @@
1
+ require 'rest-client'
2
+
3
+ module Sync
4
+ class SessionsWorker
5
+ include Sidekiq::Worker
6
+
7
+ sidekiq_options :retry => false, :queue => :sync
8
+
9
+ def perform(from_time)
10
+ sessions = SessionRecord.where("started_at > ?", from_time).order("started_at").all
11
+
12
+ sessions.each do |session|
13
+ Sidekiq::Logging.logger.info "- submitting session #{session.message_id}"
14
+
15
+ providers = RestClient::Request.execute(
16
+ :method => :post,
17
+ :url => "#{Terminal.config.host}/session_records",
18
+ :timeout => 40,
19
+ :open_timeout => 60,
20
+ :payload => {
21
+ :terminal => Terminal.keyword,
22
+ :session_record => {
23
+ :message_id => session.message_id,
24
+ :started_at => session.started_at,
25
+ :upstream => session.upstream,
26
+ :downstream => session.downstream,
27
+ :time => session.time
28
+ }
29
+ }
30
+ )
31
+ end
32
+
33
+ rescue Exception => e
34
+ Sidekiq::Logging.logger.warn e.to_s
35
+ end
36
+ end
37
+ end
@@ -46,12 +46,16 @@ EventMachine.run do
46
46
  if message.key == "modem.accounting"
47
47
  message.acknowlege
48
48
 
49
- p message
50
-
51
- SessionRecord.create(started_at: message[0],
52
- upstream: message[1],
53
- downstream: message[2],
54
- time: message[3])
49
+ begin
50
+ SessionRecord.create(message_id: message.id,
51
+ started_at: message[0],
52
+ upstream: message[1],
53
+ downstream: message[2],
54
+ time: message[3])
55
+ rescue => e
56
+ Smartkiosk::Client::Logging.logger.error "unable to save session record: #{e}"
57
+ e.backtrace.each { |line| Smartkiosk::Client::Logging.logger.error line }
58
+ end
55
59
  else
56
60
  @front_orders.push ["smartware.#{message.key}", message.args].to_json
57
61
  end
@@ -1,6 +1,7 @@
1
1
  class CreateSessionRecords < ActiveRecord::Migration
2
2
  def change
3
3
  create_table :session_records do |t|
4
+ t.string :message_id
4
5
  t.integer :started_at
5
6
  t.integer :upstream
6
7
  t.integer :downstream
@@ -8,5 +9,7 @@ class CreateSessionRecords < ActiveRecord::Migration
8
9
 
9
10
  t.timestamps
10
11
  end
12
+
13
+ add_index :session_records, [ :message_id ], :unique => true
11
14
  end
12
15
  end
@@ -2,6 +2,6 @@ require 'sinatra/base'
2
2
 
3
3
  module Smartkiosk
4
4
  class Client < Sinatra::Base
5
- VERSION = '0.1.14'
5
+ VERSION = '0.1.15'
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: smartkiosk-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.14
4
+ version: 0.1.15
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -541,6 +541,7 @@ files:
541
541
  - app/controllers/config.rb
542
542
  - app/controllers/payments.rb
543
543
  - app/controllers/receipts.rb
544
+ - app/controllers/session_records.rb
544
545
  - app/controllers/terminal.rb
545
546
  - app/models/admin.rb
546
547
  - app/models/banner.rb
@@ -574,6 +575,7 @@ files:
574
575
  - app/workers/sync/logo_worker.rb
575
576
  - app/workers/sync/providers_worker.rb
576
577
  - app/workers/sync/receipt_templates_worker.rb
578
+ - app/workers/sync/sessions_worker.rb
577
579
  - bin/smartkiosk-client
578
580
  - config/boot.rb
579
581
  - config/services/application.yml
@@ -627,7 +629,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
627
629
  version: '0'
628
630
  segments:
629
631
  - 0
630
- hash: -856372630699196586
632
+ hash: -3441276206886059844
631
633
  required_rubygems_version: !ruby/object:Gem::Requirement
632
634
  none: false
633
635
  requirements:
@@ -636,10 +638,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
636
638
  version: '0'
637
639
  segments:
638
640
  - 0
639
- hash: -856372630699196586
641
+ hash: -3441276206886059844
640
642
  requirements: []
641
643
  rubyforge_project:
642
- rubygems_version: 1.8.23
644
+ rubygems_version: 1.8.24
643
645
  signing_key:
644
646
  specification_version: 3
645
647
  summary: Smartkiosk client application