Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download
Views: 39539
1
###
2
Class to track (and report in a log) stats about user_queries by the Rethink interface.
3
###
4
5
{defaults} = misc = require('smc-util/misc')
6
required = defaults.required
7
8
class exports.UserQueryStats
9
constructor: (@dbg) ->
10
@_accounts = {}
11
@_projects = {}
12
@_feeds = {}
13
14
_cnt: (account_id, project_id, table, op, eps=1) =>
15
if account_id?
16
t = @_accounts[account_id] ?= {}
17
else if project_id?
18
t = @_projects[project_id] ?= {}
19
else
20
return
21
s = t[table] ?= {}
22
s[op] ?= 0
23
s[op] += eps
24
25
report: (opts) =>
26
if opts.account_id?
27
t = @_accounts[opts.account_id]
28
head = "account_id='#{opts.account_id}'"
29
else if opts.project_id?
30
t = @_projects[opts.project_id]
31
head = "project_id='#{opts.project_id}'"
32
else
33
return
34
@dbg("#{head}: #{misc.to_json(t)}")
35
36
set_query: (opts) =>
37
opts = defaults opts,
38
account_id : undefined
39
project_id : undefined
40
table : required
41
#@dbg("set_query(account_id='#{opts.account_id}',project_id='#{opts.project_id}',table='#{opts.table}')")
42
@_cnt(opts.account_id, opts.project_id, opts.table, 'set')
43
@report(opts)
44
45
get_query: (opts) =>
46
opts = defaults opts,
47
account_id : undefined
48
project_id : undefined
49
table : required
50
#@dbg("get_query(account_id='#{opts.account_id}',project_id='#{opts.project_id}',table='#{opts.table}')")
51
@_cnt(opts.account_id, opts.project_id, opts.table, 'get')
52
@report(opts)
53
54
changefeed: (opts) =>
55
opts = defaults opts,
56
account_id : undefined
57
project_id : undefined
58
table : required
59
changefeed_id : required
60
#@dbg("changefeed(account_id='#{opts.account_id}',project_id='#{opts.project_id}',table='#{opts.table}')")
61
@_cnt(opts.account_id, opts.project_id, opts.table, 'feed')
62
@_feeds[opts.changefeed_id] = opts
63
@report(opts)
64
65
cancel_changefeed: (opts) =>
66
opts = defaults opts,
67
changefeed_id : required
68
@dbg("cancel_changefeed(changefeed_id='#{opts.changefeed_id}')")
69
x = @_feeds[opts.changefeed_id]
70
if not x?
71
@dbg("no such changefeed_id='#{opts.changefeed_id}'")
72
return
73
{account_id, project_id, table} = @_feeds[opts.changefeed_id]
74
@_cnt(account_id, project_id, table, 'feed', -1)
75
@report({account_id:account_id, project_id:project_id})
76
77
78