litestack 0.2.6 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -13,13 +13,13 @@ schema:
13
13
  name TEXT DEFAULT('___') NOT NULL ON CONFLICT REPLACE,
14
14
  key TEXT DEFAULT('___') NOT NULL ON CONFLICT REPLACE,
15
15
  count INTEGER DEFAULT(0) NOT NULL ON CONFLICT REPLACE,
16
- value INTEGER,
17
- minimum INTEGER,
18
- maximum INTEGER,
16
+ value REAL,
17
+ minimum REAL,
18
+ maximum REAL,
19
19
  created_at INTEGER DEFAULT((unixepoch()/300*300)) NOT NULL,
20
20
  resolution TEXT DEFAULT('minute') NOT NULL,
21
21
  PRIMARY KEY(resolution, created_at, topic, name, key)
22
- );
22
+ ) STRICT;
23
23
 
24
24
  create_topic_index_on_events: CREATE INDEX events_topic_index ON events (resolution, created_at, topic) WHERE name = '___';
25
25
  create_event_index_on_events: CREATE INDEX events_event_index ON events (resolution, created_at , topic, name) WHERE key = '___';
@@ -226,7 +226,7 @@ stmts:
226
226
  FROM events
227
227
  WHERE topic = ?1 AND name = ?2 AND resolution = ?3
228
228
  AND created_at <= unixepoch()
229
- AND created_at >= unixepoch() -?6
229
+ AND created_at >= unixepoch() - ?6
230
230
  AND key != '___'
231
231
  AND iif(length(?5) > 0, rowid IN (SELECT rowid FROM events_search WHERE key LIKE ?5), TRUE)
232
232
  GROUP BY key
@@ -251,7 +251,7 @@ stmts:
251
251
  FROM events
252
252
  WHERE topic = ?1 AND name = ?2 AND resolution = ?3
253
253
  AND created_at <= unixepoch()
254
- AND created_at >= unixepoch() -?6
254
+ AND created_at >= unixepoch() - ?6
255
255
  AND key != '___'
256
256
  AND iif(length(?5) > 0, rowid IN (SELECT rowid FROM events_search WHERE key LIKE ?5), TRUE)
257
257
  GROUP BY key
@@ -272,8 +272,8 @@ stmts:
272
272
  SELECT
273
273
  datetime(ts.slot, 'unixepoch') AS rtime,
274
274
  ts.slot AS unixtime,
