• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

knowledgepixels / nanodash / 27622721129

16 Jun 2026 01:55PM UTC coverage: 26.963% (+6.3%) from 20.697%
27622721129

Pull #483

github

web-flow
Merge 73a4d0fe1 into 663f14f46
Pull Request #483: Space/resource About pages, ref-aware spaces, and magic query params

1542 of 6717 branches covered (22.96%)

Branch coverage included in aggregate %.

3407 of 11638 relevant lines covered (29.27%)

4.31 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

60.66
src/main/java/com/knowledgepixels/nanodash/QueryApiAccess.java
1
package com.knowledgepixels.nanodash;
2

3
import org.apache.commons.lang3.tuple.Pair;
4
import org.eclipse.rdf4j.model.IRI;
5
import org.nanopub.extra.services.*;
6
import org.slf4j.Logger;
7
import org.slf4j.LoggerFactory;
8

9
import java.util.concurrent.ConcurrentHashMap;
10
import java.util.concurrent.ConcurrentMap;
11

12
/**
13
 * Utility class for accessing and managing API queries.
14
 * Provides methods to retrieve query results, manage query IDs, and fetch the latest versions of nanopublications.
15
 */
16
public class QueryApiAccess {
17

18
    private QueryApiAccess() {
19
    }  // no instances allowed
20

21
    // Query IDs (full id = RA.../query-name)
22
    public static final String GET_LATEST_NANOPUBS_FROM_PUBKEYS = "RAe-oA5eSmkCXCALZ99-0k4imnlI74KPqURfhHOmnzo6A/get-latest-nanopubs-from-pubkeys";
23
    public static final String GET_LATEST_NANOPUBS_FROM_USERID = "RAuy4N1h4vZ1wgBUMvTiWw2y_Y0_5oFYRTwdq-xj2qqNM/get-latest-nanopubs-from-userid";
24
    public static final String GET_USER_STATS_FROM_PUBKEYS = "RAiCBvPL2hRGzI8g5L68O-C9yEXryC_vG35GdEm5jtH_s/get-user-stats-from-pubkeys";
25
    public static final String GET_USER_STATS_FROM_USERID = "RA3U23LL3xbNwsu92fAqsKb0kagOud4f9TlRQq3evNJck/get-user-stats-from-userid";
26
    public static final String GET_TOP_CREATORS_LAST30D = "RAcNvmEiUNUb2a7O4fwRvy2x2BCN640AC880fTzFworr8/get-top-creators-last30d";
27
    public static final String GET_LATEST_USERS = "RAr27GmRUKQmvPbfmB34N9l9lX-xYK7nQhvOMbQCk3byI/get-latest-users";
28
    public static final String GET_MOST_RECENT_NANOPUBS = "RAYNg6rfvXIVvJY2u8oS0EEjxnVvimLLVZG1rOar_nWIY/get-most-recent-nanopubs";
29
    public static final String GET_PUBLISHER_VERSION = "RAPGhXDRzeGu-Qk0AkjleEtxMxqAvJ-dZn7985gzAbyhs/get-publisher-version";
30
    public static final String GET_MOST_USED_TEMPLATES_LAST30D = "RAvL7pe2ppsfq4mVWTdJjssYGsjrmliNd_sZO2ytLvg1Y/get-most-used-templates-last30d";
31
    public static final String GET_LATEST_NANOPUBS_BY_TYPE = "RANn4Mu8r8bqJA9KJMGXTQAEGAEvtNKGFsuhRIC6BRIOo/get-latest-nanopubs-by-type";
32
    public static final String GET_LATEST_VERSION_OF_NP = "RAiRsB2YywxjsBMkVRTREJBooXhf2ZOHoUs5lxciEl37I/get-latest-version-of-np";
33
    public static final String GET_ALL_USER_INTROS = "RAjHh6P11QFUaoPiMRBavdAnTq4YMJW4PB85oVFSBfYjU/get-all-user-intros";
34
    public static final String GET_ALL_USER_PROFILE_PICS= "RAtcodMPmTrmBvdOqwYIrNNFDO74f8B_xo0qsOcKlCwTA/get-all-user-profile-pics";
35
    public static final String GET_ALL_USER_DEFAULT_LICENSE = "RA-_IwzReR2_HfTLz4YcNM6Mh3Vt16y0RUS12tpJTN9FI/get-all-user-default-license";
36
    public static final String GET_SUGGESTED_TEMPLATES_TO_GET_STARTED = "RA-tlMmQA7iT2wR2aS3PlONrepX7vdXbkzeWluea7AECg/get-suggested-templates-to-get-started";
37
    public static final String GET_MONTHLY_TYPE_OVERVIEW_BY_PUBKEYS = "RAhI-C2KsqS_IvnxwyBrbMFsoj65dhLWE_CBo_KtcVEVA/get-monthly-type-overview-by-pubkeys";
38
    public static final String GET_APPROVED_NANOPUBS = "RAn3agwsH2yk-8132RJApGYxdPSHHCXDAIYiCaSBBo6tg/get-approved-nanopubs";
39
    public static final String FIND_URI_REFERENCES = "RAz1ogtMxSTKSOYwHAfD5M3Y-vd1vd46OZta_vvbqh8kY/find-uri-references";
40
    public static final String GET_NANOPUBS_BY_TYPE = "RAE35dYJQlpnqim7VeKuu07E9I1LQUZpkdYQR4RvU3KMU/get-nanopubs-by-type";
41
    public static final String GET_INTRODUCING_NANOPUB = "RALZXWg5lZoJoQ0VHL5mpDgNxYpqU6FoDLWGp4rs8A6b8/get-introducing-nanopub";
42
    public static final String FULLTEXT_SEARCH = "RAxdh5xkc6K6SMLY23yKu__zTWJPXeRFc0qgNNxkbOkpY/fulltext-search";
43
    public static final String FIND_THINGS = "RAyMrQ89RECTi9gZK5q7gjL1wKTiP8StkLy0NIkkCiyew/find-things";
44
    public static final String GET_INSTANCES = "RAjt1H9rCSr6A9VGzlhye00zPdH69JdGc3kd_2VjDmzVg/get-instances";
45
    public static final String GET_CLASSES_FOR_THING = "RAH06iUwnvj_pRARY15ayJAY5tuJau3rCvHhPPhe49fVI/get-classes-for-thing";
46
    public static final String FIND_REFERENCING_NANOPUBS = "RAJStXEm1wZcg34ZLPqe00VPSzIVCwC2rrxdj_JR8v5DY/find-referencing-nanopubs";
47
    public static final String GET_LABELS_FOR_THING = "RAtftxAXJubB4rlm9fOvvHNVIkXvWQLC6Ag_MiV7HL0ow/get-labels-for-thing";
48
    public static final String GET_TEMPLATES_WITH_URI = "RARtWHRzNY5hh31X2VB5eOCJAdp9Cjv4CakA0Idqz69MI/get-templates-with-uri";
49
    public static final String GET_NEWER_VERSIONS_OF_NP = "RAqmmNSxQaRNWRYH0o4Da3GSOwvoFLObhXfAGUCOqEtfw/get-newer-versions-of-np";
50
    public static final String GET_QUERIES = "RAQqjXQYlxYQeI4Y3UQy9OrD5Jx1E3PJ8KwKKQlWbiYSw/get-queries";
51
    public static final String GET_LATEST_THING_NANOPUB = "RAzXDzCHoZmJITgYYquLwDDkSyNf3eKKQz9NfQPYB1cyE/get-latest-thing-nanopub";
52
    public static final String GET_PROJECTS = "RAnpimW7SPwaum2fefdS6_jpzYxcTRGjE-pmgNTL_BBJU/get-projects";
53
    public static final String GET_OWNERS = "RApiw7Z0NeP3RaLiqX6Q7Ml5CfEWbt-PysUbMNljuiLJw/get-owners";
54
    public static final String GET_MEMBERS = "RASyFJyADTtG-l_Qe3a5PE_e2yUJR-PydXfkZjjrBuV7U/get-members";
55
    public static final String GET_PARTS = "RAJmZoM0xCGE8OL6EgmQBOd1M58ggNkwZ0IUqHOAPRfvE/get-parts";
56
    public static final String GET_ASSERTION_TEMPLATES = "RA6bgrU3Ezfg5VAiLru0BFYHaSj6vZU6jJTscxNl8Wqvc/get-assertion-templates";
57
    public static final String GET_PROVENANCE_TEMPLATES = "RA4bt3MQRnEPC2nSsdbCJc74wT-e1w68dSCpYVyvG0274/get-provenance-templates";
58
    public static final String GET_PUBINFO_TEMPLATES = "RAMcdiJpvvk8424AJIH1jsDUQVcPYOLRw0DNnZt_ND_LQ/get-pubinfo-templates";
59
    public static final String GET_FILTERED_NANOPUB_LIST = "RAeoXI4vBzLV_BM2lfI5DWkFSfm6y1z3fOk4E1IncXWUo/get-filtered-nanopub-list";
60
    public static final String GET_LATEST_ACCEPTED_BDJ = "RAkoDiXZG_CYt978-dZ_vffK-UTbN6e1bmtFy6qdmFzC4/get-latest-accepted-bdj";
61
    public static final String GET_LATEST_BIODIV_CANDIDATES = "RAgnLJH8kcI_e488VdoyQ0g3-wcumj4mSiusxPmeAYsSI/get-latest-biodiv-candidates";
62
    public static final String GET_LATEST_ACCEPTED_DS = "RATpsBysLf8yXeMpY7PHKj-aKNCa4-4Okg1hi97OLDXIo/get-latest-accepted-ds";
63
    public static final String GET_LATEST_DS_CANDIDATES = "RAFNTW3jhWKnNvhMSOfYvG53ZAurxrFv_-vnIJkZyfAuo/get-latest-ds-candidates";
64
    public static final String GET_DS_REACTIONS = "RA0FiH8gukovvEHPBMn72zUDdMQylQmUwtIGNLYBZXGfk/get-ds-reactions";
65
    public static final String GET_LATEST_ACCEPTED_RIO = "RAAXmnJdXHO86GqJs8VTdqapUWqCrHKRgRT2b4NfjAfgk/get-latest-accepted-rio";
66
    public static final String GET_LATEST_RIO_CANDIDATES = "RAehKOCOnZ3uDBmI0kkCNTh5k9Nl6YYNj7tyc20tVymxY/get-latest-rio-candidates";
67
    public static final String GET_REACTIONS = "RAe7k3L0oElPOrFoUMkUhqU9dGUqfBaUSw3cVplOUn3Fk/get-reactions";
68
    public static final String GET_TERM_DEFINITIONS = "RAZUsK7jU85oUYEVKvMPFlqbwn19oR55IQuFkXuiS_Tkg/get-term-definitions";
69
    // v10 (issue #302): standalone + preset-supplied views (unbound ?display), gated to
70
    // admins/maintainers of the owning space or the affected user themselves. Each
71
    // referenced view is resolved to its latest version server-side: the version tree's
72
    // most recent current head (a nanopub itself neither superseded nor validly retracted
73
    // via npx:invalidates), robust to backdated supersedes and retracted versions, so
74
    // ?view is already the latest and needs no separate per-view lookup. v10 wraps that
75
    // resolution in a run-once sub-SELECT so the cross-repo lookup federates once for the
76
    // whole view set instead of once per referenced view -- cut a 44-display page from
77
    // ~4.5s to ~1.7s (the per-view federation round-trips were the dominant cost).
78
    public static final String GET_VIEW_DISPLAYS = "RAy49uUd2fPLHJAZ_7QKDtIDVgqaQ589OgQhMwNamKy-4/get-view-displays";
79

80
    // Spaces-repo queries (endpoint: nanopub-query .../repo/spaces)
81
    // v2: IRI-keyed get-spaces. Prior client head, retained for reference; deployments up
82
    // to this release stay pinned on it, and the roll-out fork-merge will supersede both
83
    // it and v3. No longer fetched by SpaceRepository (now uses GET_SPACES_REF).
84
    public static final String GET_SPACES = "RAxGboS_juHuMyJQghGV3elEgZmQTew5oyw_aC9O9FFQI/get-spaces";
85
    // v3: ref-aware get-spaces (adds ?ref + ?root so the client can key one space per
86
    // ref). Published as an independent nanopub (no npx:supersedes). Active query used by
87
    // SpaceRepository. Source at docs/queries/get-spaces-ref.trig. See
88
    // docs/space-ref-identity.md.
89
    public static final String GET_SPACES_REF = "RAD5KmWO6uqjM04tK7tb2IREgbxA1GTGyRhaRjjaVIKPw/get-spaces";
90
    public static final String GET_SUB_SPACE_LINKS = "RAWgoQbP9_B9h3Bnwd1FGYX1gLYPyZFOxaeqIeA3TTPSU/get-sub-space-links";
91
    public static final String GET_MAINTAINED_RESOURCES = "RAOOq81R84exTUKUBQT3BbgCaSJyC2lqPDXIP2XaDTosM/get-maintained-resources";
92
    public static final String GET_SPACE_ADMINS = "RAaHOXMQ7Kq37T9syR9at0RqushclHenlPOFRwFDn0Cfs/get-space-admins";
93
    // Ref-scoped admins (Stage 2): takes the ref's root nanopub (root_np), matches admins
94
    // on npa:forSpaceRef, so multi-ref spaces don't merge admin sets across refs. Published
95
    // independently. Source at docs/queries/get-space-admins-ref.trig. See
96
    // docs/space-ref-identity.md.
97
    public static final String GET_SPACE_ADMINS_REF = "RAWM8qlKbV3DEH_NsPJ6hIyTrBwIp8sNeg9MGDgu8la1o/get-space-admins";
98
    public static final String GET_SPACE_ADMIN_PUBKEY_HASHES = "RAJvvNY6KXqveJivZKh-chTCntrsY_KJSGLVNRQdi0pUc/get-space-admin-pubkey-hashes";
99
    // Ref-scoped admin pubkey hashes (Stage 2): takes the ref's root nanopub (root_np),
100
    // matches admins on npa:forSpaceRef, so multi-ref spaces don't merge admin keys across
101
    // refs. Published independently. Source at docs/queries/get-space-admin-pubkey-hashes-ref.trig.
102
    public static final String GET_SPACE_ADMIN_PUBKEY_HASHES_REF = "RAO8KDdS4_Z0-R1qCSKqWcewg0WUSaiQDh_p1N1Bg-zic/get-space-admin-pubkey-hashes";
103
    public static final String GET_SPACE_ROLES = "RAKJFw-xIQ2r_aSKT4-6Pm3JkeqlWC_wmypfpA1JWPJl8/get-space-roles";
104
    // Ref-scoped roles (Stage 2): takes the ref's root nanopub (root_np), matches
105
    // RoleAssignments on npa:forSpaceRef, so multi-ref spaces don't merge role sets across
106
    // refs. Published independently. Source at docs/queries/get-space-roles-ref.trig.
107
    public static final String GET_SPACE_ROLES_REF = "RAqUWUfmEmzxpkeuXek7oEiVSnwjuzRfV8kRe7pQSpe4c/get-space-roles";
108
    public static final String GET_SPACE_MEMBERS = "RAo0c4UNoD-uTP3xATU_-TB6vO-nMO4Ya-mvdaGjX5qVE/get-space-members";
109
    // Ref-scoped members (Stage 2): takes the ref's root nanopub (root_np), resolves the
110
    // ref + its space IRI, and returns ALL non-admin RoleInstantiations naming that IRI
111
    // (raw npa:spacesGraph, matching the looser pre-migration semantic), each with a
112
    // ?validated flag = whether it is also in the trust-state-validated current-state graph
113
    // (i.e. the agent's key has a trust-approved AccountState from an accepted intro). Shows
114
    // every self-declared member while flagging the un-introduced ones, rather than hiding
115
    // them. Published independently. Source at docs/queries/get-space-members-ref.trig.
116
    public static final String GET_SPACE_MEMBERS_REF = "RAqp9TSM4oAwvJ0UQrvZ-qzEuS4R8zpsuD_lw1lyW5MOw/get-space-members";
117
    // Ref-scoped observers (Stage 2): takes the ref's root nanopub (root_np), lists observers
118
    // INCLUDING un-introduced self-declared ones (not in the validated state), each flagged
119
    // via a headerless ?unverified_noheader column (⚠️ when unvalidated). Drives the existing
120
    // Observers view's table (the view nanopub is left untouched). Published independently.
121
    // Source at docs/queries/list-space-observers-ref.trig.
122
    public static final String LIST_SPACE_OBSERVERS_REF = "RARc37t3fXMzrFP-PYsdmIqsDdloZaNklY4eYxpUKaLHI/list-space-observers";
123

124
    private static final Logger logger = LoggerFactory.getLogger(QueryApiAccess.class);
9✔
125

126
    private static ConcurrentMap<String, Pair<Long, String>> latestVersionMap = new ConcurrentHashMap<>();
15✔
127

128
    private static final String queryIriPattern = "^(.*[^A-Za-z0-9-_])(RA[A-Za-z0-9-_]{43})[/#]([^/#]+)$";
129

130
    /**
131
     * Forces the retrieval of an API response for a given query name and parameters.
132
     * Retries until a valid response is received.
133
     *
134
     * @param queryRef The query reference
135
     * @return The API response.
136
     */
137
    public static ApiResponse forcedGet(QueryRef queryRef) {
138
        long deadline = System.currentTimeMillis() + 30_000;
12✔
139
        long sleepMs = 1000;
6✔
140
        while (System.currentTimeMillis() < deadline) {
12!
141
            try {
142
                ApiResponse resp = QueryApiAccess.get(queryRef);
9✔
143
                if (resp != null) return resp;
12✔
144
            } catch (Exception ex) {
×
145
                logger.error("Error while forcing API get for query {}", queryRef, ex);
×
146
            }
3✔
147
            try {
148
                Thread.sleep(Math.min(sleepMs, Math.max(0, deadline - System.currentTimeMillis())));
24✔
149
                sleepMs = Math.min(sleepMs * 2, 16_000);
18✔
150
            } catch (InterruptedException ex) {
×
151
                Thread.currentThread().interrupt();
×
152
                break;
×
153
            }
3✔
154
        }
155
        throw new RuntimeException("Timed out forcing API get for query: " + queryRef);
×
156
    }
157

158
    /**
159
     * Retrieves an API response for a given query reference.
160
     *
161
     * @param queryRef The query reference
162
     * @return The API response.
163
     * @throws org.nanopub.extra.services.FailedApiCallException         If the API call fails.
164
     * @throws org.nanopub.extra.services.APINotReachableException       If the API is not reachable.
165
     * @throws org.nanopub.extra.services.NotEnoughAPIInstancesException If there are not enough API instances.
166
     */
167
    public static ApiResponse get(QueryRef queryRef) throws FailedApiCallException, APINotReachableException, NotEnoughAPIInstancesException {
168
        if (!queryRef.getQueryId().matches("^RA[A-Za-z0-9-_]{43}/.*$")) {
15!
169
            throw new IllegalArgumentException("QueryRef name must be full query ID: " + queryRef.getQueryId());
×
170
        }
171
        return QueryAccess.get(queryRef);
9✔
172
    }
173

174
    /**
175
     * Retrieves the latest version ID of a given nanopublication.
176
     *
177
     * @param nanopubId The ID of the nanopublication.
178
     * @return The latest version ID.
179
     */
180
    public static String getLatestVersionId(String nanopubId) {
181
        long currentTime = System.currentTimeMillis();
6✔
182
        if (!latestVersionMap.containsKey(nanopubId) || currentTime - latestVersionMap.get(nanopubId).getLeft() > 1000 * 60) {
12!
183
            // Re-fetch if existing value is older than 1 minute
184
            try {
185
                ApiResponse r = ApiCache.retrieveResponseSync(new QueryRef(GET_LATEST_VERSION_OF_NP, "np", nanopubId), false);
27✔
186
                if (r != null && r.getData().size() == 1) {
21!
187
                    String l = r.getData().get(0).get("latest");
24✔
188
                    latestVersionMap.put(nanopubId, Pair.of(currentTime, l));
24✔
189
                }
190
            } catch (Exception ex) {
×
191
                logger.error("Error while getting latest version of nanopub '{}'", nanopubId, ex);
×
192
            }
3✔
193
        }
194
        Pair<Long, String> cached = latestVersionMap.get(nanopubId);
15✔
195
        return cached != null ? cached.getRight() : nanopubId;
21!
196
    }
197

198
    /**
199
     * Extracts the query ID from a given query IRI.
200
     *
201
     * @param queryIri The query IRI.
202
     * @return The query ID, or null if the IRI is invalid.
203
     */
204
    public static String getQueryId(IRI queryIri) {
205
        if (queryIri == null) return null;
×
206
        if (!queryIri.stringValue().matches(queryIriPattern)) return null;
×
207
        return queryIri.stringValue().replaceFirst(queryIriPattern, "$2/$3");
×
208
    }
209

210
    /**
211
     * Extracts the query name from a given query IRI.
212
     *
213
     * @param queryIri The query IRI.
214
     * @return The query name, or null if the IRI is invalid.
215
     */
216
    public static String getQueryName(IRI queryIri) {
217
        if (queryIri == null) return null;
12✔
218
        if (!queryIri.stringValue().matches(queryIriPattern)) return null;
21✔
219
        return queryIri.stringValue().replaceFirst(queryIriPattern, "$3");
18✔
220
    }
221

222
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc