You are here: Home » Topic » counter per user

counter per user

This topic contains 8 replies, has 3 voices, and was last updated by  rpedde 10 years, 11 months ago.

Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #866

    WzrdOfOOst

    Hello I have been using mt-daapd server since a couple of months and wrote a bit of php code to display the number of played songs per user. it uses the log file to check. I am not a very experienced php-programmer actually this is my first bit of code. So don`t flame me…
    Check my blog for my experiences with the daap server.


    < ?php
    $myFile = "mt-daapd.log";
    $fh = fopen($myFile, 'r');
    $theData = fread($fh, filesize($myFile));
    fclose($fh);

    $address = array('/172.19.3.22/','/172.19.3.19/','/172.19.3.18/') ;
    $name = array(john, Joe, Lisa);
    for ( $counter = 0; $counter < = 2; $counter += 1) {

    if (preg_match_all($address[$counter], $theData, $user)) {
    echo $name[$counter]," ", count($user[0]), "
    ";
    } else {
    echo $name[$counter]," did not play any songs";
    }
    }
    ?>

    [url][/url]

    #7732

    rpedde
    Participant

    @WzrdOfOOst wrote:

    Hello I have been using mt-daapd server since a couple of months and wrote a bit of php code to display the number of played songs per user. it uses the log file to check. I am not a very experienced php-programmer actually this is my first bit of code. So don`t flame me…
    Check my blog for my experiences with the daap server.


    < ?php
    $myFile = "mt-daapd.log";
    $fh = fopen($myFile, 'r');
    $theData = fread($fh, filesize($myFile));
    fclose($fh);

    $address = array('/172.19.3.22/','/172.19.3.19/','/172.19.3.18/') ;
    $name = array(john, Joe, Lisa);
    for ( $counter = 0; $counter < = 2; $counter += 1) {

    if (preg_match_all($address[$counter], $theData, $user)) {
    echo $name[$counter]," ", count($user[0]), "
    ";
    } else {
    echo $name[$counter]," did not play any songs";
    }
    }
    ?>

    [url][/url]

    Neat! There are some interesting things you could do with that if you wanted to play in the database as well… Using php and sqlite, you could take results like that and make them an iTunes playlist by poking it right into the database. Then you could have iTunes playlists (for example), with the last 5 things everyone played.

    Or the last 20 songs anyone has played. Or what have you.

    Neat stuff you can do by reaching right into the database and poking around.

    — Ron

    #7733

    fizze
    Participant

    yup!

    I think this would play real nice together with riro’s DB “hack” 😉
    only problem is, that the DB doesnt know whom it was played to the last time. So correlating the DB with the log file might be difficult….

    #7734

    CCRDude
    Participant

    Wasn’t there some problem that sqlite wasn’t designed for multiple concurrent accesses? I remember when I tried to fool around with the sqlite db while mt-daapd was running, that caused some problems.

    Small thing about the php: unless you’ve logrotate setup for the logfile, you probably should read only the last n KB, otherwise you may need more memory than php got assigned.

    #7735

    WzrdOfOOst

    @ccrdude wrote:

    Small thing about the php: unless you’ve logrotate setup for the logfile, you probably should read only the last n KB, otherwise you may need more memory than php got assigned.

    Thanks for your comment, but I do not quite understand what you mean. How do I read the last n KB of the file and still get all the information into the string?

    #7736

    CCRDude
    Participant

    Well, depends on what you need 😉

    My thought was that the last 64 KB or so of text would be sufficient: would mean nearly 1000 log lines. Everything older probably doesn’t count as “last played” anyway 😉

    $readsize = 65535;
    fseek($fh, filesize($myFile) - $readsize);
    $theData = fread($fh, $readsize);

    If you always want to parse the full file, I would recommend reading and parsing in blocks, like:


    $readsize = 8192;
    while (!feof($fh))
    { $theData = fread($fh, $readsize);
    processTheData($theData);
    }

    Which is untested code, just to demonstrate a bit what I mean 😉
    The later would mean an improved parsing, but then that would have advantages as well, as you could split the text into an array of lines, and use grep on each line.

    Oh, one more thing: your for-loop can be made easier…

    $names = array(john, Joe, Lisa);
    foreach ($names as $name)
    { // you can use $name as the current array part in the loop now
    }

    edit: fgets to read line by line would be even better of course 😉

    edit2: I did just create a script myself that parses the full log file, and inserts each play event into a separate mysql db. Nice for creating some kind of statistics that’ll tell which song got played how often etc. 😉
    Only thing thats not so nice about this is that the log does contain only the filename really. Something like full path, music data (without tags) checksum, or some other clear identifier that would survive a full db scan (or creating a new db) would be very nice 😉

    Btw here’s a full ereg()-Expression to separate each line into the proper fields:

    ([0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}) (([0-9]{8})): Session ([0-9]{1,2}): Streaming file '([a-zA-Z0-9 .]*)' to ([0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}) (offset ([0-9]{1,2}))

    ereg() returns an array where you can access the following data by index:
    0=full term, 1=date/time, 2=cardinal, 3=session id, 4=filename, 5=client ip, 6=offset.

    #7737

    rpedde
    Participant

    @ccrdude wrote:

    Wasn’t there some problem that sqlite wasn’t designed for multiple concurrent accesses? I remember when I tried to fool around with the sqlite db while mt-daapd was running, that caused some problems.

    Yes. The db is locked during transactions, so you have to have a timeout. If you have timeouts set in the php code and you do your business quickly in the db, it should be okay. The server should keep retrying for some time (30 sec, iirc) until it can get a lock on the db.

    #7738

    fizze
    Participant

    just out of curiosity, how big is the overhead of a running mySQL DB on a slug with mt-daapd vs. the sqlite?

    I want to run swisscenter on a slug anyhow, which requires a DB. Right now Im thinking of running mt-daapd on the same slug, and using mySQL for both, then transactions etc wouldnt be that of an issue.

    any clues?

    #7739

    rpedde
    Participant

    @fizze wrote:

    just out of curiosity, how big is the overhead of a running mySQL DB on a slug with mt-daapd vs. the sqlite?

    I want to run swisscenter on a slug anyhow, which requires a DB. Right now Im thinking of running mt-daapd on the same slug, and using mySQL for both, then transactions etc wouldnt be that of an issue.

    any clues?

    My understanding is that it pretty much tanks it. I had heard people say that they tried to run a lamp stack on it, and it wasn’t the php+apache that was the killer, it was the mysql.

Viewing 9 posts - 1 through 9 (of 9 total)

You must be logged in to reply to this topic.