logfiction 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 162ac121bd2301e280c2356317b0b402a01d6614f30790c0474149e6a08474ca
4
+ data.tar.gz: 4789b4a0b9a13d1a761fdfeb20e25d9d8af321224e1ab693c82f381ad1cb42ae
5
+ SHA512:
6
+ metadata.gz: 280807f16e592033080bc0de0192ae01b3962fb4590fe38b4c6354c25904f17d95ff8f9c21e935742a22000e2fca7461ed7f525bc940f8479f2699ffa1ea92a9
7
+ data.tar.gz: 344c49ac6037f3d804d8ea885a317a279101f84e81ab571360ca02dc4d43b3bbf8792ef15569fd1abe37cf1dd347295bccfa0e9408176d7c50a33592392e1a44
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.6.1
7
+ before_install: gem install bundler -v 2.0.1
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at tomohiro.furusawa.629@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in logfiction.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,35 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ logfiction (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ diff-lcs (1.3)
10
+ rake (10.5.0)
11
+ rspec (3.8.0)
12
+ rspec-core (~> 3.8.0)
13
+ rspec-expectations (~> 3.8.0)
14
+ rspec-mocks (~> 3.8.0)
15
+ rspec-core (3.8.0)
16
+ rspec-support (~> 3.8.0)
17
+ rspec-expectations (3.8.3)
18
+ diff-lcs (>= 1.2.0, < 2.0)
19
+ rspec-support (~> 3.8.0)
20
+ rspec-mocks (3.8.0)
21
+ diff-lcs (>= 1.2.0, < 2.0)
22
+ rspec-support (~> 3.8.0)
23
+ rspec-support (3.8.0)
24
+
25
+ PLATFORMS
26
+ ruby
27
+
28
+ DEPENDENCIES
29
+ bundler (~> 2.0)
30
+ logfiction!
31
+ rake (~> 10.0)
32
+ rspec (~> 3.0)
33
+
34
+ BUNDLED WITH
35
+ 2.0.1
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 rilmayer
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,74 @@
1
+ # Logfiction
2
+
3
+ This gem is log data generator for learning log analysis, simulation, etc...
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'logfiction'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install logfiction
20
+
21
+ ## Usage
22
+ ```ruby
23
+ require 'logfiction'
24
+
25
+ la = Logfiction::AccessLog.new()
26
+
27
+ # generate 10000 row fiction log!
28
+ logs = la.generate_accesslog(n=10000)
29
+
30
+ #=> [{:timestamp=>2018-06-29 09:04:08 +0900,
31
+ # :user_id=>54,
32
+ # :state_id=>2,
33
+ # :items=>[23],
34
+ # :state_name=>"detail_page_view"},
35
+ # {:timestamp=>2018-06-29 09:04:09 +0900,
36
+ # :user_id=>23,
37
+ # :state_id=>0,
38
+ # :items=>[],
39
+ # :state_name=>"top_page_view"},
40
+ # {:timestamp=>2018-06-29 09:04:10 +0900,
41
+ # :user_id=>36,
42
+ # :state_id=>1,
43
+ # :items=>[30, 31, 32, 33, 34, 35, 36, 37, 38, 39],
44
+ # :state_name=>"list_page_view"},
45
+ # ...
46
+
47
+ # CSV Output
48
+ la.export_logfile(filetype='CSV',filepath='/path/to/file')
49
+
50
+ # => timestamp,user_id,state_id,items,state_name
51
+ # 2018-06-29 11:18:58 +0900,47,2,65,detail_page_view
52
+ # 2018-06-29 11:18:59 +0900,66,0,"",top_page_view
53
+ # 2018-06-29 11:19:02 +0900,64,1,50:51:52:53:54:55:56:57:58:59,list_page_view
54
+ # 2018-06-29 11:19:11 +0900,12,3,89,item_purchase
55
+ # 2018-06-29 11:19:12 +0900,12,0,"",top_page_view
56
+ ```
57
+
58
+ ## Development
59
+
60
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
61
+
62
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
63
+
64
+ ## Contributing
65
+
66
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/logfiction. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
67
+
68
+ ## License
69
+
70
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
71
+
72
+ ## Code of Conduct
73
+
74
+ Everyone interacting in the Logfiction project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/logfiction/blob/master/CODE_OF_CONDUCT.md).
data/README_ja.md ADDED
@@ -0,0 +1,3 @@
1
+ # Logfiction
2
+
3
+ [TODO] 日本語の説明書く
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "logfiction"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/exe/logfiction ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'logfiction'
4
+ n = ARGV[0]
5
+ path = ARGV[1] if ARGV[1]
6
+ la = Logfiction::AccessLog.new()
7
+ la.generate_accesslog(n)
8
+ la.export_logfile(filetype='CSV',filepath='./fictionlog.csv')
@@ -0,0 +1,3 @@
1
+ module Logfiction
2
+ VERSION = "0.1.0"
3
+ end
data/lib/logfiction.rb ADDED
@@ -0,0 +1,419 @@
1
+ require "logfiction/version"
2
+ require 'time'
3
+ require 'csv'
4
+
5
+ module Logfiction
6
+ class FileTypeError < StandardError; end
7
+
8
+ SESSION_OUT_MIN_TIME = 30 * 60 # sec (30 min)
9
+ USER_ACTION_INTERVAL = [1, 120] # lowwer to upper(sec)
10
+
11
+ attr_accessor :users, :items, :states, :transions, :assumptions, :access_log
12
+
13
+ class AccessLog
14
+ def initialize(assumptions={})
15
+ @users = []
16
+ @items = []
17
+ @states = []
18
+ @start_state = []
19
+ @transitions = []
20
+ @auto_transiton = {}
21
+ @assumptions = {}
22
+ @access_log = {}
23
+ # access_log: user_id & seuquence of states
24
+ # ex.) {user_id1(int):
25
+ # [{timestamp: timestamp(str),
26
+ # state_id: state_id(int),
27
+ # item [item(int), ...]}, ...], ...}
28
+
29
+ # set assumption
30
+ set_assumptions(assumptions)
31
+ end
32
+
33
+ # Set assumptions
34
+ # assumptions: object(hash)
35
+ # time_access_from(str): from what time generate logs
36
+ # user_n_sessions(int): how many sessions in day
37
+ # user_max_states(int): how many states in session
38
+ # n_users(int): how many users
39
+ # n_items(int): how many items
40
+ def set_assumptions(assumptions={})
41
+ @assumptions = {
42
+ time_access_from: Time.parse("2018-06-29 09:00:00"),
43
+ user_max_sessions: 5,
44
+ user_max_actions: 100,
45
+ n_users: 100,
46
+ n_items: 100
47
+ }
48
+ if assumptions != {}
49
+ # [TODO] validate assumptions
50
+ assumptions.each do |key, value|
51
+ @assumptions[key] = value
52
+ end
53
+ end
54
+ end
55
+
56
+ # User generater
57
+ # Input: n_users(int), users(hash)
58
+ # Ouptput: array of user_info
59
+ # user_info(hash): {user_id: user_id(int), options}
60
+ def generate_users(n_users=100, users=[])
61
+ if users.size == 0
62
+ user_ids = (0..n_users - 1).to_a
63
+ @users = user_ids.map{|user_id| { user_id: user_id } }
64
+ else
65
+ # [TODO] validate input data
66
+ @users = users
67
+ end
68
+ end
69
+
70
+ # Item generater
71
+ # Input: n_items(int), items(hash)
72
+ # Ouptput: object of array
73
+ # item(object): { item_id: item_id, options}
74
+ def generate_items(n_items=100, items=[])
75
+ if items.size == 0
76
+ item_ids = (0..n_items - 1).to_a
77
+ @items = item_ids.map{|item_id| { item_id: item_id } }
78
+ else
79
+ # [TODO] validate input data
80
+ @items = items
81
+ end
82
+ end
83
+
84
+ # States generater
85
+ # Input: state_transtion(object)
86
+ # Output: nill
87
+ def generate_state_transiton(state_transtion={})
88
+ if state_transtion == {}
89
+ # default setting is like EC
90
+ states = [
91
+ {state_id: 0, state_name: 'top_page_view', item_type: :no_item, request: '/'},
92
+ {state_id: 1, state_name: 'list_page_view', item_type: :many, request: '/list'},
93
+ {state_id: 2, state_name: 'detail_page_view', item_type: :one, request: '/item'},
94
+ {state_id: 3, state_name: 'item_purchase', item_type: :one, request: '/purchace'}
95
+ ]
96
+
97
+ start_state = [0]
98
+
99
+ taranstion = [
100
+ # probability is 0.6 if user transit from top(id:0) to list(id:1) page.
101
+ # transition restrict by item is none.
102
+ {from: 0, to: 1, probability: 0.6, dependent_item: false},
103
+
104
+ # probability is 0.4 if user transit from list(id:1) to detail(id:2) page
105
+ # "to state" item should be choosed "from state" items.
106
+ {from: 1, to: 2, probability: 0.4, dependent_item: true},
107
+
108
+ # probability is 0.2 if user transit from detatil(id:2) to purchase(id:3) page
109
+ # "to state" item should be choosed "from state" items.
110
+ # after transition to state '3', automatically transition to state "0"
111
+ {from: 2, to: 3, probability: 0.2, dependent_item: true, auto_transiton: 0}
112
+ ]
113
+ @start_state, @transitions = start_state, taranstion
114
+ else
115
+ @start_state = state_transtion[:start_state]
116
+ @transitions = state_transtion[:states]
117
+ end
118
+ # convert states
119
+ states_hash = {}
120
+ states.each do |state|
121
+ states_hash[state[:state_id]] = state
122
+ end
123
+ @states = states_hash
124
+
125
+ # generate auto transiton
126
+ @transitions.each do |transition|
127
+ if transition[:auto_transiton]
128
+ @auto_transiton[transition[:to]] = transition[:auto_transiton]
129
+ end
130
+ end
131
+ end
132
+
133
+ # generate state with items
134
+ # Input: items
135
+ # Output: items
136
+ def get_next_items(from_state_id, to_state_id, current_items)
137
+ unless from_state_id
138
+ to_item_type = @states[to_state_id]
139
+
140
+ unless to_item_type
141
+ return []
142
+ end
143
+
144
+ to_item_type = [:item_type]
145
+ if to_item_type == 'many'
146
+ item_list = @items.each_slice(10).to_a
147
+ pick_index = rand(item_list.size)
148
+ return item_list[pick_index]
149
+ elsif to_item_type == 'one'
150
+ pick_index = rand(@items.size)
151
+ return [@items[pick_index]]
152
+ else
153
+ return []
154
+ end
155
+ else
156
+ from_item_type = @states[from_state_id][:item_type]
157
+
158
+ to_state = @states[to_state_id]
159
+ unless to_state
160
+ return []
161
+ end
162
+ to_item_type = to_state[:item_type]
163
+
164
+ dependent_item = false
165
+ @transitions.each do |transiton|
166
+ # normal transition
167
+ dependent_item = true if transiton[:from] == from_state_id && transiton[:to] == to_state_id
168
+
169
+ # back transition
170
+ dependent_item = true if transiton[:from] == to_state_id && transiton[:to] == from_state_id
171
+ end
172
+
173
+ unless dependent_item
174
+ return []
175
+ end
176
+
177
+ # no_item -> many
178
+ if from_item_type == :no_item && to_item_type == :many
179
+ item_list = @items.each_slice(10).to_a
180
+ pick_index = rand(item_list.size)
181
+ return item_list[pick_index]
182
+
183
+ # many -> one
184
+ elsif from_item_type == :many && to_item_type == :one
185
+ pick_index = rand(current_items.size)
186
+ return [current_items[pick_index]]
187
+
188
+ # one -> many
189
+ elsif from_item_type == :one && to_item_type == :many
190
+ next_items = []
191
+ item_list = @items.each_slice(10).to_a
192
+ item_list.each_with_index do |items, i|
193
+ if items.include?(current_items[0])
194
+ next_items = items
195
+ end
196
+ end
197
+ return next_items
198
+
199
+ # one -> one
200
+ elsif from_item_type == :one && to_item_type == :one
201
+ return current_items
202
+
203
+ else
204
+ return []
205
+ end
206
+ end
207
+ end
208
+
209
+ # randam choice from states, which has a different probability
210
+ # [TODO] more logically correct sampling
211
+ # Input: states_with_probability(Array of state_with_probability(Hash))
212
+ # state_id(int): probability(int)
213
+ # Output: state_id(int)
214
+ def choice_next_state(states_with_probability)
215
+ # transrate probability to number of trials
216
+ n_trials = 100
217
+ total = states_with_probability.values.inject(:+) * n_trials
218
+ pick = rand(total)
219
+ currentStack = 0
220
+ states_with_probability.each do |state_id, probability|
221
+ if (pick <= currentStack + probability * n_trials)
222
+ return state_id
223
+ else
224
+ currentStack += probability * n_trials
225
+ end
226
+ end
227
+ return states_with_probability.keys.sample
228
+ end
229
+
230
+ # random walk update user state
231
+ # Input:
232
+ # current_states(Hash):
233
+ # before_state(Hash):
234
+ # state_id(int): state_id
235
+ # item(Array): item list
236
+ # states_sequence(Array): states sequence list
237
+ def update_user_state(user_id)
238
+ next_state_interval = USER_ACTION_INTERVAL[0] + rand * USER_ACTION_INTERVAL[1]
239
+ states_sequence = @access_log[user_id] || []
240
+
241
+ # first action
242
+ if states_sequence == []
243
+ @access_log[user_id] = []
244
+ next_state_id = @start_state.sample
245
+ next_timestamp = @assumptions[:time_access_from] + next_state_interval
246
+ next_items = []
247
+ else
248
+ from_state = states_sequence.last
249
+
250
+ # check auto transiton
251
+ auto_transiton_state = @auto_transiton[from_state[:state_id]]
252
+ unless auto_transiton_state == nil
253
+ next_state_id = auto_transiton_state
254
+ next_timestamp = from_state[:timestamp] + 1
255
+ else
256
+
257
+ # new session
258
+ unless states_sequence.last[:state_id]
259
+ next_state_id = @start_state.sample
260
+ next_timestamp = from_state[:timestamp] + (next_state_interval + SESSION_OUT_MIN_TIME)
261
+ else
262
+
263
+ # pickup possible transition states
264
+ possible_transition_states = []
265
+
266
+ # add state from transition_states
267
+ total_probability = 0
268
+ @transitions.each do |transition|
269
+ if transition[:from] == from_state[:state_id]
270
+ possible_transition_states << {state_id: transition[:to], probability: transition[:probability]}
271
+ total_probability += transition[:probability]
272
+ end
273
+ end
274
+
275
+ # add state back and exit
276
+ # exclude auto transion
277
+ back_state_id = states_sequence.last(2)[0][:state_id]
278
+ if states_sequence.size == 1 || @auto_transiton.keys.include?(back_state_id)
279
+ # exit only
280
+ possible_transition_states << {state_id: false, probability: 1 - total_probability}
281
+ else
282
+ exit_probability = (1 - total_probability) * 0.3
283
+ back_probability = (1 - total_probability) * 0.7
284
+
285
+ # exit and back
286
+ possible_transition_states << {state_id: false, probability: exit_probability}
287
+ possible_transition_states << {state_id: back_state_id, probability: back_probability}
288
+
289
+ end
290
+
291
+ # choice next state
292
+ state_probability_hash = possible_transition_states.map {|sp| {sp[:state_id] => sp[:probability]} }.reduce(&:merge)
293
+ next_state_id = choice_next_state(state_probability_hash)
294
+ next_timestamp = from_state[:timestamp] + next_state_interval
295
+ end
296
+ end
297
+
298
+ from_state_id = from_state[:state_id]
299
+ from_statre_items = from_state[:items]
300
+ next_items = self.get_next_items(from_state_id, next_state_id, from_statre_items)
301
+ end
302
+
303
+ log = {
304
+ timestamp: next_timestamp,
305
+ state_id: next_state_id,
306
+ items: next_items
307
+ }
308
+
309
+ @access_log[user_id] << log
310
+
311
+ # return n_actions and n_sessions
312
+ n_actions = @access_log[user_id].size
313
+ n_sessions = 0
314
+ @access_log[user_id].each do |state|
315
+ n_sessions += 1 unless state[:state_id]
316
+ end
317
+
318
+ return n_actions, n_sessions
319
+ end
320
+
321
+ # Output:
322
+ # n_actions(int): total number of user's actions
323
+ # n_sessions: total number of user's sessions
324
+ def generate_accesslog(n=10000, output_form)
325
+
326
+ # set default value unless another manual settting done
327
+ if @transitions.size == 0
328
+ self.generate_state_transiton()
329
+ end
330
+
331
+ if @users.size == 0
332
+ self.generate_users(n_users=100, users=[])
333
+ end
334
+
335
+ if @items.size == 0
336
+ self.generate_items(n_items=100, items=[])
337
+ end
338
+
339
+ n_row = 1
340
+ while n_row < n
341
+ @users.each do |user|
342
+ user_id = user[:user_id]
343
+ n_actions = 0
344
+ n_sessions = 0
345
+ user_max_sessions = @assumptions[:user_max_sessions]
346
+ user_max_actions = @assumptions[:user_max_actions]
347
+ while n_actions < user_max_actions && n_sessions < user_max_sessions
348
+ n_actions, n_sessions = self.update_user_state(user_id)
349
+ n_row += 1
350
+ end
351
+ end
352
+ end
353
+ self.output_accesslog(n=10000, output_form={})
354
+ end
355
+
356
+ def output_accesslog(n=10000, output_form={})
357
+ # default settings
358
+ output_form = {
359
+ basic_log: [:timestamp, :user_id, :state_id, :items],
360
+ state: [:state_name],
361
+ user: []
362
+ }
363
+ if output_form != {}
364
+ output_form.each do |key, value|
365
+ output_form[key] = value
366
+ end
367
+ end
368
+
369
+ output_accesslogs = []
370
+ @access_log.each do |user_id, logs|
371
+ logs.each do |log|
372
+ if log[:state_id]
373
+ output_accesslog = {}
374
+
375
+ # basic_log
376
+ output_form[:basic_log].each do |log_item|
377
+ if log_item == :items
378
+ output_accesslog[log_item] = log[log_item].map{|e| e[:item_id] }.join(":")
379
+ elsif log_item == :user_id
380
+ output_accesslog[log_item] = user_id
381
+ else
382
+ output_accesslog[log_item] = log[log_item]
383
+ end
384
+ end
385
+
386
+ # states
387
+ output_form[:state].each do |log_item|
388
+ output_accesslog[log_item] = @states[log[:state_id]][log_item]
389
+ end
390
+
391
+ # users
392
+ output_form[:user].each do |log_item|
393
+ output_accesslog[log_item] = @users[log[:state_id]][log_item]
394
+ end
395
+
396
+ output_accesslogs << output_accesslog
397
+ end
398
+ end
399
+ end
400
+ output_accesslogs.sort{|a, b| a[:timestamp] <=> b[:timestamp]}
401
+ end
402
+
403
+ def export_logfile(n_max=10000, filetype='CSV', filepath='./fiction_log.csv')
404
+ logs = self.generate_accesslog(n_max=10000)
405
+ headers = logs.first.keys
406
+ if filetype == 'CSV'
407
+ CSV.open(filepath, "wb") do |output|
408
+ output.puts headers
409
+ logs.each do |log|
410
+ output.puts headers.map{|key| log[key]}
411
+ end
412
+ end
413
+ else
414
+ #[TODO] support output type (json, tsv, ...)
415
+ raise FileTypeError, "Your input file type is not support..."
416
+ end
417
+ end
418
+ end
419
+ end
@@ -0,0 +1,40 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "logfiction/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "logfiction"
8
+ spec.version = Logfiction::VERSION
9
+ spec.authors = ["rilmayer"]
10
+ spec.email = ["tomohiro.furusawa.629@gmail.com"]
11
+
12
+ spec.summary = %q{log data generator for learning log analysis, simulation, etc...}
13
+ spec.description = %q{log data generator for learning log analysis, simulation, etc...}
14
+ spec.homepage = "https://github.com/rilmayer/logfiction"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+ if spec.respond_to?(:metadata)
20
+ spec.metadata["homepage_uri"] = spec.homepage
21
+ spec.metadata["source_code_uri"] = "https://github.com/rilmayer/logfiction"
22
+ spec.metadata["changelog_uri"] = "https://github.com/rilmayer/logfiction"
23
+ else
24
+ raise "RubyGems 2.0 or newer is required to protect against " \
25
+ "public gem pushes."
26
+ end
27
+
28
+ # Specify which files should be added to the gem when it is released.
29
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
30
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
31
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
32
+ end
33
+ spec.bindir = "exe"
34
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
35
+ spec.require_paths = ["lib"]
36
+
37
+ spec.add_development_dependency "bundler", "~> 2.0"
38
+ spec.add_development_dependency "rake", "~> 10.0"
39
+ spec.add_development_dependency "rspec", "~> 3.0"
40
+ end
metadata ADDED
@@ -0,0 +1,105 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: logfiction
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - rilmayer
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-04-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description: log data generator for learning log analysis, simulation, etc...
56
+ email:
57
+ - tomohiro.furusawa.629@gmail.com
58
+ executables:
59
+ - logfiction
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - ".rspec"
65
+ - ".travis.yml"
66
+ - CODE_OF_CONDUCT.md
67
+ - Gemfile
68
+ - Gemfile.lock
69
+ - LICENSE.txt
70
+ - README.md
71
+ - README_ja.md
72
+ - Rakefile
73
+ - bin/console
74
+ - bin/setup
75
+ - exe/logfiction
76
+ - lib/logfiction.rb
77
+ - lib/logfiction/version.rb
78
+ - logfiction.gemspec
79
+ homepage: https://github.com/rilmayer/logfiction
80
+ licenses:
81
+ - MIT
82
+ metadata:
83
+ homepage_uri: https://github.com/rilmayer/logfiction
84
+ source_code_uri: https://github.com/rilmayer/logfiction
85
+ changelog_uri: https://github.com/rilmayer/logfiction
86
+ post_install_message:
87
+ rdoc_options: []
88
+ require_paths:
89
+ - lib
90
+ required_ruby_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ required_rubygems_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ requirements: []
101
+ rubygems_version: 3.0.3
102
+ signing_key:
103
+ specification_version: 4
104
+ summary: log data generator for learning log analysis, simulation, etc...
105
+ test_files: []