create a variable to see date when model was run with dbt - date

I need to add to my dbt model a column which shows the current date when the model is run.
I have tried {{ run_started_at.strftime("%Y-%m-%d") }} by adding it directly to my model and also have tried on setting a variable called date and creating this piece of code ( {% set report_date = dbt_utils.get_query_results_as_dict("select dateadd(day,-1,current_date()) as col")["COL"][0] %} ) but the problem is that the dates, as we are today at 2021-12-14, the result for this column is 1995 ( this is the substraction of 2021 - 12 - 14). So, does anybody now a way to cast this variable to a date? thanks!

Well I think I know the secret now
Please remember dbt is a tool for code-generating-like
In your case, I guess you must to wrap your jinja code in a string quote
WRONG:
SELECT {{ run_started_at.strftime("%Y-%m-%d") }} as YourColumn
--compiled: SELECT 2021-12-14 as YourColumn
--YourColumn=1995
CORRECT:
SELECT '{{ run_started_at.strftime("%Y-%m-%d") }}' as YourColumn
--compiled: SELECT '2021-12-14' as YourColumn
--YourColumn=2021-12-14

Related

CSV Data in jekyll: how to sort and display data according to "Publication year"

I am trying to work on a simple workflow to sort out my publications csv to a list in my jekyll-based website.
This is my references.csv extracted from referencing software Mendeley and converted from bib to csv using online converter. I want to place this in my _data folder and make a list of publications in the page on jekyll website hosted on github. The sampe data is shown below:
"Item type","Authors","Title","Journal","Publication year","Volume","Issue","Pages","Publisher","Date published","ISSN","URLs","DOI","PMID","Abstract","Keywords"
"Book","Wilden H","PCI design handbook: Precast and prestressed concrete","","2017","","","","Precast/Prestressed Concrete Institute Wakefield, MA, USA","2017","","","","","",""
"Journal Article","Nanni A,Di Ludovico M,Parretti R","Shear strengthening of a PC bridge girder with NSM CFRP rectangular bars","Advances in Structural Engineering","2004","7","4","297-309","SAGE PublicationsSage UK: London, England","2004-11","1369-4332","https://journals.sagepub.com/doi/10.1260/1369433041653570;http://dx.doi.org/10.1260/1369433041653570","10.1260/1369433041653570","","trimmed down abstract","keyword 1"
"Journal Article","Di B,Wang J,Li H,Zheng J,Zheng Y,Song G","Investigation of Bonding Behavior of FRP and Steel Bars in Self-Compacting Concrete Structures Using Acoustic Emission Method","Sensors 2019, Vol. 19, Page 159","2019","19","1","159","Multidisciplinary Digital Publishing Institute","2019-01","1424-8220","http://dx.doi.org/10.3390/S19010159;https://www.ncbi.nlm.nih.gov/pubmed/30621189","10.3390/S19010159","30621189","To extend ","FRP,acoustic emission,bonding,compacting concrete,out test,pull,self"
"Journal Article","Crivelli D,Bland S","Structural health monitoring via acoustic emission","Reinforced Plastics","2016","60","6","390-392","Elsevier Ltd","2016-11","0034-3617","http://dx.doi.org/10.1016/j.repl.2015.05.004","10.1016/j.repl.2015.05.004","","Reinforced Plastics spoke to Dr Davide Crivelli from the Politecnico di Milano about new methods for detecting damage in composites.",""
"Journal Article","Tonelli D,Luchetta M,Rossi F,Migliorino P,Zonta D","Structural health monitoring based on acoustic emissions: Validation on a prestressed concrete bridge tested to failure","Sensors (Switzerland)","2020","20","24","1-20","Multidisciplinary Digital Publishing Institute","2020-12","1424-8220","http://dx.doi.org/10.3390/s20247272;https://www.ncbi.nlm.nih.gov/pubmed/33352961","10.3390/s20247272","33352961","very small abstract","AE"
Now, I want to make the list from these data as a citation data without getting into complexities of _includes. I want to keep it simple in a separate "Publications" page.
For example, I used the following code
{% assign cards = site.data.references | sort: 'Publication_year' | 'reverse' %}
{% for card in cards %}
<ul>
<li>{{ card.Publication year }} {{ card.Title }} {{ card.Publication }} </li>
<ul> {{ card.Authors }} {{ card.Authors }} </ul>
</ul>
{% endfor %}
In the code, when I use the heading in csv as "Year" and then sort using sort: 'Year' | 'reverse' %,it works. Maybe, there is a different way to write it for "two-worded" headings, as I think the card.Publication year won't work. i have spent a lot of time on this and I think I need to just use the code once and then just work with the csv file only.
The output I intend to only pick up the Journal Article from the Item type and prepare a list with the following information with items shown in the following:
[Publication year][title][Journal]
[Authors]
Also, I tried italisizing the text in one of the items, it should work like {{ __card.Publication year__ }} but it does not. I also read somewhere that the csv encoding doesn't allow to extract the first column due to some BOM or UTF-8 something encoding. But I want to filter using the item type.
ps I have tried to compromise with another simplified code with headings same as this, but the google scholar exported csv has ; as delimiters and while using card.Authors does not work to call the data.
I apologize I am not at all familiar with this and I am struggling with using this syntax. However, I foresee that if the code works, it will make my life easier. I would really appreciate your help. I apologize for any lack of proper jargon while explaining the problem.
Thank you in advance.
I tried to display the data from csv data file on the website but some values are missing. I cannot figure out the solution
You're on the right track but you are incorrectly referencing the CSV file column. Since "Publication year" is a two-worded heading, you need to reference it using underscores instead of spaces.
Try changing this line:
{% assign cards = site.data.references | sort: 'Publication_year' | 'reverse' %}
You can use sort_by instead of sort and provide 'Publication_year' as an argument.
{% assign cards = site.data.references | sort_by: 'Publication_year' | reverse %}
When you reference the column in the for loop, you should also use underscores:
<li>{{ card.Publication_year }} {{ card.Title }} {{ card.Journal }} </li>

