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

knowledgepixels / nanodash / 19924761602

04 Dec 2025 09:52AM UTC coverage: 15.239% (-0.02%) from 15.258%
19924761602

push

github

tkuhn
fix(ProfiledResource): Working regular refreshing

583 of 4982 branches covered (11.7%)

Branch coverage included in aggregate %.

1561 of 9087 relevant lines covered (17.18%)

0.76 hits per line

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

16.43
src/main/java/com/knowledgepixels/nanodash/ProfiledResource.java
1
package com.knowledgepixels.nanodash;
2

3
import com.knowledgepixels.nanodash.vocabulary.KPXL_TERMS;
4
import org.eclipse.rdf4j.model.IRI;
5
import org.nanopub.Nanopub;
6
import org.nanopub.extra.services.ApiResponseEntry;
7
import org.nanopub.extra.services.QueryRef;
8
import org.slf4j.Logger;
9
import org.slf4j.LoggerFactory;
10

11
import java.io.Serializable;
12
import java.util.*;
13

14
public class ProfiledResource implements Serializable {
15

16
    private static final Logger logger = LoggerFactory.getLogger(ProfiledResource.class);
3✔
17

18
    protected static class ResourceData implements Serializable {
2✔
19
        List<ViewDisplay> viewDisplays = new ArrayList<>();
6✔
20
    }
21

22
    private static List<ProfiledResource> instances = new ArrayList<>();
5✔
23

24
    public static void refresh() {
25
        for (ProfiledResource r : instances) {
10✔
26
            r.setDataNeedsUpdate();
2✔
27
        }
1✔
28
    }
1✔
29

30
    private String id;
31
    private Space space;
32
    private ResourceData data = new ResourceData();
5✔
33
    private boolean dataInitialized = false;
3✔
34
    private boolean dataNeedsUpdate = true;
3✔
35
    private Long runUpdateAfter = null;
3✔
36

37
    protected ProfiledResource(String id) {
2✔
38
        this.id = id;
3✔
39
        instances.add(this);
4✔
40
    }
1✔
41

42
    protected void initSpace(Space space) {
43
        this.space = space;
3✔
44
    }
1✔
45

46
    public String getId() {
47
        return id;
3✔
48
    }
49

50
    protected synchronized Thread triggerDataUpdate() {
51
        if (dataNeedsUpdate) {
×
52
            Thread thread = new Thread(() -> {
×
53
                try {
54
                    if (runUpdateAfter != null) {
×
55
                        while (System.currentTimeMillis() < runUpdateAfter) {
×
56
                            Thread.sleep(100);
×
57
                        }
58
                        runUpdateAfter = null;
×
59
                    }
60

61
                    ResourceData newData = new ResourceData();
×
62

63
                    for (ApiResponseEntry r : QueryApiAccess.get(new QueryRef("get-view-displays", "resource", id)).getData()) {
×
64
                        if (space != null && !space.isAdminPubkey(r.get("pubkey"))) continue;
×
65
                        try {
66
                            newData.viewDisplays.add(ViewDisplay.get(r.get("display")));
×
67
                        } catch (IllegalArgumentException ex) {
×
68
                            logger.error("Couldn't generate view display object", ex);
×
69
                        }
×
70
                    }
×
71
                    data = newData;
×
72
                    dataInitialized = true;
×
73
                } catch (Exception ex) {
×
74
                    logger.error("Error while trying to update space data: {}", ex);
×
75
                    dataNeedsUpdate = true;
×
76
                }
×
77
            });
×
78
            thread.start();
×
79
            dataNeedsUpdate = false;
×
80
            return thread;
×
81
        }
82
        return null;
×
83
    }
84

85
    public void forceRefresh(long waitMillis) {
86
        dataNeedsUpdate = true;
×
87
        dataInitialized = false;
×
88
        runUpdateAfter = System.currentTimeMillis() + waitMillis;
×
89
    }
×
90

91
    public Long getRunUpdateAfter() {
92
        return runUpdateAfter;
×
93
    }
94

95
    public Space getSpace() {
96
        return space;
×
97
    }
98

99
    public String getNanopubId() {
100
        return null;
×
101
    }
102

103
    public Nanopub getNanopub() {
104
        return null;
×
105
    }
106

107
    public String getNamespace() {
108
        return null;
×
109
    }
110

111
    public void setDataNeedsUpdate() {
112
        dataNeedsUpdate = true;
3✔
113
    }
1✔
114

115
    public boolean isDataInitialized() {
116
        triggerDataUpdate();
×
117
        return dataInitialized;
×
118
    }
119

120
    public List<ViewDisplay> getViewDisplays() {
121
        return data.viewDisplays;
×
122
    }
123

124
    public List<ViewDisplay> getViewDisplays(boolean toplevel, Set<IRI> classes) {
125
        triggerDataUpdate();
×
126
        List<ViewDisplay> viewDisplays = new ArrayList<>();
×
127
        Set<IRI> viewKinds = new HashSet<>();
×
128

129
        for (ViewDisplay vd : getViewDisplays()) {
×
130
            IRI kind = vd.getViewKindIri();
×
131
            if (kind != null) {
×
132
                if (viewKinds.contains(kind)) continue;
×
133
                viewKinds.add(vd.getViewKindIri());
×
134
            }
135
            if (vd.hasType(KPXL_TERMS.DEACTIVATED_VIEW_DISPLAY)) continue;
×
136

137
            if (!toplevel && vd.hasType(KPXL_TERMS.TOP_LEVEL_VIEW_DISPLAY)) {
×
138
                // Deprecated
139
                // do nothing
140
            } else if (vd.appliesTo(getId(), classes)) {
×
141
                viewDisplays.add(vd);
×
142
            } else if (toplevel && vd.hasType(KPXL_TERMS.TOP_LEVEL_VIEW_DISPLAY)) {
×
143
                // Deprecated
144
                viewDisplays.add(vd);
×
145
            }
146
        }
×
147

148
        Collections.sort(viewDisplays);
×
149
        return viewDisplays;
×
150
    }
151

152
    public String getLabel() {
153
        return space.getLabel();
×
154
    }
155

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

161
    /**
162
     * Gets the chain of superspaces from the current space up to the root space.
163
     *
164
     * @return the list of superspaces from the given space to the root space
165
     */
166
    public List<ProfiledResource> getAllSuperSpacesUntilRoot() {
167
        List<ProfiledResource> chain = new ArrayList<>();
×
168
        Set<String> visited = new HashSet<>();
×
169
        collectAncestors(space, chain, visited);
×
170
        Collections.reverse(chain);
×
171
        return chain;
×
172
    }
173

174
    private void collectAncestors(Space current, List<ProfiledResource> chain, Set<String> visited) {
175
        if (current == null) {
×
176
            return;
×
177
        }
178
        List<Space> parents = current.getSuperspaces();
×
179
        if (parents == null || parents.isEmpty()) {
×
180
            return;
×
181
        }
182
        Space parent = parents.getFirst();
×
183
        if (parent == null) {
×
184
            return;
×
185
        }
186
        String pid = parent.getId();
×
187
        if (pid == null || !visited.add(pid)) {
×
188
            return;
×
189
        }
190
        chain.add(parent);
×
191
        collectAncestors(parent, chain, visited);
×
192
    }
×
193

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