Anonymous
Domino 2.0 Rich Internet Applications with IBM Lotus Notes/Domino
You are here: Today » Caching HTML: Tag cloud in LotusScript
« Announcing Domino Workspace
Caching HTML: blog archive list with document counts »

Caching HTML: Tag cloud in LotusScript

I converted my Ajax tag cloud in a LotusScript version. The result is stored in a field on a document and displayed on all pages with a @formula.

From categorized view to tag cloud

I used the stringbuffer class to store the labels and document counts. These are then converted into arrays. After that, it is just a matter of calculating the distribution and generating the HTML:

Function getTagCloud
    On Error Goto catch
    Dim ns As New NotesSession
    Dim view As notesView
    Dim nav As NotesViewNavigator
    Dim entry As NotesViewEntry
    Dim out As New StringBuffer(100)
    Dim labels As New StringBuffer(500)
    Dim counts As New StringBuffer(500)
    Dim i As Long
    Dim href As String,tmp As String,txt As String,clss As String
    Dim txtArray As Variant,countArray As Variant,resArray As Variant
    
    Set view=db.GetView("categories")
    Set nav = view.CreateViewNav
    out.add |<div class="tagcloud"><h4>Tag cloud</h4><ul>|
    Set entry=nav.GetFirst
'collect labels and counts in stringbuffers
    While Not entry Is Nothing
        labels.add entry.ColumnValues(0)
        counts.add entry.ChildCount
        Set entry=nav.GetNextCategory(entry)
    Wend
'convert the stringbuffers to arrays
    txtArray=labels.getArray
    countArray=counts.getArray
    resArray=Distribute(countArray,6)
'build the HTML    
    For i=0 To Ubound(txtArray)-1
        txt=txtArray(i)
        href=dbPath & "categories!Open&amp;Count=-1&amp;RestrictToCategory=" & txt
        tmp=txt & " (" & countArray(i) & | posts)|
        clss="tag" & resArray(i)
        out.add |<li><a href="| & href & |" title="| & tmp & |" class="| & clss & |">| & txt & |</a></li>|
    Next
    out.add |<ul></div>|
    
    Goto finally
catch:
    Error Err, Error & " in " & Getthreadinfo(1) & ", line " & Erl
    Resume finally
finally:
    getTagCloud=out.collapse(Chr(13)&Chr(10))
End Function

Calculating the distribution

This is done by the function Distribute, first parameter is the array of document counts, second the number of different sizes, according to the classes defined in the CSS.

Function Distribute(a As Variant,Byval b As Integer) As Variant
    On Error Goto catch
    Dim n As Integer, i As Integer,q As Integer,q1 As Long, q2 As Long
    Dim max As Integer, min As Integer, avg As Integer,num As Integer
    Dim res As Variant
    
    n=Ubound(a)
    Redim res(n)
    max=0
    min=32767
    For i=0 To n-1
        num=Cint(a(i))
        If num>max Then max=num
        If num<min Then min=num
    Next
    q=max-min
    For i=0 To n-1
        q1=((b-1)/q)*Cint(a(i))
        q2=(1*max-b*min)/q
        res(i)=Cstr(Round(q1+q2,0))
    Next
    
    Goto finally
catch:
    Error Err, Error$ & " in " & Getthreadinfo(1) & ", line " & Erl
    Resume finally
finally:
    Distribute=res
End Function

Rendering the HTML on the page

The tagcloud HTML is stored in a field in a document. A few @formulas in a computed field renders them on each page:

a:=@GetDocField(DbProfileId; "TagCloudHtml");
@If(@IsError(a); @Text(a)+":"+DbProfile; a)

After testing with FireBug, I notice a distinct improvement in replacing the Ajax calls for the tag cloud and the document count on the archive. Just by trimming down the JavaScript and removing the two obsolete Ajax HTTP requests improved the overall performance by about 50 percent. The homepage now only has 5 requests and delivers the total of 18k in 0.27 to 0.36 seconds, measured from the same backbone.

I tested the same two HTML blocks, but computed on the fly in a WebQueryOpen agent without noticing any decrease in performance. Refreshing the cache document in the Notes Client however does cause a very noticable delay, because in this case probably the Agent manager kicks in, while on the web the agent is run from within the HTTP task.

Star rating

0%

Comments

  1. 11/20/2007 03:30:05 PM, Theo Heselmans

    Michel, 2 variables are not defined in the code above: - db, but that's obvious - dppath: what do you put in this ? Useful would also be to put in a link to the correct Stringbuffer class, as I used the wrong one the first time. I will certainly use this code. Tx

  2. 20/11/2007 19:16:54, Michel Van der Meiren

    Hello Theo. I've posted my latest version of the Stringbuffer class. As for DbPath: it contains the short URL defined in a field in my DbSetup document. For this blog, it is: "/domino/"

To add a comment, log in or register as new user. It's free and safe.