Grafana Alert Templating

I am struggling to include values into my alerting template
(Using Grafana 9.03 with classic condition alerting)
Query
Alert Evaluation
I tried to use {{ $values.B0 }} which works but only displaying the value. I want to use the Tag "Limit" for example "ImportStatNSC4903"
I did some more tries with:
{{ $values.B0.Limit }}
{{ $values.B0.labels }}
{{ $labels }}
Any ideas what I am doing wrong?
I am struggling to include values into my alerting template. I want a summery that tells me clearly why this alert was triggered
For example:
Limit ImportStatNSC4903 is outside the defined Boundaries

Symfony twig get data value from collection object

Hello,
when editing a form with obj-values from a collection in Symfony 4.2, i need each collection-objects ID for frontend stuff.
If I dump a collection object,
{{ dump(form.vars.data) }}
I can see something like this:
CollectionObj1 {#3341 ▼
-id: 21167
-value1: null
-value2: 74
If I now want to access to the id with
{{ dump(form.vars.data.id) }}
I get the following error:
Impossible to access an attribute ("id") on a null variable.
Can somebody tell me, how I can access to the id of the object in the collection?
Thanks very much in advance
Okay, two things I did not mention lead to the error:
My id is not called id. Due to an old database it is called someting like this: K_RESULTS_ID
I am using symfony's collection prototypes: https://symfony.com/doc/current/form/form_collections.html#allowing-new-tags-with-the-prototype and called the dump in the respective form-block
The first error was that you have to call such an id-name without underscores, it should be called like that:
{{ dump(form.vars.data.KRESULTSID) }}
The second one was, that I do have to check, if the object is not null, before fetching it. So I needed to add something like that:
{% if form.vars.data is not null %}
{% set resultId = form.vars.data.KRESULTSID%}
{% else %}
Now it works like expected.
Thanks anyway, #wp78de and sorry for not posting the full truth ;)

Jekyll on Github suddenly do not work because of 'nil' date

Well, I swear I changed no setting at all.
I setup my Jekyll blog, and it did work before.
Every time I want to write something new, I just copy and old post file, rename it to the current date and modify the content within it.
But just one day, after I push my new content to Github, I received an email, noticing me that
The page build failed for the master branch with the following error:
The value 'nil' was passed to a date-related filter that expects valid dates in /_layouts/post.html or one of its layouts. For more information, see https://help.github.com/articles/page-build-failed-date-is-not-a-valid-datetime/.
For information on troubleshooting Jekyll see:
https://help.github.com/articles/troubleshooting-jekyll-builds
First, I did not modify /_layouts/post.html. Second, I do not use date info in YAML, because Jekyll already uses the strong name requirement like YY-MM-DD-title.md.
I am just some guy who wants to write something fun but knows little about IT tech. I searched the Jekyll doc, and read the post on StackOverflow like below:
Invalid date while building on Github Pages
But as my post.html file content is different from that post, I do not know what I can do.
I built it on my Mac with bundle exec Jekyll build, it just shows the same information as the email
$ bundle exec jekyll build
Configuration file: > /Users/zhanglidong/Documents/ASyncFiles/jetorz.github.io/_config.yml
Source: /Users/zhanglidong/Documents/ASyncFiles/jetorz.github.io
Destination: > /Users/zhanglidong/Documents/ASyncFiles/jetorz.github.io/_site
Incremental build: disabled. Enable with --incremental
Generating...
Liquid Exception: Invalid Date: 'nil' is not a valid datetime. in /_layouts/post.html
ERROR: YOUR SITE COULD NOT BE BUILT:
------------------------------------
Invalid Date: 'nil' is not a valid datetime.
My GitHub repo: https://github.com/jetorz/jetorz.github.io
My jekyll blog: https://jetorz.github.io/
How can I fix it? Any one who wants to help the pity me? :(
Like answer above suggested,
I rechecked the link I provided:
Invalid date while building on Github Pages
There is and if block outside the <time> part.
I do not know much about Liquid,
But I think it worth trying.
So I add this if to my post.html like below
{% if page.date %}
<time datetime="{{ page.date | date_to_xmlschema }}" itemprop="datePublished">
{% assign date_format = site.minima.date_format | default: "%b %-d, %Y" %}
{{ page.date | date: date_format }}
</time>
{% endif %}
I do not know exactly what it is about,
but It does work :)
Now again I can post my blog.

Displaying a DateInterval in Twig

I'm trying to display a DateInterval in Twig using the following code:
{{ event.endTime.diff(event.startTime)|date("i's''") }}
where event is an Entity who get 2 DateTime object: endTime and startTime. With that command I've got this output:
i's''
Instead of min'sec'' like 08'15''
It is said in the date doc that
The date filter accepts [...] DateInterval instances
This work to display min and sec from a date object.
Note that doing: {{ (event.endTime.diff(event.startTime))|date("i's''") }} doesn't change anything
I also tried {{ date(event.endTime.diff(event.startTime))|date("i's''") }} but this lead to an exception Object of class DateInterval could not be converted to string
I've also seen time_diff from Twig Extensions but this return a string (in or ago) instead of a Date object, then I can't display it as I want.
Let me know if you need more informations. Thank you for your help.
As #Yoshi said:
(new \DateTime('tomorrow'))->diff(new \DateTime('now'))->format("i's''") also gives "i's''", so I'd say this is not a twig-thing. From the manual:
"The following characters are recognized in the format parameter
string. Each format character must be prefixed by a percent sign (%)."
So to fix my proble I just had to do:
{{ event.endTime.diff(event.startTime)|date("%i'%s''") }}
When putting in the method format the parameters 'i' and 's' in lowercase, when the value to be displayed is less than 10, put a single digit.
Example if you need to display the time of 5 minutes and 2 seconds by showing a leading zero (05:02). You have to capitalize the parameters, it would be like this:
{{ event.endTime.diff(event.startTime)|date("%I:%S") }}
The allowed parameters can be found in: DateInterval format