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

knowledgepixels / nanodash / 18775407157

24 Oct 2025 09:15AM UTC coverage: 13.662% (+0.8%) from 12.864%
18775407157

push

github

tkuhn
fix(MaintainedResource): Include in regular refreshing

476 of 4404 branches covered (10.81%)

Branch coverage included in aggregate %.

1252 of 8244 relevant lines covered (15.19%)

0.68 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
        System.err.println("REFRESH...");
×
27
        resourceList = new ArrayList<>();
×
28
        resourcesById = new HashMap<>();
×
29
        resourcesBySpace = new HashMap<>();
×
30
        for (ApiResponseEntry entry : resp.getData()) {
×
31
            Space space = Space.get(entry.get("space"));
×
32
            if (space == null) continue;
×
33
            MaintainedResource resource = new MaintainedResource(entry, space);
×
34
            if (resourcesById.containsKey(resource.getId())) continue;
×
35
            resourceList.add(resource);
×
36
            resourcesById.put(resource.getId(), resource);
×
37
            resourcesBySpace.computeIfAbsent(space, k -> new ArrayList<>()).add(resource);
×
38
        }
×
39
        loaded = true;
×
40
    }
×
41

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

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

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

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

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

85
    public static void refresh() {
86
        ensureLoaded();
×
87
        for (MaintainedResource resource : resourceList) {
×
88
            resource.dataNeedsUpdate = true;
×
89
        }
×
90
    }
×
91

92
    private String id, label, nanopubId;
93
    private Space space;
94
    private Nanopub nanopub;
95
    private ResourceData data = new ResourceData();
×
96

97
    private boolean dataInitialized = false;
×
98
    private boolean dataNeedsUpdate = true;
×
99

100
    private static class ResourceData implements Serializable {
×
101
        List<ResourceView> views = new ArrayList<>();
×
102
    }
103

104
    private MaintainedResource(ApiResponseEntry resp, Space space) {
×
105
        this.space = space;
×
106
        this.id = resp.get("resource");
×
107
        this.label = resp.get("label");
×
108
        this.nanopubId = resp.get("np");
×
109
        this.nanopub = Utils.getAsNanopub(nanopubId);
×
110
    }
×
111

112
    public Space getSpace() {
113
        return space;
×
114
    }
115

116
    public String getId() {
117
        return id;
×
118
    }
119

120
    public String getNanopubId() {
121
        return nanopubId;
×
122
    }
123

124
    public Nanopub getNanopub() {
125
        return nanopub;
×
126
    }
127

128
    public String getLabel() {
129
        return label;
×
130
    }
131

132
    public boolean isDataInitialized() {
133
        triggerDataUpdate();
×
134
        return dataInitialized;
×
135
    }
136

137
    public List<ResourceView> getViews() {
138
        triggerDataUpdate();
×
139
        return data.views;
×
140
    }
141

142
    private synchronized void triggerDataUpdate() {
143
        if (dataNeedsUpdate) {
×
144
            new Thread(() -> {
×
145
                try {
146
                    ResourceData newData = new ResourceData();
×
147

148
                    for (ApiResponseEntry r : QueryApiAccess.get(new QueryRef("get-view-displays", "resource", id)).getData()) {
×
149
                        if (!space.isAdminPubkey(r.get("pubkey"))) continue;
×
150
                        ResourceView view = ResourceView.get(r.get("view"));
×
151
                        if (view == null) continue;
×
152
                        newData.views.add(view);
×
153
                    }
×
154
                    data = newData;
×
155
                    dataInitialized = true;
×
156
                } catch (Exception ex) {
×
157
                    logger.error("Error while trying to update space data: {}", ex);
×
158
                    dataNeedsUpdate = true;
×
159
                }
×
160
            }).start();
×
161
            dataNeedsUpdate = false;
×
162
        }
163
    }
×
164

165
    @Override
166
    public String toString() {
167
        return id;
×
168
    }
169

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