Browse Source

Major changes
Heartbeat now tracks counts for each term
Heartbeat now also has a term_usage table which tracks each event of a term being used
StatusTwitter class has been updated to make use of these new methods

logicp 7 years ago
parent
commit
a519694f3e
3 changed files with 68 additions and 4 deletions
  1. 24 0
      heartbeat.install
  2. 40 0
      src/Entity/Heartbeat.php
  3. 4 4
      src/HeartbeatStreamServices.php

+ 24 - 0
heartbeat.install

@@ -149,6 +149,30 @@ function heartbeat_schema() {
       'uid_relation' => array('uid', 'uid_target')
     )
   );
+
+  $schema['heartbeat_term_usage'] = array(
+    'fields' => array(
+      'id' => array(
+        'type' => 'serial',
+        'not null' => TRUE,
+      ),
+      'tid' => array(
+        'type' => 'int',
+        'unsigned' => TRUE,
+        'not null' => TRUE,
+        'default' => 0,
+      ),
+      'timestamp' => array(
+        'type' => 'int',
+        'unsigned' => TRUE,
+        'not null' => TRUE,
+      ),
+    ),
+    'primary key' => array('id'),
+    'unique keys' => array(
+      'tid_use' => array('tid', 'timestamp')
+    )
+  );
 //    'indexes' => array(
 //      'created' => array('created'),
 //    ),

+ 40 - 0
src/Entity/Heartbeat.php

@@ -882,6 +882,46 @@ class Heartbeat extends RevisionableContentEntityBase implements HeartbeatInterf
         }
       }
     }
+    return null;
+  }
+
+  /**
+   * @param $tid
+   */
+  public static function updateTermUsage($tid, $vid = null) {
+    $update = Database::getConnection()->update('taxonomy_term__field_count')
+      ->expression('field_count_value', 'field_count_value + 1')
+      ->condition('entity_id', $tid);
+
+    if (!$update->execute()) {
+      $insert = Database::getConnection()->insert('taxonomy_term__field_count')
+        ->fields([
+          'entity_id' => $tid,
+          'revision_id' => $tid,
+          'bundle' => $vid,
+          'langcode' => \Drupal::languageManager()->getDefaultLanguage()->getId(),
+          'delta' => 0,
+        'field_count_value' => 1]);
+      if (!$insert->execute()) {
+        \Drupal::logger('Heartbeat')
+          ->error('Unable to update term counts for Term with ID = %tid', array('%tid' => $tid));
+      } else {
+        self::newTermUsage($tid);
+      }
+    } else {
+     self::newTermUsage($tid);
+    }
+  }
+
+  /**
+   * @param $tid
+   */
+  public static function newTermUsage($tid) {
+    $insert = Database::getConnection()->insert('heartbeat_term_usage')
+      ->fields(['tid' => $tid, 'timestamp' => time()]);
+    if (!$insert->execute()) {
+      \Drupal::logger('Heartbeat')->error('Unable to update term usage for Term with ID = %tid', array('%tid' => $tid));
+    }
   }
 
 

+ 4 - 4
src/HeartbeatStreamServices.php

@@ -130,7 +130,7 @@ class HeartbeatStreamServices {
   }
 
   public function createStreamForUids($uids) {
-    return $this->entityTypeManager->getStorage('heartbeat')->loadMultiple($this->entityQuery->get('heartbeat')->condition('status', 1)->condition('uid', $uids, 'IN')->sort('created', 'DESC')->execute());
+    return $this->entityTypeManager->getStorage('heartbeat')->loadMultiple($this->entityQuery->get('heartbeat')->condition('status', 1)->condition('uid', $uids, 'IN')->sort('created', 'DESC')->range(0,50)->range(0,50)->execute());
   }
 
   public function createStreamByType($type) {
@@ -143,7 +143,7 @@ class HeartbeatStreamServices {
           $types[] = $value;
         }
       }
-      $beats = $this->entityTypeManager->getStorage('heartbeat')->loadMultiple($this->entityQuery->get('heartbeat')->condition('status', 1)->condition('type', $types, 'IN')->sort('created', 'DESC')->execute());
+      $beats = $this->entityTypeManager->getStorage('heartbeat')->loadMultiple($this->entityQuery->get('heartbeat')->condition('status', 1)->condition('type', $types, 'IN')->sort('created', 'DESC')->range(0,50)->execute());
 
       if (count($beats) > 0) {
         $this->lastId = call_user_func('end', array_keys($beats));
@@ -169,7 +169,7 @@ class HeartbeatStreamServices {
         }
       }
 //      $uids[] = $currentUid;
-      $beats = $this->entityTypeManager->getStorage('heartbeat')->loadMultiple($this->entityQuery->get('heartbeat')->condition('status', 1)->condition('type', $types, 'IN')->condition('uid', $uids, 'IN')->sort('created', 'DESC')->execute());
+      $beats = $this->entityTypeManager->getStorage('heartbeat')->loadMultiple($this->entityQuery->get('heartbeat')->condition('status', 1)->condition('type', $types, 'IN')->condition('uid', $uids, 'IN')->sort('created', 'DESC')->range(0,50)->execute());
 
       if (count($beats) > 0) {
         $this->lastId = call_user_func('end', array_keys($beats));
@@ -243,7 +243,7 @@ class HeartbeatStreamServices {
     $stream = $this->entityTypeManager->getStorage('heartbeat_stream')->load(array_values($this->loadStream($type))[0]);
     $uids[] = $currentUid;
     return $this->entityTypeManager->getStorage('heartbeat')->loadMultiple($this->entityQuery->get('heartbeat')->condition('status', 1)->condition('revision_created', $this->latestTimestamp, '>')->condition('type', array_column($stream->getTypes(), 'target_id'), 'IN')->condition('uid', $uids, 'IN')->sort('created', 'DESC')->execute());
-//range(0,50)->=pppp
+//range(0,50)->
   }
 
 }