Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: review telemetry #675

Merged
merged 5 commits into from
Jun 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion fyo/telemetry/telemetry.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Fyo } from 'fyo';
import { Noun, Telemetry, Verb } from './types';
import { ModelNameEnum } from 'models/types';

/**
* # Telemetry
Expand All @@ -26,6 +27,11 @@ import { Noun, Telemetry, Verb } from './types';
* the app is hidden.
*/

const ignoreList: string[] = [
ModelNameEnum.AccountingLedgerEntry,
ModelNameEnum.StockLedgerEntry,
];

export class TelemetryManager {
#url = '';
#token = '';
Expand Down Expand Up @@ -80,7 +86,11 @@ export class TelemetryManager {
}

#sendBeacon(verb: Verb, noun: Noun, more?: Record<string, unknown>) {
if (!this.hasCreds || this.fyo.store.skipTelemetryLogging) {
if (
!this.hasCreds ||
this.fyo.store.skipTelemetryLogging ||
ignoreList.includes(noun)
) {
return;
}

Expand Down
3 changes: 3 additions & 0 deletions fyo/telemetry/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@ export type UniqueId = string;
export type Timestamp = string;

export enum Verb {
Started = 'started',
Completed = 'completed',
Created = 'created',
Deleted = 'deleted',
Submitted = 'submitted',
Cancelled = 'cancelled',
Imported = 'imported',
Exported = 'exported',
Printed = 'printed',
Closed = 'closed',
Opened = 'opened',
Resumed = 'resumed',
Expand Down
29 changes: 5 additions & 24 deletions main/saveHtmlAsPdf.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { emitMainProcessError } from 'backend/helpers';
import { App, BrowserWindow } from 'electron';
import fs from 'fs/promises';
import path from 'path';
Expand Down Expand Up @@ -31,29 +30,11 @@ export async function saveHtmlAsPdf(
printSelectionOnly: false,
};

/**
* After the printWindow content is ready, save as pdf and
* then close the window and delete the temp html file.
*/
return await new Promise((resolve) => {
printWindow.webContents.once('did-finish-load', () => {
printWindow.webContents
.printToPDF(printOptions)
.then((data) => {
fs.writeFile(savePath, data)
.then(() => {
printWindow.close();
fs.unlink(htmlPath)
.then(() => {
resolve(true);
})
.catch((err) => emitMainProcessError(err));
})
.catch((err) => emitMainProcessError(err));
})
.catch((err) => emitMainProcessError(err));
});
});
const data = await printWindow.webContents.printToPDF(printOptions);
await fs.writeFile(savePath, data);
printWindow.close();
await fs.unlink(htmlPath);
return true;
}

async function getInitializedPrintWindow(
Expand Down
12 changes: 8 additions & 4 deletions src/components/ExportWizard.vue
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
<Int
v-if="configFields.limit"
:df="configFields.limit"
:value="limit"
:value="limit ?? undefined"
:border="true"
@change="(value: number) => (limit = value)"
/>
Expand Down Expand Up @@ -104,6 +104,7 @@ import Check from './Controls/Check.vue';
import Int from './Controls/Int.vue';
import Select from './Controls/Select.vue';
import FormHeader from './FormHeader.vue';
import { Verb } from 'fyo/telemetry/types';

interface ExportWizardData {
useListFilters: boolean;
Expand Down Expand Up @@ -165,13 +166,13 @@ export default defineComponent({
fieldtype: 'Check',
label: t`Use List Filters`,
fieldname: 'useListFilters',
},
} as Field,
limit: {
placeholder: 'Limit number of rows',
fieldtype: 'Int',
label: t`Limit`,
fieldname: 'limit',
},
} as Field,
exportFormat: {
fieldtype: 'Select',
label: t`Export Format`,
Expand All @@ -180,7 +181,7 @@ export default defineComponent({
{ value: 'json', label: 'JSON' },
{ value: 'csv', label: 'CSV' },
],
},
} as Field,
};
},
},
Expand Down Expand Up @@ -257,6 +258,9 @@ export default defineComponent({
}

await saveData(data, filePath);
this.fyo.telemetry.log(Verb.Exported, this.schemaName, {
extension: this.exportFormat,
});
showExportInFolder(fyo.t`Export Successful`, filePath);
},
getFileName() {
Expand Down
3 changes: 2 additions & 1 deletion src/pages/DatabaseSelector.vue
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@
<script lang="ts">
import { setupDummyInstance } from 'dummy';
import { t } from 'fyo';
import { Verb } from 'fyo/telemetry/types';
import { DateTime } from 'luxon';
import Button from 'src/components/Button.vue';
import LanguageSelector from 'src/components/Controls/LanguageSelector.vue';
Expand Down Expand Up @@ -334,7 +335,7 @@ export default defineComponent({
updateConfigFiles(fyo);
await fyo.purgeCache();
await this.setFiles();

this.fyo.telemetry.log(Verb.Created, 'dummy-instance');
this.creatingDemo = false;
},
async setFiles() {
Expand Down
2 changes: 2 additions & 0 deletions src/pages/ImportWizard.vue
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,7 @@
<script lang="ts">
import { DocValue } from 'fyo/core/types';
import { Action } from 'fyo/model/types';
import { Verb } from 'fyo/telemetry/types';
import { ValidationError } from 'fyo/utils/errors';
import { ModelNameEnum } from 'models/types';
import { OptionField, RawValue, SelectOption } from 'schemas/types';
Expand Down Expand Up @@ -864,6 +865,7 @@ export default defineComponent({
}
}

this.fyo.telemetry.log(Verb.Imported, this.importer.schemaName);
this.isMakingEntries = false;
this.complete = true;
},
Expand Down
9 changes: 5 additions & 4 deletions src/pages/PrintView/PrintView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
<PrintContainer
v-if="printProps"
ref="printContainer"
:print-schema-name="schemaName"
:template="printProps.template"
:values="printProps.values"
:scale="scale"
Expand All @@ -55,11 +56,11 @@ import PageHeader from 'src/components/PageHeader.vue';
import { handleErrorWithDialog } from 'src/errorHandling';
import { fyo } from 'src/initFyo';
import { getPrintTemplatePropValues } from 'src/utils/printTemplates';
import { showSidebar } from 'src/utils/refs';
import { PrintValues } from 'src/utils/types';
import { getFormRoute, openSettings, routeTo } from 'src/utils/ui';
import { defineComponent } from 'vue';
import PrintContainer from '../TemplateBuilder/PrintContainer.vue';
import { showSidebar } from 'src/utils/refs';

export default defineComponent({
name: 'PrintView',
Expand Down Expand Up @@ -244,16 +245,16 @@ export default defineComponent({

this.templateList = list.map(({ name }) => name);
},
savePDF() {
async savePDF() {
const printContainer = this.$refs.printContainer as {
savePDF: (name?: string) => void;
savePDF: (name?: string) => Promise<void>;
};

if (!printContainer?.savePDF) {
return;
}

printContainer.savePDF(this.doc?.name);
await printContainer.savePDF(this.doc?.name);
},
async setTemplateFromDefault() {
const defaultName =
Expand Down
13 changes: 8 additions & 5 deletions src/pages/PrintView/ReportPrintView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -145,20 +145,21 @@
</div>
</template>
<script lang="ts">
import { Verb } from 'fyo/telemetry/types';
import { Report } from 'reports/Report';
import { reports } from 'reports/index';
import { OptionField } from 'schemas/types';
import Button from 'src/components/Button.vue';
import Check from 'src/components/Controls/Check.vue';
import Int from 'src/components/Controls/Int.vue';
import Select from 'src/components/Controls/Select.vue';
import PageHeader from 'src/components/PageHeader.vue';
import { getReport } from 'src/utils/misc';
import { PropType, defineComponent } from 'vue';
import ScaledContainer from '../TemplateBuilder/ScaledContainer.vue';
import { getPathAndMakePDF } from 'src/utils/printTemplates';
import { OptionField } from 'schemas/types';
import { paperSizeMap, printSizes } from 'src/utils/ui';
import Select from 'src/components/Controls/Select.vue';
import { showSidebar } from 'src/utils/refs';
import { paperSizeMap, printSizes } from 'src/utils/ui';
import { PropType, defineComponent } from 'vue';
import ScaledContainer from '../TemplateBuilder/ScaledContainer.vue';

export default defineComponent({
components: { PageHeader, Button, Check, Int, ScaledContainer, Select },
Expand Down Expand Up @@ -276,6 +277,8 @@ export default defineComponent({
this.size.width,
this.size.height
);

this.fyo.telemetry.log(Verb.Printed, this.report!.reportName);
},
cellClasses(cIdx: number, rIdx: number): string[] {
const classes: string[] = [];
Expand Down
10 changes: 9 additions & 1 deletion src/pages/SetupWizard/SetupWizard.vue
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
<p v-if="loading" class="text-base text-gray-600">
{{ t`Loading instance...` }}
</p>
<Button v-if="!loading" class="w-24" @click="$emit('setup-canceled')">{{
<Button v-if="!loading" class="w-24" @click="cancel">{{
t`Cancel`
}}</Button>
<Button
Expand All @@ -72,6 +72,8 @@
<script lang="ts">
import { DocValue } from 'fyo/core/types';
import { Doc } from 'fyo/model/doc';
import { Verb } from 'fyo/telemetry/types';
import { ModelNameEnum } from 'models/types';
import { Field } from 'schemas/types';
import Button from 'src/components/Button.vue';
import FormContainer from 'src/components/FormContainer.vue';
Expand Down Expand Up @@ -153,6 +155,7 @@ export default defineComponent({
// @ts-ignore
window.sw = this;
}
this.fyo.telemetry.log(Verb.Started, ModelNameEnum.SetupWizard);
},
methods: {
async fill() {
Expand Down Expand Up @@ -198,8 +201,13 @@ export default defineComponent({
}

this.loading = true;
this.fyo.telemetry.log(Verb.Completed, ModelNameEnum.SetupWizard);
this.$emit('setup-complete', this.doc.getValidDict());
},
cancel() {
this.fyo.telemetry.log(Verb.Cancelled, ModelNameEnum.SetupWizard);
this.$emit('setup-canceled');
},
},
});
</script>
4 changes: 4 additions & 0 deletions src/pages/TemplateBuilder/PrintContainer.vue
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import {
generateCodeFrame,
SourceLocation,
} from '@vue/compiler-dom';
import { Verb } from 'fyo/telemetry/types';
import ErrorBoundary from 'src/components/ErrorBoundary.vue';
import { getPathAndMakePDF } from 'src/utils/printTemplates';
import { PrintValues } from 'src/utils/types';
Expand All @@ -66,6 +67,7 @@ export default defineComponent({
components: { ScaledContainer, ErrorBoundary },
props: {
template: { type: String, required: true },
printSchemaName: { type: String, required: true },
scale: { type: Number, default: 0.65 },
width: { type: Number, default: 21 },
height: { type: Number, default: 29.7 },
Expand Down Expand Up @@ -179,6 +181,8 @@ export default defineComponent({
this.width,
this.height
);

this.fyo.telemetry.log(Verb.Printed, this.printSchemaName);
},
},
});
Expand Down
1 change: 1 addition & 0 deletions src/pages/TemplateBuilder/TemplateBuilder.vue
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
<div v-if="canDisplayPreview" class="p-4 overflow-auto custom-scroll">
<PrintContainer
ref="printContainer"
:print-schema-name="displayDoc!.schemaName"
:template="doc.template!"
:values="values!"
:scale="scale"
Expand Down
3 changes: 3 additions & 0 deletions src/utils/printTemplates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,8 @@ export async function updatePrintTemplates(fyo: Fyo) {
updateList.push(...updates);
}

const isLogging = fyo.store.skipTelemetryLogging;
fyo.store.skipTelemetryLogging = true;
for (const { name, type, template } of updateList) {
const doc = await getDocFromNameIfExistsElseNew(
ModelNameEnum.PrintTemplate,
Expand All @@ -299,6 +301,7 @@ export async function updatePrintTemplates(fyo: Fyo) {
await doc.set({ name, type, template, isCustom: false });
await doc.sync();
}
fyo.store.skipTelemetryLogging = isLogging;
}

function getPrintTemplateUpdateList(
Expand Down