<html>
<head>
<base href="https://wiki.asterisk.org/wiki">
<link rel="stylesheet" href="/wiki/s/2042/1/7/_/styles/combined.css?spaceKey=AST&forWysiwyg=true" type="text/css">
</head>
<body style="background: white;" bgcolor="white" class="email-body">
<div id="pageContent">
<div id="notificationFormat">
<div class="wiki-content">
<div class="email">
<h2><a href="https://wiki.asterisk.org/wiki/display/AST/MySQL+CDR+Backend?focusedCommentId=13076842#comment-13076842">MySQL CDR Backend</a></h2>
<h4>Comment edited by <a href="https://wiki.asterisk.org/wiki/display/~kwk">Konrad Kleine</a>
:</h4>
<br/>
<h4>Changes (1)</h4>
<div id="page-diffs">
<table class="diff" cellpadding="0" cellspacing="0">
<tr><td class="diff-snipped" >...<br></td></tr>
<tr><td class="diff-unchanged" >SELECT <br> *, <br></td></tr>
<tr><td class="diff-changed-lines" >CAST( `calldate`+(`duration`-`billsec`) AS DATETIME ) <span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">as</span> <span class="diff-added-words"style="background-color: #dfd;">AS</span> `answer`, <br></td></tr>
<tr><td class="diff-unchanged" > CAST( `calldate`+`duration` AS DATETIME ) AS `end` <br>FROM <br></td></tr>
<tr><td class="diff-snipped" >...<br></td></tr>
</table>
</div> <h4>Full Content</h4>
<div class="notificationGreySide">
<p>The <b>start</b> is already there, but it's called <b>calldate</b> in MySQL for instance.</p>
<p>If you do a</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<script type="syntaxhighlighter" class="toolbar: false; theme: Confluence; brush: bash; gutter: false"><![CDATA[
grep "^[^;]" /etc/asterisk/cdr_mysql.conf
]]></script>
</div></div>
<p>on an Asterisk 1.8.4 installation amongst other things it reveals:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<script type="syntaxhighlighter" class="toolbar: false; theme: Confluence; brush: bash; gutter: false"><![CDATA[
[columns]
alias start => calldate
alias callerid => clid
]]></script>
</div></div>
<p>So what's Asterisk internally called <b>start</b> is called <b>calldate</b> in MySQL. As you can see from the <b>.conf</b> excerpt, the same goes for <b>callerid</b> and <b>clid</b>.</p>
<p>If I'm not totally mistaken, <b>end</b> and <b>answer</b> can be derived from <b>duration</b> and <b>billsec</b>. In the original description of the <a href="/wiki/display/AST/CDR+Fields" title="CDR Fields">CDR Fields</a> it says this:</p>
<blockquote>
<ul>
        <li>start: Start of call (date/time)</li>
        <li>answer: Answer of call (date/time)</li>
        <li>end: End of call (date/time)</li>
        <li>duration: Total time in system, in seconds (integer), from dial to hangup</li>
        <li>billsec: Total time call is up, in seconds (integer), from answer to hangup</li>
</ul>
</blockquote>
<p>In MySQL you only have <b>duration</b>, <b>billsec</b>, and <b>start</b> which is actually called <b>calldate</b>.</p>
<p>Thus, this should be what you're looking for:</p>
<p><b>start</b> = calldate<br/>
<b>answer</b> = start + (duration - billsec)<br/>
<b>end</b> = start + duration</p>
<p>In SQL this would look like this:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<script type="syntaxhighlighter" class="toolbar: false; theme: Confluence; brush: sql; gutter: false"><![CDATA[
SELECT
*,
(`calldate`+(`duration`-`billsec`)) AS `answer`,
(`calldate`+`duration`) AS `end`
FROM
cdr;
]]></script>
</div></div>
<p>If you want to create a view to avoid this calculation by hand you can do so with this command:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<script type="syntaxhighlighter" class="toolbar: false; theme: Confluence; brush: sql; gutter: false"><![CDATA[
CREATE VIEW cdr_full AS
SELECT
*,
(`calldate`+(`duration`-`billsec`)) AS `answer`,
(`calldate`+`duration`) AS `end`
FROM
cdr;
]]></script>
</div></div>
<p>There is one problem with the column types though: <b>answer</b> and <b>end</b> in my case are of type <b>double(23,6)</b> which is not what we want. To fix this we need to do some casting (Don't know if this is MySQL specific):</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<script type="syntaxhighlighter" class="toolbar: false; theme: Confluence; brush: sql; gutter: false"><![CDATA[
CREATE VIEW cdr_full AS
SELECT
*,
CAST( `calldate`+(`duration`-`billsec`) AS DATETIME ) AS `answer`,
CAST( `calldate`+`duration` AS DATETIME ) AS `end`
FROM
`cdr`;
]]></script>
</div></div>
<p>If you want to be perfectly correct, the colum attributes like <b>NOT NULL</b> and default values need to be set correctly.</p>
</div>
<div id="commentsSection" class="wiki-content pageSection">
<div style="float: right;">
<a href="https://wiki.asterisk.org/wiki/users/viewnotifications.action" class="grey">Change Notification Preferences</a>
</div>
<a href="https://wiki.asterisk.org/wiki/display/AST/MySQL+CDR+Backend?focusedCommentId=13076842#comment-13076842">View Online</a>
|
<a id="reply-13076842" href="https://wiki.asterisk.org/wiki/display/AST/MySQL+CDR+Backend?replyToComment=13076842#comment-13076842">Reply To This</a>
</div>
</div>
</div>
</div>
</div>
</body>
</html>