File size: 2,428 Bytes
0e7a78d
38b46ea
0e7a78d
4635160
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0e7a78d
 
 
4635160
 
3f568cf
 
 
 
 
4635160
 
38b46ea
3f568cf
38b46ea
4635160
3f568cf
38b46ea
 
 
 
4635160
 
38b46ea
 
 
4635160
 
3f568cf
4635160
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import { isEmpty } from 'lodash';
import { v4 as uuid } from 'uuid';

class KeyGenerator {
  idx = 0;
  chars: string[] = [];
  constructor() {
    const chars = Array(26)
      .fill(1)
      .map((x, idx) => String.fromCharCode(97 + idx)); // 26 char
    this.chars = chars;
  }
  generateKey() {
    const key = this.chars[this.idx];
    this.idx++;
    return key;
  }
}

// Classify nodes based on edge relationships
export class Converter {
  keyGenerator;
  dict: Record<string, string> = {}; // key is node id, value is combo
  constructor() {
    this.keyGenerator = new KeyGenerator();
  }
  buildDict(edges: { source: string; target: string }[]) {
    edges.forEach((x) => {
      if (this.dict[x.source] && !this.dict[x.target]) {
        this.dict[x.target] = this.dict[x.source];
      } else if (!this.dict[x.source] && this.dict[x.target]) {
        this.dict[x.source] = this.dict[x.target];
      } else if (!this.dict[x.source] && !this.dict[x.target]) {
        this.dict[x.source] = this.dict[x.target] =
          this.keyGenerator.generateKey();
      }
    });
    return this.dict;
  }
  buildNodesAndCombos(nodes: any[], edges: any[]) {
    this.buildDict(edges);
    const nextNodes = nodes.map((x) => ({ ...x, combo: this.dict[x.id] }));

    const combos = Object.values(this.dict).reduce<any[]>((pre, cur) => {
      if (pre.every((x) => x.id !== cur)) {
        pre.push({
          id: cur,
          data: {
            label: `Combo ${cur}`,
          },
        });
      }
      return pre;
    }, []);

    return { nodes: nextNodes, combos };
  }
}

export const isDataExist = (data: any) => {
  return (
    data?.data && typeof data?.data !== 'boolean' && !isEmpty(data?.data?.graph)
  );
};

const findCombo = (communities: string[]) => {
  const combo = Array.isArray(communities) ? communities[0] : undefined;
  return combo;
};

export const buildNodesAndCombos = (nodes: any[]) => {
  const combos: any[] = [];
  nodes.forEach((x) => {
    const combo = findCombo(x?.communities);
    if (combo && combos.every((y) => y.data.label !== combo)) {
      combos.push({
        isCombo: true,
        id: uuid(),
        data: {
          label: combo,
        },
      });
    }
  });

  const nextNodes = nodes.map((x) => {
    return {
      ...x,
      combo: combos.find((y) => y.data.label === findCombo(x?.communities))?.id,
    };
  });

  return { nodes: nextNodes, combos };
};