Permalink
Browse files

Use commit timestamps for ElasticSearch and sort on last commit time.

  • Loading branch information...
1 parent 8d29b8e commit d5901a1fcc8b79f9a23bd3888ef2f281f07b9459 @jnovack jnovack committed Sep 30, 2012
View
@@ -11,3 +11,14 @@
{% 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 %}
View
@@ -38,7 +38,7 @@
{% block sidebar %}
{% if recents.results %}
- <h3>Recently created</h3>
+ <h3>Recently updated</h3>
<ul>
{% for repo in recents.repos %}
{% include 'repo-sidebar-list.htm' %}
@@ -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>
View
@@ -37,6 +37,8 @@ function ($class) {
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);
}
?>
@@ -9,15 +9,18 @@ public function __construct()
}
- 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 @@ public function updateRepo(Repository $repo, $crdate = null)
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 @@ protected function getCrDate(Repository $repo)
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(
@@ -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 @@ public function __construct()
* @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 @@ public function listAll($page = 0, $perPage = 10, $sort = 'id', $sortOrder = nul
$r = new Repository();
$r->loadById($hit->_source->id);
$r->crdate = strtotime($hit->_source->crdate);
+ $r->modate = strtotime($hit->_source->modate);
$sres->repos[] = $r;
}
@@ -59,9 +59,13 @@ public function reset()
//creation date
'type' => 'date',
),
- 'tstamp' => (object)array(
+ 'modate' => (object)array(
//modification date
'type' => 'date',
+ ),
+ 'tstamp' => (object)array(
+ //last indexed date
+ 'type' => 'date',
)
)
)
@@ -3,11 +3,11 @@
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)
{
}
@@ -3,8 +3,8 @@
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);
}
@@ -13,7 +13,8 @@ public function search($term, $page = 0, $perPage = 10);
* @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
*/
View
@@ -22,11 +22,14 @@
$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(),
)
);
?>
View
@@ -28,7 +28,7 @@
'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(),
)
);

0 comments on commit d5901a1

Please sign in to comment.