Commit b087edc2 by Torkel Ödegaard Committed by GitHub

Typescript: null check fixes, and news panel fix (#21595)

parent e7621906
...@@ -9,12 +9,20 @@ export async function loadRSSFeed(url: string): Promise<RssFeed> { ...@@ -9,12 +9,20 @@ export async function loadRSSFeed(url: string): Promise<RssFeed> {
items: [], items: [],
}; };
const getProperty = (node: Element, property: string) => {
const propNode = node.querySelector(property);
if (propNode) {
return propNode.textContent ?? '';
}
return '';
};
doc.querySelectorAll('item').forEach(node => { doc.querySelectorAll('item').forEach(node => {
const item: RssItem = { const item: RssItem = {
title: node.querySelector('title').textContent, title: getProperty(node, 'title'),
link: node.querySelector('link').textContent, link: getProperty(node, 'link'),
content: node.querySelector('description').textContent, content: getProperty(node, 'description'),
pubDate: node.querySelector('pubDate').textContent, pubDate: getProperty(node, 'pubDate'),
}; };
feed.items.push(item); feed.items.push(item);
......
...@@ -15,13 +15,10 @@ export function feedToDataFrame(feed: RssFeed): DataFrame { ...@@ -15,13 +15,10 @@ export function feedToDataFrame(feed: RssFeed): DataFrame {
title.buffer.push(item.title); title.buffer.push(item.title);
link.buffer.push(item.link); link.buffer.push(item.link);
let body = item.content.replace(/<\/?[^>]+(>|$)/g, ''); if (item.content) {
const body = item.content.replace(/<\/?[^>]+(>|$)/g, '');
if (body && body.length > 300) { content.buffer.push(body);
body = body.substr(0, 300);
} }
content.buffer.push(body);
} catch (err) { } catch (err) {
console.warn('Error reading news item:', err, item); console.warn('Error reading news item:', err, item);
} }
......
...@@ -73,7 +73,7 @@ describe('SingleStatCtrl', () => { ...@@ -73,7 +73,7 @@ describe('SingleStatCtrl', () => {
}); });
it('should set formatted falue', () => { it('should set formatted falue', () => {
expect(ctx.data.display.text).toBe('15'); expect(ctx.data.display!.text).toBe('15');
}); });
}); });
...@@ -96,7 +96,7 @@ describe('SingleStatCtrl', () => { ...@@ -96,7 +96,7 @@ describe('SingleStatCtrl', () => {
}); });
it('should set formatted value', () => { it('should set formatted value', () => {
expect(ctx.data.display.text).toBe('test.cpu1'); expect(ctx.data.display!.text).toBe('test.cpu1');
}); });
}); });
...@@ -121,7 +121,7 @@ describe('SingleStatCtrl', () => { ...@@ -121,7 +121,7 @@ describe('SingleStatCtrl', () => {
}); });
it('should set formatted value', () => { it('should set formatted value', () => {
expect(dateTime(ctx.data.display.text).valueOf()).toBe(1505634997000); expect(dateTime(ctx.data.display!.text).valueOf()).toBe(1505634997000);
}); });
}); });
...@@ -142,7 +142,7 @@ describe('SingleStatCtrl', () => { ...@@ -142,7 +142,7 @@ describe('SingleStatCtrl', () => {
}); });
it('should set value', () => { it('should set value', () => {
expect(ctx.data.display.text).toBe('1970-01-01 00:00:05'); expect(ctx.data.display!.text).toBe('1970-01-01 00:00:05');
}); });
}); });
...@@ -167,7 +167,7 @@ describe('SingleStatCtrl', () => { ...@@ -167,7 +167,7 @@ describe('SingleStatCtrl', () => {
}); });
it('should set formatted value', () => { it('should set formatted value', () => {
expect(ctx.data.display.text).toBe(dateTime(1505634997920).format('MM/DD/YYYY h:mm:ss a')); expect(ctx.data.display!.text).toBe(dateTime(1505634997920).format('MM/DD/YYYY h:mm:ss a'));
}); });
}); });
...@@ -188,7 +188,7 @@ describe('SingleStatCtrl', () => { ...@@ -188,7 +188,7 @@ describe('SingleStatCtrl', () => {
}); });
it('should set formatted value', () => { it('should set formatted value', () => {
expect(ctx.data.display.text).toBe('01/01/1970 12:00:05 am'); expect(ctx.data.display!.text).toBe('01/01/1970 12:00:05 am');
}); });
}); });
...@@ -212,7 +212,7 @@ describe('SingleStatCtrl', () => { ...@@ -212,7 +212,7 @@ describe('SingleStatCtrl', () => {
}); });
it('should set formatted value', () => { it('should set formatted value', () => {
expect(ctx.data.display.text).toBe('2 days ago'); expect(ctx.data.display!.text).toBe('2 days ago');
}); });
}); });
...@@ -232,7 +232,7 @@ describe('SingleStatCtrl', () => { ...@@ -232,7 +232,7 @@ describe('SingleStatCtrl', () => {
}); });
it('should set formatted value', () => { it('should set formatted value', () => {
expect(ctx.data.display.text).toBe('2 days ago'); expect(ctx.data.display!.text).toBe('2 days ago');
}); });
}); });
...@@ -258,7 +258,7 @@ describe('SingleStatCtrl', () => { ...@@ -258,7 +258,7 @@ describe('SingleStatCtrl', () => {
}); });
it('should set formatted value', () => { it('should set formatted value', () => {
expect(ctx.data.display.text).toBe('100'); expect(ctx.data.display!.text).toBe('100');
}); });
} }
); );
...@@ -274,7 +274,7 @@ describe('SingleStatCtrl', () => { ...@@ -274,7 +274,7 @@ describe('SingleStatCtrl', () => {
}); });
it('Should replace value with text', () => { it('Should replace value with text', () => {
expect(ctx.data.display.text).toBe('OK'); expect(ctx.data.display!.text).toBe('OK');
}); });
}); });
...@@ -289,7 +289,7 @@ describe('SingleStatCtrl', () => { ...@@ -289,7 +289,7 @@ describe('SingleStatCtrl', () => {
}); });
it('Should replace value with text', () => { it('Should replace value with text', () => {
expect(ctx.data.display.text).toBe('XYZ'); expect(ctx.data.display!.text).toBe('XYZ');
}); });
}); });
...@@ -304,7 +304,7 @@ describe('SingleStatCtrl', () => { ...@@ -304,7 +304,7 @@ describe('SingleStatCtrl', () => {
}); });
it('Should replace value with text OK', () => { it('Should replace value with text OK', () => {
expect(ctx.data.display.text).toBe('OK'); expect(ctx.data.display!.text).toBe('OK');
}); });
}); });
...@@ -319,7 +319,7 @@ describe('SingleStatCtrl', () => { ...@@ -319,7 +319,7 @@ describe('SingleStatCtrl', () => {
}); });
it('Should replace value with text NOT OK', () => { it('Should replace value with text NOT OK', () => {
expect(ctx.data.display.text).toBe('NOT OK'); expect(ctx.data.display!.text).toBe('NOT OK');
}); });
}); });
...@@ -347,7 +347,7 @@ describe('SingleStatCtrl', () => { ...@@ -347,7 +347,7 @@ describe('SingleStatCtrl', () => {
}); });
it('should set formatted value', () => { it('should set formatted value', () => {
expect(ctx.data.display.text).toBe('15'); expect(ctx.data.display!.text).toBe('15');
}); });
}); });
...@@ -377,7 +377,7 @@ describe('SingleStatCtrl', () => { ...@@ -377,7 +377,7 @@ describe('SingleStatCtrl', () => {
}); });
it('should set formatted falue', () => { it('should set formatted falue', () => {
expect(ctx.data.display.text).toBe('100'); expect(ctx.data.display!.text).toBe('100');
}); });
} }
); );
...@@ -396,7 +396,7 @@ describe('SingleStatCtrl', () => { ...@@ -396,7 +396,7 @@ describe('SingleStatCtrl', () => {
}); });
it('Should replace value with text', () => { it('Should replace value with text', () => {
expect(ctx.data.display.text).toBe('OK'); expect(ctx.data.display!.text).toBe('OK');
}); });
}); });
...@@ -413,7 +413,7 @@ describe('SingleStatCtrl', () => { ...@@ -413,7 +413,7 @@ describe('SingleStatCtrl', () => {
}); });
it('Should replace value with text OK', () => { it('Should replace value with text OK', () => {
expect(ctx.data.display.text).toBe('OK'); expect(ctx.data.display!.text).toBe('OK');
}); });
}); });
...@@ -430,7 +430,7 @@ describe('SingleStatCtrl', () => { ...@@ -430,7 +430,7 @@ describe('SingleStatCtrl', () => {
}); });
it('Should replace value with text NOT OK', () => { it('Should replace value with text NOT OK', () => {
expect(ctx.data.display.text).toBe('NOT OK'); expect(ctx.data.display!.text).toBe('NOT OK');
}); });
}); });
...@@ -443,7 +443,7 @@ describe('SingleStatCtrl', () => { ...@@ -443,7 +443,7 @@ describe('SingleStatCtrl', () => {
}); });
it('Should replace value with text NOT OK', () => { it('Should replace value with text NOT OK', () => {
expect(ctx.data.display.text).toBe('ignore1'); expect(ctx.data.display!.text).toBe('ignore1');
}); });
}); });
......
...@@ -53,7 +53,7 @@ ...@@ -53,7 +53,7 @@
"id": 4, "id": 4,
"links": [], "links": [],
"options": { "options": {
"feedUrl": "https://grafana.com/blog/index.xml" "feedUrl": "https://grafana.com/blog/news.xml"
}, },
"title": "Latest from the blog", "title": "Latest from the blog",
"type": "news" "type": "news"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment