Skip to content

Commit

Permalink
move out repetative ingressenum and move in sourceenum
Browse files Browse the repository at this point in the history
  • Loading branch information
alnutile committed Jun 17, 2023
1 parent 9f41a3b commit e9fa4ba
Show file tree
Hide file tree
Showing 9 changed files with 17 additions and 28 deletions.
1 change: 0 additions & 1 deletion app/Http/Controllers/Sources/WebHookSourceController.php
Expand Up @@ -3,7 +3,6 @@
namespace App\Http\Controllers\Sources;

use App\Events\SourceRunCompleteEvent;
use App\Jobs\ProcessSourceJob;
use App\Models\Project;
use App\Models\Source;
use App\Source\SourceEnum;
Expand Down
9 changes: 0 additions & 9 deletions app/Ingress/IngressTypeEnum.php

This file was deleted.

4 changes: 2 additions & 2 deletions app/Models/Document.php
Expand Up @@ -2,7 +2,6 @@

namespace App\Models;

use App\Ingress\IngressTypeEnum;
use App\Ingress\StatusEnum;
use App\Source\SourceEnum;
use Illuminate\Database\Eloquent\Collection;
Expand All @@ -21,6 +20,7 @@
* @property Source $source
* @property Project $project
* @property Collection $document_chunks
* @property SourceEnum $type
*
* @method Source source()
* @method Project project()
Expand All @@ -34,7 +34,7 @@ class Document extends Model

protected $casts = [
'status' => StatusEnum::class,
'type' => IngressTypeEnum::class,
'type' => SourceEnum::class,
'meta_data' => 'array',
'embedding' => Vector::class,
];
Expand Down
7 changes: 3 additions & 4 deletions app/Transformer/Types/JsonTransformer.php
Expand Up @@ -7,22 +7,21 @@
use App\Models\Transformer;
use App\Transformer\BaseTransformer;
use Ramsey\Uuid\Uuid;
use Soundasleep\Html2Text as Helper;

class JsonTransformer extends BaseTransformer
{
public function handle(Transformer $transformer): Document
{

$config = config('larachain.sources.' . $this->document->source->type->value . '.transformers', null);
$config = config('larachain.sources.'.$this->document->source->type->value.'.transformers', null);

if($config && in_array('App\\Transformer\\Types\\JsonTransformer', $config)) {
if ($config && in_array('App\\Transformer\\Types\\JsonTransformer', $config)) {
$fileContents = $this->document->content;
$guid = md5($fileContents);

if (! DocumentChunk::query()
->where('document_id', $this->document->id)
->where("guid", $guid)
->where('guid', $guid)
->exists()) {
DocumentChunk::create([
'guid' => Uuid::uuid4()->toString(),
Expand Down
2 changes: 1 addition & 1 deletion config/larachain.php
Expand Up @@ -22,7 +22,7 @@
'description' => 'You can take data from a webhook or trigger a listener',
'class' => 'App\\Source\\Types\\WebHook',
'transformers' => [
'App\\Transformer\\Types\\JsonTransformer'
'App\\Transformer\\Types\\JsonTransformer',
],
'requires' => [
],
Expand Down
2 changes: 2 additions & 0 deletions database/factories/DocumentFactory.php
Expand Up @@ -4,6 +4,7 @@

use App\Ingress\StatusEnum;
use App\Models\Source;
use App\Source\SourceEnum;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Facades\File;

Expand All @@ -24,6 +25,7 @@ public function definition(): array
return [
'token_count' => fake()->randomDigitNotZero(),
'status' => StatusEnum::Pending,
'type' => SourceEnum::WebHook,
'guid' => fake()->uuid(),
'content' => fake()->sentence(10),
'source_id' => Source::factory(),
Expand Down
4 changes: 2 additions & 2 deletions tests/Feature/DataToDocumentDtoDataTest.php
Expand Up @@ -3,7 +3,7 @@
namespace Tests\Feature;

use App\Data\DataToDocumentDtoData;
use App\Ingress\IngressTypeEnum;
use App\Source\SourceEnum;
use Illuminate\Support\Str;
use Tests\TestCase;

Expand All @@ -13,7 +13,7 @@ public function test_dto()
{
$dto = new DataToDocumentDtoData(
'foobar',
IngressTypeEnum::WebScrape,
SourceEnum::ScrapeWebPage,
'foobaz',
'project_id_'.Str::random(),
[
Expand Down
Expand Up @@ -77,7 +77,7 @@ public function testRunWebHook()

$response->assertRedirect(route('projects.show', ['project' => $project->id]));

Queue::assertPushed(ProcessSourceJob::class);
Queue::assertNotPushed(ProcessSourceJob::class);
}

public function testUpdateWebHook()
Expand Down
14 changes: 6 additions & 8 deletions tests/Feature/JsonTransformerTest.php
Expand Up @@ -5,35 +5,33 @@
use App\Models\Document;
use App\Models\Source;
use App\Models\Transformer;
use App\Transformer\TransformerEnum;
use App\Transformer\Types\JsonTransformer;
use Illuminate\Support\Facades\Storage;
use Tests\TestCase;

class JsonTransformerTest extends TestCase
{
use SharedSetupForPdfFile;

public function test_runs_base_on_source() {
public function test_runs_base_on_source()
{
/** @var Source $source */
$source = Source::factory()
->webHook()
->create();

$document = Document::factory()->create([
'source_id' => $source->id
'source_id' => $source->id,
]);

/** @var Transformer $transformer */
$transformerModel = Transformer::factory()->create([
'project_id' => $source->project_id
'project_id' => $source->project_id,
]);

$this->assertDatabaseCount("document_chunks", 0);
$this->assertDatabaseCount('document_chunks', 0);

$transformer = new JsonTransformer($document);
$transformer->handle($transformerModel);
$this->assertDatabaseCount("document_chunks", 1);
$this->assertDatabaseCount('document_chunks', 1);
}

}

0 comments on commit e9fa4ba

Please sign in to comment.