1+ using  System . Data ; 
2+ using  System . Diagnostics ; 
3+ using  System . Text ; 
4+ using  CodeMechanic . Async ; 
5+ using  CodeMechanic . Diagnostics ; 
6+ using  CodeMechanic . Types ; 
7+ using  Dapper ; 
8+ using  Microsoft . AspNetCore . Mvc ; 
9+ using  Microsoft . AspNetCore . Mvc . RazorPages ; 
10+ using  MySqlConnector ; 
11+ 
12+ namespace  evantage . Pages . Logs ; 
13+ 
14+ public  class  Index  :  PageModel 
15+ { 
16+     private  static string  connectionString ; 
17+ 
18+     public  void  OnGet ( ) 
19+     { 
20+     } 
21+ 
22+     public  async  Task < IActionResult >  OnGetAllLogs ( ) 
23+     { 
24+         string  query  = 
25+                 """ 
26+                     select operation_name, 
27+                            exception_text, 
28+                            payload, 
29+                            diff, 
30+                            sql_parameters, 
31+                            table_name, 
32+                            breadcrumb 
33+                     from logs 
34+                     order by created_at asc, modified_at asc; 
35+                 """ 
36+             ; 
37+         string  connectionstring  =  GetConnectionString ( ) ; 
38+         using  var  connection  =  new  MySqlConnection ( connectionstring ) ; 
39+ 
40+         var  results  =  connection 
41+             . Query < LogRecord > ( query 
42+                 ,  commandType :  CommandType . Text 
43+             ) 
44+             . ToList ( ) ; 
45+ 
46+         int  count  =  results . Count ; 
47+ 
48+         // return Content($"{count}"); 
49+         return  Partial ( "_LogsTable" ,  results ) ; 
50+     } 
51+ 
52+     private  static string  GetConnectionString ( ) 
53+     { 
54+         var  connectionString  =  new  MySqlConnectionStringBuilder ( ) 
55+         { 
56+             Database  =  Environment . GetEnvironmentVariable ( "MYSQLDATABASE" ) , 
57+             Server  =  Environment . GetEnvironmentVariable ( "MYSQLHOST" ) , 
58+             Password  =  Environment . GetEnvironmentVariable ( "MYSQLPASSWORD" ) , 
59+             UserID  =  Environment . GetEnvironmentVariable ( "MYSQLUSER" ) , 
60+             Port  =  ( uint ) Environment . GetEnvironmentVariable ( "MYSQLPORT" ) . ToInt ( ) 
61+         } . ToString ( ) ; 
62+ 
63+         if  ( connectionString  ==  null )  throw  new  ArgumentNullException ( nameof ( connectionString ) ) ; 
64+         return  connectionString ; 
65+     } 
66+ 
67+     private  async  Task < List < LogRecord > >  BulkUpsertLogs ( List < LogRecord >  logRecords ) 
68+     { 
69+         var  batch_size  = 
70+             1000 ;  //(int)Math.Round(Math.Log2(logRecords.Count * 1.0) * Math.Log10(logRecords.Count) * 100, 1); 
71+         Console . WriteLine ( "batch size :>> "  +  batch_size ) ; 
72+         var  Q  =  new  SerialQueue ( ) ; 
73+         Console . WriteLine ( "Running Q of bulk upserts ... " ) ; 
74+         Stopwatch  watch  =  new  Stopwatch ( ) ; 
75+         watch . Start ( ) ; 
76+         var  tasks  =  logRecords 
77+             . Batch ( batch_size ) 
78+             . Select ( log_batch =>  Q 
79+                 . Enqueue ( async  ( )  =>  await  UpsertLogs ( log_batch ,  debug_mode :  false ) ) ) ; 
80+ 
81+         await  Task . WhenAll ( tasks ) ; 
82+         watch . Stop ( ) ; 
83+         watch . PrintRuntime ( $ "Done upserting { logRecords . Count }  logs! ") ; 
84+         return  logRecords ; 
85+     } 
86+ 
87+     private  async  Task < List < LogRecord > >  UpsertLogs ( 
88+         IEnumerable < LogRecord >  logRecords 
89+         ,  bool  debug_mode  =  false 
90+     ) 
91+     { 
92+         var  insert_values  =  logRecords 
93+             . Aggregate ( new  StringBuilder ( ) ,  ( builder ,  next )  => 
94+             { 
95+                 builder 
96+                     . AppendLine ( $ "( '{ next . application_name } '") 
97+                     . AppendLine ( $ ", '{ next . database_name } '   ") 
98+                     . AppendLine ( $ ", '{ next . exception_text } '  ") 
99+                     . AppendLine ( $ ", '{ next . breadcrumb } '      ") 
100+                     . AppendLine ( $ ", '{ next . issue_url } '       ") 
101+                     . AppendLine ( $ ", '{ next . created_by } '      ") 
102+                     . AppendLine ( $ ", '{ next . modified_by } '     ") 
103+                     . AppendLine ( $ ", null") 
104+                     . AppendLine ( $ ", null") 
105+                     . AppendLine ( $ ", '{ next . is_archived } '     ") 
106+                     . AppendLine ( $ ", '{ next . is_deleted } '      ") 
107+                     . AppendLine ( $ ", '{ next . is_enabled } '      ") 
108+                     . AppendLine ( $ ")") 
109+                     . ToString ( ) 
110+                     . Trim ( ) ; 
111+                 builder . Append ( "," ) ; 
112+                 return  builder ; 
113+             } ) . ToString ( ) ; 
114+ 
115+         if  ( debug_mode )  Console . WriteLine ( "values query :>> "  +  insert_values ) ; 
116+ 
117+         string  insert_begin  =  """  
118+                         insert into logs  
119+                         (  
120+                             table_name 
121+                          , database_name 
122+                          , exception_text 
123+                          , breadcrumb 
124+                          , issue_url 
125+                          , created_by 
126+                          , modified_by 
127+                          , created_at 
128+                          , modified_at 
129+                          , is_deleted 
130+                          , is_archived 
131+                          , is_enabled 
132+                      ) 
133+                     values 
134+                     """ ; 
135+ 
136+         var  query  =  StringBuilderExtensions . RemoveFromEnd ( new  StringBuilder ( ) 
137+                 . AppendLine ( insert_begin ) 
138+                 . AppendLine ( insert_values ) ,  2 )  // remove last comma 
139+             . Append ( ";" )  // adds the delimiter for mysql 
140+             . ToString ( ) ; 
141+ 
142+         if  ( debug_mode )  Console . WriteLine ( "full query :>> "  +  query ) ; 
143+ 
144+         try 
145+         { 
146+             var  connectionString  =  GetConnectionString ( ) ; 
147+             using  var  connection  =  new  MySqlConnection ( connectionString ) ; 
148+ 
149+             var  results  =  connection 
150+                 . Query < LogRecord > ( query 
151+                     ,  commandType :  CommandType . Text 
152+                 ) 
153+                 . ToList ( ) ; 
154+ 
155+             return  results  ??  new  List < LogRecord > ( ) ; 
156+         } 
157+         catch  ( Exception  e ) 
158+         { 
159+             Console . WriteLine ( e ) ; 
160+             WriteLocalLogfile ( e . ToString ( )  +  query ) ; 
161+             throw ; 
162+         } 
163+     } 
164+ 
165+     private  static void  WriteLocalLogfile ( string  content ) 
166+     { 
167+         var  cwd  =  Environment . CurrentDirectory ; 
168+         string  filepath  =  Path . Combine ( cwd ,  "Admin.log" ) ; 
169+         Console . WriteLine ( "writing to :>> "  +  filepath ) ; 
170+         System . IO . File . WriteAllText ( filepath ,  content ) ; 
171+     } 
172+ } 
173+ 
174+ public  sealed  class  LogRecord 
175+ { 
176+     public  string  id  {  get ;  set ;  }  =  string . Empty ; 
177+     public  string  exception_text  {  get ;  set ;  }  =  string . Empty ; 
178+     public  string  exception_message  {  get ;  set ;  }  =  "OOOPS!" ;  // = string.Empty; 
179+ 
180+     public  string  exception_severity  {  get ;  set ;  }  =  "HIGH" ; 
181+ 
182+     public  string  sql_parameters  {  get ;  set ;  }  =  string . Empty ; 
183+     public  string  payload  {  get ;  set ;  }  =  string . Empty ; 
184+     public  string  diff  {  get ;  set ;  }  =  "{}" ; 
185+     public  string  operation_name  {  get ;  set ;  }  =  string . Empty ; 
186+     public  string  breadcrumb  {  get ;  set ;  }  =  string . Empty ; 
187+     public  string  table_name  {  get ;  set ;  }  =  string . Empty ; 
188+     public  string  server_name  {  get ;  set ;  }  =  string . Empty ; 
189+     public  string  database_name  {  get ;  set ;  }  =  string . Empty ; 
190+     public  string  application_name  {  get ;  set ;  }  =  string . Empty ; 
191+     public  string  modified_by  {  get ;  set ;  }  =  string . Empty ; 
192+     public  string  created_by  {  get ;  set ;  }  =  string . Empty ; 
193+     public  DateTime  modified_at  {  get ;  set ;  } 
194+     public  DateTime  created_at  {  get ;  set ;  } 
195+ 
196+     public  string  commit_url  {  get ;  set ;  }  =  string . Empty ; 
197+     public  string  issue_url  {  get ;  set ;  }  =  string . Empty ; 
198+     public  bool  is_deleted  {  get ;  set ;  } 
199+     public  bool  is_archived  {  get ;  set ;  } 
200+     public  bool  is_enabled  {  get ;  set ;  } 
201+ } 
0 commit comments