<commit_before>
local elastic = require 'elastic'
local JSON = require 'cjson'
local input = require 'parse'

function handle(r)
    r.content_type = "application/json"
    local get = r:parseargs()
    local post = input.parse(r)
    
    if get.delete then
        local doc = elastic.get('rule', get.delete)
        if doc then
            elastic.delete('rule', get.delete)
        end
    end
    
    if post.addrule then
        local name = post.addrule.name
        local t = post.addrule.type
        local limit = post.addrule.limit
        local span = post.addrule.span or 24
        local tbl = {}
        for k, v in pairs(post.addrule.query) do
            table.insert(tbl, v)
        end
        local id = r:sha1(math.random(1, os.time()) .. r.clock() .. r.useragent_ip)
        if #tbl > 0 then
            local doc = {
                name = name,
                ['type'] = t,
                limit = limit,
                span = span,
                query = tbl
            }
            if post.addrule.id then
                local xdoc = get('rule', post.addrule.id)
                if xdoc then
                    elastic.update('rule', post.addrule.id, doc)
                end
            else
                elastic.index(r, id, 'rule', doc)
            end
        end
    end
    
    ruleList = elastic.raw ({
            sort = {
                {name = "asc"}
            },
            size = 999
        }, 'rule')
    local rl = {}
    if ruleList and ruleList.hits.hits then<commit_msg>no need to sort this just yet<commit_after>
local elastic = require 'elastic'
local JSON = require 'cjson'
local input = require 'parse'

function handle(r)
    r.content_type = "application/json"
    local get = r:parseargs()
    local post = input.parse(r)
    
    if get.delete then
        local doc = elastic.get('rule', get.delete)
        if doc then
            elastic.delete('rule', get.delete)
        end
    end
    
    if post.addrule then
        local name = post.addrule.name
        local t = post.addrule.type
        local limit = post.addrule.limit
        local span = post.addrule.span or 24
        local tbl = {}
        for k, v in pairs(post.addrule.query) do
            table.insert(tbl, v)
        end
        local id = r:sha1(math.random(1, os.time()) .. r.clock() .. r.useragent_ip)
        if #tbl > 0 then
            local doc = {
                name = name,
                ['type'] = t,
                limit = limit,
                span = span,
                query = tbl
            }
            if post.addrule.id then
                local xdoc = get('rule', post.addrule.id)
                if xdoc then
                    elastic.update('rule', post.addrule.id, doc)
                end
            else
                elastic.index(r, id, 'rule', doc)
            end
        end
    end
    
    ruleList = elastic.raw ({
            size = 999
        }, 'rule')
    local rl = {}
    if ruleList and ruleList.hits.hits then
{'fraction_non_alphanumeric': 0.10069790628115653, 'fraction_numerical': 0.005317381189764041, 'mean_word_length': 1.3297213622291022, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '5343425', 'n_tokens_mistral': 910, 'n_tokens_neox': 858, 'n_words': 250}