[sr-dev] git:master: modules/usrloc: rpc statistics command update

Juha Heinanen jh at tutpro.com
Sat Jun 1 16:50:43 CEST 2013


Module: sip-router
Branch: master
Commit: 7cb062ce8eb9935b69298b61aa6a59450b97dbaf
URL:    http://git.sip-router.org/cgi-bin/gitweb.cgi/sip-router/?a=commit;h=7cb062ce8eb9935b69298b61aa6a59450b97dbaf

Author: Juha Heinanen <jh at tutpro.com>
Committer: Juha Heinanen <jh at tutpro.com>
Date:   Sat Jun  1 17:47:58 2013 +0300

modules/usrloc: rpc statistics command update

- ul.db_users and ul.db_contacts now deal with unexpired records only
- new command ul.db_expired_contacts tells number of expired contacts

---

 modules/usrloc/README               |   16 ++++++++-
 modules/usrloc/doc/usrloc_admin.xml |   19 ++++++++++-
 modules/usrloc/ul_rpc.c             |   61 ++++++++++++++++++++++++++++++++--
 3 files changed, 88 insertions(+), 8 deletions(-)

diff --git a/modules/usrloc/README b/modules/usrloc/README
index 79448c3..6b48425 100644
--- a/modules/usrloc/README
+++ b/modules/usrloc/README
@@ -89,6 +89,7 @@ Bogdan-Andrei Iancu
               6.6. ul.add
               6.7. ul.db_users
               6.8. ul.db_contacts
+              6.9. ul.db_expired_contacts
 
         7. Statistics
 
@@ -225,6 +226,7 @@ Chapter 1. Admin Guide
         6.6. ul.add
         6.7. ul.db_users
         6.8. ul.db_contacts
+        6.9. ul.db_expired_contacts
 
    7. Statistics
 
@@ -842,6 +844,7 @@ modparam("usrloc", "db_ops_ruid", 1)
    6.6. ul.add
    6.7. ul.db_users
    6.8. ul.db_contacts
+   6.9. ul.db_expired_contacts
 
 6.1.  ul.dump
 
@@ -900,7 +903,8 @@ modparam("usrloc", "db_ops_ruid", 1)
 
 6.7.  ul.db_users
 
-   Tell number of different users (AoRs) in a location table.
+   Tell number of different users (AoRs) in a location table that have
+   unexpired contacts.
 
    Parameters:
      * table name - location table where the users are looked for, for
@@ -908,7 +912,15 @@ modparam("usrloc", "db_ops_ruid", 1)
 
 6.8.  ul.db_contacts
 
-   Tell number of contacts in a location table.
+   Tell number of unexpired contacts in a location table.
+
+   Parameters:
+     * table name - location table where the contacts are looked for, for
+       example, location.
+
+6.9.  ul.db_expired_contacts
+
+   Tell number of expired contacts in a location table.
 
    Parameters:
      * table name - location table where the contacts are looked for, for
diff --git a/modules/usrloc/doc/usrloc_admin.xml b/modules/usrloc/doc/usrloc_admin.xml
index 8720471..21e0c87 100644
--- a/modules/usrloc/doc/usrloc_admin.xml
+++ b/modules/usrloc/doc/usrloc_admin.xml
@@ -1163,7 +1163,7 @@ modparam("usrloc", "db_ops_ruid", 1)
 		<function moreinfo="none">ul.db_users</function>
 		</title>
 		<para>
-		Tell number of different users (AoRs) in a location table.
+		Tell number of different users (AoRs) in a location table that have unexpired contacts.
 		</para>
 		<para>Parameters: </para>
 		<itemizedlist>
@@ -1178,7 +1178,22 @@ modparam("usrloc", "db_ops_ruid", 1)
 		<function moreinfo="none">ul.db_contacts</function>
 		</title>
 		<para>
-		Tell number of contacts in a location table.
+		Tell number of unexpired contacts in a location table.
+		</para>
+		<para>Parameters: </para>
+		<itemizedlist>
+			<listitem><para>
+				<emphasis>table name</emphasis> - location table where the contacts are looked for, for example, location.
+			</para></listitem>
+		</itemizedlist>
+	</section>
+
+	<section>
+		<title>
+		<function moreinfo="none">ul.db_expired_contacts</function>
+		</title>
+		<para>
+		Tell number of expired contacts in a location table.
 		</para>
 		<para>Parameters: </para>
 		<itemizedlist>
