Skip to content

Commit

Permalink
Merge pull request #69 from phiHero/feat/analytics-events
Browse files Browse the repository at this point in the history
Add support for analytics events
  • Loading branch information
jasonbosco authored Jun 25, 2024
2 parents f88d50f + 1d34282 commit 2b04944
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 22 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:
runs-on: ubuntu-latest
services:
typesense:
image: typesense/typesense:26.0
image: typesense/typesense:27.0.rc21
ports:
- 8108:8108/tcp
volumes:
Expand Down
20 changes: 10 additions & 10 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
version: '3.5'

services:
typesense:
image: typesense/typesense:26.0
environment:
TYPESENSE_DATA_DIR: /data
TYPESENSE_API_KEY: xyz
volumes:
- /tmp/typesense-server-data:/data
ports:
- 8108:8108
restart: "no"
typesense:
image: typesense/typesense:27.0.rc21
environment:
TYPESENSE_DATA_DIR: /data
TYPESENSE_API_KEY: xyz
volumes:
- /tmp/typesense-server-data:/data
ports:
- 8108:8108
restart: 'no'
10 changes: 10 additions & 0 deletions src/Analytics.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ class Analytics

private AnalyticsRules $rules;

private AnalyticsEvents $events;

public function __construct(ApiCall $apiCall)
{
$this->apiCall = $apiCall;
Expand All @@ -22,4 +24,12 @@ public function rules()
}
return $this->rules;
}

public function events()
{
if (!isset($this->events)) {
$this->events = new AnalyticsEvents($this->apiCall);
}
return $this->events;
}
}
47 changes: 47 additions & 0 deletions src/AnalyticsEvents.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace Typesense;

/**
* Class AnalyticsEvents
*
* @package \Typesense
*/
class AnalyticsEvents
{
const RESOURCE_PATH = '/analytics/events';

/**
* @var ApiCall
*/
private ApiCall $apiCall;

/**
* AnalyticsEvents constructor.
*
* @param ApiCall $apiCall
*/
public function __construct(ApiCall $apiCall)
{
$this->apiCall = $apiCall;
}

/**
* @param array $params
*
* @return array
* @throws TypesenseClientError|HttpClientException
*/
public function create($params)
{
return $this->apiCall->post($this->endpoint_path(), $params);
}

/**
* @return string
*/
private function endpoint_path($operation = null)
{
return self::RESOURCE_PATH . ($operation === null ? '' : "/$operation");
}
}
69 changes: 59 additions & 10 deletions tests/Feature/AnalyticsEventsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,66 @@

class AnalyticsEventsTest extends TestCase
{
//* there is no method for sending events
// public function testCanCreateAnEvent(): void
// {
// $returnData = $this->client()->analytics->rules()->even
// $this->assertEquals($returnData['name'], $this->ruleName);
// }
private $ruleName = 'product_queries_aggregation';

public function testNeedImplementationForAnalyticsEvents(): void
protected function setUp(): void
{
$this->markTestIncomplete(
'This test has not been implemented yet.',
);
parent::setUp();
$this->client()->collections->create([
"name" => "products",
"fields" => [
[
"name" => "title",
"type" => "string"
],
[
"name" => "popularity",
"type" => "int32",
"optional" => true
]
]
]);
$this->client()->analytics->rules()->upsert($this->ruleName, [
"name" => "products_popularity",
"type" => "counter",
"params" => [
"source" => [
"collections" => [
"products"
],
"events" => [
[
"type" => "click",
"weight" => 1,
"name" => "products_click_event"
]
]
],
"destination" => [
"collection" => "products",
"counter_field" => "popularity"
]
]
]);
}

protected function tearDown(): void
{
parent::tearDown();
$this->client()->analytics->rules()->{'product_queries_aggregation'}->delete();
}

public function testCanCreateAnEvent(): void
{
$response = $this->client()->analytics->events()->create([
"type" => "click",
"name" => "products_click_event",
"data" => [
"q" => "nike shoes",
"doc_id" => "1024",
"user_id" => "111112"
]
]);
$this->assertTrue($response['ok']);
}
}
10 changes: 9 additions & 1 deletion tests/Feature/AnalyticsRulesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

class AnalyticsRulesTest extends TestCase
{
private $ruleName = 'product_queries_aggregation';
private $ruleName = 'test_rule';
private $ruleConfiguration = [
"type" => "popular_queries",
"params" => [
Expand All @@ -29,6 +29,14 @@ protected function setUp(): void
$this->ruleUpsertResponse = $this->client()->analytics->rules()->upsert($this->ruleName, $this->ruleConfiguration);
}

protected function tearDown(): void
{
$rules = $this->client()->analytics->rules()->retrieve();
foreach ($rules['rules'] as $rule) {
$this->client()->analytics->rules()->{$rule['name']}->delete();
}
}

public function testCanUpsertARule(): void
{
$this->assertEquals($this->ruleName, $this->ruleUpsertResponse['name']);
Expand Down

0 comments on commit 2b04944

Please sign in to comment.