8 – Programmatically log in a user

I am trying to authenticate users as below. I don’t get any error, but it doesn’t log in users.

autoLogin.php is located in the root folder. Visiting http://example.com/drupal8/autoLogin.php shows me the Login required page every time.

use DrupalCoreDrupalKernel;
use SymfonyComponentHttpFoundationRequest;

$autoloader = require_once 'autoload.php';

$kernel = new DrupalKernel('prod', $autoloader);

$request = Request::createFromGlobals();
$response = $kernel->handle($request);

$uid = 1; 
$user = DrupaluserEntityUser::load(1); 
$kernel->prepareLegacyRequest($request);
user_login_finalize($user);
$response->send();
$kernel->terminate($request, $response);

logrotate – can I rotate log based on file creation time?

I have logrotate config like this:

/var/log/example*.log {
    weekly
    rotate 1
    missingok
    notifempty
    compress
}

Some of the example*.log files have existed for quite long time more than few months. I want to compress them with logrotate. But the config is newly added, thus it will only start to rotate them after another week.

rotating pattern: /var/log/example*.log  weekly (1 rotations)
empty log files are not rotated, old logs are removed
considering log /var/log/example-a.log
Creating new state
  Now: 2021-01-23 10:07
  Last rotated at 2021-01-23 10:00
  log does not need rotating (log has been already rotated)
considering log /var/log/example-b.log
Creating new state
  Now: 2021-01-23 10:07
  Last rotated at 2021-01-23 10:00
  log does not need rotating (log has been already rotated)
considering log /var/log/example-c.log
Creating new state
  Now: 2021-01-23 10:07
  Last rotated at 2021-01-23 10:00
  log does not need rotating (log has been already rotated)

I wonder is there a way to let the logrotate rotate files based on the creation time or mtime, when it is too old then compress them.

Another problem is the files matching wild card are all rotated together. No matter if the log file is newly created or not. I checked the /var/lib/logrotate/logrotate.status It seems like example*.log is treated as whole. That means all files match this pattern will be rotated at same day. For example: a new log file example-d.log has been added after 6 days of last rotation. Then it will be rotated the next day after it created. But I would like the newly added log example-d.log to rotate after 1 week. Is there any other tool can rotate logs like what I want?

transaction log – What Query Filled My Log File

My Log file seems to have blown up overnight. I think it was around 50GB and is now 200GB. I want to find out if it was a bad query that someone ran, or if this is just normal growth. There is a lot of development happening on the instance. I ran this to try to identify long running queries that may have caused it:
SELECT st.text, qp.query_plan, qs.* FROM ( SELECT TOP 50 * FROM sys.dm_exec_query_stats ORDER BY total_worker_time DESC ) AS qs CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) AS st CROSS APPLY sys.dm_exec_query_plan(qs.plan_handle) AS qp WHERE qs.max_worker_time > 300 OR qs.max_elapsed_time > 300

The top 3 rows returned were for the same query: /* ================================================================ Object Name: dbo.fn_IsEdCptCode Created by: Creation Date: 1/13/2021 Purpose: Determines if a passed in code is a valid CPT Code for Emergency Dept Returns a bit 1=Yes 0=No Usage: Produces a year result based on the default date, GETDATE(), using the 'default' keyword(s): SELECT dbo.fn_IsEdCptCode('99283') SELECT dbo.fn_IsEdCptCode('ABCD') Server: Database: ------- Data Modifications ------- Date Author Changes Made ------------------------------------------------ ================================================================ */ CREATE FUNCTION (dbo).(fn_IsEdCptCode) ( @ProcedureCD VARCHAR(50) ) --Revenue Code to check RETURNS BIT AS BEGIN DECLARE @Found BIT SET @Found = 1 -- emergency cpt codes IF EXISTS( SELECT Code FROM Reference.HEDIS.ValueSetToCode edcpt WITH (NOLOCK) WHERE 1=1 AND edcpt.Code = @ProcedureCD AND edcpt.ValueSetName = 'ED' AND edcpt.CodeSystem = 'CPT' GROUP BY Code) BEGIN SET @Found = 1 END ELSE BEGIN SET @Found = 0 END RETURN @Found END

I think these are the pertinent stats.
enter image description here

Does this look like it could be the culprit? I’m not sure if the 322 million is the number of times the query ran?

