Ejemplo n.º 1
0
 /**
  * @throws \Exception
  */
 public function testCreate()
 {
     $dummy = new DummyDatabase();
     $this->assertTrue(MonipdbQuery::is_a(Query::create('monipdb', __DIR__ . '/17monipdb.dat')));
     $this->assertTrue(QqwryQuery::is_a(Query::create('qqwry', __DIR__ . '/qqwry.dat')));
     $this->assertTrue(FullQuery::is_a(Query::create('full', $dummy)));
     $this->assertTrue(MiniQuery::is_a(Query::create('mini', $dummy)));
     $this->assertTrue(ChinaQuery::is_a(Query::create('china', $dummy)));
     $this->assertTrue(WorldQuery::is_a(Query::create('world', $dummy)));
     $this->assertTrue(FreeipipQuery::is_a(Query::create('freeipip', null)));
     $this->assertTrue(SinaQuery::is_a(Query::create('sina', null)));
     $this->assertTrue(TaobaoQuery::is_a(Query::create('taobao', null)));
     $this->assertTrue(BaidumapQuery::is_a(Query::create('baidumap', null)));
     $this->assertTrue(DummyQuery::is_a(Query::create('qqwry', ['class' => DummyQuery::className()])));
 }
Ejemplo n.º 2
0
 /**
  * @param Query $query
  * @return array
  */
 protected function version(Query $query)
 {
     return ['ipv4.larryli.cn', date('Ymd'), $query->className(), $query->name()];
 }
Ejemplo n.º 3
0
 /**
  * @param OutputInterface $output
  * @param Query $query
  * @param string $name
  * @throws \Exception
  */
 private function clean(OutputInterface $output, Query $query, $name)
 {
     $output->write("<info>clean {$name}:</info>");
     $query->clean();
     $output->writeln('<info> completed!</info>');
 }
Ejemplo n.º 4
0
 /**
  * @param string $name
  * @param mixed $options
  * @param array $providers
  * @return Query|null
  * @throws \Exception
  */
 public function createQuery($name, $options, array $providers = [])
 {
     $query = $this->getQuery($name);
     if ($query == null) {
         $query = Query::create($name, $options);
         $query->setProviders(array_map(function ($provider) {
             return $this->getQuery($provider);
         }, $providers));
         $this->objects[$name] = $query;
     }
     return $query;
 }
Ejemplo n.º 5
0
 /**
  * @param Query $query
  * @param string[] $divisions
  * @return array
  */
 private function divisionsWithId(Query $query, $divisions)
 {
     $result = [];
     $this->stdout("translate division to division_id:\n", Console::FG_GREEN);
     $total = count($divisions);
     Console::startProgress(0, $total);
     $n = 0;
     $time = Query::time();
     foreach ($divisions as $division) {
         $result[$division] = $query->idByDivision($division);
         $n++;
         if ($time < Query::time()) {
             Console::updateProgress($n, $total);
             $time = Query::time();
         }
     }
     Console::updateProgress($total, $total);
     Console::endProgress();
     $this->stdout(" completed!\n", Console::FG_GREEN);
     return $result;
 }
Ejemplo n.º 6
0
 /**
  * @param Query $query
  * @param string $name
  * @param integer $ip
  * @throws \Exception
  */
 private function query(Query $query, $name, $ip)
 {
     $address = $query->find($ip);
     $this->stdout("\t{$name}: ", Console::FG_YELLOW);
     $this->stdout("{$address}\n");
 }
Ejemplo n.º 7
0
 /**
  * @param OutputInterface $output
  * @param Query $query
  * @param string $name
  * @param integer $ip
  * @throws \Exception
  */
 private function query(OutputInterface $output, Query $query, $name, $ip)
 {
     $address = $query->find($ip);
     $output->writeln("\t<comment>{$name}:</comment> {$address}");
 }
Ejemplo n.º 8
0
 /**
  * @param Query $query
  * @param string $name
  * @param bool $force
  * @return void
  * @throws \Exception
  */
 protected function generate(Query $query, $name, $force)
 {
     $use = implode(', ', $query->getProviders());
     if (!$force && count($query) > 0) {
         $this->stdout("use exist {$name} table.\n", Console::FG_YELLOW);
     } else {
         $this->stdout("generate {$name} table with {$use}:\n", Console::FG_GREEN);
         if (empty($this->controller->noProgress)) {
             $query->init(function ($code, $n) {
                 static $total = 0;
                 switch ($code) {
                     case 0:
                         Console::startProgress(0, $n);
                         $total = $n;
                         break;
                     case 1:
                         Console::updateProgress($n, $total);
                         break;
                     case 2:
                         Console::updateProgress($total, $total);
                         Console::endProgress();
                         break;
                 }
             });
         } else {
             $query->init();
         }
         $this->stdout(" completed!\n", Console::FG_GREEN);
     }
 }
