can i trigger dag with default values params? - airflow-scheduler

i create dag that calculate yesterdays date and use it in all the dag's tasks.
Is there any way to pass the dag a parameter (in my case a date) and use it in all my operators call ? (i cant change operator's code i can only use it) and if my dag doe'snt get any date from outside - to calculate the yesterdays date and use it in all the flow?
so basically i want to ask if we can trigger the dag manually with parameter and if the dag doesnt get any parameter to use/calculate default value (in my case is to calculate yesterday's date)
need your help :)
ag_args = {
'owner': 'airflow',
'start_date': airflow.utils.dates.days_ago(1),
'email': ['lalala#example.com'],
'email_on_failure': True,
'email_on_retry': False,
'email_on_success': True,
'retries': 1,
}
dag = DAG(
dag_id='example_dag',
default_args=dag_args,
schedule_interval=None
)
t1 = SimpleHttpOperator(
task_id='check_if_daily_report_ready',
method='GET',
endpoint="/bla/bla?date={date}".format(
date=(datetime.now() - timedelta(days=1)).strftime("%Y-%m-%d")),
http_conn_id="conn1",
headers={"Content-Type": "application/json"},
response_check=lambda response: True if response.status_code == 200 else False,
dag=dag,
)
t2 = Queryperator(
task_id='cal',
query_file='ca.sql',
query_folder='include/sql_files/bla',
token='Token',
default_param_dict={"date": (datetime.now() - timedelta(days=1)).strftime("%Y-%m-%d")},
dag=dag
)
export_file_from_S3_to_HDFS = ExportOperator(
task_id='export_file_from_s3_to_hdfs',
source_dict=s3_source_properties,
target_dict='{user}/bla_{yesterday}_bla_raw_data.csv'.format(
user=s3_source_bucket_path,
yesterday=(datetime.now() - timedelta(days=1)).strftime("%Y%m%d"))),
token='Token',
dag=dag
)

To pass a parameter into your dag you can use the method described here.
Then just use a template to define the operator's parameter, I guess the template would be something like this:
'{{ dag_run.conf.get("your_parameter_key", (datetime.now() - timedelta(days=1)).strftime("%Y-%m-%d") }}" '

Related

I want to get data from JavaScript with Django

There is price filtering written in JavaScript in the template. I want to take the price range given in this filter with dajngo and write a filtering function. I couldn't because I don't know JavaScript. How will I do?
So, i want to write a django function that takes the given start and end values and sorts the products accordingly.
main.js
// PRICE SLIDER
var slider = document.getElementById('price-slider');
if (slider) {
noUiSlider.create(slider, {
start: [1, 100],
connect: true,
tooltips: [true, true],
format: {
to: function(value) {
return value.toFixed(2) + '₼';
},
from: function(value) {
return value
}
},
range: {
'min': 1,
'max': 100
}
});
}
I'm not familiar with noUiSlider but you would need to get the from and to values into Django - you can do that either by submitting a form when clicking FILTER or by sending an AJAX request. I presume you would just submit the form in a standard page submission as you aren't familiar with JS (and therefore AJAX).
def your_view(request)
filter_from = request.POST.get('slider_from')
filter_to = request.POST.get('slider_to')
YourModel.objects.filter(value__gte=filter_from, value__lte=filter_to)
...
You will need to replace slider_from and slider_to with the key values that are sent by the slider input in request.POST - this will be the name of the inputs themselves. You can wrap request.POST in a print statement to easily see what these are. It's just a matter of getting the values and passing them into the filter() function of your model.

Airflow won't let me convert try_number macros to an integer

