Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Bitestats: From API to PDF

Bitestats: From API to PDF

Future of Web Apps presentation about the architecture of bitestats and how all the various pieces fit together to make a great PHP application

D33d8bdd9096c80b8d1acca8d28410b5?s=128

Lorna Mitchell

October 16, 2012
Tweet

More Decks by Lorna Mitchell

Other Decks in Technology

Transcript

  1. Bitestats: from API to Bitestats: from API to PDF PDF

  2. About Me About Me Lorna Mitchell http://lornajane.net @lornajane

  3. About BiteStats About BiteStats “A one-page report summarising your web

    stats, which arrives in your inbox monthly”
  4. Monthly Report Monthly Report

  5. None
  6. None
  7. Zend Framework Website Zend Framework Website Outward facing site, written

    in Zend Framework 1 http://framework.zend.com Users can: register pay get one-off reports enable/disable reporting for each site
  8. None
  9. None
  10. Wordpress Wordpress Simple site for managing content and has feedback

    forms http://wordpress.com Zend_Layout and some @miss_jwo magic to make the two systems match
  11. None
  12. None
  13. Google OAuth (v1) Google OAuth (v1) Allow users to authorise

    BiteStats to have access to their data 1) Get a Request Token <?php // Get Request Token $oauth = new new OAuth($config['consumer_key'], $config['consumer_secret'] ); $oauth->setAuthType(OAUTH_AUTH_TYPE_URI); $request_options = array array("scope" => $config['scope'], "oauth_callback" => $config['callbackurl']); $url = $config['requesturl'] . '?' . http_build_query($request_options); $token_req = $oauth->getRequestToken($url);
  14. Google OAuth (v1) Google OAuth (v1) Allow users to authorise

    BiteStats to have access to their data 2) Send the user off to google to authenticate <?php $oauth_options = array array( "oauth_token" => $token_req['oauth_token'], "oauth_token_secret" => $token_req['oauth_token_secret'] ); $url = $config['oauthurl'] . '?' . http_build_query($oauth_options); $this->_redirect($url);
  15. Google OAuth (v1) Google OAuth (v1) Allow users to authorise

    BiteStats to have access to their data 3) User returns, and we can get the access token <?php $oauth = new new OAuth($config['consumer_key'], $config['consumer_secret'] ); $oauth->setAuthType(OAUTH_AUTH_TYPE_URI); $oauth->setToken($config['request_token'], $config['request_token_secret']); $access = $oauth->getAccessToken($config['access_uri'], null null, $config['verification']);
  16. None
  17. None
  18. Google Analytics Google Analytics Which profiles and sites does a

    user have access to? <?php $oauth = new new OAuth($consumer_key, $consumer_secret ); $oauth->setToken($access_token, $access_token_secret); $result = $oauth->fetch( 'https://www.google.com/analytics/feeds/datasources/ga/accounts/' . $account_id . '/webproperties/~all/profiles'); They can pick which ones they want every month, or request one NOW
  19. None
  20. None
  21. Gearman Gearman Gearman is a job queue. http://gearman.org The web

    servers add jobs to the queue Web server then gets on with whatever it was doing Worker processes fetch jobs from the queue, and process them Gearman oversees each job being succesfully completed
  22. Adding Jobs to Gearman Adding Jobs to Gearman <?php $job_data

    = array array(); $job_data['user_id'] = $auth->user_id; $job_data['date_requested'] = $data['date_requested']; $job_data['google_profile'] = $auth->gp; $gearman_client = new new Application_Model_Gearman_Client($options); $gearman_client->doBackground('basicReport', json_encode($job_data));
  23. Forgot The Most Forgot The Most Important Thing Important Thing

  24. None
  25. None
  26. Paypal Integration Paypal Integration Set up subscriptions and button via

    PayPal website IPN: Instant Payment Notification PayPal POSTs requests to us when events occur
  27. Handling Paypal IPN Calls Handling Paypal IPN Calls We acknowledge

    Paypal's call, otherwise they keep sending it! Relate the transactions back to the user and keep state
  28. None
  29. None
  30. Bootstrap ZF from the Command Bootstrap ZF from the Command

    Line Line Used by worker scripts and cron jobs Separate bootstrap process and config, shared MVC structure
  31. Workers Workers Command line scripts, monitored by supervisord http://supervisord.org Check

    for new jobs, sleep, check ...
  32. Worker Account Checks Worker Account Checks Look at incoming data,

    which report, which date range, which user? Check that Google OAuth token is valid Check that the Paypal subscription is current
  33. Zend_Pdf Zend_Pdf A Zend Framework module with helper functions for

    PDF Still need to do some maths yourself! The origin (0,0) is in the bottom left of the page ... public function drawLine($x1, $y1, $x2, $y2); public function drawText($text, $x, $y, $charEncoding = ''); public function drawImage(Zend_Pdf_Resource_Image $image, $x1, $y1, $x2, $y2);
  34. Google Analytics Data Google Analytics Data Fetch visits by day

    for two date ranges:
  35. None
  36. None
  37. Google Image Charts Google Image Charts Build a URL, use

    as src attribute for img tag. http://chart.apis.google.com/chart?chs=800x256&cht=lc&chco=524066,46464666& chd=t:_,_,841,805,794,391,383,834,916,848,886,744,406,385,900,1553,916,1037,1418, 510,470,1055,1038,972,966,828,422,366,922,1015,959,361,0|979,839,760,845,778,370, 399,944,1244,1041,917,782,415,364,825,958,1342,1892,998,542,637,1203,994,923,1022, 787,413,402,1003,910,_,_,_&chdl=Aug|Jul&chds=0,1892&chxt=x,x,y&chxl=0:|M|T|W|T|F| S|S|M|T|W|T|F|S|S|M|T|W|T|F|S|S|M|T|W|T|F|S|S|M|T|W|T|F|1:||Day+Of+Week|&chem=y; s=bubble_text_small;d=bb,Max:+1553,FFFFFF,524066;ds=0;dp=15|y;s=bubble_text_small; d=bbtr,Min:+0,FFFFFF,524066;ds=0;dp=32&chma=0,0,60,60&chxr=2,0,1892&chdls=000000, 14&chtt=Visitors+per+Day&chts=000000,14
  38. Google Image Charts Google Image Charts Build a URL, use

    as src attribute for img tag. http://chart.apis.google.com/chart?chs=800x256&cht=lc&chco=524066,46464666&chd=t:_, _,841,805,794,391,383,834,916,848,886,744,406,385,900,1553,916,1037,1418, 510,470,1055,1038,972,966,828,422,366,922,1015,959,361,0|979,839,760,845, 778,370,399,944,1244,1041,917,782,415,364,825,958,1342,1892,998,542,637, 1203,994,923,1022,787,413,402,1003,910,_,_,_&chdl=Aug|Jul&chds=0,1892&chxt=x, x,ychxl=0:|M|T|W|T|F|S|S|M|T|W|T|F|S|S|M|T|W|T|F|S|S|M|T|W|T|F|S|S|M|T|W|T|F|1:|| Day+Of+Week|&chem=y;s=bubble_text_small;d=bb,Max:+1553,FFFFFF,524066;ds=0;dp=15|y; s=bubble_text_small;d=bbtr,Min:+0,FFFFFF,524066;ds=0;dp=32&chma=0,0,60,60&chxr=2, 0,1892&chdls=000000,14&chtt=Visitors+per+Day&chts=000000,14
  39. Google Image Charts Google Image Charts Build a URL, use

    as src attribute for img tag. http://chart.apis.google.com/chart?chs=800x256&cht=lc&chco=524066,46464666&chd=t: _,_,841,805,794,391,383,834,916,848,886,744,406,385,900,1553,916,1037,1418,510,470, 1055,1038,972,966,828,422,366,922,1015,959,361,0|979,839,760,845,778,370,399,944, 1244,1041,917,782,415,364,825,958,1342,1892,998,542,637,1203,994,923,1022,787,413, 402,1003,910,_,_,_&chdl=Aug|Jul&chds=0,1892&chxt=x,x,y&chxl=0:|M|T|W|T|F|S| S|M|T|W|T|F|S|S|M|T|W|T|F|S|S|M|T|W|T|F|S|S|M|T|W|T|F|1:||Day+Of+Week|& chem=y;s=bubble_text_small;d=bb,Max:+1553,FFFFFF,524066;ds=0;dp=15|y; s=bubble_text_small;d=bbtr,Min:+0,FFFFFF,524066;ds=0;dp=32&chma=0,0,60,60& chxr=2,0,1892&chdls=000000,14&chtt=Visitors+per+Day&chts=000000,14
  40. Google Image Charts Google Image Charts Build a URL, use

    as src attribute for img tag. http://chart.apis.google.com/chart?chs=800x256&cht=lc&chco=524066,46464666&chd=t: _,_,841,805,794,391,383,834,916,848,886,744,406,385,900,1553,916,1037,1418,510,470, 1055,1038,972,966,828,422,366,922,1015,959,361,0|979,839,760,845,778,370,399,944, 1244,1041,917,782,415,364,825,958,1342,1892,998,542,637,1203,994,923,1022,787,413, 402,1003,910,_,_,_&chdl=Aug|Jul&chds=0,1892&chxt=x,x,y&chxl=0:|M|T|W|T|F|S|S|M|T| W|T|F|S|S|M|T|W|T|F|S|S|M|T|W|T|F|S|S|M|T|W|T|F|1:||Day+Of+Week|&chem=y; s=bubble_text_small;d=bb,Max:+1553,FFFFFF,524066;ds=0;dp=15|y; s=bubble_text_small;d=bbtr,Min:+0,FFFFFF,524066;ds=0;dp=32&chma=0,0,60,60& chxr=2,0,1892&chdls=000000,14&chtt=Visitors+per+Day&chts=000000,14
  41. Google Image Charts Google Image Charts Build a URL, use

    as src attribute for img tag. http://chart.apis.google.com/chart?chs=800x256&cht=lc&chco=524066,46464666&chd=t: _,_,841,805,794,391,383,834,916,848,886,744,406,385,900,1553,916,1037,1418,510,470, 1055,1038,972,966,828,422,366,922,1015,959,361,0|979,839,760,845,778,370,399,944, 1244,1041,917,782,415,364,825,958,1342,1892,998,542,637,1203,994,923,1022,787,413, 402,1003,910,_,_,_&chdl=Aug|Jul&chds=0,1892&chxt=x,x,y&chxl=0:|M|T|W|T|F|S|S|M|T| W|T|F|S|S|M|T|W|T|F|S|S|M|T|W|T|F|S|S|M|T|W|T|F|1:||Day+Of+Week|&chem=y; s=bubble_text_small;d=bb,Max:+1553,FFFFFF,524066;ds=0;dp=15|y;s=bubble_text_small; d=bbtr,Min:+0,FFFFFF,524066;ds=0;dp=32&chma=0,0,60,60&chxr=2,0,1892& chdls=000000,14&chtt=Visitors+per+Day&chts=000000,14
  42. Google Analytics Data Google Analytics Data Fetch visits by week

    for two date ranges:
  43. Google Analytics Data Google Analytics Data Fetch visits by pagetitle

    for the month:
  44. Google Analytics Data Google Analytics Data Fetch visits by source

    for the month:
  45. Supporting Cast Supporting Cast

  46. Other Tools Other Tools Mercurial on BitBucket http://bitbucket.org Phing for

    deployment http://phing.info
  47. Mercurial and BitBucket Mercurial and BitBucket A DVCS aimed at

    normal people - intentionally humane and intuitive BitBucket offers: Git and Hg Unlimited private repositories Up to 5 collaborators on free accounts
  48. Phing Phing PHP-based deployment tool My build.xml follows these steps:

    create incrementally-numbered tag export code and tar it up transfer it to live, and untar it handle db patches and things point the docroot symlink at the new files restart the gearman workers ... all in one command
  49. Bitestats: from API to PDF Bitestats: from API to PDF

  50. Questions? Questions?

  51. Thanks Thanks Lorna Mitchell http://lornajane.net @lornajane https://joind.in/7500