Extending homer

35 views
Skip to first unread message

David Villasmil

unread,
Dec 18, 2016, 9:43:16 AM12/18/16
to Homer Developers
Hello,

I'm working on extending Homer to create stats (ASR) for specified components, not just global.
That's all done, but when I want to chart it, I'm returning what I think is a correct response, but it's not graphed (it's a new Statistic.php/datasource.js option)

Help is very much appreciated!

This is what I'm returning:

{
    "status": 200,
    "sid": "oljnn2ufq21p5e5pmqovbok715",
    "auth": "true",
    "message": "ok",
    "data": [
        {
            "id": "146582",
            "component": "1.2.3.4",
            "total": "38",
            "from_ts": "1482067200",
            "to_ts": "1482067500"
        },
        {
            "id": "146574",
            "component": "1.2.3.4",
            "total": "40",
            "from_ts": "1482066900",
            "to_ts": "1482067200"
        },
        {
            "id": "146566",
            "component": "1.2.3.4",
            "total": "41",
            "from_ts": "1482066600",
            "to_ts": "1482066900"
        },
        {
            "id": "146559",
            "component": "1.2.3.4",
            "total": "37",
            "from_ts": "1482066300",
            "to_ts": "1482066600"
        },
        {
            "id": "146551",
            "component": "1.2.3.4",
            "total": "37",
            "from_ts": "1482066000",
            "to_ts": "1482066300"
        },
        {
            "id": "146543",
            "component": "1.2.3.4",
            "total": "40",
            "from_ts": "1482065700",
            "to_ts": "1482066000"
        },
        {
            "id": "146539",
            "component": "1.2.3.4",
            "total": "37",
            "from_ts": "1482065400",
            "to_ts": "1482065700"
        },
        {
            "id": "146528",
            "component": "1.2.3.4",
            "total": "41",
            "from_ts": "1482065100",
            "to_ts": "1482065400"
        },
        {
            "id": "146522",
            "component": "1.2.3.4",
            "total": "34",
            "from_ts": "1482064800",
            "to_ts": "1482065100"
        },
        {
            "id": "146510",
            "component": "1.2.3.4",
            "total": "44",
            "from_ts": "1482064500",
            "to_ts": "1482064800"
        },
        {
            "id": "146507",
            "component": "1.2.3.4",
            "total": "41",
            "from_ts": "1482064200",
            "to_ts": "1482064500"
        },
        {
            "id": "146500",
            "component": "1.2.3.4",
            "total": "37",
            "from_ts": "1482063900",
            "to_ts": "1482064200"
        },
        {
            "id": "146489",
            "component": "1.2.3.4",
            "total": "41",
            "from_ts": "1482063600",
            "to_ts": "1482063900"
        },
        {
            "id": "146483",
            "component": "1.2.3.4",
            "total": "40",
            "from_ts": "1482063300",
            "to_ts": "1482063600"
        },
        {
            "id": "146473",
            "component": "1.2.3.4",
            "total": "38",
            "from_ts": "1482063000",
            "to_ts": "1482063300"
        },
        {
            "id": "146464",
            "component": "1.2.3.4",
            "total": "41",
            "from_ts": "1482062700",
            "to_ts": "1482063000"
        },
        {
            "id": "146460",
            "component": "1.2.3.4",
            "total": "41",
            "from_ts": "1482062400",
            "to_ts": "1482062700"
        },
        {
            "id": "146448",
            "component": "1.2.3.4",
            "total": "39",
            "from_ts": "1482062100",
            "to_ts": "1482062400"
        },
        {
            "id": "146439",
            "component": "1.2.3.4",
            "total": "42",
            "from_ts": "1482061800",
            "to_ts": "1482062100"
        },
        {
            "id": "146436",
            "component": "1.2.3.4",
            "total": "39",
            "from_ts": "1482061500",
            "to_ts": "1482061800"
        },
        {
            "id": "146424",
            "component": "1.2.3.4",
            "total": "40",
            "from_ts": "1482061200",
            "to_ts": "1482061500"
        },
        {
            "id": "146415",
            "component": "1.2.3.4",
            "total": "38",
            "from_ts": "1482060900",
            "to_ts": "1482061200"
        },
        {
            "id": "146410",
            "component": "1.2.3.4",
            "total": "39",
            "from_ts": "1482060600",
            "to_ts": "1482060900"
        },
        {
            "id": "146399",
            "component": "1.2.3.4",
            "total": "40",
            "from_ts": "1482060300",
            "to_ts": "1482060600"
        },
        {
            "id": "146391",
            "component": "1.2.3.4",
            "total": "42",
            "from_ts": "1482060000",
            "to_ts": "1482060300"
        },
        {
            "id": "146387",
            "component": "1.2.3.4",
            "total": "38",
            "from_ts": "1482059700",
            "to_ts": "1482060000"
        },
        {
            "id": "146379",
            "component": "1.2.3.4",
            "total": "38",
            "from_ts": "1482059400",
            "to_ts": "1482059700"
        },
        {
            "id": "146367",
            "component": "1.2.3.4",
            "total": "36",
            "from_ts": "1482059100",
            "to_ts": "1482059400"
        },
        {
            "id": "146359",
            "component": "1.2.3.4",
            "total": "39",
            "from_ts": "1482058800",
            "to_ts": "1482059100"
        },
        {
            "id": "146355",
            "component": "1.2.3.4",
            "total": "40",
            "from_ts": "1482058500",
            "to_ts": "1482058800"
        },
        {
            "id": "146343",
            "component": "1.2.3.4",
            "total": "40",
            "from_ts": "1482058200",
            "to_ts": "1482058500"
        },
        {
            "id": "146336",
            "component": "1.2.3.4",
            "total": "40",
            "from_ts": "1482057900",
            "to_ts": "1482058200"
        },
        {
            "id": "146332",
            "component": "1.2.3.4",
            "total": "37",
            "from_ts": "1482057600",
            "to_ts": "1482057900"
        },
        {
            "id": "146321",
            "component": "1.2.3.4",
            "total": "39",
            "from_ts": "1482057300",
            "to_ts": "1482057600"
        },
        {
            "id": "146313",
            "component": "1.2.3.4",
            "total": "41",
            "from_ts": "1482057000",
            "to_ts": "1482057300"
        },
        {
            "id": "146303",
            "component": "1.2.3.4",
            "total": "40",
            "from_ts": "1482056700",
            "to_ts": "1482057000"
        }
    ],
    "count": 36
}

