show some statistics on the index page
authorChristian Weiske <cweiske@cweiske.de>
Fri, 22 Aug 2014 15:17:01 +0000 (17:17 +0200)
committerChristian Weiske <cweiske@cweiske.de>
Fri, 22 Aug 2014 15:17:01 +0000 (17:17 +0200)
controller/guicontroller.php
lib/tokenstorage.php
templates/index.php
templates/indexStats.php [new file with mode: 0644]

index 06ea681af572b62cba800ed64a9f8de4dcab2c03..2c05750b0dc746dc3fa8902610060e26e5c7e0e1 100644 (file)
@@ -61,6 +61,7 @@ class GuiController extends Controller
         $res = new TemplateResponse('grauphel', 'index');
         $res->setParams(array('apiurl' => $this->getApiUrl()));
         $this->addNavigation($res);
+        $this->addStats($res);
         return $res;
     }
 
@@ -74,6 +75,26 @@ class GuiController extends Controller
         $res->setParams($params);
     }
 
+    protected function addStats(TemplateResponse $res)
+    {
+        if ($this->user === null) {
+            return;
+        }
+
+        $username = $this->user->getUid();
+        $notes  = new \OCA\Grauphel\Lib\NoteStorage($this->urlGen);
+        $tokens = new \OCA\Grauphel\Lib\TokenStorage();
+
+        $nav = new \OCP\Template('grauphel', 'indexStats', '');
+        $nav->assign('notes', count($notes->loadNotesOverview($username)));
+        $nav->assign('syncrev', $notes->loadSyncData($username)->latestSyncRevision);
+        $nav->assign('tokens', count($tokens->loadForUser($username, 'access')));
+
+        $params = $res->getParams();
+        $params['stats'] = $nav;
+        $res->setParams($params);
+    }
+
     protected function checkDeps()
     {
         if (!class_exists('OAuthProvider')) {
index 9a173f5c03c26248512848b98e38e2d4515cf97a..f8f0806f95b89d61f9f092c483852af42fb674ce 100644 (file)
@@ -106,6 +106,30 @@ class TokenStorage
         return $token;
     }
 
+    /**
+     * Load multiple tokens
+     *
+     * @param string $username User name
+     * @param string $type     Token type: temp, access, verify
+     *
+     * @return array Array of Token objects
+     */
+    public function loadForUser($username, $type)
+    {
+        $result = \OC_DB::executeAudited(
+            'SELECT * FROM `*PREFIX*grauphel_oauth_tokens`'
+            . ' WHERE `token_user` = ? AND `token_type` = ?',
+            array($username, $type)
+        );
+
+        $tokens = array();
+        while ($tokenRow = $result->fetchRow()) {
+            $tokens[] = $this->fromDb($tokenRow);
+        }
+
+        return $tokens;
+    }
+
     protected function fromDb($tokenRow)
     {
         $token = new Token();
index 7b7c6a6350810d30343a4b8a7beb0c42c4b1ef3f..e63df374b6ca8ed51c223f4e6d1b6b81a47cf441 100644 (file)
@@ -1,13 +1,45 @@
+<style type="text/css">
+.app-grauphel #app-content {
+    box-sizing: border-box;
+    padding: 2ex;
+}
+
+.app-grauphel #app-content h1 {
+    font-weight: bold;
+    font-size: 2em;
+    margin-bottom: 1ex;
+}
+.app-grauphel #app-content h2 {
+    font-weight: bold;
+    font-size: 150%;
+    margin-bottom: 1ex;
+    margin-top: 2ex;
+}
+.app-grauphel #app-content dt {
+    font-weight: bold;
+}
+.app-grauphel #app-content dd {
+    margin-left: 3ex;
+}
+.app-grauphel #app-content pre {
+    margin: 1em;
+    background-color: #DDD;
+    padding: 1ex;
+    font-family: monospace;
+}
+</style>
+
 <?php /** @var $l OC_L10N */ ?>
 <?php $_['appNavigation']->printPage(); ?>
 
 <div id="app-content">
-  <div style="margin: 1ex">
-    <h1 style="font-size: 2em; font-weight: bold">Tomboy notes server</h1>
-    <p style="margin: 1em;">
+  <div>
+    <h1>Tomboy notes server</h1>
+    <p>
       Use the following sync server URL with tomboy/conboy/tomdroid:
     </p>
-    <pre style="margin: 1em; background-color: #DDD; padding: 1ex; font-family: monospace"><?php p($_['apiurl']); ?></pre>
+    <pre><?php p($_['apiurl']); ?></pre>
   </div>
 
+  <?php isset($_['stats']) && $_['stats']->printPage(); ?>
 </div>
diff --git a/templates/indexStats.php b/templates/indexStats.php
new file mode 100644 (file)
index 0000000..78274da
--- /dev/null
@@ -0,0 +1,11 @@
+<h2>Stats</h2>
+<dl>
+ <dt>Number of notes</dt>
+ <dd><?php p($_['notes']); ?></dd>
+
+ <dt>Number of registered apps</dt>
+ <dd><?php p($_['tokens']); ?></dd>
+
+ <dt>Sync revision</dt>
+ <dd><?php p($_['syncrev']); ?></dd>
+</dl>