Ultimately I guess I’m trying to determine if my Log file is now appropriately sized for the work that is going to happening there or if it’s oversized.

It’s SQL Server 2019.

8 – How to use the REST Endpoint `user/login` to log a user in programatically

I’m creating a custom login endpoint to log a user in and send some information back that the regular login endpoint does not. So what I’d like to do is

  1. Get username and password from request body
  2. Make request to builtin REST endpoint user/login
  3. Parse response to get session data and csrf token and such
  4. Get some extra user data we want to return with the login response
  5. Construct together a new body and return

However, I’m having issues using the built-in user/login endpoint and getting the response back in a JSON format

My custom route points to this method:

function login(Request $request) {
  $data = Json::decode($request->getContent(), TRUE);

  $host = Drupal::request()->getSchemeAndHttpHost();
  $url = "$host/user/login";

  $req_options = (
    'json' => (
      'name' => $data('username'),
      'pass' => $data('password')
    )
  );
  
  try {
    $response = $client->request('POST', $url, $req_options);

    $body = Json::decode($response->getBody());    

    $success = TRUE;

  } catch (RequestException $e) {
    $log_error = "User API - Call Endpointnn";
    $log_error .= "Request: <pre><code>" . print_r($e->getRequest(), TRUE) . "</code></pre>nn";
    $log_error .= "Response: <pre><code>" . print_r($e->getResponse(), TRUE) . "</code></pre>nn";
    Drupal::logger('ytp')->error($log_error);

    $success = FALSE;
  }

  // Parse login response to get necessary data and construct custom response

  return new JsonResponse($customBody, 200);
}

The issue I’m having is that the response from $response = $client->request('POST', $url, $req_options); is not a json format, it’s the http page like I just went to the webpage.

If I add this to the $req_options array:

'headers' => (
  'Accept' => 'application/json'
  'Content-Type' => 'application/hal+json'
)

then I just get an error

GuzzleHttpExceptionClientException: Client error: `POST http://ytplocal.local:8888/user/login` resulted in a `403 Forbidden` response: {"message":"This route can only be accessed by authenticated users."}

which makes absolutely zero sense. If I’m trying to authorize a user via login, how can it only be accessed by authenticated users?

How can I log a user in via REST? It seems like it should be simple, but somehow Drupal has made it incredibly complicated.

algorithms – Using chebyshev polynomials to calculate LOG, EXP and TRIG functions

I originally posted this over on Software Engineering, but it was suggested that I repost it here.

I am working on a BCD Floating point library and I’m a bit stuck on the EXP (antilog) functions. The goal is to maintain accuracy for the entire length of the number which could exceed 100 significant digits.

So the requirements are fast and accurate to N digits.

I have been experimenting with the Taylor series and found that it will yield an answer, but requires about 2 iterations per digit. It takes two multiplies and one divide per iteration. Plus I am not satisfied that it is all that accurate because it seems to converge on the correct answer only to drift away.

I would like to know if this can be done with chebyshev polynomials to converge on the correct answer in fewer iterations and clock cycles. I do have a chebyshev example, but it is limited to 8 iterations and about 9 digits of accuracy. I found no explanation as to how to expand it for more significant digits.

Does anybody have a chebyshev, or better, algorithm that can calculate EXP (any base)? Please no complicated math proofs as they are likely over my head.

logging – Obtain log of when audio starts and stops

I want to get a list of timestamped events for when the audio player starts and stops. By “audio player” I mean the one that is common for all apps that you see when you pull down the notification bar in Android 11. So when audio that is not system notifications start or stop.

Any way of doing it would be okay. Either an app or through adb and system logs, or something else. As far as I can tell, Android destroys logs from before that last reboot, but I might be wrong about this. If I can access old logs, that would be a solution.

mariadb – Log to disk all queries that waited for a lock over a period of time

I manage a mariadb 10.4.17 server, and I’m having the issues with a table, it seems randomly I will get lots of queries “waiting for metadata lock” causing my entire database to be useless.

I usually have to restart my server to get the database running again.

I’m looking for methods that can consistently log to disk all the queries who can use problems, without affecting performance too much.

I like something like this

watch -n 0.5 ‘mysqladmin -u root -ppassword “processlist”‘ > log.txt

But I don’t know how to order by state. Anyway I’m open to any ideas. Looking for something I can look at to see what happened in the past because when the issue happens I just want to restart the db to get back online asap and don’t have time to dig the root of the issue