275
- format('%.2f', sum(events.count)) AS rcount,
276
- format('%.2f', avg(events.value/events.count)) AS ravg
275
+ sum(events.count) AS rcount,
276
+ avg(events.value/events.count) AS ravg
277
277
  FROM (
278
278
  WITH RECURSIVE timeslot(x) AS (
279
279
  SELECT (unixepoch()/(?1))*(?1)
@@ -294,8 +294,9 @@ stmts:
294
294
  SELECT
295
295
  datetime(ts.slot, 'unixepoch') AS rtime,
296
296
  ts.slot AS unixtime,
297
- format('%.2f', sum(events.count)) AS rcount,
298
- format('%.2f', avg(events.value/events.count)) AS ravg
297
+ events.count AS rcount,
298
+ events.value / events.count AS ravg,
299
+ events.value AS rtotal
299
300
  FROM (
300
301
  WITH RECURSIVE timeslot(x) AS (
301
302
  SELECT (unixepoch()/(?1))*(?1)
@@ -317,8 +318,9 @@ stmts:
317
318
  SELECT
318
319
  datetime(ts.slot, 'unixepoch') AS rtime,
319
320
  ts.slot AS unixtime,
320
- format('%.2f', sum(events.count)) AS rcount,
321
- format('%.2f', avg(events.value/events.count)) AS ravg
321
+ events.count AS rcount,
322
+ events.value / events.count AS ravg,
323
+ events.value AS rtotal
322
324
  FROM (
323
325
  WITH RECURSIVE timeslot(x) AS (
324
326
  SELECT (unixepoch()/(?1))*(?1)
@@ -7,13 +7,13 @@ schema:
7
7
  name TEXT DEFAULT('___') NOT NULL ON CONFLICT REPLACE,
8
8
  key TEXT DEFAULT('___') NOT NULL ON CONFLICT REPLACE,
9
9
  count INTEGER DEFAULT(0) NOT NULL ON CONFLICT REPLACE,
10
- value INTEGER,
11
- minimum INTEGER,
12
- maximum INTEGER,
10
+ value REAL,
11
+ minimum REAL,
12
+ maximum REAL,
13
13
  created_at INTEGER DEFAULT((unixepoch()/300*300)) NOT NULL ON CONFLICT REPLACE,
14
14
  resolution TEXT DEFAULT('minute') NOT NULL,
15
15
  PRIMARY KEY(resolution, created_at, topic, name, key)
16
- );
16
+ ) STRICT;
17
17
 
18
18
  stmts:
19
19
 
@@ -78,12 +78,12 @@ class Litequeue
78
78
 
79
79
  # deletes all the entries in all queues, or if a queue name is given, deletes all entries in that specific queue
80
80
  def clear(queue=nil)
81
- run_sql("DELETE FROM queue WHERE iif(?, name = ?, 1)", queue)
81
+ run_sql("DELETE FROM queue WHERE iif(?1 IS NOT NULL, name = ?1, TRUE)", queue)
82
82
  end
83
83
 
84
84
  # returns a count of entries in all queues, or if a queue name is given, reutrns the count of entries in that queue
85
85
  def count(queue=nil)
86
- run_sql("SELECT count(*) FROM queue WHERE iif(?, name = ?, 1)", queue)[0][0]
86
+ run_sql("SELECT count(*) FROM queue WHERE iif(?1 IS NOT NULL, name = ?1, TRUE)", queue)[0][0]
87
87
  end
88
88
 
89
89
  # return the size of the queue file on disk
@@ -116,27 +116,16 @@ class Litequeue
116
116
  private
117
117
 
118
118
  def create_connection
119
- conn = super
120
- conn.wal_autocheckpoint = 10000
121
- sql = YAML.load_file("#{__dir__}/litequeue.sql.yml")
122
- version = conn.get_first_value("PRAGMA user_version")
123
- sql["schema"].each_pair do |v, obj|
124
- if v > version
125
- conn.transaction(:immediate) do
126
- obj.each{|k, s| conn.execute(s)}
127
- conn.user_version = v
119
+ super("#{__dir__}/litequeue.sql.yml") do |conn|
120
+ conn.wal_autocheckpoint = 10000
121
+ # check if there is an old database and convert entries to the new format
122
+ if conn.get_first_value("select count(*) from sqlite_master where name = '_ul_queue_'") == 1
123
+ conn.transaction(:immediate) do
124
+ conn.execute("INSERT INTO queue(fire_at, name, value, created_at) SELECT fire_at, queue, value, created_at FROM _ul_queue_")
125
+ conn.execute("DROP TABLE _ul_queue_")
128
126
  end
129
127
  end
130
- end
131
- sql["stmts"].each { |k, v| conn.stmts[k.to_sym] = conn.prepare(v) }
132
- # check if there is an old database and convert entries to the new format
133
- if conn.get_first_value("select count(*) from sqlite_master where name = '_ul_queue_'") == 1
134
- conn.transaction(:immediate) do
135
- conn.execute("INSERT INTO queue(fire_at, name, value, created_at) SELECT fire_at, queue, value, created_at FROM _ul_queue_")
136
- conn.execute("DROP TABLE _ul_queue_")
137
- end
138
128
  end
139
- conn
140
129
  end
141
130
 
142
131
  end
@@ -23,7 +23,7 @@ module Litesupport
23
23
  elsif ENV["RACK_ENV"]
24
24
  ENV["RACK_ENV"]
25
25
  elsif ENV["APP_ENV"]
26
- ENV["RACK_ENV"]
26
+ ENV["APP_ENV"]
27
27
  else
28
28
  "development"
29
29
  end
@@ -99,8 +99,8 @@ module Litesupport
99
99
  @@mutex ||= Mutex.new
100
100
  end
101
101
 
102
- # bold assumption, we will only synchronize threaded code
103
- # if some code explicitly wants to synchronize a fiber
102
+ # bold assumption, we will only synchronize threaded code!
103
+ # If some code explicitly wants to synchronize a fiber
104
104
  # they must send (true) as a parameter to this method
105
105
  # else it is a no-op for fibers
106
106
  def self.synchronize(fiber_sync = false, &block)
@@ -162,21 +162,6 @@ module Litesupport
162
162
 
163
163
  end
164
164
 
165
- module Forkable
166
-
167
- def _fork(*args)
168
- ppid = Process.pid
169
- result = super
170
- if Process.pid != ppid
171
- # trigger a restart of all connections owned by Litesupport::Pool
172
- end
173
- result
174
- end
175
-
176
- end
177
-
178
- #::Process.singleton_class.prepend(::Litesupport::Forkable)
179
-
180
165
  class Pool
181
166
 
182
167
  def initialize(count, &block)
@@ -333,13 +318,18 @@ module Litesupport
333
318
  def run_method(method, *args)
334
319
  @conn.acquire{|q| q.send(method, *args)}
335
320
  end
321
+
322
+ def run_stmt_method(stmt, method, *args)
323
+ @conn.acquire{|q| q.stmts[stmt].send(method, *args)}
324
+ end
325
+
336
326
 
337
327
  def create_pooled_connection(count = 1)
338
328
  Litesupport::Pool.new(1){create_connection}
339
329
  end
340
330
 
341
331
  # common db object options
342
- def create_connection
332
+ def create_connection(path_to_sql_file = nil)
343
333
  conn = SQLite3::Database.new(@options[:path])
344
334
  conn.busy_handler{ Litesupport.switch || sleep(rand * 0.002) }
345
335
  conn.journal_mode = "WAL"
@@ -349,9 +339,40 @@ module Litesupport
349
339
  class << conn
350
340
  attr_reader :stmts
351
341
  end
342
+ yield conn if block_given?
343
+ # use the <client>.sql.yml file to define the schema and compile prepared statements
344
+ unless path_to_sql_file.nil?
345
+ sql = YAML.load_file(path_to_sql_file)
346
+ version = conn.get_first_value("PRAGMA user_version")
347
+ sql["schema"].each_pair do |v, obj|
348
+ if v > version
349
+ conn.transaction do
350
+ obj.each do |k, s|
351
+ begin
352
+ conn.execute(s)
353
+ rescue Exception => e
354
+ STDERR.puts "Error parsing #{k}"
355
+ STDERR.puts s
356
+ raise e
357
+ end
358
+ end
359
+ conn.user_version = v
360
+ end
361
+ end
362
+ end
363
+ sql["stmts"].each do |k, v|
364
+ begin
365
+ conn.stmts[k.to_sym] = conn.prepare(v)
366
+ rescue Exception => e
367
+ STDERR.puts "Error parsing #{k}"
368
+ STDERR.puts v
369
+ raise e
370
+ end
371
+ end
372
+ end
352
373
  conn
353
374
  end
354
-
375
+
355
376
  end
356
377
 
357
378
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Litestack
4
- VERSION = "0.2.6"
4
+ VERSION = "0.3.0"
5
5
  end
data/lib/litestack.rb CHANGED
@@ -16,7 +16,7 @@ require_relative "./railties/rails/commands/dbconsole" if defined? Rails && defi
16
16
  require_relative "./active_support/cache/litecache" if defined? ActiveSupport
17
17
  require_relative "./active_job/queue_adapters/litejob_adapter" if defined? ActiveJob
18
18
  require_relative "./action_cable/subscription_adapter/litecable" if defined? ActionCable
19
- require_relative "./litestack/railtie" if defined? Rails
19
+ require_relative "./litestack/railtie" if defined? Rails::Railtie
20
20
 
21
21
  module Litestack
22
22
  class NotImplementedError < Exception; end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: litestack
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.6
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mohamed Hassan
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-07-16 00:00:00.000000000 Z
11
+ date: 2023-08-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sqlite3
@@ -109,12 +109,15 @@ files:
109
109
  - README.md
110
110
  - Rakefile
111
111
  - WHYLITESTACK.md
112
+ - assets/event_page.png
113
+ - assets/index_page.png
112
114
  - assets/litecable_logo_teal.png
113
115
  - assets/litecache_logo_teal.png
114
116
  - assets/litedb_logo_teal.png
115
117
  - assets/litejob_logo_teal.png
116
118
  - assets/litestack_logo_teal.png
117
119
  - assets/litestack_logo_teal_large.png
120
+ - assets/topic_page.png
118
121
  - bench/bench.rb
119
122
  - bench/bench_cache_rails.rb
120
123
  - bench/bench_cache_raw.rb
@@ -138,6 +141,10 @@ files:
138
141
  - lib/litestack/liteboard/views/event.erb
139
142
  - lib/litestack/liteboard/views/index.erb
140
143
  - lib/litestack/liteboard/views/layout.erb
144
+ - lib/litestack/liteboard/views/litecable.erb
145
+ - lib/litestack/liteboard/views/litecache.erb
146
+ - lib/litestack/liteboard/views/litedb.erb
147
+ - lib/litestack/liteboard/views/litejob.erb
141
148
  - lib/litestack/liteboard/views/topic.erb
142
149
  - lib/litestack/litecable.rb
143
150
  - lib/litestack/litecable.sql.yml