Database abstraction of site results

Nowadays I abstract all database matters away from Google Apps Script with Database abstraction with google apps script. The scheduled analytics update writes the matched site page and analytic data to an abstracted database. At the time of writing, I happen to be using Driver MongoLab.

Once the Matching the site tree to the analytics data is complete, the data needs to be flattened from a tree object so we can create a row per site page. This page is later retrieved by the gadget which creates the visual representation that page's analytics. 

That's achieved in this step
  // combine the topic and page data
  var tood = combine(topics,topics);
  
  // commit
  refreshDb ( options, tood);

The database parameters are defined in Storing options and parameters and accessed from that library. This means that multiple projects can use synchronised parameters for the same data.
/** 
 * refreshDb
 * @param {object} options with properties needed to open a dbabstraction database
 * @param {Array.object} pages and array of pages with their stats;
 */
function refreshDb (options,pages) {
  
   // whatever the normal database is
   var handler = cSiteStats.getDataHandle(options.siteCode);
  
  if (!handler.isHappy()) throw 'unable to get handler' + JSON.stringify(options);
      
  // delete all thats there
  var result  = handler.remove();
  if (result.handleCode < 0) throw result;
  
  //save
  var result = handler.save(pages);
  if (result.handleCode < 0) throw result;
  
}

The data stored for each page will look something like this, and will be indexed in the client gadget using page.url.
{
    "_id": {
        "$oid": "543011a6e4b0dbebfeff60af"
    },
    "topic": {
        "url": "https://sites.google.com/a/mcpher.com/share/Home/excelquirks/json",
        "numChildren": 0,
        "analytics": [
            {
                "varieties": 32,
                "name": "Past Month",
                "pageViews": 6461,
                "favorite": "https://sites.google.com/a/mcpher.com/share/Home/excelquirks/snippets",
                "favoriteTitle": "Get Started Snippets",
                "universe": 73,
                "rank": 1
            },
            {
                "varieties": 49,
                "name": "Past Year",
                "pageViews": 88844,
                "favorite": "https://sites.google.com/a/mcpher.com/share/Home/excelquirks/json",
                "favoriteTitle": "jSon",
                "universe": 73,
                "rank": 0
            },
            {
                "varieties": 77,
                "name": "All Time",
                "pageViews": 210390,
                "favorite": "https://sites.google.com/a/mcpher.com/share/Home/excelquirks/json",
                "favoriteTitle": "jSon",
                "universe": 73,
                "rank": 0
            }
        ],
        "name": "json",
        "title": "jSon",
        "plusOnes": 107
    },
    "page": {
        "url": "https://sites.google.com/a/mcpher.com/share/Home/excelquirks/json/rest/arraydots",
        "numChildren": 0,
        "pageType": 4,
        "analytics": [
            {
                "varieties": 1,
                "name": "Past Month",
                "pageViews": 28,
                "favorite": "https://sites.google.com/a/mcpher.com/share/Home/excelquirks/json/excel-json-conversion",
                "favoriteTitle": "Excel JSON conversion",
                "universe": 33,
                "rank": 16
            },
            {
                "varieties": 1,
                "name": "Past Year",
                "pageViews": 277,
                "favorite": "https://sites.google.com/a/mcpher.com/share/Home/excelquirks/json/excel-json-conversion",
                "favoriteTitle": "Excel JSON conversion",
                "universe": 33,
                "rank": 21
            },
            {
                "varieties": 1,
                "name": "All Time",
                "pageViews": 337,
                "favorite": "https://sites.google.com/a/mcpher.com/share/Home/excelquirks/json",
                "favoriteTitle": "jSon",
                "universe": 33,
                "rank": 23
            }
        ],
        "topicRoot": false,
        "name": "arraydots",
        "title": "JSON arrays of objects",
        "plusOnes": 3
    },
    "site": {
        "url": "https://sites.google.com/a/mcpher.com/share/",
        "numChildren": 4,
        "analytics": [
            {
                "varieties": 1000,
                "name": "Past Month",
                "pageViews": 47989,
                "favorite": "",
                "favoriteTitle": "",
                "universe": 0,
                "rank": 0
            },
            {
                "varieties": 1000,
                "name": "Past Year",
                "pageViews": 557876,
                "favorite": "",
                "favoriteTitle": "",
                "universe": 0,
                "rank": 0
            },
            {
                "varieties": 999,
                "name": "All Time",
                "pageViews": 1177090,
                "favorite": "",
                "favoriteTitle": "",
                "universe": 0,
                "rank": 0
            }
        ],
        "name": "share",
        "title": "Desktop Liberation",
        "plusOnes": 2313
    }
}

For help and more information join our forum,follow the blog or follow me on twitter .


Comments