private function buildChartForm()
 {
     $request = $this->getRequest();
     $user = $request->getUser();
     $table = new PhabricatorFactRaw();
     $conn_r = $table->establishConnection('r');
     $table_name = $table->getTableName();
     $facts = queryfx_all($conn_r, 'SELECT DISTINCT factType from %T', $table_name);
     $specs = PhabricatorFactSpec::newSpecsForFactTypes(PhabricatorFactEngine::loadAllEngines(), ipull($facts, 'factType'));
     $options = array();
     foreach ($specs as $spec) {
         if ($spec->getUnit() == PhabricatorFactSpec::UNIT_COUNT) {
             $options[$spec->getType()] = $spec->getName();
         }
     }
     if (!$options) {
         return id(new AphrontErrorView())->setSeverity(AphrontErrorView::SEVERITY_NOTICE)->setTitle(pht('No Chartable Facts'))->appendChild('<p>' . pht('There are no facts that can be plotted yet.') . '</p>');
     }
     $form = id(new AphrontFormView())->setUser($user)->appendChild(id(new AphrontFormSelectControl())->setLabel('Y-Axis')->setName('y1')->setOptions($options))->appendChild(id(new AphrontFormSubmitControl())->setValue('Plot Chart'));
     $panel = new AphrontPanelView();
     $panel->appendChild($form);
     $panel->setWidth(AphrontPanelView::WIDTH_FORM);
     $panel->setHeader('Plot Chart');
     return $panel;
 }
 public function computeAggregateFacts()
 {
     $table = new PhabricatorFactRaw();
     $table_name = $table->getTableName();
     $conn = $table->establishConnection('r');
     $counts = queryfx_all($conn, 'SELECT factType, SUM(valueX) N FROM %T WHERE factType LIKE %>
     GROUP BY factType', $table_name, 'N:');
     $facts = array();
     foreach ($counts as $count) {
         $facts[] = id(new PhabricatorFactAggregate())->setFactType('+' . $count['factType'])->setValueX($count['N']);
     }
     return $facts;
 }
 public function processRequest()
 {
     $request = $this->getRequest();
     $user = $request->getUser();
     $table = new PhabricatorFactRaw();
     $conn_r = $table->establishConnection('r');
     $table_name = $table->getTableName();
     $series = $request->getStr('y1');
     $specs = PhabricatorFactSpec::newSpecsForFactTypes(PhabricatorFactEngine::loadAllEngines(), array($series));
     $spec = idx($specs, $series);
     $data = queryfx_all($conn_r, 'SELECT valueX, epoch FROM %T WHERE factType = %s ORDER BY epoch ASC', $table_name, $series);
     $points = array();
     $sum = 0;
     foreach ($data as $key => $row) {
         $sum += (int) $row['valueX'];
         $points[(int) $row['epoch']] = $sum;
     }
     if (!$points) {
         // NOTE: Raphael crashes Safari if you hand it series with no points.
         throw new Exception('No data to show!');
     }
     // Limit amount of data passed to browser.
     $count = count($points);
     $limit = 2000;
     if ($count > $limit) {
         $i = 0;
         $every = ceil($count / $limit);
         foreach ($points as $epoch => $sum) {
             $i++;
             if ($i % $every && $i != $count) {
                 unset($points[$epoch]);
             }
         }
     }
     $x = array_keys($points);
     $y = array_values($points);
     $id = celerity_generate_unique_node_id();
     $chart = phutil_tag('div', array('id' => $id, 'style' => 'border: 1px solid #6f6f6f; ' . 'margin: 1em 2em; ' . 'background: #ffffff; ' . 'height: 400px; '), '');
     require_celerity_resource('raphael-core');
     require_celerity_resource('raphael-g');
     require_celerity_resource('raphael-g-line');
     Javelin::initBehavior('line-chart', array('hardpoint' => $id, 'x' => array($x), 'y' => array($y), 'xformat' => 'epoch', 'colors' => array('#0000ff')));
     $panel = new AphrontPanelView();
     $panel->setHeader('Count of ' . $spec->getName());
     $panel->appendChild($chart);
     $crumbs = $this->buildApplicationCrumbs();
     $crumbs->addTextCrumb(pht('Chart'));
     return $this->buildApplicationPage(array($crumbs, $panel), array('title' => 'Chart', 'device' => false));
 }
Пример #4
0
 private function updateRawFacts(array $map)
 {
     foreach ($map as $phid => $facts) {
         assert_instances_of($facts, 'PhabricatorFactRaw');
     }
     $phids = array_keys($map);
     if (!$phids) {
         return;
     }
     $table = new PhabricatorFactRaw();
     $conn = $table->establishConnection('w');
     $table_name = $table->getTableName();
     $sql = array();
     foreach ($map as $phid => $facts) {
         foreach ($facts as $fact) {
             $sql[] = qsprintf($conn, '(%s, %s, %s, %d, %d, %d)', $fact->getFactType(), $fact->getObjectPHID(), $fact->getObjectA(), $fact->getValueX(), $fact->getValueY(), $fact->getEpoch());
         }
     }
     $table->openTransaction();
     queryfx($conn, 'DELETE FROM %T WHERE objectPHID IN (%Ls)', $table_name, $phids);
     if ($sql) {
         foreach (array_chunk($sql, 256) as $chunk) {
             queryfx($conn, 'INSERT INTO %T
           (factType, objectPHID, objectA, valueX, valueY, epoch)
           VALUES %Q', $table_name, implode(', ', $chunk));
         }
     }
     $table->saveTransaction();
 }
 public function handleRequest(AphrontRequest $request)
 {
     $viewer = $request->getViewer();
     $table = new PhabricatorFactRaw();
     $conn_r = $table->establishConnection('r');
     $table_name = $table->getTableName();
     $series = $request->getStr('y1');
     $specs = PhabricatorFactSpec::newSpecsForFactTypes(PhabricatorFactEngine::loadAllEngines(), array($series));
     $spec = idx($specs, $series);
     $data = queryfx_all($conn_r, 'SELECT valueX, epoch FROM %T WHERE factType = %s ORDER BY epoch ASC', $table_name, $series);
     $points = array();
     $sum = 0;
     foreach ($data as $key => $row) {
         $sum += (int) $row['valueX'];
         $points[(int) $row['epoch']] = $sum;
     }
     if (!$points) {
         throw new Exception('No data to show!');
     }
     // Limit amount of data passed to browser.
     $count = count($points);
     $limit = 2000;
     if ($count > $limit) {
         $i = 0;
         $every = ceil($count / $limit);
         foreach ($points as $epoch => $sum) {
             $i++;
             if ($i % $every && $i != $count) {
                 unset($points[$epoch]);
             }
         }
     }
     $x = array_keys($points);
     $y = array_values($points);
     $id = celerity_generate_unique_node_id();
     $chart = phutil_tag('div', array('id' => $id, 'style' => 'background: #ffffff; ' . 'height: 480px; '), '');
     require_celerity_resource('d3');
     Javelin::initBehavior('line-chart', array('hardpoint' => $id, 'x' => array($x), 'y' => array($y), 'xformat' => 'epoch', 'colors' => array('#0000ff')));
     $box = id(new PHUIObjectBoxView())->setHeaderText(pht('Count of %s', $spec->getName()))->appendChild($chart);
     $crumbs = $this->buildApplicationCrumbs();
     $crumbs->addTextCrumb(pht('Chart'));
     $title = pht('Chart');
     return $this->newPage()->setTitle($title)->setCrumbs($crumbs)->appendChild($box);
 }