Skip to content

Commit 70eebaf

Browse files
committed
[DOC] Add example script discarding all sessions for an activity
Signed-off-by: Olivier Mehani <[email protected]>
1 parent 343a033 commit 70eebaf

File tree

1 file changed

+108
-0
lines changed

1 file changed

+108
-0
lines changed
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
#!/usr/bin/env ruby
2+
require 'net/http'
3+
require 'json'
4+
5+
require 'learnosity/sdk/request/init'
6+
7+
activityId = 'demoActivity'
8+
maxCount = 100
9+
10+
class SessionDiscarder
11+
def initialize
12+
@sessionsStatusesUri = URI('https://data.learnosity.com/v1/sessions/statuses')
13+
14+
@security_packet = {
15+
# XXX: This is a Learnosity Demos consumer; replace it with your own consumer key
16+
'consumer_key' => 'yis0TYCu7U9V4o7M',
17+
'domain' => 'localhost'
18+
}
19+
# XXX: The consumer secret should be in a properly secured credential store, and *NEVER* checked in in revision control
20+
@consumer_secret = '74c5fd430cf1242a527f6223aebd42d30464be22'
21+
end
22+
23+
def get_sessions_statuses(sessionIds)
24+
request = { "session_id": sessionIds }
25+
return do_get_sessions_statuses(request)
26+
end
27+
28+
def get_sessions_statuses_by_activity(activityId)
29+
request = { "activity_id": [ activityId ] }
30+
return do_get_sessions_statuses(request)
31+
end
32+
33+
def do_get_sessions_statuses(request)
34+
35+
reqno = 0
36+
continue = true
37+
sessions = []
38+
while continue do
39+
reqno += 1
40+
41+
init = Learnosity::Sdk::Request::Init.new(
42+
'data',
43+
@security_packet,
44+
@consumer_secret,
45+
request
46+
)
47+
signedRequest = init.generate
48+
49+
puts ">>> [#{@sessionsStatusesUri} (#{reqno})] #{JSON::generate(signedRequest)}"
50+
51+
res = Net::HTTP.post_form(@sessionsStatusesUri, signedRequest)
52+
response = JSON.parse(res.body)
53+
54+
puts "<<< [#{res.code}] #{response['meta']['records']} records, next: #{response['meta']['next']}"
55+
56+
if ( !response['meta']['next'].nil? \
57+
and !response['meta']['records'].nil? and response['meta']['records'] > 0)
58+
request['next'] = response['meta']['next']
59+
else
60+
continue = false
61+
end
62+
63+
response['data'].each { |session|
64+
sessions += [ session ]
65+
}
66+
end
67+
return sessions
68+
end
69+
70+
def update_statuses(statuses)
71+
sessionsDiscardRequest = { "statuses": statuses }
72+
73+
init = Learnosity::Sdk::Request::Init.new(
74+
'data',
75+
@security_packet,
76+
@consumer_secret,
77+
sessionsDiscardRequest,
78+
'update'
79+
)
80+
request = init.generate
81+
82+
puts ">>> [#{@sessionsStatusesUri}] #{JSON::generate(sessionsDiscardRequest)}"
83+
84+
res = Net::HTTP.post_form(@sessionsStatusesUri, request)
85+
response = JSON.parse(res.body)
86+
87+
puts "<<< [#{res.code}] job_reference: #{response['data']['job_reference']}"
88+
end
89+
end
90+
91+
sd = SessionDiscarder.new
92+
93+
sessions = sd.get_sessions_for_activity(activityId)
94+
95+
statuses = []
96+
sessions.each { |session|
97+
if session['status'] != 'Discarded'
98+
session['status'] = 'Discarded'
99+
statuses += [ session ]
100+
if statuses.count == maxCount
101+
sd.update_statuses(statuses)
102+
statuses = []
103+
end
104+
end
105+
}
106+
if statuses.count > 0
107+
sd.update_statuses(statuses)
108+
end

0 commit comments

Comments
 (0)