Advertisement
filhotecmail

BackLog Database Monitor

Nov 6th, 2021
1,357
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 4.63 KB | None | 0 0
  1. {***************************************************************}
  2. {                                                               }
  3. {           Leopard Report                                      }
  4. {           Copyright (C) 2012 Carlos Dias da Silva F.          }
  5. {           filhotecmail@gmail.com                              }
  6. {***************************************************************}
  7. unit Dao.Transactions.Monitor.Service.Work;
  8.  
  9. interface
  10.  uses
  11.   Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  12.   Vcl.Controls, Vcl.Forms, Vcl.Dialogs, FireDAC.Stan.Intf, FireDAC.Stan.Option, FireDAC.Stan.Error, FireDAC.UI.Intf,
  13.   FireDAC.Phys.Intf, FireDAC.Stan.Def, FireDAC.Stan.Pool, FireDAC.Stan.Async, FireDAC.Phys, FireDAC.Phys.FB,
  14.   FireDAC.Phys.FBDef, FireDAC.VCLUI.Wait, FireDAC.Stan.Param, FireDAC.DatS, FireDAC.DApt.Intf, FireDAC.DApt,
  15.   Vcl.StdCtrls, Data.DB, FireDAC.Comp.DataSet, FireDAC.Comp.Client, FireDAC.Moni.Base,
  16.   FireDAC.Moni.RemoteClient,FireDAC.Moni.Custom;
  17.  
  18.  type TCustomMonitorTypes = (MonitorCustom,MonitorFlat,MonitorRemote);
  19.  
  20.  type
  21.       ITransactMonitorServiceWork = Interface
  22.       ['{A43E4929-A02D-4353-BBC1-303462FE6BCC}']
  23.       function SendNotify(Sender: TProc<TObject>):ITransactMonitorServiceWork;
  24.       function Monitor:TObject;
  25.   End;
  26.  
  27.  type TTransactMonitorServiceWork = class Abstract(TInterfacedObject, ITransactMonitorServiceWork)
  28.  
  29.   private
  30.      FConnection: TFDCustomConnection;
  31.      FMonitor   : TObject;
  32.      FDataLog   : TFDMemTable;
  33.   {$REGION 'prepair Components'}
  34.      procedure InternalCreateCustomMonitor;
  35.      procedure InternalPrepareDataLog;
  36.   {$ENDREGION}
  37.   protected
  38.     function SendNotify(Sender: TProc<TObject>):ITransactMonitorServiceWork; virtual;
  39.     procedure OnOutputEvent(ASender: TFDMoniClientLinkBase; const AClassName, AObjName,
  40.               AMessage: string);
  41.   public
  42.     procedure AfterConstruction; override;
  43.     procedure BeforeDestruction; override;
  44.     Constructor Create(AConnection: TFDCustomConnection; AMonitorType: TCustomMonitorTypes); Virtual;
  45.     class function New(AConnection: TFDCustomConnection; AMonitorType: TCustomMonitorTypes):IInterface; virtual; abstract;
  46.     function Monitor: TObject;
  47.   end;
  48.  
  49.   type TTransactCustomMonitorServiceWork<ITransactMonitorServiceWork> = class(TTransactMonitorServiceWork)
  50.   public
  51.     class function New(AConnection: TFDCustomConnection;
  52.           AMonitorType: TCustomMonitorTypes = MonitorCustom): IInterface; reintroduce; Overload;
  53.   end;
  54.  
  55. implementation
  56.  
  57. { TTransactMonitorServiceWork }
  58. procedure TTransactMonitorServiceWork.AfterConstruction;
  59. begin
  60.   inherited;
  61.  
  62. end;
  63.  
  64. procedure TTransactMonitorServiceWork.BeforeDestruction;
  65. begin
  66.   inherited;
  67.   FreeAndNil( FDataLog );
  68. end;
  69.  
  70. constructor TTransactMonitorServiceWork.Create(AConnection: TFDCustomConnection; AMonitorType: TCustomMonitorTypes);
  71. begin
  72.  Assert(AConnection <> nil,'O Componente de Conexão TFDCustomConnection não pode ser nil');
  73.  FConnection := AConnection;
  74.  FDataLog := TFDMemTable.Create(nil);
  75.  case AMonitorType of
  76.   MonitorCustom: InternalCreateCustomMonitor;
  77.   MonitorFlat  : ;
  78.   MonitorRemote: ;
  79.  end;
  80.  
  81. end;
  82.  
  83. procedure TTransactMonitorServiceWork.InternalCreateCustomMonitor;
  84. begin
  85.  FMonitor:= TFDMoniCustomClientLink.Create(FConnection);
  86.  ( FMonitor as TFDMoniCustomClientLink).OnOutput := OnOutputEvent;
  87.  ( FConnection as TFDConnection).Params.MonitorBy:= mbCustom;
  88. end;
  89.  
  90. procedure TTransactMonitorServiceWork.InternalPrepareDataLog;
  91. begin
  92.  FDataLog.FieldDefs.Clear;
  93.  with FDataLog.FieldDefs do
  94.  begin
  95.    Add('Data',ftDate);
  96.    Add('Hora',ftTime);
  97.    Add('ObjectName',ftString,60);
  98.    Add('ClassName',ftString,60);
  99.    Add('Message',ftWideMemo,5000);
  100.  end;
  101.  FDataLog.CreateDataSet;
  102.  FDataLog.Open;
  103. end;
  104.  
  105. function TTransactMonitorServiceWork.Monitor: TObject;
  106. begin
  107.  Result := FMonitor;
  108. end;
  109.  
  110. procedure TTransactMonitorServiceWork.OnOutputEvent(ASender: TFDMoniClientLinkBase; const AClassName, AObjName,
  111.   AMessage: string);
  112. begin
  113.  FDataLog.AppendRecord([Now,Now,AObjName,AClassName,AMessage]);
  114.  FDataLog.Refresh;
  115. end;
  116.  
  117. function TTransactMonitorServiceWork.SendNotify(Sender: TProc<TObject>): ITransactMonitorServiceWork;
  118. begin
  119.  if Assigned(Sender) then
  120.     Sender(FDataLog);
  121. end;
  122.  
  123. { TTransactCustomMonitorServiceWork<ITransactMonitorServiceWork> }
  124.  
  125. class function TTransactCustomMonitorServiceWork<ITransactMonitorServiceWork>.New(
  126.   AConnection: TFDCustomConnection; AMonitorType: TCustomMonitorTypes = MonitorCustom): IInterface;
  127. begin
  128.  Result := TTransactCustomMonitorServiceWork<ITransactMonitorServiceWork>.Create(AConnection,MonitorCustom);
  129.  
  130. end;
  131.  
  132. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement