import { PostgreSQL } from "../types" ; import { query } from "../query" ; import { TIMEOUT_S } from "./const" ; export async function update_site_license_usage_log ( db ) { await update_site_license_usage_log_running_projects ( db ) ; await update_site_license_usage_log_not_running_projects ( db ) ; } async function update_site_license_usage_log_running_projects ( db ) { const dbg = db . _dbg ( "update_site_license_usage_log_running_projects" ) ; dbg ( ) ; const q = `
WITH missing AS
(
   WITH running_license_info AS
   (
      SELECT
         project_id,
         (
            jsonb_each_text(site_license)
         )
         .*
      FROM
         projects
      WHERE
         state #>> '{state}' = 'running'
   )
   SELECT
      running_license_info.project_id AS project_id,
      running_license_info.key::UUID AS license_id
   FROM
      running_license_info
   WHERE
      running_license_info.value != '{}'
      AND NOT EXISTS
      (
         SELECT
         FROM
            site_license_usage_log
         WHERE
            site_license_usage_log.stop IS NULL
            AND site_license_usage_log.project_id = running_license_info.project_id
            AND site_license_usage_log.license_id = running_license_info.key::UUID
      )
)
INSERT INTO
   site_license_usage_log(project_id, license_id, start)
   SELECT
      project_id,
      license_id,
      NOW()
   FROM
      missing;

` ; await query ( { db , query : q , timeout_s : TIMEOUT_S } ) ; } async function update_site_license_usage_log_not_running_projects ( db ) { const dbg = db . _dbg ( "update_site_license_usage_log_not_running_projects" ) ; dbg ( ) ; const q = `
WITH stopped AS
(
   WITH running_license_info AS
   (
      SELECT
         project_id,
         (
            jsonb_each_text(site_license)
         )
         .*
      FROM
         projects
      WHERE
         state #>> '{state}' = 'running'
   )
   SELECT
      site_license_usage_log.license_id AS license_id,
      site_license_usage_log.project_id AS project_id,
      site_license_usage_log.start AS start
   FROM
      site_license_usage_log
   WHERE
      stop IS NULL
      AND NOT EXISTS
      (
         SELECT
         FROM
            running_license_info
         WHERE
            running_license_info.value != '{}'
            AND running_license_info.project_id = site_license_usage_log.project_id
            AND site_license_usage_log.license_id = running_license_info.key::UUID
      )
)
UPDATE
   site_license_usage_log
SET
   stop = NOW()
FROM
   stopped
WHERE
   site_license_usage_log.license_id = stopped.license_id
   AND site_license_usage_log.project_id = stopped.project_id
   AND site_license_usage_log.start = stopped.start;
` ; await query ( { db , query : q , timeout_s : TIMEOUT_S } ) ; } export interface Interval { begin : Date ; end : Date ; } export async function number_of_projects_that_used_license ( db , license_id , interval ) { const dbg = db . _dbg ( `number_of_projects_that_used_license("${ license_id }",${ interval . begin },${ interval . end })` ) ; dbg ( ) ; return - 1 ; } export async function number_of_hours_projects_used_license ( db , license_id , interval ) { const dbg = db . _dbg ( `number_of_hours_projects_used_license("${ license_id }",${ interval . begin },${ interval . end })` ) ; dbg ( ) ; return - 1 ; } export async function projects_that_used_license ( db , license_id , interval , fields = [ "project_id" ] , limit = 500 ) { const dbg = db . _dbg ( `projects_that_used_license("${ license_id }",${ interval . begin },${ interval . end })` ) ; dbg ( [ fields , limit ] ) ; return [ ] ; }