|
| 1 | +import { Router } from "express"; |
| 2 | +import { Document } from "mongodb"; |
| 3 | +import { dbConnect } from "../db/utils"; |
| 4 | + |
| 5 | +const router = Router(); |
| 6 | + |
| 7 | +const availableFields = { |
| 8 | + owner: 1, |
| 9 | + |
| 10 | + // ioPackage fields |
| 11 | + tier: "$ioPackage.common.tier", |
| 12 | + mode: "$ioPackage.common.mode", |
| 13 | + type: "$ioPackage.common.type", |
| 14 | + compact: "$ioPackage.common.compact", |
| 15 | + connectionType: "$ioPackage.common.connectionType", |
| 16 | + dataSource: "$ioPackage.common.dataSource", |
| 17 | + adminUiConfig: "$ioPackage.common.adminUI.config", |
| 18 | + adminUiTab: "$ioPackage.common.adminUI.tab", |
| 19 | + depJsController: "$ioPackage.common.dependencies.js-controller", |
| 20 | + depAdmin: "$ioPackage.common.globalDependencies.admin", |
| 21 | + |
| 22 | + // adapter creator fields |
| 23 | + target: "$createAdapter.target", |
| 24 | + quotes: "$createAdapter.quotes", |
| 25 | + indentation: "$createAdapter.indentation", |
| 26 | + connectionIndicator: "$createAdapter.connectionIndicator", |
| 27 | + language: "$createAdapter.language", |
| 28 | + nodeVersion: "$createAdapter.nodeVersion", |
| 29 | + creatorVersion: "$createAdapter.creatorVersion", |
| 30 | + dependabot: "$createAdapter.dependabot", |
| 31 | + releaseScript: "$createAdapter.releaseScript", |
| 32 | + |
| 33 | + // computed fields |
| 34 | + publishState: { |
| 35 | + $cond: [ |
| 36 | + { |
| 37 | + $gt: [ |
| 38 | + { |
| 39 | + $size: { |
| 40 | + $filter: { |
| 41 | + input: "$repo-adapters", |
| 42 | + as: "ra", |
| 43 | + cond: { |
| 44 | + $eq: ["$$ra.source", "stable"], |
| 45 | + }, |
| 46 | + }, |
| 47 | + }, |
| 48 | + }, |
| 49 | + 0, |
| 50 | + ], |
| 51 | + }, |
| 52 | + "stable", |
| 53 | + { |
| 54 | + $cond: [ |
| 55 | + { |
| 56 | + $gt: [ |
| 57 | + { |
| 58 | + $size: { |
| 59 | + $filter: { |
| 60 | + input: "$repo-adapters", |
| 61 | + as: "ra", |
| 62 | + cond: { |
| 63 | + $eq: ["$$ra.source", "latest"], |
| 64 | + }, |
| 65 | + }, |
| 66 | + }, |
| 67 | + }, |
| 68 | + 0, |
| 69 | + ], |
| 70 | + }, |
| 71 | + "latest", |
| 72 | + { |
| 73 | + $cond: [ |
| 74 | + { |
| 75 | + $gt: ["$npmMetadata", null], |
| 76 | + }, |
| 77 | + "npm", |
| 78 | + "github", |
| 79 | + ], |
| 80 | + }, |
| 81 | + ], |
| 82 | + }, |
| 83 | + ], |
| 84 | + }, |
| 85 | +} as const; |
| 86 | + |
| 87 | +router.get("/api/statistics", async function (req, res) { |
| 88 | + const db = await dbConnect(); |
| 89 | + const repos = db.gitHubRepos(); |
| 90 | + |
| 91 | + const filters = Object.keys(req.query).filter( |
| 92 | + (key) => |
| 93 | + key !== "stats" && |
| 94 | + typeof req.query[key] === "string" && |
| 95 | + availableFields.hasOwnProperty(key), |
| 96 | + ) as (keyof typeof availableFields)[]; |
| 97 | + |
| 98 | + const statsToInclude = |
| 99 | + ((req.query.stats as string)?.split( |
| 100 | + ",", |
| 101 | + ) as (keyof typeof availableFields)[]) ?? []; |
| 102 | + statsToInclude.push(...filters); |
| 103 | + |
| 104 | + const pipeline: Document[] = [ |
| 105 | + { |
| 106 | + $match: { |
| 107 | + valid: true, |
| 108 | + }, |
| 109 | + }, |
| 110 | + ]; |
| 111 | + if (statsToInclude.includes("publishState") || req.query.publishState) { |
| 112 | + pipeline.push({ |
| 113 | + $lookup: { |
| 114 | + as: "repo-adapters", |
| 115 | + from: "repo-adapters", |
| 116 | + foreignField: "name", |
| 117 | + localField: "adapterName", |
| 118 | + }, |
| 119 | + }); |
| 120 | + } |
| 121 | + |
| 122 | + const $project: Record<string, any> = { |
| 123 | + _id: 0, |
| 124 | + }; |
| 125 | + for (const field in statsToInclude) { |
| 126 | + if (availableFields.hasOwnProperty(statsToInclude[field])) { |
| 127 | + $project[statsToInclude[field]] = |
| 128 | + availableFields[statsToInclude[field]]; |
| 129 | + } |
| 130 | + } |
| 131 | + pipeline.push({ |
| 132 | + $project, |
| 133 | + }); |
| 134 | + |
| 135 | + if (filters.length) { |
| 136 | + const $match: Record<string, any> = {}; |
| 137 | + for (const field of filters) { |
| 138 | + $match[field] = req.query[field]; |
| 139 | + } |
| 140 | + pipeline.push({ |
| 141 | + $match, |
| 142 | + }); |
| 143 | + } |
| 144 | + |
| 145 | + const stream = repos.aggregate(pipeline).stream(); |
| 146 | + |
| 147 | + const stats = new Map<string, Map<string, number>>(); |
| 148 | + for await (const doc of stream) { |
| 149 | + for (const [key, value] of Object.entries(doc)) { |
| 150 | + if (req.query[key]) { |
| 151 | + // skip stats that are used as filter |
| 152 | + continue; |
| 153 | + } |
| 154 | + if (!stats.has(key)) { |
| 155 | + stats.set(key, new Map<string, number>()); |
| 156 | + } |
| 157 | + const valString = String(value); |
| 158 | + const stat = stats.get(key)!; |
| 159 | + const oldValue = stat.get(valString) || 0; |
| 160 | + stat.set(valString, oldValue + 1); |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + const result: Record<string, Record<string, number>> = {}; |
| 165 | + for (const [key, stat] of stats.entries()) { |
| 166 | + result[key] = {}; |
| 167 | + for (const [value, count] of stat.entries()) { |
| 168 | + result[key][value] = count; |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + res.send(result); |
| 173 | +}); |
| 174 | + |
| 175 | +export default router; |
0 commit comments