Skip to content

Commit

Permalink
updates from discussion
Browse files Browse the repository at this point in the history
  • Loading branch information
Cisphyx committed Jan 8, 2025
1 parent 85a21d7 commit aabe272
Show file tree
Hide file tree
Showing 13 changed files with 164 additions and 171 deletions.
48 changes: 20 additions & 28 deletions synapse/cortex.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,6 @@

reqver = '>=0.2.0,<3.0.0'

NEXSVERS_CRON_USER = (2, 193)

# Constants returned in results from syncLayersEvents and syncIndexEvents
SYNC_NODEEDITS = 0 # A nodeedits: (<offs>, 0, <etyp>, (<etype args>), {<meta>})
SYNC_NODEEDIT = 1 # A nodeedit: (<offs>, 0, <etyp>, (<etype args>))
Expand Down Expand Up @@ -959,7 +957,6 @@ async def initServiceStorage(self):
await self._bumpCellVers('cortex:storage', (
(1, self._storUpdateMacros),
(4, self._storCortexHiveMigration),
(5, self._cronCreatorToUser),
), nexs=False)

# Perform module loading
Expand Down Expand Up @@ -1098,12 +1095,17 @@ async def _viewNomergeToProtected(self):
await view.setViewInfo('protected', nomerge)
await view.setViewInfo('nomerge', None)

async def _cronCreatorToUser(self):
apptdefs = self.cortexdata.getSubKeyVal('agenda:appt:')
for iden, info in apptdefs.items():
if (user := info.pop('creator', None)) is not None:
info['user'] = user
apptdefs.set(iden, info)
async def _addCronUser(self):
for iden, cron in self.agenda.list():
await self.editCronJob(iden, 'user', cron.creator)

@s_nexus.Pusher.onPushAuto('cortex:add:trigger:creator')
async def _addTriggerCreator(self):
for view in self.views.values():
for iden, trig in view.triggers.list():
if trig.tdef.get('creator') is None:
trig.tdef['creator'] = trig.tdef['user']
view.trigdict.set(iden, trig.tdef)

async def _storUpdateMacros(self):
for name, node in await self.hive.open(('cortex', 'storm', 'macros')):
Expand Down Expand Up @@ -1536,6 +1538,8 @@ async def _execCellUpdates(self):
await self._bumpCellVers('cortex:defaults', (
(1, self._addAllLayrRead),
(2, self._viewNomergeToProtected),
(3, self._addCronUser),
(4, self._addTriggerCreator),
))

