utils.ts 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import { ArrayVector, FieldType, DataFrame, dateTime } from '@grafana/data';
  2. import { Feed } from './types';
  3. export function feedToDataFrame(feed: Feed): DataFrame {
  4. const date = new ArrayVector<number>([]);
  5. const title = new ArrayVector<string>([]);
  6. const link = new ArrayVector<string>([]);
  7. const content = new ArrayVector<string>([]);
  8. const ogImage = new ArrayVector<string | undefined | null>([]);
  9. for (const item of feed.items) {
  10. const val = dateTime(item.pubDate);
  11. try {
  12. date.buffer.push(val.valueOf());
  13. title.buffer.push(item.title);
  14. link.buffer.push(item.link);
  15. ogImage.buffer.push(item.ogImage);
  16. if (item.content) {
  17. const body = item.content.replace(/<\/?[^>]+(>|$)/g, '');
  18. content.buffer.push(body);
  19. }
  20. } catch (err) {
  21. console.warn('Error reading news item:', err, item);
  22. }
  23. }
  24. return {
  25. fields: [
  26. { name: 'date', type: FieldType.time, config: { displayName: 'Date' }, values: date },
  27. { name: 'title', type: FieldType.string, config: {}, values: title },
  28. { name: 'link', type: FieldType.string, config: {}, values: link },
  29. { name: 'content', type: FieldType.string, config: {}, values: content },
  30. { name: 'ogImage', type: FieldType.string, config: {}, values: ogImage },
  31. ],
  32. length: date.length,
  33. };
  34. }