Thanks!!

David Villasmil

unread,
Dec 18, 2016, 10:24:09 AM12/18/16
to Homer Developers
So, to provide more info:

my datasource.js is extended like so:

{
            "name": "Component",
            "type": "JSON",
            "settings": {
                "path": "statistic\/component",
                "query": "{\n   \"timestamp\": {\n          \"from\": \"@from_ts\",\n          \"to\":  \"@to_ts\"\n   },\n  \"param\": {\n        \"filter\": [ \n             \"@filters\"\n       ],\n       \"limit\": \"@limit\",\n       \"total\": \"@total\"\n   }\n}",
                "method": "GET",
                "limit": 200,
                "total": false,
                "eval": {
                    incoming: {
                        name: "test incoming",
                        value: "var object = @incoming; return object"
                    }
                },
                "timefields" : [
                    { "field": "from_ts", "desc": "From Timestamp" },
                    { "field": "to_ts", "desc": "To Timestamp" }
                ],
                "fieldvalues": [
                    { "field": "total", "desc": "Total" }
                ],
                "filters": [
                    { 
                        "type": "ip_address", 
                        "desc": "Info type", 
                        options: [ 
                            { "value": "1.2.3.4" }, 
                            { "value": "6.7.8.9" }  
                        ] 
                    }
                ]
            }
        },

