awa_unit_2.4.0_59135a52/awa/plugins/awa-counters/src/awa-counters-beans.adb

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
-----------------------------------------------------------------------
--  awa-counters-beans -- Counter bean definition
--  Copyright (C) 2015, 2016, 2018, 2022 Stephane Carrez
--  Written by Stephane Carrez (Stephane.Carrez@gmail.com)
--
--  Licensed under the Apache License, Version 2.0 (the "License");
--  you may not use this file except in compliance with the License.
--  You may obtain a copy of the License at
--
--      http://www.apache.org/licenses/LICENSE-2.0
--
--  Unless required by applicable law or agreed to in writing, software
--  distributed under the License is distributed on an "AS IS" BASIS,
--  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
--  See the License for the specific language governing permissions and
--  limitations under the License.
-----------------------------------------------------------------------
with Ada.Calendar;
with Util.Dates.ISO8601;
with ADO.Queries.Loaders;
with ADO.Sessions.Entities;
with AWA.Services.Contexts;
package body AWA.Counters.Beans is

   package ASC renames AWA.Services.Contexts;

   function Parse_Date (Date : in String;
                        Now  : in Ada.Calendar.Time) return Ada.Calendar.Time;

   --  ------------------------------
   --  Get the value identified by the name.
   --  ------------------------------
   overriding
   function Get_Value (From : in Counter_Bean;
                       Name : in String) return Util.Beans.Objects.Object is
      pragma Unreferenced (Name);
   begin
      return Util.Beans.Objects.To_Object (From.Value);
   end Get_Value;

   overriding
   function Get_Value (List : in Counter_Stat_Bean;
                       Name : in String) return Util.Beans.Objects.Object is
   begin
      if Name = "stats" then
         return Util.Beans.Objects.To_Object (Value   => List.Stats_Bean,
                                              Storage => Util.Beans.Objects.STATIC);
      else
         return AWA.Counters.Models.Stat_List_Bean (List).Get_Value (Name);
      end if;
   end Get_Value;

   --  ------------------------------
   --  Set the value identified by the name.
   --  ------------------------------
   overriding
   procedure Set_Value (From  : in out Counter_Stat_Bean;
                        Name  : in String;
                        Value : in Util.Beans.Objects.Object) is
   begin
      if not Util.Beans.Objects.Is_Empty (Value) then
         AWA.Counters.Models.Stat_List_Bean (From).Set_Value (Name, Value);
      end if;
   end Set_Value;

   --  ------------------------------
   --  Get the query definition to collect the counter statistics.
   --  ------------------------------
   function Get_Query (From : in Counter_Stat_Bean) return ADO.Queries.Query_Definition_Access is
      Name : constant String := Ada.Strings.Unbounded.To_String (From.Query_Name);
   begin
      return ADO.Queries.Loaders.Find_Query (Name);
   end Get_Query;

   function Parse_Date (Date : in String;
                        Now  : in Ada.Calendar.Time) return Ada.Calendar.Time is
      use type Ada.Calendar.Time;

      Day : Natural;
   begin
      if Date'Length = 0 or else Date = "now" then
         return Now;
      elsif Date'Length > 5 and then Date (Date'First .. Date'First + 3) = "now-" then
         Day := Natural'Value (Date (Date'First + 4 .. Date'Last));
         return Now - Duration (Day * 86400);
      else
         return Util.Dates.ISO8601.Value (Date);
      end if;
   end Parse_Date;

   --  ------------------------------
   --  Load the statistics information.
   --  ------------------------------
   overriding
   procedure Load (List    : in out Counter_Stat_Bean;
                   Outcome : in out Ada.Strings.Unbounded.Unbounded_String) is
      pragma Unreferenced (Outcome);
      use type ADO.Identifier;
      use type ADO.Queries.Query_Definition_Access;

      Ctx         : constant ASC.Service_Context_Access := ASC.Current;
      User        : constant ADO.Identifier := Ctx.Get_User_Identifier;
      Now         : constant Ada.Calendar.Time := Ada.Calendar.Clock;
      Def         : constant ADO.Queries.Query_Definition_Access := List.Get_Query;
      Entity_Type : constant String := Ada.Strings.Unbounded.To_String (List.Entity_Type);
      Session     : ADO.Sessions.Session := ASC.Get_Session (Ctx);
      Kind        : ADO.Entity_Type;
      First_Date  : Ada.Calendar.Time;
      Last_Date   : Ada.Calendar.Time;
      Query       : ADO.Queries.Context;
   begin
      Kind := ADO.Sessions.Entities.Find_Entity_Type (Session, Entity_Type);
      First_Date := Parse_Date (Ada.Strings.Unbounded.To_String (List.First_Date), Now);
      Last_Date := Parse_Date (Ada.Strings.Unbounded.To_String (List.Last_Date), Now);
      if List.Entity_Id /= ADO.NO_IDENTIFIER and then Def /= null then
         Query.Set_Query (Def);
         Query.Bind_Param ("entity_type", Kind);
         Query.Bind_Param ("entity_id", List.Entity_Id);
         Query.Bind_Param ("user_id", User);
         Query.Bind_Param ("first_date", First_Date);
         Query.Bind_Param ("last_date", Last_Date);
         Query.Bind_Param ("counter_name", List.Counter_Name);

         AWA.Counters.Models.List (List.Stats, Session, Query);
      end if;
   end Load;

   --  ------------------------------
   --  Create the Blog_Stat_Bean bean instance.
   --  ------------------------------
   function Create_Counter_Stat_Bean (Module : in AWA.Counters.Modules.Counter_Module_Access)
                                      return Util.Beans.Basic.Readonly_Bean_Access is
      Object : constant Counter_Stat_Bean_Access := new Counter_Stat_Bean;
   begin
      Object.Module     := Module;
      Object.Stats_Bean := Object.Stats'Access;
      return Object.all'Access;
   end Create_Counter_Stat_Bean;

end AWA.Counters.Beans;