CDR is broken, billsec is broken

I use Asterisk from 15 years and I’ve just spent 9 hours trying to get the correct CDR(billsec) duration of the second leg of a call.
The call originates from a .call file, when answered it goes to a context, which runs an agi, which calls an agent.

I’ve read the documentation here: https://wiki.asterisk.org/wiki/display/AST/Asterisk+12+CDR+Specification
and I sure loved that there isn’t a single example.

I’ve tried all the combinations of ResetCDR, ForkCDR, CDR_PROP(disable)=false, CDR(party_a)=true
with all the possible arguments, in every possible order, is every possible location (the .call file itself, the dialplan, the agi script), resulting in 97 test calls.

I’ve read everything I could find on the web
and sure appreciated the usual rtfm answer style of the devs,
mocking the users because it’s their fault and they are not using it properly.

It was cheerful to see not a single CDR thread or issue solved
like those:
https://issues.asterisk.org/jira/browse/ASTERISK-27425
https://issues.asterisk.org/jira/browse/ASTERISK-25328


http://forums.asterisk.org/viewtopic.php?f=1&t=91596
https://issues.asterisk.org/jira/browse/ASTERISK-24439
and having the users just give up or resort to some awkward workaround.

No matter what I try, I always get the total duration of the call
and not just the duration of the last answered one.

At the end I resorted using CEL
but of course it’s more important to have a native interface to postgresql than mysql
and please don’t bother to document how to configure odbc to use an external mysql server.
All I could find were examples for freepbx (which didn’t work)
and indian and russian howtos with voodoo swapping of packages between the Asterisk compilation and the actual usage (which didn’t work).

Btw this is your “CEL Configuration Examples” page:
https://wiki.asterisk.org/wiki/display/AST/CEL+Configuration+Examples

Between an inconsistent error and the other I managed to have it writing to an sqlite3 database,
of course it ignores the ‘apps’ and ‘events’ keywords and it writes 13 rows for a single call.

At this point my agi waits 5 seconds at the end of the call, opens the sqlite database,
looks for the matching events, and calculates the time difference between the ANSWER and the HANGUP events of the second call.
Then it updates my cdr table on my mysql external database with the correct value.

Of course I’ll need to code the logic to do some housekeeping and delete the rows from the sqlite3 database,
otherwise it will grow in size and bottleneck.

So now my Frankenstein agi script has the agi libraries, the mysql class and the sqlite3 interface.

Wow telephony, much revolution.