#!/usr/bin/env python3 # -*- coding: utf-8 -*- """Orientacni srovnani: online (Graph totalItemCount) vs Mongo po slozkach.""" import sys import msal, requests from pymongo import MongoClient TENANT="7d269944-37a4-43a1-8140-c7517dc426e9" CLIENT="4b222bfd-78c9-4239-a53f-43006b3ed07f" SECRET="Txg8Q~MjhocuopxsJyJBhPmDfMxZ2r5WpTFj1dfk" GRAPH="https://graph.microsoft.com/v1.0" MBOX = sys.argv[1] if len(sys.argv)>1 else "michaela.buzalkova@buzalka.cz" app=msal.ConfidentialClientApplication(CLIENT,authority=f"https://login.microsoftonline.com/{TENANT}",client_credential=SECRET) tok=app.acquire_token_for_client(scopes=["https://graph.microsoft.com/.default"])["access_token"] H={"Authorization":f"Bearer {tok}"} def folders(parent=None,ppath=""): url=f"{GRAPH}/users/{MBOX}/mailFolders" if parent is None else f"{GRAPH}/users/{MBOX}/mailFolders/{parent}/childFolders" params={"$top":100,"$select":"id,displayName,totalItemCount,childFolderCount"} out=[] while url: d=requests.get(url,headers=H,params=params,timeout=60).json() for f in d.get("value",[]): path=f"{ppath}/{f['displayName']}".lstrip("/") out.append((path,f.get("totalItemCount",0))) if f.get("childFolderCount",0)>0: out+=folders(f["id"],path) url=d.get("@odata.nextLink"); params=None return out online=folders() online_d={p:c for p,c in online} cli=MongoClient("mongodb://192.168.1.76:27017") col=cli["emaily"][MBOX] mongo_d={} for r in col.aggregate([{"$group":{"_id":"$folder_path","n":{"$sum":1}}}]): mongo_d[r["_id"]]=r["n"] deleted=col.count_documents({"permanently_deleted":True}) allpaths=sorted(set(online_d)|set(mongo_d)) print(f"{'SLOZKA':40} {'ONLINE':>8} {'MONGO':>8} {'ROZDIL':>8}") print("-"*68) to=tm=0 for p in allpaths: o=online_d.get(p); m=mongo_d.get(p,0) to+=(o or 0); tm+=m print(f"{(p or '(none)')[:40]:40} {('-' if o is None else o):>8} {m:>8} {('' if o is None else o-m):>8}") print("-"*68) print(f"{'CELKEM':40} {to:>8} {tm:>8} {to-tm:>8}") print(f"\nMongo total docs: {col.count_documents({})} | z toho permanently_deleted: {deleted}") print(f"Online folders: {len(online_d)} | Mongo folders: {len(mongo_d)}")