My issue is rather simple, but yet no real solution is really offered. The DAG I am trying to build will have a critical environment variable that might change depending on the number of retries that it goes through. For example, after 3 retries we will switch the value of the environment variable "critical_value" value mentioned in the code below.
So when I push the code below, I get the following broken DAG error:
Broken DAG: [/usr/local/airflow/dags/sync/dags/example_dag.py] invalid literal for int() with base 10: '{{ task_instance.try_number }}'
This is an error we get when we try to convert a string to an int. In this case, at runtime '{{ task_instance.try_number }}' will take the appropriate value of the try number of the run. I have tried also removing the int() converter but then get an error of comparing an integer and a string.
Any help will be immensely appreciated! Thank you in advance.
# Required DAG arguments
default_args = {
'owner':airflow,
"depend_on_past":False,
'start_date':datetime(2021, 6, 4),
"retries": 3
}
# Instantiate DAG
with DAG(
example_dag,
default_args=default_args,
schedule_interval=#once,
) as dag:
# Run python script of loader job
full_script_task = KubernetesPodOperator(
name=example_task,
task_id=example_task_id,
namespace=Variable.get("kubernetes_namespace"),
image=some_image,
configmaps=CONFIG_MAPS,
secrets=SECRETS,
volumes=[logging_volume],
volume_mounts=[logging_volume_mount],
env_vars={
"critical_value": "value_if_true" if int('{{ task_instance.try_number }}') >= 3 else "value_if_false"
}
)
Aiflow can handle the if expression and correct typing evaluation in Jinja but the whole value needs to be in the template syntax. Try this (env_vars needs to be a list based on the documentation of the KubernetesPodOperator otherwise the value will be a stringified list):
env_vars=[
{
"critical_value": "{{ 'value_if_true' if task_instance.try_number >= 3 else 'value_if_false' }}"
}
],
As we confirmed with your test, you cannot use jinja templated variables in an if clause inside the parameter. You tried this and the variable was read properly:
env_vars={
"critical_value": "{{ task_instance.try_number }}"
}
What you can do is to process the try_number inside the operator and instead of acting upon the critical_value, just use the try_number.

Cannot define custom metrics in ray

I'm using a framework called FLOW RL. It enables me to use rllib and ray for my RL algorithm. I have been trying to plot non learning data on tensorboard. Following ray documentation ( link ), I have tried to add custom metrics. Therefore, I need to use the info dict, which is accessed by on_episode_step(info). An "episode" element is supposed to be present in this dictionary. That lets me access to my custom scalars.
However, every time I try to access to the episode element, I get an error because it does not exist in the info dict. Is this normal?
File "examples/rllib/newGreenWaveGrid2.py", line 295, in on_episode_start
episode = info["episode"]
KeyError: 'episode'
def on_episode_step(info):
episode = info["episode"]
whatever = abs(episode.last_observation_for()[2])
episode.user_data["whatever"].append(whatever)
if __name__ == '__main__':
alg_run, gym_name, config = setup_exps()
ray.init(num_cpus=N_CPUS + 1, redirect_output=False)
trials = run_experiments({
flow_params['exp_tag']: {
'run': alg_run,
'env': gym_name,
'config': {
**config,
'callbacks': {
"on_episode_start": on_episode_start,
"on_episode_step": on_episode_step,
"on_episode_end": on_episode_end,
}
},
'checkpoint_freq': 20,
'max_failures': 999,
'stop': {
'training_iteration': 200,
},
},
})

easy-thumbnails: Using the scale_and_crop image processor in a thumbnail alias

I can't figure out the proper format to use in specifying that specs for the scale_and_crop image processor in a thumbnail alias.
I've tried many variations, most recently this one:
'year_thumb': {
'scale_and_crop': (
('size', (120, 0)),
('crop', 'scale'),
('upscale', True),
)
},
Not sure how to get this right.
So, unfortunately I never reconciled the setting of image processor arguments in code vs. in thumbnail alias settings, but here's how I accomplished the same, or similar thing:
'year_thumb': {
'size': (120, 0),
'autocrop': True,
'crop': 'smart',
'upscale': True,
},

Redmine API - Creating Issues

I am trying to use Redmine's API to create issues, but it doesn't seem to let me specify who is submitting the ticket. I have tried getting author_id and it doesn't seem to work.
This is what the JSON data in the POST looks like:
{
"issue": {
"author_id": 3,
"project_id": 26,
"subject": "Q-12345678",
"description": "This is a test"
}
}
The issue is being created, but the author is being set to anonymous. Any suggestions?
Well, I figured it out. As it turns out author_id is not set as a "safe parameter".
I opened up app/models/issue.rb and added author_id on line 337.
So now that block of code looks like this:
safe_attributes 'tracker_id',
'status_id',
'category_id',
'author_id',
'assigned_to_id',
'priority_id',
'fixed_version_id',
'subject',
'description',
'start_date',
'due_date',
'done_ratio',
'estimated_hours',
'custom_field_values',
'custom_fields',
'lock_version',
'notes',
:if => lambda {|issue, user| issue.new_record? || user.allowed_to?(:edit_issues, issue.project) }
EDIT
I think this might be a better solution in app/models/issue.rb (line 375)
After
safe_attributes 'parent_issue_id',
:if => lambda {|issue, user| (issue.new_record? || user.allowed_to?(:edit_issues, issue.project)) &&
user.allowed_to?(:manage_subtasks, issue.project)}
I added
safe_attributes 'author_id',
:if => lambda {|issue, user| issue.new_record? || user.allowed_to?(:admin, issue.project)}