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

knowledgepixels / nanodash / 18752810964

23 Oct 2025 03:03PM UTC coverage: 12.866% (+0.03%) from 12.836%
18752810964

push

github

tkuhn
fix: Refresh maintained resources when creating list

452 of 4402 branches covered (10.27%)

Branch coverage included in aggregate %.

1174 of 8236 relevant lines covered (14.25%)

0.64 hits per line

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

0.0
src/main/java/com/knowledgepixels/nanodash/MaintainedResource.java
1
package com.knowledgepixels.nanodash;
2

3
import java.io.Serializable;
4
import java.util.ArrayList;
5
import java.util.HashMap;
6
import java.util.List;
7
import java.util.Map;
8

9
import org.nanopub.Nanopub;
10
import org.nanopub.extra.services.ApiResponse;
11
import org.nanopub.extra.services.ApiResponseEntry;
12
import org.nanopub.extra.services.QueryRef;
13
import org.slf4j.Logger;
14
import org.slf4j.LoggerFactory;
15

16
public class MaintainedResource implements Serializable {
17

18
    private static final Logger logger = LoggerFactory.getLogger(MaintainedResource.class);
×
19

20
    private static List<MaintainedResource> resourceList;
21
    private static Map<String, MaintainedResource> resourcesById;
22
    private static Map<Space, List<MaintainedResource>> resourcesBySpace;
23
    private static boolean loaded = false;
×
24

25
    public static synchronized void refresh(ApiResponse resp) {
26
        resourceList = new ArrayList<>();
×
27
        resourcesById = new HashMap<>();
×
28
        resourcesBySpace = new HashMap<>();
×
29
        for (ApiResponseEntry entry : resp.getData()) {
×
30
            Space space = Space.get(entry.get("space"));
×
31
            if (space == null) continue;
×
32
            MaintainedResource resource = new MaintainedResource(entry, space);
×
33
            if (resourcesById.containsKey(resource.getId())) continue;
×
34
            resourceList.add(resource);
×
35
            resourcesById.put(resource.getId(), resource);
×
36
            resourcesBySpace.computeIfAbsent(space, k -> new ArrayList<>()).add(resource);
×
37
        }
×
38
        loaded = true;
×
39
    }
×
40

41
    /**
42
     * Check if the resources have been loaded.
43
     *
44
     * @return true if loaded, false otherwise.
45
     */
46
    public static boolean isLoaded() {
47
        return loaded;
×
48
    }
49

50
    /**
51
     * Ensure that the resources are loaded, fetching them from the API if necessary.
52
     */
53
    public static void ensureLoaded() {
54
        if (resourceList == null) {
×
55
            refresh(QueryApiAccess.forcedGet(new QueryRef("get-maintained-resources")));
×
56
        }
57
    }
×
58

59
    /**
60
     * Get the list of all maintained resources.
61
     *
62
     * @return List of resources.
63
     */
64
    public static List<MaintainedResource> getResourceList() {
65
        ensureLoaded();
×
66
        return resourceList;
×
67
    }
68

69
    public static List<MaintainedResource> getResourcesBySpace(Space space) {
70
        return resourcesBySpace.computeIfAbsent(space, k -> new ArrayList<>());
×
71
    }
72

73
    /**
74
     * Get a maintained resource by its id.
75
     *
76
     * @param id The id of the resource.
77
     * @return The corresponding MaintainedResource object, or null if not found.
78
     */
79
    public static MaintainedResource get(String id) {
80
        ensureLoaded();
×
81
        return resourcesById.get(id);
×
82
    }
83

84
    private String id, label, nanopubId;
85
    private Space space;
86
    private Nanopub nanopub;
87
    private ResourceData data = new ResourceData();
×
88

89
    private boolean dataInitialized = false;
×
90
    private boolean dataNeedsUpdate = true;
×
91

92
    private static class ResourceData implements Serializable {
×
93
        List<ResourceView> views = new ArrayList<>();
×
94
    }
95

96
    private MaintainedResource(ApiResponseEntry resp, Space space) {
×
97
        this.space = space;
×
98
        this.id = resp.get("resource");
×
99
        this.label = resp.get("label");
×
100
        this.nanopubId = resp.get("np");
×
101
        this.nanopub = Utils.getAsNanopub(nanopubId);
×
102
    }
×
103

104
    public Space getSpace() {
105
        return space;
×
106
    }
107

108
    public String getId() {
109
        return id;
×
110
    }
111

112
    public String getNanopubId() {
113
        return nanopubId;
×
114
    }
115

116
    public Nanopub getNanopub() {
117
        return nanopub;
×
118
    }
119

120
    public String getLabel() {
121
        return label;
×
122
    }
123

124
    public boolean isDataInitialized() {
125
        triggerDataUpdate();
×
126
        return dataInitialized;
×
127
    }
128

129
    public List<ResourceView> getViews() {
130
        triggerDataUpdate();
×
131
        return data.views;
×
132
    }
133

134
    private synchronized void triggerDataUpdate() {
135
        if (dataNeedsUpdate) {
×
136
            new Thread(() -> {
×
137
                try {
138
                    ResourceData newData = new ResourceData();
×
139

140
                    for (ApiResponseEntry r : QueryApiAccess.get(new QueryRef("get-view-displays", "resource", id)).getData()) {
×
141
                        if (!space.isAdminPubkey(r.get("pubkey"))) continue;
×
142
                        ResourceView view = ResourceView.get(r.get("view"));
×
143
                        if (view == null) continue;
×
144
                        newData.views.add(view);
×
145
                    }
×
146
                    data = newData;
×
147
                    dataInitialized = true;
×
148
                } catch (Exception ex) {
×
149
                    logger.error("Error while trying to update space data: {}", ex);
×
150
                }
×
151
            }).start();
×
152
            dataNeedsUpdate = false;
×
153
        }
154
    }
×
155

156
    @Override
157
    public String toString() {
158
        return id;
×
159
    }
160

161
}
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