Skip to content

Commit 7d3d1b8

Browse files
committed
Add user on cronjobs and creator on triggers, display edge trigger details (SYN-8229, SYN-6850)
1 parent a897257 commit 7d3d1b8

10 files changed

+168
-96
lines changed

synapse/cortex.py

+10-6
Original file line numberDiff line numberDiff line change
@@ -5796,7 +5796,7 @@ async def addCronJob(self, cdef):
57965796

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

5799-
opts = {'user': cdef['creator'], 'view': cdef.get('view')}
5799+
opts = {'user': cdef['user'], 'view': cdef.get('view')}
58005800

58015801
view = self._viewFromOpts(opts)
58025802
cdef['view'] = view.iden
@@ -5814,7 +5814,7 @@ async def _onAddCronJob(self, cdef):
58145814

58155815
self.auth.reqNoAuthGate(iden)
58165816

5817-
user = await self.auth.reqUser(cdef['creator'])
5817+
user = await self.auth.reqUser(cdef['user'])
58185818

58195819
cdef = await self.agenda.add(cdef)
58205820

@@ -5849,7 +5849,7 @@ async def editCronJob(self, iden, edits):
58495849
realedits = {}
58505850

58515851
for name, valu in edits.items():
5852-
if name == 'creator':
5852+
if name == 'user':
58535853
await self.auth.reqUser(valu)
58545854

58555855
elif name == 'view':
@@ -5885,9 +5885,9 @@ async def _editCronJob(self, iden, edits):
58855885
appt = await self.agenda.get(iden)
58865886

58875887
for name, valu in edits.items():
5888-
if name == 'creator':
5888+
if name == 'user':
58895889
await self.auth.reqUser(valu)
5890-
appt.creator = valu
5890+
appt.user = valu
58915891

58925892
elif name == 'view':
58935893
self.reqView(valu)
@@ -5960,10 +5960,14 @@ async def listCronJobs(self):
59605960

59615961
info = cron.pack()
59625962

5963-
user = self.auth.user(cron.creator)
5963+
user = self.auth.user(cron.user)
59645964
if user is not None:
59655965
info['username'] = user.name
59665966

5967+
creator = self.auth.user(cron.creator)
5968+
if creator is not None:
5969+
info['creatorname'] = creator.name
5970+
59675971
crons.append(info)
59685972

59695973
return crons

synapse/lib/agenda.py

+21-15
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ class _Appt:
270270
'lastfinishtime',
271271
}
272272

