Use commit timestamps for ElasticSearch and sort on last commit time.
authorJustin J. Novack <jnovack@gmail.com>
Sun, 30 Sep 2012 21:43:58 +0000 (17:43 -0400)
committerJustin J. Novack <jnovack@gmail.com>
Mon, 1 Oct 2012 21:21:56 +0000 (17:21 -0400)
12 files changed:
data/templates/list.htm
data/templates/new.htm
data/templates/repo-sidebar-list.htm
scripts/index.php
src/phorkie/Database/Adapter/Elasticsearch/Indexer.php
src/phorkie/Database/Adapter/Elasticsearch/Search.php
src/phorkie/Database/Adapter/Elasticsearch/Setup.php
src/phorkie/Database/Adapter/Null/Indexer.php
src/phorkie/Database/IIndexer.php
src/phorkie/Database/ISearch.php
www/list.php
www/new.php

index d0732ea..aee3100 100644 (file)
  {% include 'pager.htm' %}
 
 {% endblock %}
+
+{% block sidebar %}
+ {% if recents.results %}
+  <h3>Recently updated</h3>
+  <ul>
+  {% for repo in recents.repos %}
+   {% include 'repo-sidebar-list.htm' %}
+  {% endfor %}
+  </ul>
+ {% endif %}
+{% endblock %}
index 7548c2a..0f69719 100644 (file)
@@ -38,7 +38,7 @@ $(document).ready(function() {
 
 {% block sidebar %}
  {% if recents.results %}
- <h3>Recently created</h3>
+ <h3>Recently updated</h3>
  <ul>
  {% for repo in recents.repos %}
    {% include 'repo-sidebar-list.htm' %}
index ed262ac..cb9f3d8 100644 (file)
@@ -2,5 +2,5 @@
   <a href="{{repo.getLink('display')}}">
    {{repo.getTitle}}
   </a><br/>
-  &#160;&#160;&#160;<span title="{{repo.crdate|date('c')}}">{{dh.get(repo.crdate)}}</span>
+  &#160;&#160;&#160;<span title="{{repo.modate|date('c')}}">{{dh.get(repo.modate)}}</span>
  </li>
index 4b529d1..5da912e 100644 (file)
@@ -37,6 +37,8 @@ $rs = new Repositories();
 list($repos, $count) = $rs->getList(0, 10000);
 foreach ($repos as $repo) {
     echo 'Indexing ' . $repo->id . "\n";
-    $idx->addRepo($repo, filectime($repo->gitDir));
+    $commits = $repo->getHistory();
+    $first = count($commits)-1;
+    $idx->addRepo($repo, $commits[$first]->committerTime, $commits[0]->committerTime);
 }
 ?>
index cf50bbe..e93cfa7 100644 (file)
@@ -9,15 +9,18 @@ class Database_Adapter_Elasticsearch_Indexer implements Database_IIndexer
     }
 
 
-    public function addRepo(Repository $repo, $crdate = null)
+    public function addRepo(Repository $repo, $crdate = null, $modate = null)
     {
         if ($crdate == null) {
             $crdate = time();
         }
-        $this->updateRepo($repo, $crdate);
+        if ($modate == null) {
+            $modate = time();
+        }
+        $this->updateRepo($repo, $crdate, $modate);
     }
 
-    public function updateRepo(Repository $repo, $crdate = null)
+    public function updateRepo(Repository $repo, $crdate = null, $modate = null)
     {
         //add repository
         $r = new Database_Adapter_Elasticsearch_HTTPRequest(
@@ -35,6 +38,12 @@ class Database_Adapter_Elasticsearch_Indexer implements Database_IIndexer
         if ($crdate !== null) {
             $repoData['crdate'] = gmdate('c', $crdate);
         }
+        if ($modate == null) {
+            $modate = $this->getMoDate($repo);
+        }
+        if ($modate !== null) {
+            $repoData['modate'] = gmdate('c', $modate);
+        }
 
         $r->setBody(json_encode((object)$repoData));
         $r->send();
@@ -83,6 +92,28 @@ class Database_Adapter_Elasticsearch_Indexer implements Database_IIndexer
         return strtotime($json->_source->crdate);
     }
 
+    /**
+     * When updating the repository, we don't have a modification date.
+     * We need to keep it, but elasticsearch does not have a simple way
+     * to update some fields only (without using a custom script).
+     *
+     * @return integer Unix timestamp
+     */
+    protected function getMoDate(Repository $repo)
+    {
+        $r = new Database_Adapter_Elasticsearch_HTTPRequest(
+            $this->searchInstance . 'repo/' . $repo->id,
+            \HTTP_Request2::METHOD_GET
+        );
+        $json = json_decode($r->send()->getBody());
+
+        if (!isset($json->_source->modate)) {
+            return null;
+        }
+
+        return strtotime($json->_source->modate);
+    }
+
     public function deleteAllRepos()
     {
         $r = new Database_Adapter_Elasticsearch_HTTPRequest(
index e960e04..ddbc13b 100644 (file)
@@ -6,6 +6,7 @@ class Database_Adapter_Elasticsearch_Search implements Database_ISearch
     protected static $sortMap = array(
         'id' => array('id', 'asc'),
         'crdate' => array('crdate', 'desc'),
+        'modate' => array('modate', 'desc'),
         'tstamp' => array('tstamp', 'desc'),
     );
 
@@ -22,7 +23,8 @@ class Database_Adapter_Elasticsearch_Search implements Database_ISearch
      * @param string  $sort    Sort order. Allowed values:
      *                         - id     - repository id
      *                         - crdate - creation date
-     *                         - tstamp - modification date
+     *                         - modate - modification date
+     *                         - tstamp - last index date
      *
      * @return Search_Result Search result object
      */
@@ -64,6 +66,7 @@ class Database_Adapter_Elasticsearch_Search implements Database_ISearch
             $r = new Repository();
             $r->loadById($hit->_source->id);
             $r->crdate = strtotime($hit->_source->crdate);
+            $r->modate = strtotime($hit->_source->modate);
             $sres->repos[] = $r;
         }
 
index 0df2874..bb76d54 100644 (file)
@@ -59,9 +59,13 @@ class Database_Adapter_Elasticsearch_Setup implements Database_ISetup
                                 //creation date
                                 'type' => 'date',
                             ),
-                            'tstamp' => (object)array(
+                            'modate' => (object)array(
                                 //modification date
                                 'type' => 'date',
+                            ),
+                            'tstamp' => (object)array(
+                                //last indexed date
+                                'type' => 'date',
                             )
                         )
                     )
index 3b0c990..4ad63f1 100644 (file)
@@ -3,11 +3,11 @@ namespace phorkie;
 
 class Database_Adapter_Null_Indexer
 {
-    public function addRepo(Repository $repo, $crdate = null)
+    public function addRepo(Repository $repo, $crdate = null, $modate = null)
     {
     }
 
-    public function updateRepo(Repository $repo, $crdate = null)
+    public function updateRepo(Repository $repo, $crdate = null, $modate = null)
     {
     }
 
index 61b8a87..232e1ee 100644 (file)
@@ -3,8 +3,8 @@ namespace phorkie;
 
 interface Database_IIndexer
 {
-    public function addRepo(Repository $repo, $crdate = null);
-    public function updateRepo(Repository $repo, $crdate = null);
+    public function addRepo(Repository $repo, $crdate = null, $modate = null);
+    public function updateRepo(Repository $repo, $crdate = null, $modate = null);
     public function deleteAllRepos();
     public function deleteRepo(Repository $repo);
 }
index c33867b..f67dec6 100644 (file)
@@ -13,7 +13,8 @@ interface Database_ISearch
      * @param string  $sort    Sort order. Allowed values:
      *                         - id     - repository id
      *                         - crdate - creation date
-     *                         - tstamp - modification date
+     *                         - modate - modification date
+     *                         - tstamp - last index date
      *
      * @return Search_Result Search result object
      */
index 750e811..d4934bd 100644 (file)
@@ -22,11 +22,14 @@ $pager = new Html_Pager(
     $repoCount, $perPage, $page + 1, '/list/%d'
 );
 
+$db = new Database();
 render(
     'list',
     array(
-        'repos' => $repos,
-        'pager' => $pager,
+        'repos'   => $repos,
+        'pager'   => $pager,
+        'recents' => $db->getSearch()->listAll(0, 5, 'modate', 'desc'),
+        'dh'      => new \Date_HumanDiff(),
     )
 );
 ?>
index 7a70891..965f534 100644 (file)
@@ -28,7 +28,7 @@ render(
         'files'       => $phork,
         'description' => '',
         'htmlhelper'  => new HtmlHelper(),
-        'recents'     => $db->getSearch()->listAll(0, 5, 'crdate', 'desc'),
+        'recents'     => $db->getSearch()->listAll(0, 5, 'modate', 'desc'),
         'dh'          => new \Date_HumanDiff(),
     )
 );