and my Statistic.php:

    /* api/statictic/component */
    public function doStatisticComponent($timestamp, $param){

    /* auth */
        if(count(($adata = $this->getLoggedIn()))) return $adata;

        /* get our DB */
        $db = $this->getContainer('db');
        $db->select_db(DB_STATISTIC);
        $db->dbconnect();
        /* get our DB Abstract Layer */
        $layer = $this->getContainer('layer');

        $data = array();

        $search = array();
        $callwhere = array();
        $calldata = array();
        $arrwhere = "";
        
        foreach($param['filter'] as $key=>$filter) {

            $search[$key]['ip_address'] = getVar('ip_address', NULL, $filter, 'string');
            $callwhere = generateWhere($search[$key], 1, $db, 0);
            if( count($callwhere) ) $calldata[] = "(". implode(" AND ", $callwhere). ")";
        }

        if(count($calldata)) $arrwhere = " AND (". implode(" OR ", $calldata). ")";

        $time['from'] = getVar('from', round((microtime(true) - 300) * 1000), $timestamp, 'long');
        $time['to'] = getVar('to', round(microtime(true) * 1000), $timestamp, 'long');
        $time['from_ts'] = intval($time['from']/1000);
        $time['to_ts'] = intval($time['to']/1000);

        $and_or = getVar('orand', NULL, $param['filter'], 'string');
        $limit = getVar('limit', 500, $param, 'int');
        $total = getVar('total', false, $param, 'bool');

        $layerHelper = array();
        $layerHelper['table'] = array();
        $layerHelper['order'] = array();
        $layerHelper['where'] = array();
        $layerHelper['fields'] = array();
        $layerHelper['values'] = array();
        $layerHelper['table']['base'] = "stats_traffic";
        $layerHelper['where']['type'] = $and_or ? "OR" : "AND";
        $layerHelper['where']['param'] = $calldata;
        $layerHelper['time'] = $time;

        $layerHelper['fields']['ts'] = array();
        $layerHelper['fields']['ts'][0] = array();
        $layerHelper['fields']['ts'][0]['name'] = 'from_date';
        $layerHelper['fields']['ts'][0]['alias'] = 'from_ts';
        $layerHelper['fields']['ts'][1]=array();
        $layerHelper['fields']['ts'][1]['name'] = 'to_date';
        $layerHelper['fields']['ts'][1]['alias'] = 'to_ts';

        $layerHelper['fields']['time'] = "to_date";
        $layerHelper['order']['by'] = "id";
        $layerHelper['order']['type'] = "DESC";
        syslog(LOG_WARNING, "layerHelper: " . print_r( $layerHelper, true ) );
        syslog(LOG_WARNING, "calldata: " . print_r( $calldata, true ) );

if($total)
{
$layerHelper['values'][] = "id, SUM(total) as cnt, ( SUM( IF(type='connect',total,0) ) / sum(total) ) * 100 as total";
$layerHelper['group']['by'] = "id,from_ts,to_ts";
}
else
{
$layerHelper['values'][] = "id, ip_address, CAST( ( SUM( IF(type='connect',total,0) ) / sum(total) ) * 100 as UNSIGNED ) as total";
$layerHelper['group']['by'] = "from_ts, to_ts, ip_address";
        }

        $query = $layer->querySearchData($layerHelper);
        $data = $db->loadObjectArray($query);

        /* sorting */
        //usort($data, create_function('$a, $b', 'return $a["micro_ts"] > $b["micro_ts"] ? 1 : -1;'));

        $answer = array();

        if(empty($data)) {

                $answer['sid'] = session_id();
                $answer['auth'] = 'true';
                $answer['status'] = 200;
                $answer['message'] = 'no data';
                $answer['data'] = $data;
                $answer['count'] = count($data);
        }
        else {
                $answer['status'] = 200;
                $answer['sid'] = session_id();
                $answer['auth'] = 'true';
                $answer['message'] = 'ok';
                $answer['data'] = $data;
                $answer['count'] = count($data);
        }

        return $answer;
    }

    public function getStatisticComponent($raw_get_data){

        $timestamp = $raw_get_data['timestamp'];
        $param = $raw_get_data['param'];
        if(SYSLOG_ENABLE == 1) syslog(LOG_WARNING,"raw_get_data: " . $raw_get_data); ;
        return $this->doStatisticComponent($timestamp, $param);
    }


And the table containing the data:

