<commit_before>###
The raw router.
###

fs           = require('fs')
os_path      = require('path')
async        = require('async')

express      = require('express')

serve_static = require('serve-static')

misc         = require('smc-util/misc')
{defaults, required} = misc

{public_access_request} = require('./access')

listing = require('./listing')

HEXCHARS = ("#{i}" for i in [0..9]).concat(String.fromCharCode(i) for i in [97..122])

# redirect /[uuid] and /[uuid]?query=123 to /[uuid]/ and /[uuid]/?query=123
redirect_to_directory = (req, res) ->
    query = req.url.slice(req.path.length)
    res.redirect(301, req.baseUrl + req.path + '/' + query)


exports.raw_router = (opts) ->
    opts = defaults opts,
        database : required
        path     : required
        logger   : undefined

    if opts.logger?
        d = (args...) ->
            opts.logger.debug("raw_router: ", args...)
    else
        d = ->

    d()

    router = express.Router()

    router.get '/', (req, res) ->
        # simple listing by first 2 characters in project_id
        # CREATE INDEX IF NOT EXISTS project_id_2char ON public_paths (substring(project_id::text from 0 for 2));
        links = ("<a href='#{l}'>#{l}...</a>" for l in HEXCHARS)
        links = links.join('<br/>')
        out = """
        <h1>Public Projects</h1>
        #{links}
        """
        res.send(out)

    router.get /^\/[0-9a-z]$/, (req, res) ->
        # matches one uuid char
        c1 = req.path[1]
        links = ("<a href='#{c1}#{l}'>#{c1}#{l}...</a>" for l in HEXCHARS)
        links = links.join('<br/>')
        out = """
        <h1>Public Projects</h1>
        <a href='./'>UP</a><br/><br/>
        #{links}
        """
        res.send(out)

    router.get /^\/[0-9a-z]{2}$/, (req, res) ->
        # matches two uuid char
        c2 = req.path[1..2]
        console.log(c2)
        opts.database._query
            query : 'SELECT DISTINCT(project_id) FROM public_paths'
            where :
                "substring(project_id::text from 1 for 2) = $::TEXT" : c2
            order_by : 'project_id'
            cb    : (err, result) ->
                if err
                    res.send(JSON.stringify(err))
                else
                    out = """
                    <h1>Public Projects</h1>
                    <a href='#{c2[0]}'>UP</a><br/><br/>
                    Found #{result.rowCount}<br/><br/>
                    """
                    for row in result.rows<commit_msg>hub/raw server: show number of projects<commit_after>###
The raw router.
###

fs           = require('fs')
os_path      = require('path')
async        = require('async')

express      = require('express')

serve_static = require('serve-static')

misc         = require('smc-util/misc')
{defaults, required} = misc

{public_access_request} = require('./access')

listing = require('./listing')

HEXCHARS = ("#{i}" for i in [0..9]).concat(String.fromCharCode(i) for i in [97..122])

# redirect /[uuid] and /[uuid]?query=123 to /[uuid]/ and /[uuid]/?query=123
redirect_to_directory = (req, res) ->
    query = req.url.slice(req.path.length)
    res.redirect(301, req.baseUrl + req.path + '/' + query)


exports.raw_router = (opts) ->
    opts = defaults opts,
        database : required
        path     : required
        logger   : undefined

    if opts.logger?
        d = (args...) ->
            opts.logger.debug("raw_router: ", args...)
    else
        d = ->

    d()

    router = express.Router()

    router.get '/', (req, res) ->
        # simple listing by first 2 characters in project_id
        # CREATE INDEX IF NOT EXISTS project_id_2char ON public_paths (substring(project_id::text from 1 for 2));
        # CREATE INDEX IF NOT EXISTS project_id_1char ON public_paths (substring(project_id::text from 1 for 1));

        opts.database._query
            query : '''
                    SELECT DISTINCT(substring(project_id::text from 1 for 1)) AS prefix, COUNT(DISTINCT project_id) AS num FROM public_paths
                    GROUP BY prefix
                    ORDER BY prefix
                    '''
            cb    : (err, result) ->
                if err
                    res.send(JSON.stringify(err))
                else
                    out = """
                    <h1>Public Projects</h1>
                    """
                    for row in result.rows
                        out += "<a href='#{row.prefix}'>#{row.prefix}...</a> (#{row.num})<br/>"
                    res.send(out)

    router.get /^\/[0-9a-z]$/, (req, res) ->
        # matches one uuid char
        c1 = req.path[1]

        opts.database._query
            query : '''
                    SELECT DISTINCT(substring(project_id::text from 1 for 2)) AS prefix, COUNT(DISTINCT project_id) AS num FROM public_paths
                    WHERE substring(project_id::text from 1 for 1) = $1::TEXT
                    GROUP BY prefix
                    ORDER BY prefix
                    '''
            params : [c1]
            cb    : (err, result) ->
                if err
                    res.send(JSON.stringify(err))
                else
                    out = """
                    <h1>Public Projects</h1>
                    <a href='./'>UP</a><br/><br/>
                    """
                    for row in result.rows
                        out += "<a href='#{row.prefix}'>#{row.prefix}...</a> (#{row.num})<br/>"
                    res.send(out)

    router.get /^\/[0-9a-z]{2}$/, (req, res) ->
        # matches two uuid char
        c2 = req.path[1..2]
        console.log(c2)
        opts.database._query
            query : 'SELECT DISTINCT(project_id) FROM public_paths'
            where :
                "substring(project_id::text from 1 for 2) = $::TEXT" : c2
            order_by : 'project_id'
            cb    : (err, result) ->
                if err
                    res.send(JSON.stringify(err))
                else
                    out = """
                    <h1>Public Projects</h1>
                    <a href='#{c2[0]}'>UP</a><br/><br/>
                    Found #{result.rowCount}<br/><br/>
                    """
                    for row in result.rows
{'fraction_non_alphanumeric': 0.15217391304347827, 'fraction_numerical': 0.015619967793880838, 'mean_word_length': 1.5227457351746547, 'pattern_counts': {'":': 0, '<': 47, '<?xml version=': 0, '>': 65, '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': '7122722', 'n_tokens_mistral': 1968, 'n_tokens_neox': 1849, 'n_words': 442}