Posts Tagged ‘DekiScript’

08 Feb 11

DekiScript Page Profiler

speedy-gonzales-300x180

One of the exciting new features in the next major release (code-named Pipestone), is the DekiScript Page Profiler.  I’m confident this new feature will delight many DekiScript developers.

In the past, it has been a hassle to figure what part of a complicated, dynamically rendered page requires the most attention.  Often, the only method was to comment out sections one by one to determine the offending line or page inclusion.  This is now a thing of the past.  The page profiler provides a detailed view of every single DekiScript function called and every page included.  It’s your backstage pass to see exactly what’s going on!

The profiler output is split into 4 summaries:

  1. Rendered Content lists all the pages that were rendered and what functions were invoked on each page.  Function invocations are grouped together by location to show what each line is doing.  Additionally, each function includes an invocation count and total elapsed time.
  2. Function Summary lists all functions invoked across all pages with their total elapsed time, total invocation count, average time, and max time.
  3. Page Summary lists all pages with their total rendering time, inclusion count, average time, and max time.
  4. Data Access Summary lists how many database queries were issued and the performance of the optional cache.

Obtaining the page profiler report requires directly accessing the API, a task that shouldn’t be a hurdleto DekiScript developers.

Here is how you get your page profiler XML document:

  1. Identify the page ID of the page you want to profile.  The easiest is to look at the page source and look for the line that says:
    Deki.PageId = 123;
  2. The page meta-data for any page can be accessed by its ID:
    http://mysite/@api/deki/pages/123
  3. Now append /contents/explain and you’ll get the page profiler report:
    http://mysite/@api/deki/pages/123/contents/explain

Depending on how many operations your page performs, the returned XML can become fairly large, but that’s because it contains lots of details.  I would recommend looking at the function and page summaries first to get a sense for the potential culprits and use that information to track down the actual call that is causing the slowdown.

Finally, if your site your users report from sporadic slowdowns that you can’t reproduce, you can add this configuration key to have the profiler log its output when a page takes more than 3 seconds to render:

stats/slow-page-render-alert = 3.0

This will make it easier to capture those elusive moments where for some reason a page takes unusually long to render.

And remember when optimizing your pages: the fastest code is the one that never needs to run!

13 Jul 10

What’s New in DekiScript for Olympic

There is a lot of great new technology and capabilities in Olympic.  I and the rest of the team couldn’t be prouder of this release!  Beyond all the glitz and shiny newness that is getting all the attention is also an improved DekiScript engine with new capabilities.

Variable Scopes

For Olympic, we decided to make a change in how variables are scoped.  Pre-Olympic, variables have been scoped to their innermost defining context and therefore could shadow other variables without affecting them.  This is the scoping rule used by C#, Java, and many other programming languages.  JavaScript, on the other hand, scopes variables to the innermost function definition.  This design greatly simplifies the runtime in implementation and overhead.  After review, we decided to depart from our previous design and follow JavaScript’s lead.  Concretely, here is an example of what code will be affected by this change.

var x = 1;
if(condition) {
    var x = 2;
}
x;

Pre-Olympic, the above code will print 1, post-Olympic it prints 2 since the inner ‘x’ is not shadowing anymore.  The most positive impact of this change is that it is now possible to define variables at the beginning of a page and then refer to them anywhere in the body of the page following their definition.  This has been a much requested feature and I’m happy that it’s finally available!

Return Statement

Another part of the DekiScript runtime that has received significant attention is how output is accumulated and computed into a final value.  Not only is the new output buffer implementation much faster and cleaner, it also enabled the proper implementation of the semantics of the return statement, another much requested feature.

The return statement aborts the currently executing DekiScript block and produces an immediate value as output.  To some degree, the following three statements are identical.

1;
2;
return;
1;
return 2;
return (1; 2);

In all cases, the output will be 12.  In the first case, this output is produced by outputting 1 followed by 2.  In the second case, 1 is outputted and then 2 is appended by the return statement.  In the last case, nothing it outputted until the return statement appends the result of the compound expression.

However, there are cases where the output is quite different!  Consider the following three statements.

1;
<span>
    2;
    return;