CREATE TABLE IF NOT EXISTS `stats_traffic` (
`id` bigint(20) unsigned NOT NULL,
  `from_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `to_date` timestamp NOT NULL DEFAULT '1971-01-01 05:00:01',
  `ip_address` varchar(50) NOT NULL DEFAULT '',
  `type` varchar(50) NOT NULL DEFAULT '',
  `total` int(20) NOT NULL DEFAULT '0'
)


The getting of the data works properly, and it is returned to the browser, but i'm doing something wrong because it is not graphing it. I just changed the returned name in hope of maybe the graph expects a field with the same filter name, but no:

{
    "status": 200,
    "sid": "oljnn2ufq21p5e5pmqovbok715",
    "auth": "true",
    "message": "ok",
    "data": [
        {
            "id": "146745",
            "ip_address": "1.2.3.4",
            "total": "32",
            "from_ts": "1482073200",
            "to_ts": "1482073500"
        },
        {
            "id": "146734",
            "ip_address": "1.2.3.4",
            "total": "31",
            "from_ts": "1482072900",
            "to_ts": "1482073200"
        },
        {
            "id": "146727",
            "ip_address": "1.2.3.4",
            "total": "37",
            "from_ts": "1482072600",
            "to_ts": "1482072900"
        },
        {
            "id": "146721",
            "ip_address": "1.2.3.4",
            "total": "37",
            "from_ts": "1482072300",
            "to_ts": "1482072600"
        },
        {
            "id": "146710",
            "ip_address": "1.2.3.4",
            "total": "35",
            "from_ts": "1482072000",
            "to_ts": "1482072300"
        },
        {
            "id": "146702",
            "ip_address": "1.2.3.4",
            "total": "31",
            "from_ts": "1482071700",
            "to_ts": "1482072000"
        },
        {
            "id": "146697",
            "ip_address": "1.2.3.4",
            "total": "31",
            "from_ts": "1482071400",
            "to_ts": "1482071700"
        },
        {
            "id": "146687",
            "ip_address": "1.2.3.4",
            "total": "33",
            "from_ts": "1482071100",
            "to_ts": "1482071400"
        },
        {
            "id": "146678",
            "ip_address": "1.2.3.4",
            "total": "29",
            "from_ts": "1482070800",
            "to_ts": "1482071100"
        },
        {
            "id": "146671",
            "ip_address": "1.2.3.4",
            "total": "48",
            "from_ts": "1482070500",
            "to_ts": "1482070800"
        },
        {
            "id": "146666",
            "ip_address": "1.2.3.4",
            "total": "42",
            "from_ts": "1482070200",
            "to_ts": "1482070500"
        },
        {
            "id": "146654",
            "ip_address": "1.2.3.4",
            "total": "35",
            "from_ts": "1482069900",
            "to_ts": "1482070200"
        },
        {
            "id": "146651",
            "ip_address": "1.2.3.4",
            "total": "37",
            "from_ts": "1482069600",
            "to_ts": "1482069900"
        },
        {
            "id": "146640",
            "ip_address": "1.2.3.4",
            "total": "39",
            "from_ts": "1482069300",
            "to_ts": "1482069600"
        },
        {
            "id": "146630",
            "ip_address": "1.2.3.4",
            "total": "44",
            "from_ts": "1482069000",
            "to_ts": "1482069300"
        },
        {
            "id": "146626",
            "ip_address": "1.2.3.4",
            "total": "41",
            "from_ts": "1482068700",
            "to_ts": "1482069000"
        },
        {
            "id": "146619",
            "ip_address": "1.2.3.4",
            "total": "39",
            "from_ts": "1482068400",
            "to_ts": "1482068700"
        },
        {
            "id": "146606",
            "ip_address": "1.2.3.4",
            "total": "38",
            "from_ts": "1482068100",
            "to_ts": "1482068400"
        },
        {
            "id": "146601",
            "ip_address": "1.2.3.4",
            "total": "40",
            "from_ts": "1482067800",
            "to_ts": "1482068100"
        },
        {
            "id": "146594",
            "ip_address": "1.2.3.4",
            "total": "39",
            "from_ts": "1482067500",
            "to_ts": "1482067800"
        },
        {
            "id": "146582",
            "ip_address": "1.2.3.4",
            "total": "38",
            "from_ts": "1482067200",
            "to_ts": "1482067500"
        },
        {
            "id": "146574",
            "ip_address": "1.2.3.4",
            "total": "40",
            "from_ts": "1482066900",
            "to_ts": "1482067200"
        },
        {
            "id": "146566",
            "ip_address": "1.2.3.4",
            "total": "41",
            "from_ts": "1482066600",
            "to_ts": "1482066900"
        },
        {
            "id": "146559",
            "ip_address": "1.2.3.4",
            "total": "37",
            "from_ts": "1482066300",
            "to_ts": "1482066600"
        },
        {
            "id": "146551",
            "ip_address": "1.2.3.4",
            "total": "37",
            "from_ts": "1482066000",
            "to_ts": "1482066300"
        },
        {
            "id": "146543",
            "ip_address": "1.2.3.4",
            "total": "40",
            "from_ts": "1482065700",
            "to_ts": "1482066000"
        },
        {
            "id": "146539",
            "ip_address": "1.2.3.4",
            "total": "37",
            "from_ts": "1482065400",
            "to_ts": "1482065700"
        },
        {
            "id": "146528",
            "ip_address": "1.2.3.4",
            "total": "41",
            "from_ts": "1482065100",
            "to_ts": "1482065400"
        },
        {
            "id": "146522",
            "ip_address": "1.2.3.4",
            "total": "34",
            "from_ts": "1482064800",
            "to_ts": "1482065100"
        },
        {
            "id": "146510",
            "ip_address": "1.2.3.4",
            "total": "44",
            "from_ts": "1482064500",
            "to_ts": "1482064800"
        },
        {
            "id": "146507",
            "ip_address": "1.2.3.4",
            "total": "41",
            "from_ts": "1482064200",
            "to_ts": "1482064500"
        },
        {
            "id": "146500",
            "ip_address": "1.2.3.4",
            "total": "37",
            "from_ts": "1482063900",
            "to_ts": "1482064200"
        },
        {
            "id": "146489",
            "ip_address": "1.2.3.4",
            "total": "41",
            "from_ts": "1482063600",
            "to_ts": "1482063900"
        },
        {
            "id": "146483",
            "ip_address": "1.2.3.4",
            "total": "40",
            "from_ts": "1482063300",
            "to_ts": "1482063600"
        },
        {
            "id": "146473",
            "ip_address": "1.2.3.4",
            "total": "38",
            "from_ts": "1482063000",
            "to_ts": "1482063300"
        },
        {
            "id": "146464",
            "ip_address": "1.2.3.4",
            "total": "41",
            "from_ts": "1482062700",
            "to_ts": "1482063000"
        }
    ],
    "count": 36
}

David Villasmil

unread,
Dec 21, 2016, 4:23:34 AM12/21/16
to Homer Developers
could anyone help on this?


On Sunday, December 18, 2016 at 3:43:16 PM UTC+1, David Villasmil wrote:

Alexandr Dubovikov

unread,
Dec 21, 2016, 4:43:50 AM12/21/16
to home...@googlegroups.com
David,

please check in java console if you have any errors. Do you want apply the input to Sipcapture Chart ?

Wbr,
Alexandr

--
You received this message because you are subscribed to the Google Groups "Homer Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to homer-dev+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

David Villasmil

unread,
Dec 21, 2016, 6:26:30 AM12/21/16
to Homer Developers
Hello Alex,

Thanks for replying.

Indeed i'm trying to apply the results to a sipcapture chart. I don't get any errors on the console, that's why i'm confused, it _should_ chart it, unless i'm not returning some mandatory parameter (which i can't figure out).

This data is collected by Kamailio based on a couple of changes i've made. Using dispatcher, I'm checking whether the sender of the INVITE or 200 INVITE is in a list (alias table) and if so, then keep track of the specific component's ASR. This way we can have ASR per specified component (any component you add to the alias table would be tracked). 

This is very useful to have out-of-the-box.

Regards,

David
To unsubscribe from this group and stop receiving emails from it, send an email to homer-dev+...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages