srctree

Gregory Mullen parent 5416a648 d0db0566
fix dependency loop by ignoring types in stats

the word fix here is doing a lot of heavy lifting. Someone will stillneed to spend some time to figure this loop out.

Other options to be considered include, flattening out the structure ofthe router, and reverting to the previous @fieldParentPtrimplementation.

inlinesplit
build.zig added: 47, removed: 69, total 0
@@ -6,7 +6,6 @@ pub fn build(b: *std.Build) !void {
const use_llvm = false;
 
//if (b.args) |args| for (args) |arg| std.debug.print("arg {s}\n", .{arg});
 
//std.debug.print("default: {s}\n", .{b.default_step.name});
 
// root build options
@@ -42,6 +41,7 @@ pub fn build(b: *std.Build) !void {
if (std.fs.cwd().access("src/builtin-html/index.html", .{})) {
compiler.addDir(b.path("src/builtin-html/"));
} else |_| {}
compiler.addFile(b.path("src/builtin-html/verse-stats.html"));
 
compiler.collect() catch @panic("unreachable");
const comptime_templates = compiler.buildTemplates() catch @panic("unreachable");
@@ -108,20 +108,15 @@ const ThisBuild = @This();
 
const Compiler = struct {
b: *std.Build,
dirs: std.ArrayList(std.Build.LazyPath),
files: std.ArrayList([]const u8),
collected: std.ArrayList(std.Build.LazyPath),
dirs: std.ArrayListUnmanaged(std.Build.LazyPath),
files: std.ArrayListUnmanaged(std.Build.LazyPath),
collected: std.ArrayListUnmanaged(std.Build.LazyPath),
templates: ?*std.Build.Module = null,
structs: ?*std.Build.Module = null,
debugging: bool = false,
 
pub fn init(b: *std.Build) Compiler {
return .{
.b = b,
.dirs = std.ArrayList(std.Build.LazyPath).init(b.allocator),
.files = std.ArrayList([]const u8).init(b.allocator),
.collected = std.ArrayList(std.Build.LazyPath).init(b.allocator),
};
return .{ .b = b, .dirs = .{}, .files = .{}, .collected = .{} };
}
 
pub fn raze(self: Compiler) void {
@@ -141,23 +136,19 @@ const Compiler = struct {
}
 
pub fn addDir(self: *Compiler, dir: std.Build.LazyPath) void {
//const copy = self.b.allocator.dupe(u8, dir) catch @panic("OOM");
self.dirs.append(dir) catch @panic("OOM");
self.dirs.append(self.b.allocator, dir) catch @panic("OOM");
self.templates = null;
self.structs = null;
}
 
pub fn addFile(self: *Compiler, file: []const u8) void {
const copy = self.b.allocator.dupe(u8, file) catch @panic("OOM");
self.files.append(copy) catch @panic("OOM");
pub fn addFile(self: *Compiler, file: std.Build.LazyPath) void {
self.files.append(self.b.allocator, file) catch @panic("OOM");
self.templates = null;
self.structs = null;
}
 
pub fn buildTemplates(self: *Compiler) !*std.Build.Module {
if (self.templates) |t| return t;
 
//std.debug.print("building for {}\n", .{self.collected.items.len});
const compiled = self.b.createModule(.{
.root_source_file = self.depPath("src/template/comptime.zig"),
});
@@ -167,10 +158,6 @@ const Compiler = struct {
 
for (self.collected.items, names) |lpath, *name| {
name.* = lpath.getPath3(self.b, null).sub_path;
//std.debug.print("builder {s}\n", .{name.*});
//const base = lpath.getPath3(self.b, null).basename();
//const file = try lpath.getPath3(self.b, null).toString(self.b.allocator);
//name.* = try lpath.getPath3(self.b, null).toString(self.b.allocator);
_ = compiled.addAnonymousImport(name.*, .{
.root_source_file = lpath,
});
@@ -217,11 +204,11 @@ const Compiler = struct {
var itr = idir.iterate();
while (try itr.next()) |file| {
if (!std.mem.endsWith(u8, file.name, ".html")) continue;
try self.collected.append(srcdir.path(self.b, file.name));
try self.collected.append(self.b.allocator, srcdir.path(self.b, file.name));
}
}
for (self.files.items) |file| {
try self.collected.append(self.b.path(file));
try self.collected.append(self.b.allocator, file);
}
}
};
 
src/builtin-html/stats.html added: 47, removed: 69, total 0
@@ -20,10 +20,7 @@
</div>
 
<div class="verse-stats-contents">
<For StatsList>
<span class="verse-stats-line"><Number type="usize" /> : <Uri /> - <Time type="usize" /> - <Size type="usize" /> <Us type="usize" /></span>
</For>
<For StatsListLast>
<For VerseStatsList>
<span class="verse-stats-line"><Number type="usize" /> : <Uri /> - <Time type="usize" /> - <Size type="usize" /> <Us type="usize" /></span>
</For>
</div>
 
src/endpoint.zig added: 47, removed: 69, total 0
@@ -73,18 +73,18 @@ pub fn validateEndpoint(EP: anytype) void {
}
 
// TODO figure out why this causes a dependency loop and reenable
//if (@hasDecl(EP, "verse_routes")) {
// if (@TypeOf(EP.verse_routes) != []Router.Match) {
// @compileError("the `verse_routes` decl is reserved for a list of pre-constructed " ++
// "`Match` targets. expected []Router.Match but found " ++ @typeName(@TypeOf(EP.verse_routes)) ++ ".");
// }
// //inline for (EP.verse_routes, 0..) |route, i| {
// // if (@TypeOf(route) != Router.Match) {
// // @compileError("the `verse_routes` decl is reserved for a list of pre-constructed " ++
// // "`Match` targets. " ++ @typeName(route) ++ " at position " ++ i ++ " is invalid.");
// // }
// //}
//}
if (@hasDecl(EP, "verse_routes")) {
//if (@TypeOf(EP.verse_routes) != []Router.Match) {
// @compileError("the `verse_routes` decl is reserved for a list of pre-constructed " ++
// "`Match` targets. expected []Router.Match but found " ++ @typeName(@TypeOf(EP.verse_routes)) ++ ".");
//}
inline for (EP.verse_routes, 0..) |route, i| {
if (@TypeOf(route) != Router.Match) {
@compileError("the `verse_routes` decl is reserved for a list of pre-constructed " ++
"`Match` targets. " ++ @typeName(route) ++ " at position " ++ i ++ " is invalid.");
}
}
}
 
if (@hasDecl(EP, "verse_builder")) {
if (@TypeOf(EP.verse_builder) != Router.Builder) {
 
src/frame.zig added: 47, removed: 69, total 0
@@ -40,7 +40,7 @@ status: ?std.http.Status = null,
headers_done: bool = false,
 
/// Unstable API; may be altered or removed in the future
server: *const Server,
server: *align(8) const anyopaque,
 
const Frame = @This();
 
 
src/stats.zig added: 47, removed: 69, total 0
@@ -88,28 +88,27 @@ pub const Endpoint = struct {
const S = @import("template.zig").Structs;
pub const verse_name = .stats;
 
const StatsPage = PageData("builtin-html/stats.html");
const StatsPage = PageData("builtin-html/verse-stats.html");
 
pub const stats = index;
 
pub fn index(f: *Frame) Router.Error!void {
var data: [30]S.StatsList = @splat(.{
.number = 0,
.size = 0,
.time = 0,
.uri = "null",
.us = 0,
});
var data: [30]S.VerseStatsList = @splat(
.{ .number = 0, .size = 0, .time = 0, .uri = "null", .us = 0 },
);
var count: usize = 0;
var uptime = std.time.timestamp();
var mean_time: u64 = 0;
 
if (f.server.stats) |active| {
if (@as(*Server, @ptrCast(@constCast(f.server))).stats) |active| {
count = active.count;
uptime -|= active.start_time;
mean_time = active.mean.mean(@truncate(count));
for (&data, &active.rows) |*dst, *src| {
dst.* = .{
for (0..30) |i| {
if (i >= count) break;
const idx = count - i - 1;
const src = &active.rows[idx % active.rows.len];
data[i] = .{
.number = src.number,
.size = src.size,
.time = src.time,
@@ -119,20 +118,11 @@ pub const Endpoint = struct {
}
}
 
var first = data[0..@min(count % data.len, data.len)];
var last = data[count % data.len .. @min(count, data.len)];
 
if (count > data.len) {
last = first;
first = data[count % data.len .. @min(count, data.len)];
}
 
const page = StatsPage.init(.{
.uptime = @intCast(uptime),
.count = count,
.mean_resp_time = mean_time,
.stats_list = first,
.stats_list_last = @ptrCast(last),
.verse_stats_list = data[0..@min(count, data.len)],
});
return f.sendPage(&page);
}
@@ -142,3 +132,4 @@ pub const Endpoint = struct {
 
const std = @import("std");
const Frame = @import("frame.zig");
const Server = @import("server.zig");
 
src/testing.zig added: 47, removed: 69, total 0
@@ -53,7 +53,7 @@ pub fn smokeTest(
 
pub fn fuzzTest(trgt: Router.Target) !void {
const Context = struct {
target: *Router.Target,
target: *const Router.Target,
 
fn testOne(context: @This(), input: []const u8) anyerror!void {
var fc = try FrameCtx.initRequest(
@@ -137,7 +137,10 @@ pub const FrameCtx = struct {
.auth_provider = .invalid,
.response_data = .init(a),
.headers = headers(),
.server = undefined,
.server = &Server{
.interface = undefined,
.stats = null,
},
},
.buffer = buffer,
};