async def _addAllLayrRead(self):
Expand Down Expand Up @@ -6510,10 +6514,6 @@ async def addCronJob(self, cdef):
The incunit if not None it must be larger in unit size than all the keys in all reqs elements.
Non-recurring jobs may also have a req of 'now' which will cause the job to also execute immediately.
'''
if self.nexsvers < NEXSVERS_CRON_USER:
if (user := cdef.pop('user', None)) is not None:
cdef['creator'] = user

s_schemas.reqValidCronDef(cdef)

iden = cdef.get('iden')
Expand Down Expand Up @@ -6550,10 +6550,7 @@ async def addCronJob(self, cdef):

cdef['created'] = s_common.now()

if (user := cdef.get('user')) is None:
user = cdef.get('creator')

opts = {'user': user, 'view': cdef.get('view')}
opts = {'user': cdef['user'], 'view': cdef.get('view')}

view = self._viewFromOpts(opts)
cdef['view'] = view.iden
Expand All @@ -6563,9 +6560,6 @@ async def addCronJob(self, cdef):
@s_nexus.Pusher.onPush('cron:add')
async def _onAddCronJob(self, cdef):

if (user := cdef.pop('creator', None)) is not None:
cdef['user'] = user

iden = cdef['iden']

appt = self.agenda.appts.get(iden)
Expand Down Expand Up @@ -6688,25 +6682,23 @@ async def listCronJobs(self):
if user is not None:
info['username'] = user.name

creator = self.auth.user(cron.creator)
if creator is not None:
info['creatorname'] = creator.name

crons.append(info)

return crons

@s_nexus.Pusher.onPushAuto('cron:edit')
async def editCronJob(self, iden, name, valu):
if self.nexsvers < NEXSVERS_CRON_USER and name == 'user':
name = 'creator'

return await self._push('cron:edit', iden, name, valu)

@s_nexus.Pusher.onPush('cron:edit')
async def _editCronJob(self, iden, name, valu):
'''
Modify a cron job definition.
'''
appt = await self.agenda.get(iden)
# TODO make this generic and check cdef

if name in ('creator', 'user'):
if name == 'user':
await self.auth.reqUser(valu)
appt.user = valu

Expand Down
16 changes: 13 additions & 3 deletions synapse/lib/agenda.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ class _Appt:
'lastfinishtime',
}

def __init__(self, stor, iden, recur, indx, query, user, recs, nexttime=None, view=None, created=None, pool=False):
def __init__(self, stor, iden, recur, indx, query, creator, user, recs, nexttime=None, view=None, created=None, pool=False):
self.doc = ''
self.name = ''
self.task = None
Expand All @@ -280,6 +280,7 @@ def __init__(self, stor, iden, recur, indx, query, user, recs, nexttime=None, vi
self.indx = indx # incremented for each appt added ever. Used for nexttime tiebreaking for stable ordering
self.query = query # query to run
self.user = user # user iden to run query as
self.creator = creator # user iden which created the appt
self.recs = recs # List[ApptRec] list of the individual entries to calculate next time from
self._recidxnexttime = None # index of rec who is up next
self.view = view
Expand Down Expand Up @@ -347,6 +348,7 @@ def pack(self):
'indx': self.indx,
'query': self.query,
'user': self.user,
'creator': self.creator,
'created': self.created,
'recs': [d.pack() for d in self.recs],
'nexttime': self.nexttime,
Expand All @@ -364,7 +366,11 @@ def unpack(cls, stor, val):
if val['ver'] != 1:
raise s_exc.BadStorageVersion(mesg=f"Found version {val['ver']}") # pragma: no cover
recs = [ApptRec.unpack(tupl) for tupl in val['recs']]
appt = cls(stor, val['iden'], val['recur'], val['indx'], val['query'], val['user'], recs, nexttime=val['nexttime'], view=val.get('view'))

creator = val['creator']
user = val.get('user', creator)

appt = cls(stor, val['iden'], val['recur'], val['indx'], val['query'], creator, user, recs, nexttime=val['nexttime'], view=val.get('view'))
appt.doc = val.get('doc', '')
appt.name = val.get('name', '')
appt.pool = val.get('pool', False)
Expand Down Expand Up @@ -525,6 +531,9 @@ async def add(self, cdef):
Notes:
The cron definition may contain the following keys:
creator (str)
Iden of the user which created the appointment.
user (str)
Iden of the user used to run the Storm query.
Expand Down Expand Up @@ -558,6 +567,7 @@ async def add(self, cdef):
query = cdef.get('storm')
user = cdef.get('user')
view = cdef.get('view')
creator = cdef.get('creator', self.core.auth.rootuser.iden)
created = cdef.get('created')

pool = cdef.get('pool', False)
Expand Down Expand Up @@ -603,7 +613,7 @@ async def add(self, cdef):
incvals = (incvals, )
recs.extend(ApptRec(rd, incunit, v) for (rd, v) in itertools.product(reqdicts, incvals))

appt = _Appt(self, iden, recur, indx, query, user, recs, nexttime=nexttime, view=view, created=created, pool=pool)
appt = _Appt(self, iden, recur, indx, query, creator, user, recs, nexttime=nexttime, view=view, created=created, pool=pool)
self._addappt(iden, appt)

appt.doc = cdef.get('doc', '')
Expand Down
2 changes: 1 addition & 1 deletion synapse/lib/cell.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@

logger = logging.getLogger(__name__)

NEXUS_VERSION = (2, 193)
NEXUS_VERSION = (2, 177)

SLAB_MAP_SIZE = 128 * s_const.mebibyte
SSLCTX_CACHE_SIZE = 64
Expand Down
5 changes: 1 addition & 4 deletions synapse/lib/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,7 @@
},
},
'additionalProperties': False,
'required': ['storm'],
'if': {'required': ['user']},
'then': {'not': {'required': ['creator']}},
'else': {'required': ['creator']},
'required': ['creator', 'storm', 'user'],
'dependencies': {
'incvals': ['incunit'],
'incunit': ['incvals'],
Expand Down
88 changes: 58 additions & 30 deletions synapse/lib/storm.py
Original file line number Diff line number Diff line change
Expand Up @@ -1187,51 +1187,77 @@
('--all', {'help': 'List every trigger in every readable view, rather than just the current view.', 'action': 'store_true'}),
),
'storm': '''
$triggers = $lib.trigger.list($cmdopts.all)
init {
$conf = ({
"columns": [
{"name": "creator", "width": 24},
{"name": "user", "width": 24},
{"name": "iden", "width": 32},
{"name": "view", "width": 11},
{"name": "en?", "width": 3},
{"name": "async?", "width": 6},
{"name": "cond", "width": 9},
{"name": "object", "width": 32},
{"name": "storm query", "newlines": "split"},
],
"separators": {
"row:outline": false,
"column:outline": false,
"header:row": "#",
"data:row": "",
"column": "",
},
})
$printer = $lib.tabular.printer($conf)
}
$triggers = $lib.trigger.list($cmdopts.all)
if $triggers {
$lib.print($printer.header())
$lib.print("user iden view en? async? cond object storm query")
for $trig in $triggers {
for $trigger in $triggers {
$user = $trigger.username.ljust(10)
$iden = $trigger.iden.ljust(12)
$view = $trigger.view.ljust(12)
($ok, $async) = $lib.trycast(bool, $trigger.async)
if $ok {
$async = $lib.model.type(bool).repr($async).ljust(6)
} else {
$async = $lib.model.type(bool).repr($lib.false).ljust(6)
}
$enabled = $lib.model.type(bool).repr($trigger.enabled).ljust(6)
$cond = $trigger.cond.ljust(9)
if ($trig.enabled) { $enabled = 'Y' }
else { $enabled = 'N' }
if ($trig.async) { $async = 'Y' }
else { $async = 'N' }
$fo = ""
if $trigger.form {
$fo = $trigger.form
}
if $trig.form { $fo = $trig.form }
$pr = ""
if $trigger.prop {
$pr = $trigger.prop
}
if $trig.cond.startswith('tag:') {
if $cond.startswith('tag:') {
$obj = $fo.ljust(14)
$obj2 = $trigger.tag.ljust(10)
$obj = `{$fo}#{$trig.tag}`
} elif $trig.cond.startswith('edge:') {
$n2form = $trig.n2form
if (not $n2form) { $n2form = '*' }
if (not $fo) { $fo = '*' }
$obj = `{$fo} -({$trig.verb})> {$n2form}`
} else {
$pr = ""
if $trig.prop {
$pr = $trig.prop
}
if $pr {
$obj = $pr.ljust(14)
$obj = $pr
} elif $fo {
$obj = $fo.ljust(14)
$obj = $fo
} else {
$obj = '<missing> '
$obj = '<missing>'
}
$obj2 = ' '
}
$lib.print(`{$user} {$iden} {$view} {$enabled} {$async} {$cond} {$obj} {$obj2} {$trigger.storm}`)
$row = (
$trig.creatorname, $trig.username, $trig.iden, $trig.view,
$enabled, $async, $trig.cond, $obj, $trig.storm
)
$lib.print($printer.row($row))
}
} else {
$lib.print("No triggers found")
Expand Down Expand Up @@ -1392,6 +1418,7 @@
init {
$conf = ({
"columns": [
{"name": "creator", "width": 24},
{"name": "user", "width": 24},
{"name": "iden", "width": 10},
{"name": "view", "width": 10},
Expand Down Expand Up @@ -1420,7 +1447,7 @@
for $cron in $crons {
$job = $cron.pprint()
$row = (
$job.user, $job.idenshort, $job.viewshort, $job.enabled,
$job.creator, $job.user, $job.idenshort, $job.viewshort, $job.enabled,
$job.isrecur, $job.isrunning, $job.iserr, `{$job.startcount}`,
$job.laststart, $job.lastend, $job.query
)
Expand All @@ -1444,6 +1471,7 @@
$job = $cron.pprint()
$lib.print('iden: {iden}', iden=$job.iden)
$lib.print('creator: {creator}', creator=$job.creator)
$lib.print('user: {user}', user=$job.user)
$lib.print('enabled: {enabled}', enabled=$job.enabled)
$lib.print(`pool: {$job.pool}`)
Expand Down
Loading

0 comments on commit aabe272

Please sign in to comment.