0 votes
Hi,

 how can I access the scenario object in the custom python trigger?

import dataiku

from dataiku.scenario import Trigger, Scenario

t = Trigger()

s = Scenario()

# do something wiht the scenario, such as access it's last runs
by
reopened by

1 Answer

0 votes
Best answer

Hi,

You need to use the public Python API to access a scenario object that is not the current one:

https://doc.dataiku.com/dss/latest/python-api/rest-api-client/scenarios.html

Hope it helps,

Alex

by
selected by
Yes, I am aware of this option. But to access any object via the public python API I need a project key and the ID of the scenario. So  how can I get these attributes from the trigger?  I have found only this:

u'name' :'xx',
u'graceDelaySettings': {u'delay': 0,  u'checkAgainAfterGraceDelay': False},
 u'delay': 60,
  u'params': {u'code':  u'envSelection': {u'envMode': u'INHERIT'}},
  u'active': True,
  u'type': u'custom_python',
  u'id': u'phRSuysQ'})
What is the scenario you want to use in the trigger? Is it the same one as where the trigger is? Or an external one? In which case is it in the same project?
Yes I want to access the scenario where the trigger is stored.
I tried this:

import dataiku
from dataiku.scenario import Trigger,Scenario
t = Trigger()
s = Scenario()
# This works OK, returns the PROJECT_KEY of the project where it is executed
print(s.project_key)
# This does not work,  I would like to get the scenario ID where the trigger is executed.
print(s.scenario_trigger['scenarioId'])
# Also this does not work
print(s.scenario_trigger['runId'])

 Traceback (most recent call last):
[2019/03/29-14:19:22.567] [Exec-57020] [INFO] [process]  -   File "/data/data0/dss/data/scenarios/ADMIN/TEST_CUSTOM_TRIGGER_HOUR/custom-trigger-phRSuysQ/script.py", line 9, in <module>
[2019/03/29-14:19:22.567] [Exec-57020] [INFO] [process]  -     print(s.scenario_trigger['scenarioId'])
[2019/03/29-14:19:22.567] [Exec-57020] [INFO] [process]  - TypeError: 'NoneType' object has no attribute '__getitem__'


But when I execute the same prints in the Step python code, then I can access the scenario object:

s=Scenario()
print(s.project_key)
print(s.scenario_trigger)

 ADMIN
[2019/03/29-14:23:16.894] [Exec-57294] [INFO] [dku.utils]  - {u'scenarioId': u'TEST_CUSTOM_TRIGGER_HOUR', u'timestamp': 1553865795867, u'triggerAuthCtx'.....
You will need to pass the scenario id explicitly. You can retrieve that in the URL in your browser when you are on the scenario page.
Thanks, that was the point to not hard-code the scenario ID and to create an universal trigger code what will retrieve it's parent (scenario) - check som project variables, fetch the scenario history runs and decide to fire (run) or not.
+1 for this feature (i.e. getting the scenario ID and project Key from Trigger object)
Thanks
Hi, finally managed to get the info without hard-coding the scenario Id. Here is the code:

from datetime import datetime
import dataiku
import dataikuapi
from dataiku.scenario import Trigger, Scenario
t = Trigger()
s = Scenario()
trigger_name = t.trigger['name']
trigger_project_key = s.project_key
start_time = dataiku.get_custom_variables().get(trigger_name)

def get_scenario_by_trigger_name(project_key, trigger_name):
    # Search for a scenario by a trigger name
    # Return type dataikuapi.dss.scenario.DSSScenario
    client = dataiku.api_client()
    prj = dataikuapi.dss.project.DSSProject(client, project_key)
    for scenario in prj.list_scenarios():
        trigger_list = scenario.get('triggerDigest', '').split(',')
        if trigger_name in trigger_list:
            return prj.get_scenario(scenario['id'])

def get_status_and_last_run_date(scenario):
    # Returns the actual status of the scenario and the
    # last start and end date
    # Return type (string,datetime,datetime)
    run = scenario.get_last_runs()[0] if len(scenario.get_last_runs()) > 0 else None
    if run:
        if scenario.get_current_run():
            scenario_status = 'RUNNING'
        else:
            run_info = run.get_info()
            scenario_status = run_info.get('result', {}).get('outcome', '?')
            end_time = datetime.fromtimestamp(int(run_info['end'])/1000)
            start_time = datetime.fromtimestamp(int(run_info['start'])/1000)
        return scenario_status, start_time, end_time
trigger_scenario = get_scenario_by_trigger_name(trigger_project_key, trigger_name)
print(get_status_and_last_run_date(trigger_scenario))
...
Hi, Please note that your code will work only if you have unique trigger names within the project's scenario, or if you have only one scenario per project. We have logged your feature request to pass the information of the current scenario id from our API.
1,194 questions
1,226 answers
1,379 comments
11,754 users

┬ęDataiku 2012-2018 - Privacy Policy