Ejemplo n.º 9
0
 /**
  * @param OutputInterface $output
  * @param Query $query
  * @param string $name
  * @param bool $force
  * @param bool $noProgress
  * @return void
  * @throws \Exception
  */
 protected function generate(OutputInterface $output, Query $query, $name, $force, $noProgress)
 {
     $use = implode(', ', $query->getProviders());
     if (!$force && $query->exists()) {
         $output->writeln("<comment>use exist {$name} table.</comment>", OutputInterface::VERBOSITY_VERBOSE);
     } else {
         $output->writeln("<info>generate {$name} table with {$use}:</info>");
         if (!$noProgress) {
             $query->init(function ($code, $n) use($output) {
                 switch ($code) {
                     case 0:
                         $this->progress = new ProgressBar($output, $n);
                         $this->progress->start();
                         break;
                     case 1:
                         $this->progress->setProgress($n);
                         break;
                     case 2:
                         $this->progress->finish();
                         break;
                 }
             });
         } else {
             $query->init();
         }
         $output->writeln('<info> completed!</info>');
     }
 }
Ejemplo n.º 10
0
 /**
  * @param Query $query
  * @param string $name
  * @throws \Exception
  */
 private function clean(Query $query, $name)
 {
     $this->stdout("clean {$name}:", Console::FG_GREEN);
     $query->clean();
     $this->stdout(" completed!\n", Console::FG_GREEN);
 }
Ejemplo n.º 11
0
 /**
  * @param OutputInterface $output
  * @param Query $query
  * @param $name
  * @param bool $noProgress
  */
 private function dumpCount(OutputInterface $output, Query $query, $name, $noProgress)
 {
     $filename = 'dump_' . $name . '_count.json';
     $result = [];
     if (count($query) > 0) {
         $output->writeln("<info>dump {$filename}:</info>");
         if (!$noProgress) {
             $this->progress = new ProgressBar($output, count($query));
             $this->progress->start();
         }
         $n = 0;
         $time = Query::time();
         $last = -1;
         foreach ($query as $ip => $division) {
             if (is_integer($division)) {
                 $id = $division;
                 $division = $query->divisionById($id);
             } else {
                 $id = $query->idByDivision($division);
             }
             if ($id === null) {
                 die(long2ip($ip));
             }
             $count = $ip - $last;
             $last = $ip;
             $result[$id]['id'] = $id;
             $result[$id]['division'] = empty($id) ? '' : $division;
             @($result[$id]['records'] += 1);
             // 纪录数
             @($result[$id]['count'] += $count);
             // IP 数
             if ($id > 100000) {
                 // 中国
                 @($result[1]['records'] += 1);
                 @($result[1]['children_records'] += 1);
                 @($result[1]['count'] += $count);
                 @($result[1]['children_count'] += $count);
                 $province = intval($id / 10000) * 10000;
                 if ($province != $id) {
                     @($result[$province]['records'] += 1);
                     @($result[$province]['children_records'] += 1);
                     @($result[$province]['count'] += $count);
                     @($result[$province]['children_count'] += $count);
                 }
             }
             $n++;
             if (!$noProgress && $time < Query::time()) {
                 $this->progress->setProgress($n);
                 $time = Query::time();
             }
         }
         if (!$noProgress) {
             $this->progress->finish();
         }
         $output->writeln('<info> completed!</info>');
     }
     ksort($result);
     $result = array_map(function ($data) {
         $result = ['id' => $data['id'], 'division' => $data['division'], 'records' => $data['records'], 'count' => $data['count']];
         if (isset($data['children_records'])) {
             $result['self']['records'] = $data['records'] - $data['children_records'];
             $result['children']['records'] = $data['children_records'];
         }
         if (isset($data['children_count'])) {
             $result['self']['count'] = $data['count'] - $data['children_count'];
             $result['children']['count'] = $data['children_count'];
         }
         return $result;
     }, array_values($result));
     if (count($result) > 0) {
         $this->write($output, $filename, $result);
     }
 }