Esempio n. 1
0
 /**
  * Save all the entries from the buffer.
  */
 public function save()
 {
     $entries = $this->logBuffer->getEntries();
     $entrySkeleton = ['ts' => $this->ts, 'month' => $this->month, 'year' => $this->year];
     $dimensionSkeleton = ['ts' => $this->ts, 'month' => $this->month, 'year' => $this->year];
     /**
      * @var $e LogEntry
      */
     foreach ($entries as $e) {
         // build entry
         $entry = $entrySkeleton;
         $entry['entity'] = $e->getName();
         $entry['ref'] = $e->getRef();
         // insert or update the DAILY stat
         $this->mongo->update(self::ADB_STATS_DAILY, ['entity' => $e->getName(), 'ref' => $e->getRef(), 'ts' => $this->ts], ['$inc' => ['count' => $e->getIncrement()], '$setOnInsert' => $entry], ['upsert' => true]);
         // insert or update the MONTHLY stat
         unset($entry['ts']);
         $entry['month'] = $this->month;
         $entry['year'] = $this->year;
         $this->mongo->update(self::ADB_STATS_MONTHLY, ['entity' => $e->getName(), 'ref' => $e->getRef(), 'ts' => $this->monthTs], ['$inc' => ['count' => $e->getIncrement()], '$setOnInsert' => $entry], ['upsert' => true]);
         // insert dimensions for the entry
         $dimEntry = $dimensionSkeleton;
         $dimEntry['entity'] = $e->getName();
         /**
          * @var $dim LogDimension
          */
         foreach ($e->getDimensions() as $dim) {
             $dimEntry['name'] = $dim->getName();
             $dimEntry['value'] = $dim->getValue();
             $this->mongo->update(self::ADB_DIMS, ['name' => $dim->getName(), 'value' => $dim->getValue(), 'entity' => $e->getName(), 'ts' => $this->ts], ['$inc' => ['count.' . $e->getRef() => $dim->getIncrement(), 'total' => $dim->getIncrement()], '$setOnInsert' => $dimEntry], ['upsert' => true]);
         }
     }
     // clear buffer
     $this->logBuffer = new LogBuffer();
 }
Esempio n. 2
0
 public function testGetEntriesAddEntry()
 {
     $lb = new LogBuffer();
     $this->assertTrue(is_array($lb->getEntries()));
     $this->assertSame(0, count($lb->getEntries()));
     $lb->addEntry(new LogEntry('test'));
     $this->assertSame(1, count($lb->getEntries()));
     $this->assertInstanceOf('Webiny\\AnalyticsDb\\LogEntry', $lb->getEntries()[0]);
 }