I created a rest service which every user on the site calls from a product we are selling.
How do I count the number of calls for every user so I can generate a report of this.
D8
Drupal Answers is a question and answer site for Drupal developers and administrators. It only takes a minute to sign up.
Sign up to join this communityDo your REST service needs authentication?
If yes, you just log each access attempt by users: One attempt per one record per user or one record per user and updating the count column always.
If there is no authentication, you will have harder time to achieve this. It would be also good to know HOW they are making this rest calls (eg: clicking on a button which resolves into a rest call?).
You could still try to check session and find out which user made the requeset.
You can create a new Content Entity. With this entity you can save all calls on the rest service (p.e. user, url or product, time, ...).
Drupal creates a database table for each entity and with the ContentEntityBase
it is easy to save new entries, read all entries, read part of the entries, search or whatever you want to do.
There exists a big introduction & documentation
Addition
When you save all the calls you can easy read the calls out with the entity. And based on the informations you have saved, you can count the number of calls to your rest service and you can generate the report.
Code Examples
example_module
in modules/custom
modules/custom/example_module/example_module.info.yml
name: EXAMPLE MODULE
description: YOUR DESCRIPTION
package: YOUR PACKAGE
type: module
core: 8.x
modules/custom/example_modules/src/Entity/ExampleEntity.php
<?php
namespace Drupal\example_module\Entity;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Entity\EntityTypeInterface;
/**
* Defines the Example Module entity.
*
* @ingroup example_module
*
* @ContentEntityType(
* id = "example_entity_name",
* label = @Translation("Example entity"),
* base_table = "example_entity_name",
* entity_keys = {
* "id" = "id",
* "user_id" = "user_id",
* }
* )
*/
class ExampleEntity extends ContentEntityBase {
/**
* {@inheritdoc}
*
* Define the base fields.
*/
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
// Standard field, used as unique if primary index.
$fields['id'] = BaseFieldDefinition::create('integer')
->setLabel(t('ID'))
->setDescription(t('The ID of the entity.'))
->setReadOnly(TRUE);
// Save user_id as a string
$fields['user_id'] = BaseFieldDefinition::create('string')
->setLabel(t('User'))
->setDescription(t('The associated user.'))
->setSettings([
'max_length' => 255,
'text_processing' => 0,
]);
// You can replace the fields or add more. Feel free!
return $fields;
}
}
Enable the module: $ drush en example_module
Use the entity to save or load content. For example in a Controller of the rest service.
// Somewhere above the Controller
use Drupal\example_module\Entity\ExampleEntity;
// Create a new entry
$user_id = Drupal::request()->get('user');
$entityContent = array(
'user_id' => $user_id,
);
$exampleEntity = ExampleEntity::create($entityContent);
$exampleEntity->save();
// Load entries by user_id
$entityTypeManager = Drupal::entityTypeManager();
$exampleEntityStorage = $entityTypeManager->getStorage('example_entity_name');
$entries = $exampleEntityStorage->loadByProperties($entityContent);
// Now you can count the entries.
Be careful, there are different ways to work with Drupal and different ways where to place your own code. Without seeing your code base I can only help with this "default way".