Compare commits

...

10 Commits

Author SHA1 Message Date
Tangent
b08e96fe6b wip #8 complete rewrite of worker 2019-07-08 15:23:10 -07:00
Tangent
a6fa9a7149 a 5k dict is too small? 2019-06-28 19:38:37 -07:00
Tangent
bfe5084eef typo! 2019-06-28 19:38:09 -07:00
Tangent
624bd9c2f6 #8 worker added 2019-06-28 19:37:35 -07:00
Tangent
1fda7ef7fe fixed a bunch of tag shenanigans 2019-06-28 17:55:21 -07:00
Tangent
be734d28b1 okay NOW the pattern should be unborked 2019-06-28 17:33:40 -07:00
Tangent
0bad9154c6 un-automatic trimming 2019-06-28 17:32:19 -07:00
Tangent
dbedb15725 attempted fix of tag pages 2019-06-28 17:28:48 -07:00
Tangent
5b92aa7510 fixed tag name escaping 2019-06-27 21:03:04 -07:00
Tangent
aab578ecec fix typo in handling tag pages..again 2019-06-27 21:01:12 -07:00
5 changed files with 52 additions and 10 deletions

View File

@ -2,9 +2,9 @@ lapis = require "lapis"
console = require "lapis.console" console = require "lapis.console"
import respond_to from require "lapis.application" import respond_to from require "lapis.application"
import escape_identifier from require "lapis.db" import escape_literal, escape_identifier from require "lapis.db"
import unescape from require "lapis.util" import escape, unescape from require "lapis.util"
import create_track, update_track, escape_tag from require "helpers" import create_track, update_track from require "helpers"
import Tracks, Tags from require "models" import Tracks, Tags from require "models"
@ -52,6 +52,7 @@ class extends lapis.Application
return render: true return render: true
[tag: "/tag/:name(/:order[%a])(/:asc_desc)(/:page[%d])"]: => [tag: "/tag/:name(/:order[%a])(/:asc_desc)(/:page[%d])"]: =>
update_track(@params) if @params.id -- update a track if needed
-- validate order -- validate order
@order = tostring(@params.order)\lower! @order = tostring(@params.order)\lower!
unless Tags.fields[@order] unless Tags.fields[@order]
@ -62,7 +63,14 @@ class extends lapis.Application
@asc_desc = "ASC" @asc_desc = "ASC"
@page = tonumber(@params.page) or 1 @page = tonumber(@params.page) or 1
tracks = Tracks\paginated "WHERE tags LIKE ? ORDER BY #{escape_identifier @order} #{@asc_desc}", escape_tag(unescape(@params.name)), per_page: 32 -- create tag pattern
tag_pattern = unescape(@params.name)
if tag_pattern != tag_pattern\lower!
return redirect_to: @url_for "tag", name: escape(tag_pattern\lower!), order: @order, asc_desc: @asc_desc, page: @page
tag_pattern = escape_literal tag_pattern
tag_pattern = "'% #{tag_pattern\sub 2, -2} %'"
tracks = Tracks\paginated "WHERE tags LIKE #{tag_pattern} ORDER BY #{escape_identifier @order} #{@asc_desc}", per_page: 32
@last_page = 1 @last_page = 1
if tag = Tags\find name: unescape(@params.name) if tag = Tags\find name: unescape(@params.name)
@last_page = 1 + math.floor tag.count / 32 @last_page = 1 + math.floor tag.count / 32
@ -72,7 +80,7 @@ class extends lapis.Application
if @page > @last_page if @page > @last_page
return redirect_to: @url_for "tag", name: @params.name, order: @order, asc_desc: @asc_desc, page: @last_page return redirect_to: @url_for "tag", name: @params.name, order: @order, asc_desc: @asc_desc, page: @last_page
@tracks = tags\get_page(@page) @tracks = tracks\get_page(@page)
return render: true return render: true
[tags: "/tags(/:order[%a])(/:asc_desc)(/:page[%d])"]: => [tags: "/tags(/:order[%a])(/:asc_desc)(/:page[%d])"]: =>

View File

@ -14,9 +14,6 @@ process_tags = (tags_str) ->
table.sort taglist table.sort taglist
return " #{table.concat taglist, " "} " return " #{table.concat taglist, " "} "
escape_tag = (str) ->
return str\gsub "[%%_]", "\\%1"
update_track = (user_input) -> update_track = (user_input) ->
updates = {} updates = {}
-- automatic tags -- automatic tags
@ -49,7 +46,6 @@ update_track = (user_input) ->
return { return {
:process_tags :process_tags
:escape_tag
:update_track :update_track
create_track: update_track create_track: update_track
} }

View File

@ -11,6 +11,11 @@ http {
charset UTF-8; charset UTF-8;
include mime.types; include mime.types;
lua_shared_dict worker 1m;
init_worker_by_lua '
require("worker")
';
server { server {
listen ${{PORT}}; listen ${{PORT}};
lua_code_cache ${{CODE_CACHE}}; lua_code_cache ${{CODE_CACHE}};

View File

@ -44,7 +44,7 @@ class extends Widget
for track in *@tracks for track in *@tracks
tr class: Tracks.statuses[track.status], -> tr class: Tracks.statuses[track.status], ->
form { form {
action: @url_for "tracks", order: @order, asc_desc: @asc_desc, page: @page action: @url_for "tag", name: @params.name, order: @order, asc_desc: @asc_desc, page: @page
method: "POST" method: "POST"
enctype: "multipart/form-data" enctype: "multipart/form-data"
}, -> }, ->

33
worker.moon Normal file
View File

@ -0,0 +1,33 @@
lock = require "resty.lock"
import Tags, Tracks from require "models"
import escape_literal from require "lapis.db"
next_id = (id=0) ->
next_tag = Tags\select "WHERE id > ? ORDER BY id ASC LIMIT 1", id
unless next_tag
next_tag = Tags\select "WHERE id > '0' ORDER BY id ASC LIMIT 1"
if next_tag
return next_tag[1].id
update_tag_counts = (premature, tag_id) ->
if premature return
thread_lock = lock\new "worker", { timeout: 0, exptime: 2 }
if thread_lock\lock "update_tag_counts.thread"
time_lock = lock\new "worker", { exptime: 2 }
if time_lock\lock "update_tag_counts.time"
if tag_id
if tag = Tags\find id: tag_id
tag_pattern = escape_literal tag.name
tag_pattern = "'% #{tag_pattern\sub 2, -2} %'"
count = Tracks\count "tags LIKE #{tag_pattern}"
tag\update(:count)
tag_id = next_id(tag_id) or tag_id
thread_lock\unlock!
ngx.timer.at(2, update_tag_counts, tag_id)
ngx.timer.at(0, update_tag_counts)