CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
sagemathinc

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/hub/user-query-stats.coffee
Views: 687
1
#########################################################################
2
# This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.
3
# License: MS-RSL – see LICENSE.md for details
4
#########################################################################
5
6
###
7
Class to track (and report in a log) stats about user_queries by the Rethink interface.
8
###
9
10
{defaults} = misc = require('@cocalc/util/misc')
11
required = defaults.required
12
13
class exports.UserQueryStats
14
constructor: (@dbg) ->
15
@_accounts = {}
16
@_projects = {}
17
@_feeds = {}
18
19
_cnt: (account_id, project_id, table, op, eps=1) =>
20
if account_id?
21
t = @_accounts[account_id] ?= {}
22
else if project_id?
23
t = @_projects[project_id] ?= {}
24
else
25
return
26
s = t[table] ?= {}
27
s[op] ?= 0
28
s[op] += eps
29
30
report: (opts) =>
31
if opts.account_id?
32
t = @_accounts[opts.account_id]
33
head = "account_id='#{opts.account_id}'"
34
else if opts.project_id?
35
t = @_projects[opts.project_id]
36
head = "project_id='#{opts.project_id}'"
37
else
38
return
39
@dbg("#{head}: #{misc.to_json(t)}")
40
41
set_query: (opts) =>
42
opts = defaults opts,
43
account_id : undefined
44
project_id : undefined
45
table : required
46
#@dbg("set_query(account_id='#{opts.account_id}',project_id='#{opts.project_id}',table='#{opts.table}')")
47
@_cnt(opts.account_id, opts.project_id, opts.table, 'set')
48
@report(opts)
49
50
get_query: (opts) =>
51
opts = defaults opts,
52
account_id : undefined
53
project_id : undefined
54
table : required
55
#@dbg("get_query(account_id='#{opts.account_id}',project_id='#{opts.project_id}',table='#{opts.table}')")
56
@_cnt(opts.account_id, opts.project_id, opts.table, 'get')
57
@report(opts)
58
59
changefeed: (opts) =>
60
opts = defaults opts,
61
account_id : undefined
62
project_id : undefined
63
table : required
64
changefeed_id : required
65
#@dbg("changefeed(account_id='#{opts.account_id}',project_id='#{opts.project_id}',table='#{opts.table}')")
66
@_cnt(opts.account_id, opts.project_id, opts.table, 'feed')
67
@_feeds[opts.changefeed_id] = opts
68
@report(opts)
69
70
cancel_changefeed: (opts) =>
71
opts = defaults opts,
72
changefeed_id : required
73
@dbg("cancel_changefeed(changefeed_id='#{opts.changefeed_id}')")
74
x = @_feeds[opts.changefeed_id]
75
if not x?
76
@dbg("no such changefeed_id='#{opts.changefeed_id}'")
77
return
78
{account_id, project_id, table} = @_feeds[opts.changefeed_id]
79
@_cnt(account_id, project_id, table, 'feed', -1)
80
@report({account_id:account_id, project_id:project_id})
81
82
83