</span>
1;
<span>
    return 2;
</span>
<span>
    return (1; 2;)
</span>

In the first case, only 1 is output.  More on that later.  In the second and third case, it will output 12 as well.  In all cases, the <span> element was ignored.  The reason for this is that the <span> element was not closed when the return statement was encountered and since partial XML is invalid, the entire XML block and its contents have been discarded.  That’s why in the first case, both the <span> and the inner 2 element have been omitted.  For the other two cases, the return statement ensures that the returned expression is appended to the output buffer before execution finishes for the current DekiScript block.  And hence, the expression is appended to the output that has successfully been accumulated so far.

You may wonder “why is this behavior useful?”  The answer is fairly simple: imagine you’re computing a table output, but you want to abort the entire table if an invalid value is found.  You have two choices.  Your first choice is to loop over each item in the table beforehand and validate it.  If validation fails, you show the error message. Alternatively, you have a way to abort outputting whatever you were currently computing and return an alternative result instead.  The latter case is exactly what the return statement enables you to do.  For example, the following code will do just that:

<table>
    foreach(var row in table) {
        <tr>
            foreach(var valuein row) {
                <td>
                    if(value is nil) {
                        return <strong> "A value was not set!" </strong>;
                    }
                    value;
                </td>
            }
        </tr>
    }
</table>

Try..Catch..Finally Statement

In addition to the return statement, we’ve also added the try..catch..finally statement.  It works pretty much as in any other language.  If an error occurs inside the try block, the catch block will be evaluated, otherwise the catch block is ignored.  Regardless, the finally block is always evaluated.

Since DekiScript is an expression-oriented language, we also added the !! operator to catch errors during expression evaluation.  Therefore, the following two statements are identical.

var x;
try {
    let x = f(123);
} catch {
    let x = 456;
}
var x = f(123) !! 456;

Triple-Quoted Strings

Ever found yourself in a pickle because you needed to create a string that contained both single- and double-quotes?  Up until now, you had to escape the offending character or experience parser errors.

For Olympic, we’ve added support for triple-quoted strings.  You can either use a single- or double-quote to create a triple-quoted string.  Unless you need the same triple-quotes again inside the string, you won’t need to worry about escaping anything.  Thus, the following strings are identical.

"The ‘quick’ brown fox, jumped over the \"lazy\" dog."
"""The ‘quick’ brown fox, jumped over the "lazy" dog."""

Triple-quoted strings can be used anywhere were regular strings are allowed, including for XML attributes.

Native JEM & CSS Blocks

Another improvement is the support for JEM and CSS blocks in the editor.  Just select either kind from the ‘Insert’ menu in the editor toolbar.  There is no need to encode these anymore in DekiScript blocks.  However, as before, you’ll need UNSAFECONTENT permission for JEM or CSS to be included in the page.

Conclusion

Olympic includes the best DekiScript engine we have ever shipped.  Not only does it add some great new features to the built-in scripting language that makes MindTouch so appealing, but behind the scenes, it’s new architecture is also the foundation for continued improvements in capabilities and performance.

I’m looking forward to seeing how you and the rest of the growing MindTouch community will take advantage of it!

18 Sep 09

Using tags to make powerful apps

Over the course of the last eight months or so I’ve been working quite diligently on the DekiScript “modules” that power WhoRunsGov.com.  I have learned quite a lot about implementing largly DekiScript powered projects and I am going to share some examples of how I used tags to develop very efficient reusable DekiScript templates.

The Use Case

WhoRunsGov.com is a user generated site focused on political profiles from all facets of the government.  With well over 700 profiles they are rapidly growing their content base and have successfully utilized DekiScript to offer a fluid and intuitive form of navigation.  Their solution includes a large number of “browse by” pages that display a list political profiles based on specified tags.   To make this more challenging there are about 150 different browse by pages so  I needed to come up with a solution that was both flexible and manageable.

The Solution

To solve this issue I created one centralized DekiScript template called ProfileList.  The ProfileList template is quite complex and is responsible for:

  • selecting the profiles
  • retrieving the profile content
  • identifying the most recently uploaded profile image
  • resizing it to fit the profile

