<commit_before># computing project quotas based on settings (by admin/system) and user contributions ("upgrades")

# historical note
# old code added a "hardcoded" cpu_shares value in the setting, which was set in the DB
# we no longer have that in kucalc
# in December 2018, we removed that from the DB (LIMIT 10 to avoid locking the db too long)

###
WITH s256 AS (
    SELECT project_id
    FROM projects
    WHERE (settings ->> 'cpu_shares')::float = 256
    ORDER BY created ASC
    LIMIT 10
)
UPDATE projects AS p
SET    settings = jsonb_set(settings, '{cpu_shares}', '0')
FROM   s256
WHERE  p.project_id = s256.project_id
RETURNING p.project_id;
###

{DEFAULT_QUOTAS} = require('smc-util/upgrade-spec')
MAX_UPGRADES = require('smc-util/upgrade-spec').upgrades.max_per_project

# No matter what, every project gets SOME possibly tiny amount of guaranteed cpu.
# This is important since otherwise projects will NOT start at all, e.g., if a paying
# customer is using 100% of the cpu on the node (this will happen if their limits are
# high and they have guaranteed cpu of about 1 or more).  The project will be so slow
# it fails to start in time and times out.
MIN_POSSIBLE_CPU =
    member    : 0.05
    nonmember : 0.02

# Min possible **guaranteed** RAM.
MIN_POSSIBLE_MEMORY =
    member    : 300
    nonmember : 200

# lower bound for the RAM "limit"
# in particular, we make sure member projects are above the free quota
MIN_MEMORY_LIMIT =
    member    : 1.5 * DEFAULT_QUOTAS.memory
    nonmember : DEFAULT_QUOTAS.memory


exports.quota = (settings, users) ->
    # so can assume defined below
    settings ?= {}
    users    ?= {}

    quota =
        network        : false
        member_host    : false
        disk_quota     : DEFAULT_QUOTAS.disk_quota
        memory_limit   : DEFAULT_QUOTAS.memory   # upper bound on RAM in MB
        memory_request : 0                       # will hold guaranteed RAM in MB
        cpu_limit      : DEFAULT_QUOTAS.cores    # upper bound on vCPU's
        cpu_request    : 0           # will hold guaranteed min number of vCPU's as a float from 0 to infinity.
        privileged     : false       # for elevated docker privileges (FUSE mounting, later more)
        idle_timeout   : DEFAULT_QUOTAS.mintime

    # network access
    if settings.network  # free admin-set
        quota.network = true
    else                   # paid by some user
        for _,val of users
            if val?.upgrades?.network
                quota.network = true
                break

    # member hosting, which translates to "not pre-emptible"
    if settings.member_host  # free admin-set
        quota.member_host = true
    else                   # paid by some user
        for _,val of users
            if val?.upgrades?.member_host
                quota.member_host = true
                break

    # elevated quota for docker container (fuse mounting and maybe more ...)
    if settings.privileged
        quota.privileged = true
    # user-upgrades are disabled on purpose (security concerns and not implemented)!
    #else
    #    for _, val of users
    #        if val?.upgrades?.privileged
    #            quota.privileged = true
    #            break

    # little helper to caluclate the quotas, contributions, and limits
    # name: of the computed quota, upgrade the quota config key,
    # parse_num for converting numbers, and factor for conversions
    calc = (name, upgrade, parse_num, factor = 1) -><commit_msg>fix typo in a comment<commit_after># computing project quotas based on settings (by admin/system) and user contributions ("upgrades")

# historical note
# old code added a "hardcoded" cpu_shares value in the setting, which was set in the DB
# we no longer have that in kucalc
# in December 2018, we removed that from the DB (LIMIT 10 to avoid locking the db too long)

###
WITH s256 AS (
    SELECT project_id
    FROM projects
    WHERE (settings ->> 'cpu_shares')::float = 256
    ORDER BY created ASC
    LIMIT 10
)
UPDATE projects AS p
SET    settings = jsonb_set(settings, '{cpu_shares}', '0')
FROM   s256
WHERE  p.project_id = s256.project_id
RETURNING p.project_id;
###

{DEFAULT_QUOTAS} = require('smc-util/upgrade-spec')
MAX_UPGRADES = require('smc-util/upgrade-spec').upgrades.max_per_project

# No matter what, every project gets SOME possibly tiny amount of guaranteed cpu.
# This is important since otherwise projects will NOT start at all, e.g., if a paying
# customer is using 100% of the cpu on the node (this will happen if their limits are
# high and they have guaranteed cpu of about 1 or more).  The project will be so slow
# it fails to start in time and times out.
MIN_POSSIBLE_CPU =
    member    : 0.05
    nonmember : 0.02

# Min possible **guaranteed** RAM.
MIN_POSSIBLE_MEMORY =
    member    : 300
    nonmember : 200

# lower bound for the RAM "limit"
# in particular, we make sure member projects are above the free quota
MIN_MEMORY_LIMIT =
    member    : 1.5 * DEFAULT_QUOTAS.memory
    nonmember : DEFAULT_QUOTAS.memory


exports.quota = (settings, users) ->
    # so can assume defined below
    settings ?= {}
    users    ?= {}

    quota =
        network        : false
        member_host    : false
        disk_quota     : DEFAULT_QUOTAS.disk_quota
        memory_limit   : DEFAULT_QUOTAS.memory   # upper bound on RAM in MB
        memory_request : 0                       # will hold guaranteed RAM in MB
        cpu_limit      : DEFAULT_QUOTAS.cores    # upper bound on vCPU's
        cpu_request    : 0           # will hold guaranteed min number of vCPU's as a float from 0 to infinity.
        privileged     : false       # for elevated docker privileges (FUSE mounting, later more)
        idle_timeout   : DEFAULT_QUOTAS.mintime

    # network access
    if settings.network  # free admin-set
        quota.network = true
    else                   # paid by some user
        for _,val of users
            if val?.upgrades?.network
                quota.network = true
                break

    # member hosting, which translates to "not pre-emptible"
    if settings.member_host  # free admin-set
        quota.member_host = true
    else                   # paid by some user
        for _,val of users
            if val?.upgrades?.member_host
                quota.member_host = true
                break

    # elevated quota for docker container (fuse mounting and maybe more ...)
    if settings.privileged
        quota.privileged = true
    # user-upgrades are disabled on purpose (security concerns and not implemented)!
    #else
    #    for _, val of users
    #        if val?.upgrades?.privileged
    #            quota.privileged = true
    #            break

    # Little helper to calculate the quotas, contributions, and limits.
    # name: of the computed quota, upgrade the quota config key,
    # parse_num for converting numbers, and factor for conversions
    calc = (name, upgrade, parse_num, factor = 1) ->
{'fraction_non_alphanumeric': 0.06733668341708543, 'fraction_numerical': 0.012347451543431443, 'mean_word_length': 2.3881322957198443, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2340274', 'n_tokens_mistral': 2078, 'n_tokens_neox': 1926, 'n_words': 774}