-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
remove kafka and listen to change tabel instead (#104)
- Loading branch information
1 parent
36e3833
commit 060999c
Showing
39 changed files
with
1,532 additions
and
2,474 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
...tegrator.GeoDatabase/Postgres/SchemaMigration/1678195057_UpdateAndCreateTriggerChanges.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using FluentMigrator; | ||
using System.IO; | ||
|
||
namespace OpenFTTH.GDBIntegrator.GeoDatabase.Postgres.SchemaMigration | ||
{ | ||
[Migration(1678195057)] | ||
public class UpdateAndCreateTriggerChanges : Migration | ||
{ | ||
public override void Up() | ||
{ | ||
Execute.Script(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) | ||
+ "/Postgres/SchemaMigration/Scripts/update_and_create_trigger_changes.sql"); | ||
} | ||
|
||
public override void Down() | ||
{ | ||
// This is a hard change to do and needs to be resolved manually if a down is needed to be done. | ||
} | ||
} | ||
} |
100 changes: 100 additions & 0 deletions
100
...grator.GeoDatabase/Postgres/SchemaMigration/Scripts/update_and_create_trigger_changes.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
-- Edit operation table | ||
CREATE TABLE route_network.route_network_edit_operation ( | ||
seq_no BIGINT NOT NULL GENERATED ALWAYS AS IDENTITY, | ||
event_id UUID NULL, | ||
"before" VARCHAR NULL, | ||
"after" VARCHAR NULL, | ||
"type" VARCHAR NOT NULL, | ||
event_timestamp TIMESTAMPTZ NOT NULL DEFAULT NOW(), | ||
CONSTRAINT route_network_edit_operation_pkey PRIMARY KEY (seq_no) | ||
); | ||
|
||
-- Trigger create route node | ||
CREATE OR REPLACE FUNCTION route_network.route_node_create() | ||
RETURNS trigger | ||
LANGUAGE plpgsql | ||
AS $function$ | ||
BEGIN | ||
insert into route_network.route_network_edit_operation (event_id, before, after, type) | ||
values ( | ||
uuid_generate_v4(), | ||
null, | ||
to_json(NEW), | ||
'RouteNode' | ||
); | ||
RETURN NEW; | ||
END $function$ | ||
; | ||
|
||
-- This is a new trigger, so we have to set it. | ||
CREATE TRIGGER create_route_node BEFORE INSERT ON route_network.route_node | ||
FOR EACH ROW EXECUTE PROCEDURE route_network.route_node_create(); | ||
|
||
-- Trigger update route node | ||
CREATE OR REPLACE FUNCTION route_network.route_node_update() | ||
RETURNS trigger | ||
LANGUAGE plpgsql | ||
AS $function$ | ||
BEGIN | ||
IF NEW.delete_me = true | ||
THEN | ||
DELETE FROM route_network.route_node where mrid = OLD.mrid; | ||
RETURN null; | ||
END IF; | ||
|
||
insert into route_network.route_network_edit_operation (event_id, before, after, type) | ||
values ( | ||
uuid_generate_v4(), | ||
to_json(OLD), | ||
to_json(NEW), | ||
'RouteNode' | ||
); | ||
|
||
RETURN NEW; | ||
END $function$ | ||
; | ||
|
||
-- Trigger create route segment | ||
CREATE OR REPLACE FUNCTION route_network.route_segment_create() | ||
RETURNS trigger | ||
LANGUAGE plpgsql | ||
AS $function$ | ||
BEGIN | ||
insert into route_network.route_network_edit_operation (event_id, before, after, type) | ||
values ( | ||
uuid_generate_v4(), | ||
null, | ||
to_json(NEW), | ||
'RouteSegment' | ||
); | ||
RETURN NEW; | ||
END $function$ | ||
; | ||
|
||
-- This is a new trigger, so we have to set it. | ||
CREATE TRIGGER create_route_segment BEFORE INSERT ON route_network.route_segment | ||
FOR EACH ROW EXECUTE PROCEDURE route_network.route_segment_create(); | ||
|
||
-- Trigger update route segment | ||
CREATE OR REPLACE FUNCTION route_network.route_segment_update() | ||
RETURNS trigger | ||
LANGUAGE plpgsql | ||
AS $function$ | ||
BEGIN | ||
IF NEW.delete_me = true | ||
THEN | ||
DELETE FROM route_network.route_segment where mrid = OLD.mrid; | ||
RETURN null; | ||
END IF; | ||
|
||
insert into route_network.route_network_edit_operation (event_id, before, after, type) | ||
values ( | ||
uuid_generate_v4(), | ||
to_json(OLD), | ||
to_json(NEW), | ||
'RouteSegment' | ||
); | ||
|
||
RETURN NEW; | ||
END $function$ | ||
; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 0 additions & 6 deletions
6
src/OpenFTTH.GDBIntegrator.Integrator/ConsumerMessages/HearthBeatMessage.cs
This file was deleted.
Oops, something went wrong.
18 changes: 14 additions & 4 deletions
18
src/OpenFTTH.GDBIntegrator.Integrator/ConsumerMessages/InvalidMessage.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 14 additions & 5 deletions
19
src/OpenFTTH.GDBIntegrator.Integrator/ConsumerMessages/RouteNodeMessage.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 14 additions & 6 deletions
20
src/OpenFTTH.GDBIntegrator.Integrator/ConsumerMessages/RouteSegmentMessage.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
src/OpenFTTH.GDBIntegrator.Integrator/Store/EventIdStore.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Runtime.CompilerServices; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.Options; | ||
using Npgsql; | ||
using OpenFTTH.GDBIntegrator.Config; | ||
|
||
namespace OpenFTTH.GDBIntegrator.Integrator.Store | ||
{ | ||
public class EventIdStore : IEventIdStore | ||
{ | ||
private HashSet<Guid> _eventIds; | ||
private readonly EventStoreSetting _eventStoreSetting; | ||
|
||
public EventIdStore(IOptions<EventStoreSetting> eventStoreSetting) | ||
{ | ||
_eventStoreSetting = eventStoreSetting.Value; | ||
} | ||
|
||
public void AppendEventId(Guid eventId) | ||
{ | ||
_eventIds.Add(eventId); | ||
} | ||
|
||
public HashSet<Guid> GetEventIds() | ||
{ | ||
return _eventIds ?? throw new InvalidOperationException("EventIds has not been loaded yet."); | ||
} | ||
|
||
public async Task<long> LoadEventIds(CancellationToken token = default) | ||
{ | ||
_eventIds = new(); | ||
|
||
await foreach (var eId in GetAllEventIds(token).ConfigureAwait(false)) | ||
{ | ||
_eventIds.Add(eId); | ||
} | ||
|
||
return _eventIds.Count; | ||
} | ||
|
||
private async IAsyncEnumerable<Guid> GetAllEventIds( | ||
[EnumeratorCancellation] CancellationToken token = default) | ||
{ | ||
const string SQL = @"SELECT data->'EventId' AS event_id | ||
FROM events.mt_events | ||
WHERE type = 'route_network_edit_operation_occured_event'"; | ||
|
||
using var conn = new NpgsqlConnection(_eventStoreSetting.ConnectionString); | ||
using var cmd = new NpgsqlCommand(SQL, conn); | ||
|
||
await conn.OpenAsync(token).ConfigureAwait(false); | ||
var reader = await cmd.ExecuteReaderAsync(token).ConfigureAwait(false); | ||
|
||
while (await reader.ReadAsync(token).ConfigureAwait(false)) | ||
{ | ||
yield return reader.GetGuid(reader.GetOrdinal("event_id")); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.