In addition to these functions I also gave the template a number of different parameters.

  • $searchquery
  • $sortby
  • $limit

Template DekiScript

By adding these parameters I was able to make this template flexible enough to be called from all 150 browse by pages.  You can see that the code below uses the parameters to build a uri for the search API.   Using the search API is the fastest and most resource friendly approach to retrieving a set of pages.  Also, because lucene is so powerful you can filter the results with extreme control.

<h1>Template:ProfileList</h1>
{{
var qsortby = $sortby ?? '-tag:id-*';
var qlimit = $limit ?? 100; //

var results = web.xml(uri.build(site.api, [ 'site', 'search' ], { limit: qlimit, sortby: qsortby, format: 'search', q: $searchquery }), _, _, 900);
}}

‘Browse By’ DekiScript

When calling this template I simply passed in the $searchquery and the page displayed the appropriate profiles.

{{
profilelist{searchquery:'tag:"Governor" AND tag:id-*'}
}}

As you can see this searchquery is identifying pages that are tagged with ‘Governor’ and pages that are tagged with a tag that starts with ‘id-’.    Here are some more examples of how I used the ProfileList template for more advanced searches.

{{
profilelist{searchquery:'(tag:"administration official" OR tag:"obama administration official") AND tag:[id-a* TO id-i*]'}
}}

This query returns all pages tagged with ‘administration official’  or ‘obama administration official’ that are between A and I.

{{
profilelist{searchquery:'tag:"House member" AND tag:democrat* AND tag:congress AND tag:id-*'}
}}

This query returns all profiles that are tagged with ‘House member’, ‘congress’ and that also have a tag that starts with ‘democrat’.

Thanks for reading,

Damien Howley
@DamienH

01 Jul 09

The Rise of the MindTouch Apps

I recently wrote an in-depth technical FAQ article on how to use jQuery AJAX to access the MindTouch API.  The article was first posted on the forums for technical review and is now ready to be shared on the blog.  I’m also happy to report that it has already inspired a few community members to experiment on their own and share their newly gained insights.

jQuery is an extremely popular JavaScript library that is most famous for how easy it makes modifying page contents dynamically.  Many effects, such as hiding or showing elements, moving elements around, highlighting and so on are all easily accomplished with jQuery.  However, jQuery also provides some neat AJAX functionality.  Combined with JEM, which was introduced in MindTouch 2009, makes it incredibly easy to start building Rich Internet Application (RIAs) directly inside your wiki pages.  Though, you will need the UNSAFECONTENT permission to execute JEM.

JEM (JavaScript with Events and Messages) is a pre-processor that makes it create rich interactive, event- and message-driven code that is easily meshed with DekiScript.  Sounds confusing doesn’t it?  It might be at first, but the net result is that you can create some neat things with fairly little code.  The TODO template was one of our first proof-of-concept apps and has become quite popular.  It is a great example of well JEM and DekiScript play together to create simple and effective productivity apps.  Since then, the community has been busily at work combining the puzzle pieces into some pretty apps.  For example, neilw built a collapsible-tree template, and blakeh built a bug-tracker and a dynamic data-table template.

Over the coming months, we will continue to build new templates, mature existing ones, and also help community members like yourself create their own.  My goal is to reach critical mass to launch an open app gallery where we can feature popular apps and where new ones can easily be shared.  In the meantime, head over to the forums, join the fray, and participate in the rise of the MindTouch apps! ;)

05 May 09

Tutorial Video: Creating Charts with MindTouch

sgb_506.png

The other day someone asked about how to create charts using Visifire. I thought about just answering directly in the forums, but then it occurred to me that a tutorial video would be much more powerful.

The tutorial covers…

  • what is needed to get started,
  • how to design a chart,
  • how to feed dynamic data to the chart,
  • and how to make the chart interactive.

Click on the video thumbnail to jump directly to video on the Visifire documentation page. Or visit our MindTouch Facebook page where the video is hosted.

Let me know if the video was helpful, how we could have done it better, and what other topics you’d like to see covered in the future.

Thanks & happy charting! ;)

- Steve

Copyright © 2011 MindTouch, Inc. Powered by