273-
def __init__(self, stor, iden, recur, indx, storm, creator, recs, nexttime=None, view=None, created=None, pool=False, loglevel=None):
273+
def __init__(self, stor, iden, recur, indx, storm, creator, user, recs, nexttime=None, view=None, created=None, pool=False, loglevel=None):
274274
self.doc = ''
275275
self.name = ''
276276
self.task = None
@@ -280,7 +280,8 @@ def __init__(self, stor, iden, recur, indx, storm, creator, recs, nexttime=None,
280280
self.recur = recur # does this appointment repeat
281281
self.indx = indx # incremented for each appt added ever. Used for nexttime tiebreaking for stable ordering
282282
self.storm = storm # query to run
283-
self.creator = creator # user iden to run query as
283+
self.user = user # user iden to run query as
284+
self.creator = creator # user iden which created the appt
284285
self.recs = recs # List[ApptRec] list of the individual entries to calculate next time from
285286
self._recidxnexttime = None # index of rec who is up next
286287
self.view = view
@@ -326,6 +327,7 @@ def pack(self):
326327
'view': self.view,
327328
'indx': self.indx,
328329
'storm': self.storm,
330+
'user': self.user,
329331
'creator': self.creator,
330332
'created': self.created,
331333
'recs': [d.pack() for d in self.recs],
@@ -346,7 +348,7 @@ def unpack(cls, stor, val):
346348
recs = [ApptRec.unpack(tupl) for tupl in val['recs']]
347349
# TODO: MOAR INSANITY
348350
loglevel = val.get('loglevel', 'WARNING')
349-
appt = cls(stor, val['iden'], val['recur'], val['indx'], val['storm'], val['creator'], recs,
351+
appt = cls(stor, val['iden'], val['recur'], val['indx'], val['storm'], val['creator'], val['user'], recs,
350352
nexttime=val['nexttime'], view=val.get('view'), loglevel=loglevel)
351353
appt.doc = val.get('doc', '')
352354
appt.name = val.get('name', '')
@@ -512,7 +514,10 @@ async def add(self, cdef):
512514
The cron definition may contain the following keys:
513515
514516
creator (str)
515-
Iden of the creating user.
517+
Iden of the user which created the appointment.
518+
519+
user (str)
520+
Iden of the user used to run the Storm query.
516521
517522
iden (str)
518523
Iden of the appointment.
@@ -543,6 +548,7 @@ async def add(self, cdef):
543548
reqs = cdef.get('reqs', {})
544549
storm = cdef.get('storm')
545550
creator = cdef.get('creator')
551+
user = cdef.get('user')
546552
view = cdef.get('view')
547553
created = cdef.get('created')
548554
loglevel = cdef.get('loglevel', 'WARNING')
@@ -562,8 +568,8 @@ async def add(self, cdef):
562568

563569
await self.core.getStormQuery(storm)
564570

565-
if not creator:
566-
raise ValueError('"creator" key is cdef parameter is not present or empty')
571+
if not user:
572+
raise ValueError('"user" key is cdef parameter is not present or empty')
567573

568574
if not reqs and incunit is None:
569575
raise ValueError('at least one of reqs and incunit must be non-empty')
@@ -591,7 +597,7 @@ async def add(self, cdef):
591597
recs.extend(ApptRec(rd, incunit, v) for (rd, v) in itertools.product(reqdicts, incvals))
592598

593599
# TODO: this is insane. Make _Appt take the cdef directly...
594-
appt = _Appt(self, iden, recur, indx, storm, creator, recs, nexttime=nexttime, view=view,
600+
appt = _Appt(self, iden, recur, indx, storm, creator, user, recs, nexttime=nexttime, view=view,
595601
created=created, pool=pool, loglevel=loglevel)
596602
self._addappt(iden, appt)
597603

@@ -704,8 +710,8 @@ async def runloop(self):
704710
try:
705711
await self._execute(appt)
706712
except Exception as e:
707-
extra = {'iden': appt.iden, 'name': appt.name, 'user': appt.creator, 'view': appt.view}
708-
user = self.core.auth.user(appt.creator)
713+
extra = {'iden': appt.iden, 'name': appt.name, 'user': appt.user, 'view': appt.view}
714+
user = self.core.auth.user(appt.user)
709715
if user is not None:
710716
extra['username'] = user.name
711717
if isinstance(e, s_exc.SynErr):
@@ -720,25 +726,25 @@ async def _execute(self, appt):
720726
'''
721727
Fire off the task to make the storm query
722728
'''
723-
user = self.core.auth.user(appt.creator)
729+
user = self.core.auth.user(appt.user)
724730
if user is None:
725-
logger.warning(f'Unknown user {appt.creator} in stored appointment {appt.iden} {appt.name}',
726-
extra={'synapse': {'iden': appt.iden, 'name': appt.name, 'user': appt.creator}})
731+
logger.warning(f'Unknown user {appt.user} in stored appointment {appt.iden} {appt.name}',
732+
extra={'synapse': {'iden': appt.iden, 'name': appt.name, 'user': appt.user}})
727733
await self._markfailed(appt, 'unknown user')
728734
return
729735

730736
locked = user.info.get('locked')
731737
if locked:
732-
logger.warning(f'Cron {appt.iden} {appt.name} failed because creator {user.name} is locked',
733-
extra={'synapse': {'iden': appt.iden, 'name': appt.name, 'user': appt.creator,
738+
logger.warning(f'Cron {appt.iden} {appt.name} failed because user {user.name} is locked',
739+
extra={'synapse': {'iden': appt.iden, 'name': appt.name, 'user': appt.user,
734740
'username': user.name}})
735741
await self._markfailed(appt, 'locked user')
736742
return
737743

738744
view = self.core.getView(iden=appt.view, user=user)
739745
if view is None:
740746
logger.warning(f'Unknown view {appt.view} in stored appointment {appt.iden} {appt.name}',
741-
extra={'synapse': {'iden': appt.iden, 'name': appt.name, 'user': appt.creator,
747+
extra={'synapse': {'iden': appt.iden, 'name': appt.name, 'user': appt.user,
742748
'username': user.name, 'view': appt.view}})
743749
await self._markfailed(appt, 'unknown view')
744750
return

synapse/lib/schemas.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@
9292
'properties': {
9393
'storm': {'type': 'string', 'minlen': 1},
9494
'creator': {'type': 'string', 'pattern': s_config.re_iden},
95+
'user': {'type': 'string', 'pattern': s_config.re_iden},
9596
'created': {'type': 'integer', 'minimum': 0},
9697
'iden': {'type': 'string', 'pattern': s_config.re_iden},
9798
'view': {'type': 'string', 'pattern': s_config.re_iden},
@@ -135,7 +136,7 @@
135136
},
136137
},
137138
'additionalProperties': False,
138-
'required': ['creator', 'storm'],
139+
'required': ['creator', 'storm', 'user'],
139140
'dependencies': {
140141
'incvals': ['incunit'],
141142
'incunit': ['incvals'],

synapse/lib/storm.py

+59-31
Original file line numberDiff line numberDiff line change
@@ -785,51 +785,77 @@
785785
('--all', {'help': 'List every trigger in every readable view, rather than just the current view.', 'action': 'store_true'}),
786786
),
787787
'storm': '''
788-
$triggers = $lib.trigger.list($cmdopts.all)
788+
init {
789+
$conf = ({
790+
"columns": [
791+
{"name": "creator", "width": 24},
792+
{"name": "user", "width": 24},
793+
{"name": "iden", "width": 32},
794+
{"name": "view", "width": 11},
795+
{"name": "en?", "width": 3},
796+
{"name": "async?", "width": 6},
797+
{"name": "cond", "width": 9},
798+
{"name": "object", "width": 32},
799+
{"name": "storm query", "newlines": "split"},
800+
],
801+
"separators": {
802+
"row:outline": false,
803+
"column:outline": false,
804+
"header:row": "#",
805+
"data:row": "",
806+
"column": "",
807+
},
808+
})
809+
$printer = $lib.tabular.printer($conf)
810+
}
789811
812+
$triggers = $lib.trigger.list($cmdopts.all)
790813
if $triggers {
814+
$lib.print($printer.header())
791815
792-
$lib.print("user iden view en? async? cond object storm query")
816+
for $trig in $triggers {
793817
794-
for $trigger in $triggers {
795-
$user = $trigger.username.ljust(10)
796-
$iden = $trigger.iden.ljust(12)
797-
$view = $trigger.view.ljust(12)
798-
($ok, $async) = $lib.trycast(bool, $trigger.async)
799-
if $ok {
800-
$async = $lib.model.type(bool).repr($async).ljust(6)
801-
} else {
802-
$async = $lib.model.type(bool).repr($lib.false).ljust(6)
803-
}
804-
$enabled = $lib.model.type(bool).repr($trigger.enabled).ljust(6)
805-
$cond = $trigger.cond.ljust(9)
818+
if ($trig.enabled) { $enabled = 'Y' }
819+
else { $enabled = 'N' }
820+
821+
if ($trig.async) { $async = 'Y' }
822+
else { $async = 'N' }
806823
807824
$fo = ""
808-
if $trigger.form {
809-
$fo = $trigger.form
810-
}
825+
if $trig.form { $fo = $trig.form }
811826
812-
$pr = ""
813-
if $trigger.prop {
814-
$pr = $trigger.prop
815-
}
827+
if $trig.cond.startswith('tag:') {
816828
817-
if $cond.startswith('tag:') {
818-
$obj = $fo.ljust(14)
819-
$obj2 = $trigger.tag.ljust(10)
829+
$obj = `{$fo}#{$trig.tag}`
830+
831+
} elif $trig.cond.startswith('edge:') {
832+
833+
$n2form = $trig.n2form
834+
if (not $n2form) { $n2form = '*' }
835+
if (not $fo) { $fo = '*' }
836+
837+
$obj = `{$fo} -({$trig.verb})> {$n2form}`
820838
821839
} else {
840+
$pr = ""
841+
if $trig.prop {
842+
$pr = $trig.prop
843+
}
844+
822845
if $pr {
823-
$obj = $pr.ljust(14)
846+
$obj = $pr
824847
} elif $fo {
825-
$obj = $fo.ljust(14)
848+
$obj = $fo
826849
} else {
827-
$obj = '<missing> '
850+
$obj = '<missing>'
828851
}
829-
$obj2 = ' '
830852
}
831853
832-
$lib.print(`{$user} {$iden} {$view} {$enabled} {$async} {$cond} {$obj} {$obj2} {$trigger.storm}`)
854+
$row = (
855+
$trig.creatorname, $trig.username, $trig.iden, $trig.view,
856+
$enabled, $async, $trig.cond, $obj, $trig.storm
857+
)
858+
$lib.print($printer.row($row))
833859
}
834860
} else {
835861
$lib.print("No triggers found")
@@ -944,7 +970,7 @@
944970
('iden', {'help': 'Any prefix that matches exactly one valid cron job iden is accepted.'}),
945971
('--view', {'help': 'View to move the cron job to.'}),
946972
('--storm', {'help': 'New Storm query for the cron job.'}),
947-
('--creator', {'help': 'New user for the cron job to run as.'}),
973+
('--user', {'help': 'New user for the cron job to run as.'}),
948974
('--doc', {'help': 'New doc string for the cron job.', 'type': 'str'}),
949975
('--name', {'help': 'New name for the cron job.', 'type': 'str'}),
950976
('--pool', {'help': 'True to enable offloading the job to the Storm pool, False to disable.'}),
@@ -989,6 +1015,7 @@
9891015
init {
9901016
$conf = ({
9911017
"columns": [
1018+
{"name": "creator", "width": 24},
9921019
{"name": "user", "width": 24},
9931020
{"name": "iden", "width": 10},
9941021
{"name": "view", "width": 10},
@@ -1017,7 +1044,7 @@
10171044
for $cron in $crons {
10181045
$job = $cron.pprint()
10191046
$row = (
1020-
$job.user, $job.idenshort, $job.viewshort, $job.enabled,
1047+
$job.creator, $job.user, $job.idenshort, $job.viewshort, $job.enabled,
10211048
$job.isrecur, $job.isrunning, $job.iserr, `{$job.startcount}`,
10221049
$job.laststart, $job.lastend, $job.storm
10231050
)
@@ -1041,6 +1068,7 @@
10411068
$job = $cron.pprint()
10421069
10431070
$lib.print('iden: {iden}', iden=$job.iden)
1071+
$lib.print('creator: {creator}', creator=$job.creator)
10441072
$lib.print('user: {user}', user=$job.user)
10451073
$lib.print('enabled: {enabled}', enabled=$job.enabled)
10461074
$lib.print(`pool: {$job.pool}`)

0 commit comments

Comments
 (0)