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

knowledgepixels / nanodash / 18774433971

24 Oct 2025 08:38AM UTC coverage: 12.864% (-0.8%) from 13.626%
18774433971

push

github

tkuhn
fix(SpacePage): Avoid force-refresh of maintained resources

452 of 4402 branches covered (10.27%)

Branch coverage included in aggregate %.

1174 of 8238 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
        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
    private String id, label, nanopubId;
86
    private Space space;
87
    private Nanopub nanopub;
88
    private ResourceData data = new ResourceData();
×
89

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

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

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

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

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

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

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

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

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

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

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

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

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

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