<commit_before>const azure = require('azure-storage');
var table = require('../shared/table.js');
var clientInfo = require('../shared/clientInfo.js');

const entGen = azure.TableUtilities.entityGenerator;
const baseTableName = 'diagrams'
const partitionKeyName = 'dep'


// if (clientInfo.token === '<PASSWORD>') {
//     await table.createTableIfNotExists(tableName)
// }

exports.newDiagram = async (context, diagram) => {
    const tableName = getTableName(context)
    const { diagramId, name } = diagram.diagramData
    const canvasData = diagram?.canvases[0]
    if (!diagramId || !name || !canvasData) {
        throw new Error('missing parameters: ');
    }

    const diagramData = { diagramId: diagramId, name: name, accessed: Date.now() }

    const batch = new azure.TableBatch()
    batch.insertEntity(toDiagramDataItem(diagramData))
    batch.insertEntity(toCanvasDataItem(canvasData))

    await table.executeBatch(tableName, batch)

    const entity = await table.retrieveEntity(tableName, partitionKeyName, diagramKey(diagramId))
    return toDiagramInfo(entity)
}

exports.setCanvas = async (context, canvasData) => {
    const tableName = getTableName(context)
    if (!canvasData) {
        throw new Error('missing parameters');
    }

    const { diagramId } = canvasData
    const diagramData = { diagramId: diagramId, accessed: Date.now() }

    const batch = new azure.TableBatch()
    batch.mergeEntity(toDiagramDataItem(diagramData))
    batch.insertOrReplaceEntity(toCanvasDataItem(canvasData))

    await table.executeBatch(tableName, batch)

    const entity = await table.retrieveEntity(tableName, partitionKeyName, diagramKey(diagramId))
    return toDiagramInfo(entity)
}

exports.getAllDiagramsData = async (context) => {
    const tableName = getTableName(context)

    var tableQuery = new azure.TableQuery()
        .where('type == ?string?', 'diagram');

    const items = await table.queryEntities(tableName, tableQuery, null)
    context.log(`queried: ${items.length}`)

    return items.map(i => toDiagramInfo(i))
}

exports.getDiagram = async (context, diagramId) => {
    const tableName = getTableName(context)

    let tableQuery = new azure.TableQuery()
        .where('diagramId == ?string?', diagramId);

    const items = await table.queryEntities(tableName, tableQuery, null)


    const diagram = { diagramData: { diagramId: diagramId }, canvases: [] }

    items.forEach(i => {
        if (i.type === 'diagram') {
            diagram.diagramData = toDiagramInfo(i)
        } else if (i.type === 'canvas') {
            diagram.canvases.push(toCanvasData(i))
        }
    })

    return diagram
}

exports.deleteDiagram = async (context, parameters) => {
    const tableName = getTableName(context)
    const { diagramId } = parameters
    if (!diagramId) {
        throw new Error('Missing parameter')
    }
<commit_msg>Add create table<commit_after>const azure = require('azure-storage');
var table = require('../shared/table.js');
var clientInfo = require('../shared/clientInfo.js');

const entGen = azure.TableUtilities.entityGenerator;
const baseTableName = 'diagrams'
const partitionKeyName = 'dep'


// if (clientInfo.token === '<PASSWORD>') {
//     await table.createTableIfNotExists(tableName)
// }

exports.newDiagram = async (context, diagram) => {
    const tableName = getTableName(context)
    const { diagramId, name } = diagram.diagramData
    const canvasData = diagram?.canvases[0]
    if (!diagramId || !name || !canvasData) {
        throw new Error('missing parameters: ');
    }

    if (clientInfo.token === '<PASSWORD>') {
        await table.createTableIfNotExists(tableName)
    }

    const diagramData = { diagramId: diagramId, name: name, accessed: Date.now() }

    const batch = new azure.TableBatch()
    batch.insertEntity(toDiagramDataItem(diagramData))
    batch.insertEntity(toCanvasDataItem(canvasData))

    await table.executeBatch(tableName, batch)

    const entity = await table.retrieveEntity(tableName, partitionKeyName, diagramKey(diagramId))
    return toDiagramInfo(entity)
}

exports.setCanvas = async (context, canvasData) => {
    const tableName = getTableName(context)
    if (!canvasData) {
        throw new Error('missing parameters');
    }

    const { diagramId } = canvasData
    const diagramData = { diagramId: diagramId, accessed: Date.now() }

    const batch = new azure.TableBatch()
    batch.mergeEntity(toDiagramDataItem(diagramData))
    batch.insertOrReplaceEntity(toCanvasDataItem(canvasData))

    await table.executeBatch(tableName, batch)

    const entity = await table.retrieveEntity(tableName, partitionKeyName, diagramKey(diagramId))
    return toDiagramInfo(entity)
}

exports.getAllDiagramsData = async (context) => {
    const tableName = getTableName(context)

    var tableQuery = new azure.TableQuery()
        .where('type == ?string?', 'diagram');

    const items = await table.queryEntities(tableName, tableQuery, null)
    context.log(`queried: ${items.length}`)

    return items.map(i => toDiagramInfo(i))
}

exports.getDiagram = async (context, diagramId) => {
    const tableName = getTableName(context)
    if (clientInfo.token === '<PASSWORD>') {
        await table.createTableIfNotExists(tableName)
    }


    let tableQuery = new azure.TableQuery()
        .where('diagramId == ?string?', diagramId);

    const items = await table.queryEntities(tableName, tableQuery, null)


    const diagram = { diagramData: { diagramId: diagramId }, canvases: [] }

    items.forEach(i => {
        if (i.type === 'diagram') {
            diagram.diagramData = toDiagramInfo(i)
        } else if (i.type === 'canvas') {
            diagram.canvases.push(toCanvasData(i))
        }
    })

    return diagram
}

exports.deleteDiagram = async (context, parameters) => {
    const tableName = getTableName(context)
    const { diagramId } = parameters
    if (!diagramId) {
        throw new Error('Missing parameter')
    }

{'fraction_non_alphanumeric': 0.12514678745176983, 'fraction_numerical': 0.00033551417547391375, 'mean_word_length': 3.7130434782608694, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '775820', 'n_tokens_mistral': 1789, 'n_tokens_neox': 1766, 'n_words': 388}