diff --git a/modules/usrloc/ul_rpc.c b/modules/usrloc/ul_rpc.c
index 216d326..077b035 100644
--- a/modules/usrloc/ul_rpc.c
+++ b/modules/usrloc/ul_rpc.c
@@ -673,7 +673,7 @@ static void ul_rpc_db_users(rpc_t* rpc, void* ctx)
 	
     memset(query, 0, QUERY_LEN);
     query_str.len = snprintf(query, QUERY_LEN,
-			     "SELECT COUNT(DISTINCT %.*s, %.*s) FROM %.*s",
+			     "SELECT COUNT(DISTINCT %.*s, %.*s) FROM %.*s WHERE (UNIX_TIMESTAMP(expires) = 0) OR (expires > NOW())",
 			     user_col.len, user_col.s,
 			     domain_col.len, domain_col.s,
 			     table.len, table.s);
@@ -690,7 +690,7 @@ static void ul_rpc_db_users(rpc_t* rpc, void* ctx)
 }
 
 static const char* ul_rpc_db_users_doc[2] = {
-	"Tell number of different users (AoRs) in database table (db_mode!=0 only)",
+	"Tell number of different unexpired users (AoRs) in database table (db_mode!=0 only)",
 	0
 };
 
@@ -727,7 +727,7 @@ static void ul_rpc_db_contacts(rpc_t* rpc, void* ctx)
     }
 	
     memset(query, 0, QUERY_LEN);
-    query_str.len = snprintf(query, QUERY_LEN, "SELECT COUNT(*) FROM %.*s",
+    query_str.len = snprintf(query, QUERY_LEN, "SELECT COUNT(*) FROM %.*s WHERE (UNIX_TIMESTAMP(expires) = 0) OR (expires > NOW())",
 			     table.len, table.s);
     query_str.s = query;
     if (ul_dbf.raw_query(ul_dbh, &query_str, &res) < 0) {
@@ -742,7 +742,59 @@ static void ul_rpc_db_contacts(rpc_t* rpc, void* ctx)
 }
 
 static const char* ul_rpc_db_contacts_doc[2] = {
-	"Tell number of contacts in database table (db_mode=3 only)",
+	"Tell number of unexpired contacts in database table (db_mode=3 only)",
+	0
+};
+
+static void ul_rpc_db_expired_contacts(rpc_t* rpc, void* ctx)
+{
+    str table = {0, 0};
+    char query[QUERY_LEN];
+    str query_str;
+    db1_res_t* res;
+    int count;
+
+    if (db_mode == NO_DB) {
+	rpc->fault(ctx, 500, "Command is not supported in db_mode=0");
+	return;
+    }
+
+    if (rpc->scan(ctx, "S", &table) != 1) {
+	rpc->fault(ctx, 500, "Not enough parameters (table to lookup)");
+	return;
+    }
+
+    if (table.len + 22 > QUERY_LEN) {
+	rpc->fault(ctx, 500, "Too long database query");
+	return;
+    }
+
+    if (!DB_CAPABILITY(ul_dbf, DB_CAP_RAW_QUERY)) {
+	rpc->fault(ctx, 500, "Database does not support raw queries");
+	return;
+    }
+    if (ul_dbf.use_table(ul_dbh, &table) < 0) {
+	rpc->fault(ctx, 500, "Failed to use table");
+	return;
+    }
+	
+    memset(query, 0, QUERY_LEN);
+    query_str.len = snprintf(query, QUERY_LEN, "SELECT COUNT(*) FROM %.*s WHERE (UNIX_TIMESTAMP(expires) > 0) AND (expires <= NOW())",
+			     table.len, table.s);
+    query_str.s = query;
+    if (ul_dbf.raw_query(ul_dbh, &query_str, &res) < 0) {
+	rpc->fault(ctx, 500, "Failed to query contact count");
+	return;
+    }
+
+    count = (int)VAL_INT(ROW_VALUES(RES_ROWS(res)));
+    ul_dbf.free_result(ul_dbh, res);
+
+    rpc->add(ctx, "d", count);
+}
+
+static const char* ul_rpc_db_expired_contacts_doc[2] = {
+	"Tell number of expired contacts in database table (db_mode=3 only)",
 	0
 };
 
@@ -755,6 +807,7 @@ rpc_export_t ul_rpc[] = {
 	{"ul.add", ul_rpc_add, ul_rpc_add_doc, 0},
 	{"ul.db_users", ul_rpc_db_users, ul_rpc_db_users_doc, 0},
 	{"ul.db_contacts", ul_rpc_db_contacts, ul_rpc_db_contacts_doc, 0},
+	{"ul.db_expired_contacts", ul_rpc_db_expired_contacts, ul_rpc_db_expired_contacts_doc, 0},
 	{0, 0, 0, 0}
 };
 




More information about the sr-dev mailing list