Want to get rid of Google Ads, click here.
+ Reply to Thread
Results 1 to 14 of 14

Thread: Count in javascript

  1. #1
    Administrator tommy's Avatar
    Join Date
    Nov 2001
    Location
    Copenhagen
    Posts
    4,272

    Default Count in javascript

    Does anybody have an efficient way of counting records which does not involve counting each record one by one ?
    Best regards Tommy
    Blog - - ITIL certified - Accredited Integration Specialist – HP OpenView Service Management

    Want to keep this site alive? Consider making a donation. Click here.

  2. #2
    Senior Member
    Join Date
    Jan 2002
    Location
    The Netherlands
    Posts
    930

    Default

    It doesn't exist (yet).

    See also the SM7 context.GenerateCount.

  3. #3
    Administrator tommy's Avatar
    Join Date
    Nov 2001
    Location
    Copenhagen
    Posts
    4,272

    Default

    I found a way to do it with rtecall count.
    Best regards Tommy
    Blog - - ITIL certified - Accredited Integration Specialist – HP OpenView Service Management

    Want to keep this site alive? Consider making a donation. Click here.

  4. #4
    Senior Member oscarferreira1's Avatar
    Join Date
    Nov 2007
    Location
    Portugal
    Posts
    272

    Default

    can you show us how you use rtecount in javascript??
    I can't get mine to work

    ex: rc1 = system.functions.rtecall("count",rc,filedoselected ,query,returnCount);

    returnCount never gets affected.
    Best Regards,
    Óscar Ferreira
    Certified IT Service Management ITIL v2 & v3 (EXIN)
    Novabase

  5. #5
    Senior Member
    Join Date
    May 2009
    Location
    Philadelphia, PA
    Posts
    176

    Default

    You need to initialize your returnCount variable as an SCFile() type data.

    Add this before your rtecall:


    var returnCount = new SCFile()

  6. #6
    Junior Member
    Join Date
    May 2008
    Posts
    24

    Default

    You will also need to pass the count as an SCDatum so that the rtecall can recognize it. After the rtecall, you will need to then extract the information from the complex object:

    Code:
    var inc = new SCFile( "probsummary" );
    
    inc.doSelect( 'true' );
    
    var c = new SCDatum();
    var rc = new SCDatum();
    
    var v = system.functions.rtecall( "count", rc, inc, "true", c );
    
    print( c.getText() );
    HTH.

  7. #7
    Junior Member brvno's Avatar
    Join Date
    Jul 2008
    Location
    Slovakia
    Posts
    7

    Default

    there is my solution:
    query is up to you
    Code:
    function count( query )
    {
         var count = 0;
         var attch = new SCFile("SYSATTACHMENTS");
         var attchRec = attch.doSelect(query);
    
         if ( attchRec == RC_SUCCESS)
              do
              {
              count++;
              }
         while (attch.getNext() == RC_SUCCESS);
    
         //print("count= "+count);
    
    return count
    }
    "If" is not necessary but it working well

  8. #8
    Junior Member
    Join Date
    Oct 2007
    Location
    Chicago
    Posts
    12

    Default

    brvno, the only issue with this is that you end up iterating through the entire resultset to determine the count...a very costly operation.

    Quote Originally Posted by brvno View Post
    there is my solution:
    query is up to you
    Code:
    function count( query )
    {
    var count = 0;
    var attch = new SCFile("SYSATTACHMENTS");
    var attchRec = attch.doSelect(query);
    
    if ( attchRec == RC_SUCCESS)
    do
    {
    count++;
    }
    while (attch.getNext() == RC_SUCCESS);
    
    //print("count= "+count);
    
    return count
    }
    "If" is not necessary but it working well

  9. #9
    Junior Member brvno's Avatar
    Join Date
    Jul 2008
    Location
    Slovakia
    Posts
    7

    Default

    it used to count attachments as is declared, and it is usually 0-3, not 11000.
    Do you thing that switching between javascript - RAD - javascript does not take time?

  10. #10
    Administrator tommy's Avatar
    Join Date
    Nov 2001
    Location
    Copenhagen
    Posts
    4,272

    Default

    Quote Originally Posted by brvno View Post
    it used to count attachments as is declared, and it is usually 0-3, not 11000.
    Do you thing that switching between javascript - RAD - javascript does not take time?
    As was said counting like that requires scan of all the records (I have done the same mistake too).

    Also as a count of attachments that script is wrong because one record in SYSATTACHMENTS does not necesarely equal one attachment! One attachment can consist of 1 to many records in that file.
    Best regards Tommy
    Blog - - ITIL certified - Accredited Integration Specialist – HP OpenView Service Management

    Want to keep this site alive? Consider making a donation. Click here.

  11. #11
    Senior Member pfilaretov's Avatar
    Join Date
    Jan 2009
    Location
    St.-Petersburg, Russia
    Posts
    112

    Default

    Thank you all guys! I'm looking for the way of counting records without iterating for 2 years!
    Some test info for 2500 records:
    iterating: 6.42 seconds
    rtecall: 0.04 seconds

  12. #12
    Member
    Join Date
    Jun 2010
    Location
    India
    Posts
    54

    Default

    Hi pfilaretov,

    can you tell us how to calculate the time/seconds to perform the rquery ?

    thanks,

  13. #13
    Senior Member pfilaretov's Avatar
    Join Date
    Jan 2009
    Location
    St.-Petersburg, Russia
    Posts
    112

    Default

    Here you are:
    Code:
    /** Get time in with milliseconds
    */
    function getNowTimeString() {
        var now = new Date();
        return now.toLocaleTimeString() + "." + now.getMilliseconds() + "  ";    
    }
    
    /** 
    */
    function testCountSpeed() {
        // "requests" is my own dbdict
        var incFile = new SCFile("requests");
        // To get about 2500 records
        var query = "flag=true";
    
        // RTECALL
        print(getNowTimeString() + "RTECALL - Starting...");
        var returnCount = new SCDatum();
        var rc = new SCDatum();
    
        var rc2 = system.functions.rtecall("count", rc, incFile, query, returnCount);
        print(getNowTimeString() + "RTECALL - Count=" + returnCount.getText());
    
    
        // ITERATING
        print(getNowTimeString() + "ITERATING - Starting...");
        var rc3 = incFile.doSelect(query);
        var i = 0;
        while (rc3 == RC_SUCCESS) {
            i++;
            rc3 = incFile.getNext();
        }
        print(getNowTimeString() + "ITERATING - Count=" + i);
    }
    
    testCountSpeed();

  14. #14
    Administrator tommy's Avatar
    Join Date
    Nov 2001
    Location
    Copenhagen
    Posts
    4,272

    Default

    In 9.30 there is a new way of counting. Sligthly slower than rtecall but still significantly faster than looping and syntax a bit easier to remember that the rtecall.

    Code:
    function countRecords()
    {
    	var fh = new SCFile("contacts");
    	var sql = "true";
    	var i = fh.doCount(sql);
    	print("count: "+i);
    }
    115.000 records counter with this function took 188 milliseconds. Rtecall took 46 milliseconds.
    Best regards Tommy
    Blog - - ITIL certified - Accredited Integration Specialist – HP OpenView Service Management

    Want to keep this site alive? Consider making a donation. Click here.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Tags for this Thread

Bookmarks

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts