Skip to content
This repository has been archived by the owner on Sep 10, 2021. It is now read-only.

Commit

Permalink
ENH: optimize getRandomItem request
Browse files Browse the repository at this point in the history
added "generated in' at the bottom of the layout
  • Loading branch information
Charles Marion committed Mar 10, 2011
1 parent e309343 commit e4e8eac
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 62 deletions.
1 change: 1 addition & 0 deletions application/AppController.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ public function postDispatch()
{
parent::postDispatch();
$this->view->json=JsonComponent::encode($this->view->json);
$this->view->generatedTimer= round((microtime(true) - START_TIME),3);
if (Zend_Registry::get('config')->mode->test!=1)
{
header('Content-Type: text/html; charset=ISO-8859-1');
Expand Down
6 changes: 5 additions & 1 deletion application/layouts/layout.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,11 @@
<div class="clear"/>
</div>
</div>
<div class="Footer"><a href="http://www.kitware.com/products/midas.html">MIDAS</a> <?php echo $this->version ?> by <a href="http://www.kitware.com">Kitware</a> © <?php echo date("Y") ?></div>
<div class="Footer"><a href="http://www.kitware.com/products/midas.html">MIDAS</a> <?php echo $this->version ?> by <a href="http://www.kitware.com">Kitware</a> © <?php echo date("Y") ?> -
<?php
echo $this->t('Generated in').' '.$this->generatedTimer." ms";
?>
</div>
</div>
<hr style="display: none;"/>
</div>
Expand Down
95 changes: 41 additions & 54 deletions application/models/pdo/ItemModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,73 +97,60 @@ function getRandomItems($userDao=null,$policy=0,$limit=10,$thumbnailFilter=false
$userId = $userDao->getUserId();
}


$subqueryUser= $this->select()
->setIntegrityCheck(false)
->from(array('f' => 'item'))
->join(array('p' => 'itempolicyuser'),
'f.item_id=p.item_id',
array('p.policy'))
->where('p.policy >= ?', $policy)
->where('p.user_id = ? ',$userId);

$subqueryGroup = $this->select()
->setIntegrityCheck(false)
->from(array('f' => 'item'))
->join(array('p' => 'itempolicygroup'),
'f.item_id=p.item_id',
array('p.policy'))
->where('p.policy >= ?', $policy)
->where('( '.$this->_db->quoteInto('p.group_id = ? ',MIDAS_GROUP_ANONYMOUS_KEY).' OR
p.group_id IN (' .new Zend_Db_Expr(
$this->select()
->setIntegrityCheck(false)
->from(array('u2g' => 'user2group'),
array('group_id'))
->where('u2g.user_id = ?' , $userId)
.'))' ))
->limit($limit*2)
->order( new Zend_Db_Expr('RAND() ASC') );

$sql=$this->select()
->setIntegrityCheck(false)
->from(array('i' => 'item'))
->join(array('tt'=> $this->select()
->from(array('i' => 'item'),array('maxid'=>'MAX(item_id)'))
),
' i.item_id >= FLOOR(tt.maxid*RAND())')
->joinLeft(array('ip' => 'itempolicyuser'),'
i.item_id = ip.item_id AND '.$this->_db->quoteInto('ip.policy >= ?', $policy).'
AND '.$this->_db->quoteInto('user_id = ? ',$userId).' ',array('userpolicy'=>'ip.policy'))
->joinLeft(array('ipg' => 'itempolicygroup'),'
i.item_id = ipg.item_id AND '.$this->_db->quoteInto('ipg.policy >= ?', $policy).'
AND ( '.$this->_db->quoteInto('group_id = ? ',MIDAS_GROUP_ANONYMOUS_KEY).' OR
group_id IN (' .new Zend_Db_Expr(
$this->select()
->setIntegrityCheck(false)
->from(array('u2g' => 'user2group'),
array('group_id'))
->where('u2g.user_id = ?' , $userId)
) .'))' ,array('grouppolicy'=>'ipg.policy'))
->where(
'(
ip.item_id is not null or
ipg.item_id is not null)'
)
->limit($limit*2)
;
if($thumbnailFilter)
{
$subqueryUser->where('thumbnail != ?','');
$subqueryGroup->where('thumbnail != ?','');
$sql->where('thumbnail != ?','');
}
$sql = $this->select()
->union(array($subqueryUser, $subqueryGroup));

$rowset = $this->fetchAll($sql);
$rowsetAnalysed=array();
foreach ($rowset as $keyRow=>$row)
{
foreach($rowsetAnalysed as $keyRa=>$ra)
if($row['userpolicy']==null)$row['userpolicy']=0;
if($row['grouppolicy']==null)$row['grouppolicy']=0;
if(!isset($rowsetAnalysed[$row['item_id']])||($rowsetAnalysed[$row['item_id']]->policy<$row['userpolicy']&&$rowsetAnalysed[$row['item_id']]->policy<$row['grouppolicy']))
{
if($ra->getKey()==$row['item_id'])
$tmpDao= $this->initDao('Item', $row);
if($row['userpolicy']>=$row['grouppolicy'])
{
if($ra->policy<$row['item_id'])
{
$rowsetAnalysed[$keyRa]->policy=$row['policy'];
}
unset($row);
break;
$tmpDao->policy=$row['userpolicy'];
}
}
if(isset($row))
{
$tmpDao= $this->initDao('Item', $row);
$tmpDao->policy=$row['policy'];
$rowsetAnalysed[] = $tmpDao;
else
{
$tmpDao->policy=$row['grouppolicy'];
}
$rowsetAnalysed[$row['item_id']] = $tmpDao;
unset($tmpDao);
}
}
$i=1;
foreach($rowsetAnalysed as $keyRa=>$r)
{
if($i>$limit)
{
unset($rowsetAnalysed[$keyRa]);
}
$i++;
}
return $rowsetAnalysed;
}//end get random

Expand Down
16 changes: 12 additions & 4 deletions application/views/element/feed.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,15 @@
switch($type)
{
case MIDAS_FEED_CREATE_COMMUNITY:
echo "{$view->t("added the community")} <a href='{$view->webroot}/community/{$feed->getRessource()->getKey()}'>{$feed->getRessource()->getName()}</a>";
echo "{$view->t("added the community")} <a href='{$view->webroot}/community/{$feed->getRessource()->getKey()}'>".trimName($feed->getRessource()->getName())."</a>";
break;
case MIDAS_FEED_UPDATE_COMMUNITY:
case MIDAS_FEED_CREATE_FOLDER:
case MIDAS_FEED_CREATE_ITEM:
echo "{$view->t("added the item")} <a href='{$view->webroot}/item/{$feed->getRessource()->getKey()}'>{$feed->getRessource()->getName()}</a>";
echo "{$view->t("added the item")} <a href='{$view->webroot}/item/{$feed->getRessource()->getKey()}'>".trimName($feed->getRessource()->getName())."</a>";
break;
case MIDAS_FEED_CREATE_LINK_ITEM:
echo "{$view->t("added the link")} <a href='{$view->webroot}/item/{$feed->getRessource()->getKey()}'>{$feed->getRessource()->getName()}</a>";
echo "{$view->t("added the link")} <a href='{$view->webroot}/item/{$feed->getRessource()->getKey()}'>".trimName($feed->getRessource()->getName())."</a>";
break;
case MIDAS_FEED_CREATE_REVISION:
case MIDAS_FEED_CREATE_USER:
Expand All @@ -71,7 +71,15 @@
echo "</div>";
echo "<div style='clear:left;'></div>";
}


function trimName($name)
{
if(strlen($name)>35)
{
$name=substr($name,0, 10).'...'.substr($name,strlen($name)-35);
}
return $name;
}

?>

8 changes: 6 additions & 2 deletions public/js/layout/jquery.treeTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -260,8 +260,12 @@
{
if(name.length*7+padding>350)
{
toremove=(name.length*7+padding-350)/7;
name=name.substring(0,10)+'...'+name.substring(name.length+10+3-toremove);
toremove=(name.length*7+padding-350)/8;
if(toremove<13)
{
return 'error';
}
name=name.substring(0,10)+'...'+name.substring(name.length+13-toremove);
return name;
}
return name;
Expand Down
3 changes: 2 additions & 1 deletion translation/fr-main.csv
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,5 @@ No;Non
Plugins;Plugins
E-mail;E-mail
Remember Me;Se souvenir de moi
Members;Membres
Members;Membres
Generated in;G�n�r� en

0 comments on commit e4e8eac

Please sign in to comment.