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 d0732ea45b48d464d21e807656eea9960e80e304..aee3100bf49e417b26eb77d61f52465a7c974fc9 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 7548c2ad98d66851494b491aad4628a7afc8a57b..0f6971961ed8afb5fef589c7fd3c6cd7ce9364ba 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 ed262ac8c3e71734342d15994581b2de6cc5d3e4..cb9f3d8d79e9c4c54e184d5ca8465208eff9c7bb 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 4b529d1c7ee10b54997405f96c29fa5a02604145..5da912e2aa9750faceaa004c20841cb5847feceb 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 cf50bbe3fb2b592a1d2498fbeeae9974ab7c41e3..e93cfa7c4b93d13b209d6b09fbbbef462cf99cb3 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 e960e043eb765d917d0acdcea6bbc545cf24056b..ddbc13b7286e2793ac176aaf39f95fb7fc59dd15 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 0df287475051740bdf8b9bf84de692dbb1080f4f..bb76d548006976143a1af5ed7ce166ab5bd1c77d 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 3b0c9906b2e13648ef9798c1432eebf66a8653b1..4ad63f1d964edb5f3601fa7fb7a73ff279945cb3 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 61b8a87051df550b6923a1f83329b35a76a35af1..232e1eebfb43adb258c1de4504c462b9fa8d3a63 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 c33867b78dc8ca5f7ee5d68d89681075720418ce..f67dec6750eb1ae2959a49ca5ba0649837a9817e 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 750e811dfbbe6301d21471c872f18abd1edf8255..d4934bd271ad9a3e2e70c0fb5e023697142a8b08 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 7a708917ef45912a6be54cb6e82e069bdbc90858..965f5340e421f5eda836367bf0b27e6847502427 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(),
     )
 );